forked from jeremyevans/sequel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql.rb
417 lines (367 loc) · 15.5 KB
/
mysql.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
require 'mysql'
raise(LoadError, "require 'mysql' did not define Mysql::CLIENT_MULTI_RESULTS!\n You are probably using the pure ruby mysql.rb driver,\n which Sequel does not support. You need to install\n the C based adapter, and make sure that the mysql.so\n file is loaded instead of the mysql.rb file.\n") unless defined?(Mysql::CLIENT_MULTI_RESULTS)
Sequel.require %w'shared/mysql utils/stored_procedures', 'adapters'
module Sequel
# Module for holding all MySQL-related classes and modules for Sequel.
#
# A class level convert_invalid_date_time accessor exists if
# the native adapter is used. If set to nil or :nil, the adapter treats dates
# like 0000-00-00 and times like 838:00:00 as nil values. If set to :string,
# it returns the strings as is. It is false by default, which means that
# invalid dates and times will raise errors.
#
# Sequel::MySQL.convert_invalid_date_time = true
#
# Sequel converts the column type tinyint(1) to a boolean by default when
# using the native MySQL adapter. You can turn off the conversion to use
# tinyint as an integer:
#
# Sequel.convert_tinyint_to_bool = false
module MySQL
# Mapping of type numbers to conversion procs
MYSQL_TYPES = {}
# Use only a single proc for each type to save on memory
MYSQL_TYPE_PROCS = {
[0, 246] => lambda{|v| BigDecimal.new(v)}, # decimal
[1] => lambda{|v| convert_tinyint_to_bool ? v.to_i != 0 : v.to_i}, # tinyint
[2, 3, 8, 9, 13, 247, 248] => lambda{|v| v.to_i}, # integer
[4, 5] => lambda{|v| v.to_f}, # float
[10, 14] => lambda{|v| convert_date_time(:string_to_date, v)}, # date
[7, 12] => lambda{|v| convert_date_time(:database_to_application_timestamp, v)}, # datetime
[11] => lambda{|v| convert_date_time(:string_to_time, v)}, # time
[249, 250, 251, 252] => lambda{|v| Sequel::SQL::Blob.new(v)} # blob
}
MYSQL_TYPE_PROCS.each do |k,v|
k.each{|n| MYSQL_TYPES[n] = v}
end
@convert_invalid_date_time = false
@convert_tinyint_to_bool = true
class << self
attr_accessor :convert_invalid_date_time, :convert_tinyint_to_bool
end
# If convert_invalid_date_time is nil, :nil, or :string and
# the conversion raises an InvalidValue exception, return v
# if :string and nil otherwise.
def self.convert_date_time(meth, v)
begin
Sequel.send(meth, v)
rescue InvalidValue
case @convert_invalid_date_time
when nil, :nil
nil
when :string
v
else
raise
end
end
end
# Database class for MySQL databases used with Sequel.
class Database < Sequel::Database
include Sequel::MySQL::DatabaseMethods
# Mysql::Error messages that indicate the current connection should be disconnected
MYSQL_DATABASE_DISCONNECT_ERRORS = /\A(Commands out of sync; you can't run this command now\z|Can't connect to local MySQL server through socket)/
set_adapter_scheme :mysql
# Support stored procedures on MySQL
def call_sproc(name, opts={}, &block)
args = opts[:args] || []
execute("CALL #{name}#{args.empty? ? '()' : literal(args)}", opts.merge(:sproc=>false), &block)
end
# Connect to the database. In addition to the usual database options,
# the following options have effect:
#
# * :auto_is_null - Set to true to use MySQL default behavior of having
# a filter for an autoincrement column equals NULL to return the last
# inserted row.
# * :charset - Same as :encoding (:encoding takes precendence)
# * :compress - Set to false to not compress results from the server
# * :encoding - Set all the related character sets for this
# connection (connection, client, database, server, and results).
# * :socket - Use a unix socket file instead of connecting via TCP/IP.
# * :timeout - Set the timeout in seconds before the server will
# disconnect this connection.
def connect(server)
opts = server_opts(server)
conn = Mysql.init
conn.options(Mysql::OPT_LOCAL_INFILE, "client")
if encoding = opts[:encoding] || opts[:charset]
# set charset _before_ the connect. using an option instead of "SET (NAMES|CHARACTER_SET_*)" works across reconnects
conn.options(Mysql::SET_CHARSET_NAME, encoding)
end
conn.real_connect(
opts[:host] || 'localhost',
opts[:user],
opts[:password],
opts[:database],
opts[:port],
opts[:socket],
Mysql::CLIENT_MULTI_RESULTS +
Mysql::CLIENT_MULTI_STATEMENTS +
(opts[:compress] == false ? 0 : Mysql::CLIENT_COMPRESS)
)
# increase timeout so mysql server doesn't disconnect us
conn.query("set @@wait_timeout = #{opts[:timeout] || 2592000}")
# By default, MySQL 'where id is null' selects the last inserted id
conn.query("set SQL_AUTO_IS_NULL=0") unless opts[:auto_is_null]
class << conn
attr_accessor :prepared_statements
end
conn.prepared_statements = {}
conn.reconnect = true
conn
end
# Returns instance of Sequel::MySQL::Dataset with the given options.
def dataset(opts = nil)
MySQL::Dataset.new(self, opts)
end
# Executes the given SQL using an available connection, yielding the
# connection if the block is given.
def execute(sql, opts={}, &block)
if opts[:sproc]
call_sproc(sql, opts, &block)
elsif sql.is_a?(Symbol)
execute_prepared_statement(sql, opts, &block)
else
synchronize(opts[:server]){|conn| _execute(conn, sql, opts, &block)}
end
end
# Return the version of the MySQL server two which we are connecting.
def server_version(server=nil)
@server_version ||= (synchronize(server){|conn| conn.server_version if conn.respond_to?(:server_version)} || super)
end
private
# Execute the given SQL on the given connection. If the :type
# option is :select, yield the result of the query, otherwise
# yield the connection if a block is given.
def _execute(conn, sql, opts)
begin
log_info(sql)
r = conn.query(sql)
if opts[:type] == :select
yield r if r
if conn.respond_to?(:next_result) && conn.next_result
loop do
if r
r.free
r = nil
end
begin
r = conn.use_result
rescue Mysql::Error
break
end
yield r
break unless conn.next_result
end
end
else
yield conn if block_given?
end
rescue Mysql::Error => e
raise_error(e, :disconnect=>MYSQL_DATABASE_DISCONNECT_ERRORS.match(e.message))
ensure
if r
r.free
# Use up all results to avoid a commands out of sync message.
if conn.respond_to?(:next_result)
while conn.next_result
r = conn.use_result
r.free if r
end
end
end
end
end
# MySQL connections use the query method to execute SQL without a result
def connection_execute_method
:query
end
# MySQL doesn't need the connection pool to convert exceptions.
def connection_pool_default_options
super.merge(:pool_convert_exceptions=>false)
end
# The MySQL adapter main error class is Mysql::Error
def database_error_classes
[Mysql::Error]
end
# The database name when using the native adapter is always stored in
# the :database option.
def database_name
@opts[:database]
end
# Closes given database connection.
def disconnect_connection(c)
c.close
end
# Executes a prepared statement on an available connection. If the
# prepared statement already exists for the connection and has the same
# SQL, reuse it, otherwise, prepare the new statement. Because of the
# usual MySQL stupidity, we are forced to name arguments via separate
# SET queries. Use @sequel_arg_N (for N starting at 1) for these
# arguments.
def execute_prepared_statement(ps_name, opts, &block)
args = opts[:arguments]
ps = prepared_statements[ps_name]
sql = ps.prepared_sql
synchronize(opts[:server]) do |conn|
unless conn.prepared_statements[ps_name] == sql
conn.prepared_statements[ps_name] = sql
_execute(conn, "PREPARE #{ps_name} FROM '#{::Mysql.quote(sql)}'", opts)
end
i = 0
_execute(conn, "SET " + args.map {|arg| "@sequel_arg_#{i+=1} = #{literal(arg)}"}.join(", "), opts) unless args.empty?
_execute(conn, "EXECUTE #{ps_name}#{" USING #{(1..i).map{|j| "@sequel_arg_#{j}"}.join(', ')}" unless i == 0}", opts, &block)
end
end
# Convert tinyint(1) type to boolean if convert_tinyint_to_bool is true
def schema_column_type(db_type)
Sequel::MySQL.convert_tinyint_to_bool && db_type == 'tinyint(1)' ? :boolean : super
end
end
# Dataset class for MySQL datasets accessed via the native driver.
class Dataset < Sequel::Dataset
include Sequel::MySQL::DatasetMethods
include StoredProcedures
# Methods to add to MySQL prepared statement calls without using a
# real database prepared statement and bound variables.
module CallableStatementMethods
# Extend given dataset with this module so subselects inside subselects in
# prepared statements work.
def subselect_sql(ds)
ps = ds.to_prepared_statement(:select)
ps.extend(CallableStatementMethods)
ps.prepared_args = prepared_args
ps.prepared_sql
end
end
# Methods for MySQL prepared statements using the native driver.
module PreparedStatementMethods
include Sequel::Dataset::UnnumberedArgumentMapper
private
# Execute the prepared statement with the bind arguments instead of
# the given SQL.
def execute(sql, opts={}, &block)
super(prepared_statement_name, {:arguments=>bind_arguments}.merge(opts), &block)
end
# Same as execute, explicit due to intricacies of alias and super.
def execute_dui(sql, opts={}, &block)
super(prepared_statement_name, {:arguments=>bind_arguments}.merge(opts), &block)
end
end
# Methods for MySQL stored procedures using the native driver.
module StoredProcedureMethods
include Sequel::Dataset::StoredProcedureMethods
private
# Execute the database stored procedure with the stored arguments.
def execute(sql, opts={}, &block)
super(@sproc_name, {:args=>@sproc_args, :sproc=>true}.merge(opts), &block)
end
# Same as execute, explicit due to intricacies of alias and super.
def execute_dui(sql, opts={}, &block)
super(@sproc_name, {:args=>@sproc_args, :sproc=>true}.merge(opts), &block)
end
end
# MySQL is different in that it supports prepared statements but not bound
# variables outside of prepared statements. The default implementation
# breaks the use of subselects in prepared statements, so extend the
# temporary prepared statement that this creates with a module that
# fixes it.
def call(type, bind_arguments={}, values=nil)
ps = to_prepared_statement(type, values)
ps.extend(CallableStatementMethods)
ps.call(bind_arguments)
end
# Delete rows matching this dataset
def delete
execute_dui(delete_sql){|c| c.affected_rows}
end
# Yield all rows matching this dataset. If the dataset is set to
# split multiple statements, yield arrays of hashes one per statement
# instead of yielding results for all statements as hashes.
def fetch_rows(sql, &block)
execute(sql) do |r|
i = -1
cols = r.fetch_fields.map{|f| [output_identifier(f.name), MYSQL_TYPES[f.type], i+=1]}
@columns = cols.map{|c| c.first}
if opts[:split_multiple_result_sets]
s = []
yield_rows(r, cols){|h| s << h}
yield s
else
yield_rows(r, cols, &block)
end
end
self
end
# Don't allow graphing a dataset that splits multiple statements
def graph(*)
raise(Error, "Can't graph a dataset that splits multiple result sets") if opts[:split_multiple_result_sets]
super
end
# Insert a new value into this dataset
def insert(*values)
execute_dui(insert_sql(*values)){|c| c.insert_id}
end
# Store the given type of prepared statement in the associated database
# with the given name.
def prepare(type, name=nil, values=nil)
ps = to_prepared_statement(type, values)
ps.extend(PreparedStatementMethods)
if name
ps.prepared_statement_name = name
db.prepared_statements[name] = ps
end
ps
end
# Replace (update or insert) the matching row.
def replace(*args)
execute_dui(replace_sql(*args)){|c| c.insert_id}
end
# Makes each yield arrays of rows, with each array containing the rows
# for a given result set. Does not work with graphing. So you can submit
# SQL with multiple statements and easily determine which statement
# returned which results.
#
# Modifies the row_proc of the returned dataset so that it still works
# as expected (running on the hashes instead of on the arrays of hashes).
# If you modify the row_proc afterward, note that it will receive an array
# of hashes instead of a hash.
def split_multiple_result_sets
raise(Error, "Can't split multiple statements on a graphed dataset") if opts[:graph]
ds = clone(:split_multiple_result_sets=>true)
ds.row_proc = proc{|x| x.map{|h| row_proc.call(h)}} if row_proc
ds
end
# Update the matching rows.
def update(values={})
execute_dui(update_sql(values)){|c| c.affected_rows}
end
private
# Set the :type option to :select if it hasn't been set.
def execute(sql, opts={}, &block)
super(sql, {:type=>:select}.merge(opts), &block)
end
# Set the :type option to :dui if it hasn't been set.
def execute_dui(sql, opts={}, &block)
super(sql, {:type=>:dui}.merge(opts), &block)
end
# Handle correct quoting of strings using ::MySQL.quote.
def literal_string(v)
"'#{::Mysql.quote(v)}'"
end
# Extend the dataset with the MySQL stored procedure methods.
def prepare_extend_sproc(ds)
ds.extend(StoredProcedureMethods)
end
# Yield each row of the given result set r with columns cols
# as a hash with symbol keys
def yield_rows(r, cols)
while row = r.fetch_row
h = {}
cols.each{|n, p, i| v = row[i]; h[n] = (v && p) ? p.call(v) : v}
yield h
end
end
end
end
end