lib/rom/sql/gateway.rb in rom-sql-1.3.5 vs lib/rom/sql/gateway.rb in rom-sql-2.0.0.beta1

- old
+ new

@@ -29,10 +29,27 @@ # @!attribute [r] options # @return [Hash] Options used for connection attr_reader :options + subscribe('configuration.commands.class.before_build') do |event| + klass = event[:command] + dataset = event[:dataset] + type = dataset.db.database_type + + if type == :postgres + ext = + if klass < Commands::Create + Commands::Postgres::Create + elsif klass < Commands::Update + Commands::Postgres::Update + end + + klass.send(:include, ext) if ext + end + end + # Initialize an SQL gateway # # Gateways are typically initialized via ROM::Configuration object, gateway constructor # arguments such as URI and options are passed directly to this constructor # @@ -151,35 +168,10 @@ # @api public def dataset?(name) schema.include?(name) end - # Extend the command class with database-specific behavior - # - # @param [Class] klass Command class - # @param [Sequel::Dataset] dataset A dataset that will be used - # - # Note: Currently, only postgres is supported. - # - # @api public - def extend_command_class(klass, dataset) - type = dataset.db.database_type - - if type == :postgres - ext = - if klass < Commands::Create - Commands::Postgres::Create - elsif klass < Commands::Update - Commands::Postgres::Update - end - - klass.send(:include, ext) if ext - end - - klass - end - # Create a table using the configured connection # # @api public def create_table(*args, &block) connection.create_table(*args, &block) @@ -199,10 +191,19 @@ # @api public def schema @schema ||= connection.tables end + # Underlying database type + # + # @return [Symbol] + # + # @api public + def database_type + @database_type ||= connection.database_type.to_sym + end + private # Connect to database or reuse established connection instance # # @return [Database::Sequel] a connection instance @@ -219,16 +220,14 @@ # Load database-specific extensions # # @api private def load_extensions(exts) - db_type = connection.database_type.to_sym - - if ROM::SQL.available_extension?(db_type) - ROM::SQL.load_extensions(db_type) + if ROM::SQL.available_extension?(database_type) + ROM::SQL.load_extensions(database_type) end - extensions = (CONNECTION_EXTENSIONS.fetch(db_type, EMPTY_ARRAY) + exts).uniq + extensions = (CONNECTION_EXTENSIONS.fetch(database_type, EMPTY_ARRAY) + exts).uniq connection.extension(*extensions) # this will be default in Sequel 5.0.0 and since we don't rely # on dataset mutation it is safe to enable it already connection.extension(:freeze_datasets) unless RUBY_ENGINE == 'rbx'