lib/rom/sql/relation.rb in rom-sql-1.3.5 vs lib/rom/sql/relation.rb in rom-sql-2.0.0.beta1
- old
+ new
@@ -1,72 +1,52 @@
require 'rom/sql/types'
require 'rom/sql/schema'
+require 'rom/sql/attribute'
+require 'rom/sql/wrap'
require 'rom/sql/relation/reading'
require 'rom/sql/relation/writing'
require 'rom/sql/relation/sequel_api'
-require 'rom/plugins/relation/key_inference'
-require 'rom/plugins/relation/sql/auto_combine'
-require 'rom/plugins/relation/sql/auto_wrap'
-
module ROM
module SQL
# Sequel-specific relation extensions
#
# @api public
class Relation < ROM::Relation
- include SQL
-
adapter :sql
- use :key_inference
- use :auto_combine
- use :auto_wrap
-
+ include SQL
include Writing
include Reading
- schema_dsl SQL::Schema::DSL
+ extend Notifications::Listener
- # Set default dataset for a relation sub-class
- #
- # @api private
- def self.inherited(klass)
- super
+ schema_class SQL::Schema
+ schema_attr_class SQL::Attribute
+ schema_inferrer ROM::SQL::Schema::Inferrer.new.freeze
+ wrap_class SQL::Wrap
- klass.class_eval do
- schema_inferrer -> (name, gateway) do
- inferrer_for_db = ROM::SQL::Schema::Inferrer.get(gateway.connection.database_type.to_sym)
- begin
- inferrer_for_db.new.call(name, gateway)
- rescue Sequel::Error => e
- inferrer_for_db.on_error(klass, e)
- ROM::Schema::DEFAULT_INFERRER.()
- end
- end
+ subscribe('configuration.relations.schema.set', adapter: :sql) do |event|
+ schema = event[:schema]
+ relation = event[:relation]
- dataset do
- # TODO: feels strange to do it here - we need a new hook for this during finalization
- klass.define_default_views!
- schema = klass.schema
+ relation.dataset do
+ table = opts[:from].first
- table = opts[:from].first
-
- if db.table_exists?(table)
- if schema
- select(*schema.map(&:to_sql_name)).order(*schema.project(*schema.primary_key_names).qualified.map(&:to_sql_name))
- else
- select(*columns).order(*klass.primary_key_columns(db, table))
- end
- else
- self
- end
+ if db.table_exists?(table)
+ select(*schema.map(&:qualified)).order(*schema.project(*schema.primary_key_names).qualified)
+ else
+ self
end
end
end
+ subscribe('configuration.relations.dataset.allocated', adapter: :sql) do |event|
+ event[:relation].define_default_views!
+ end
+
# @api private
def self.define_default_views!
if schema.primary_key.size > 1
# @!method by_pk(val1, val2)
# Return a relation restricted by its composite primary key
@@ -128,10 +108,10 @@
#
# @return [Relation]
#
# @api public
def assoc(name)
- associations[name].(__registry__)
+ associations[name].()
end
# Return raw column names
#
# @return [Array<Symbol>]