lib/rom/sql/gateway.rb in rom-sql-0.9.1 vs lib/rom/sql/gateway.rb in rom-sql-1.0.0.beta1
- old
+ new
@@ -1,10 +1,14 @@
require 'logger'
+require 'dry/core/constants'
+
+require 'rom/types'
require 'rom/gateway'
require 'rom/sql/migration'
require 'rom/sql/commands'
+require 'rom/sql/transaction'
module ROM
module SQL
# SQL gateway
#
@@ -14,28 +18,31 @@
#
# users = gateway.dataset(:users)
#
# @api public
class Gateway < ROM::Gateway
- include Options
+ include Dry::Core::Constants
include Migration
class << self
attr_accessor :instance
end
CONNECTION_EXTENSIONS = {
- postgres: %i(pg_array pg_json)
+ postgres: %i(pg_array pg_json pg_enum)
}.freeze
# Return optionally configured logger
#
# @return [Object] logger
#
# @api public
attr_reader :logger
+ # @api private
+ attr_reader :options
+
# SQL gateway interface
#
# @overload connect(uri, options)
# Connects to database via uri passing options
#
@@ -53,19 +60,18 @@
# # or reuse connection
# DB = Sequel.connect('postgres://localhost/rom')
# gateway = ROM::SQL::Gateway.new(DB)
#
# @api public
- def initialize(uri, options = {})
- repo_options = self.class.option_definitions.names
- conn_options = options.reject { |k, _| repo_options.include?(k) }
-
- @connection = connect(uri, conn_options)
+ def initialize(uri, options = EMPTY_HASH)
+ @connection = connect(uri, options)
load_extensions(Array(options[:extensions]))
- super(uri, options.reject { |k, _| conn_options.keys.include?(k) })
+ @options = options
+ super
+
self.class.instance = self
end
# Disconnect from database
#
@@ -189,11 +195,20 @@
if ROM::SQL.available_extension?(db_type)
ROM::SQL.load_extensions(db_type)
end
- extensions = (CONNECTION_EXTENSIONS.fetch(db_type) { [] } + exts).uniq
+ extensions = (CONNECTION_EXTENSIONS.fetch(db_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'
+ end
+
+ # @api private
+ def transaction_runner(_)
+ ROM::SQL::Transaction.new(connection)
end
end
end
end