lib/rom/sql/gateway.rb in rom-sql-3.6.4 vs lib/rom/sql/gateway.rb in rom-sql-4.0.0.alpha1
- old
+ new
@@ -1,24 +1,28 @@
# frozen_string_literal: true
-require 'logger'
-require 'sequel/core'
+require "logger"
+require "sequel/core"
-require 'dry/core/constants'
+require "dry/core/constants"
-require 'rom/types'
-require 'rom/gateway'
-require 'rom/sql/migration'
-require 'rom/sql/commands'
-require 'rom/sql/transaction'
+require "rom/setup"
+require "rom/types"
+require "rom/gateway"
+require "rom/sql/migration"
+require "rom/sql/commands"
+require "rom/sql/transaction"
+require "rom/support/notifications"
module ROM
module SQL
# SQL gateway
#
# @api public
class Gateway < ROM::Gateway
+ extend Notifications
+
include Dry::Core::Constants
include Migration
adapter :sql
@@ -79,17 +83,22 @@
#
# @api public
def initialize(uri, options = EMPTY_HASH)
@connection = connect(uri, options)
load_extensions(Array(options[:extensions]))
- Notifications.trigger("configuration.gateway.connected", connection: @connection)
+ notifications.trigger("sql.gateway.connected", connection: @connection)
@options = options
super
end
+ # @api private
+ def notifications
+ @notifications ||= Notifications.event_bus(:sql)
+ end
+
# Disconnect from the gateway's database
#
# @api public
def disconnect
connection.disconnect
@@ -150,19 +159,19 @@
end
# Create a table using the configured connection
#
# @api public
- def create_table(*args, &block)
- connection.create_table(*args, &block)
+ def create_table(...)
+ connection.create_table(...)
end
# Drops a table
#
# @api public
- def drop_table(*args, &block)
- connection.drop_table(*args, &block)
+ def drop_table(...)
+ connection.drop_table(...)
end
# Returns a list of datasets inferred from table names
#
# @return [Array] array with table names
@@ -203,10 +212,30 @@
# @api public
def run(statement)
connection.run(statement)
end
+ # Build an SQL-specific command
+ #
+ # @return [Command]
+ #
+ # @api public
+ def command(klass, relation:, **opts)
+ return super unless relation.dataset.db.database_type == :postgres
+
+ ext =
+ if klass < Commands::Create
+ Postgres::Commands::Create
+ elsif klass < Commands::Update
+ Postgres::Commands::Update
+ end
+
+ klass.include(ext) if ext
+
+ super
+ end
+
private
# Connect to database or reuse established connection instance
#
# @return [Database::Sequel] a connection instance
@@ -234,20 +263,18 @@
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'
+ connection.extension(:freeze_datasets) unless RUBY_ENGINE == "rbx"
# for ROM::SQL::Relation#nullify
connection.extension(:null_dataset)
end
# @api private
- def transaction_runner(_)
+ def transaction_runner(**)
ROM::SQL::Transaction.new(connection)
end
end
end
-
- Configuration.register_event("configuration.gateway.connected")
end