-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathgateway.rb
280 lines (245 loc) · 6.97 KB
/
gateway.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# frozen_string_literal: true
require "logger"
require "sequel/core"
require "dry/core/constants"
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
CONNECTION_EXTENSIONS = {
postgres: %i[pg_array pg_json pg_enum]
}.freeze
# @!attribute [r] logger
# @return [Object] configured gateway logger
attr_reader :logger
# @!attribute [r] options
# @return [Hash] Options used for connection
attr_reader :options
# 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
#
# @overload initialize(uri)
# Connects to a database via URI
#
# @example
# ROM.container(:sql, 'postgres://localhost/db_name')
#
# @param [String,Symbol] uri connection URI
#
# @overload initialize(uri, options)
# Connects to a database via URI and options
#
# @example
# ROM.container(:sql, 'postgres://localhost/db_name', extensions: %w[pg_enum])
#
# @param [String,Symbol] uri connection URI
#
# @param [Hash] options connection options
#
# @option options [Array<Symbol>] :extensions
# A list of connection extensions supported by Sequel
#
# @option options [String] :user Database username
#
# @option options [String] :password Database password
#
# @overload initialize(connection)
# Creates a gateway from an existing database connection. This
# works with Sequel connections exclusively.
#
# @example
# ROM.container(:sql, Sequel.connect(:sqlite))
#
# @param [Sequel::Database] connection a connection instance
#
# @return [SQL::Gateway]
#
# @see https://github.com/jeremyevans/sequel/blob/master/doc/opening_databases.rdoc Sequel connection docs
#
# @api public
def initialize(uri, options = EMPTY_HASH)
@connection = connect(uri, options)
load_extensions(Array(options[:extensions]))
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
end
# Return dataset with the given name
#
# This returns a raw Sequel database
#
# @param [String, Symbol] name The dataset name
#
# @return [Dataset]
#
# @api public
def [](name)
connection[name]
end
# Setup a logger
#
# @example set a logger during configuration process
# rom = ROM.container(:sql, 'sqlite::memory') do |config|
# config.gateways[:default].use_logger(Logger.new($stdout))
# end
#
# @example set logger after gateway has been established
# rom = ROM.container(:sql, 'sqlite::memory')
# rom.gateways[:default].use_logger(Logger.new($stdout))
#
# @param [Object] logger
#
# @see Sequel::Database#loggers
#
# @api public
def use_logger(logger)
@logger = logger
connection.loggers << logger
end
# Return dataset with the given name
#
# @param [String] name a dataset name
#
# @return [Dataset]
#
# @api public
def dataset(name)
connection[name]
end
# Check if a dataset exists
#
# @param [String] name dataset name
#
# @api public
def dataset?(name)
schema.include?(name)
end
# Create a table using the configured connection
#
# @api public
def create_table(...)
connection.create_table(...)
end
# Drops a table
#
# @api public
def drop_table(...)
connection.drop_table(...)
end
# Returns a list of datasets inferred from table names
#
# @return [Array] array with table names
#
# @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
# Call a SQL function
#
# @example
# gateway.(:upper, 'John Doe') # => "JOHN DOE"
#
# @param [Symbol] function Function name
# @param [Array<Object>] args Function arguments
#
# @return [Object]
#
# @api public
def call(function, *args)
connection[Sequel.function(function, *args)].first.values.first
end
# Execute a statement
#
# @param [String] statement
#
# @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
#
# @api private
def connect(uri, *args)
case uri
when ::Sequel::Database
uri
when Hash
::Sequel.connect(uri, *args)
else
::Sequel.connect(uri.to_s, *args)
end
end
# Load database-specific extensions
#
# @api private
def load_extensions(exts)
if ROM::SQL.available_extension?(database_type)
ROM::SQL.load_extensions(database_type)
end
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"
# for ROM::SQL::Relation#nullify
connection.extension(:null_dataset)
end
# @api private
def transaction_runner(**)
ROM::SQL::Transaction.new(connection)
end
end
end
end