Module: ArJdbc::MySQL
Defined Under Namespace
Modules: BulkChangeTable, Column, ExplainSupport
Classes: SchemaCreation
Constant Summary
collapse
- NATIVE_DATABASE_TYPES =
{
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
:string => { :name => "varchar", :limit => 255 },
:text => { :name => "text" },
:integer => { :name => "int", :limit => 4 },
:float => { :name => "float" },
:numeric => { :name => "numeric" },
:decimal => { :name => "decimal" },
:datetime => { :name => "datetime" },
:timestamp => { :name => "datetime" },
:time => { :name => "time" },
:date => { :name => "date" },
:binary => { :name => "blob" },
:boolean => { :name => "tinyint", :limit => 1 },
:bit => { :name => "bit" },
:enum => { :name => "enum" },
:set => { :name => "set" },
:char => { :name => "char" },
}
- ADAPTER_NAME =
'MySQL'.freeze
- INDEX_TYPES =
[ :fulltext, :spatial ]
- INDEX_USINGS =
[ :btree, :hash ]
Class Method Summary
collapse
Instance Method Summary
collapse
-
#adapter_name ⇒ Object
-
#add_column(table_name, column_name, type, options = {}) ⇒ Object
-
#add_column_position!(sql, options) ⇒ Object
unless const_defined? :SchemaCreation.
-
#add_limit_offset!(sql, options) ⇒ Object
-
#case_sensitive_equality_operator ⇒ Object
-
#case_sensitive_modifier(node) ⇒ Object
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
-
#change_column_default(table_name, column_name, default) ⇒ Object
-
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
unless const_defined? :SchemaCreation.
-
#charset ⇒ Object
-
#collation ⇒ Object
-
#columns(table_name, name = nil) ⇒ Object
Returns an array of Column objects for the table specified.
-
#configure_connection ⇒ Object
-
#create_database(name, options = {}) ⇒ Object
-
#create_savepoint(name = current_savepoint_name(true)) ⇒ Object
-
#create_table(name, options = {}) ⇒ Object
-
#current_database ⇒ Object
-
#disable_referential_integrity ⇒ Object
-
#drop_database(name) ⇒ Object
-
#empty_insert_statement_value ⇒ Object
-
#indexes(table_name, name = nil) ⇒ Object
Returns an array of indexes for the given table.
-
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
-
#native_database_types ⇒ Object
-
#pk_and_sequence_for(table) ⇒ Object
Returns a table's primary key and belonging sequence.
-
#primary_key(table) ⇒ Object
Returns just a table's primary key.
-
#quote(value, column = nil) ⇒ Object
-
#quote_column_name(name) ⇒ Object
-
#quote_table_name(name) ⇒ Object
-
#quoted_columns_for_index(column_names, options = {}) ⇒ Object
protected
-
#release_savepoint(name = current_savepoint_name) ⇒ Object
-
#remove_index!(table_name, index_name) ⇒ Object
-
#rename_column(table_name, column_name, new_column_name) ⇒ Object
-
#rename_table(table_name, new_name) ⇒ Object
-
#rollback_to_savepoint(name = current_savepoint_name) ⇒ Object
-
#show_variable(var) ⇒ Object
-
#strict_mode? ⇒ Boolean
strict_mode is default since AR 4.0.
-
#structure_dump ⇒ Object
deprecated
Deprecated.
no longer used - handled with (AR built-in) Rake tasks
-
#supports_index_sort_order? ⇒ Boolean
-
#supports_migrations? ⇒ Boolean
-
#supports_primary_key? ⇒ Boolean
-
#supports_savepoints? ⇒ Boolean
-
#supports_transaction_isolation?(level = nil) ⇒ Boolean
-
#supports_views? ⇒ Boolean
-
#translate_exception(exception, message) ⇒ Object
protected
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
-
#update_sql(sql, name = nil) ⇒ Object
#add_column_sql, #add_index_sql, #add_timestamps_sql, #bulk_change_table, #change_column_sql, #remove_column_sql, #remove_columns_sql, #remove_index_sql, #remove_timestamps_sql, #rename_column_sql, #supports_bulk_alter?
Class Method Details
.arel_visitor_type(config = nil) ⇒ Object
125
126
127
|
# File 'lib/arjdbc/mysql/adapter.rb', line 125
def self.arel_visitor_type(config = nil)
::Arel::Visitors::MySQL
end
|
.column_selector ⇒ Object
5
6
7
|
# File 'lib/arjdbc/mysql/column.rb', line 5
def self.column_selector
[ /mysql/i, lambda { |config, column| column.extend(Column) } ]
end
|
.emulate_booleans ⇒ Object
Deprecated. Use #emulate_booleans? instead.
85
|
# File 'lib/arjdbc/mysql/adapter.rb', line 85
def self.emulate_booleans; @@emulate_booleans; end
|
.emulate_booleans=(emulate) ⇒ Object
87
|
# File 'lib/arjdbc/mysql/adapter.rb', line 87
def self.emulate_booleans=(emulate); @@emulate_booleans = emulate; end
|
.emulate_booleans? ⇒ Boolean
Boolean emulation can be disabled using (or using the adapter method) :
ArJdbc::MySQL.emulate_booleans = false
83
|
# File 'lib/arjdbc/mysql/adapter.rb', line 83
def self.emulate_booleans?; @@emulate_booleans; end
|
.jdbc_connection_class ⇒ Object
15
16
17
|
# File 'lib/arjdbc/mysql/adapter.rb', line 15
def self.jdbc_connection_class
::ActiveRecord::ConnectionAdapters::MySQLJdbcConnection
end
|
Instance Method Details
#adapter_name ⇒ Object
121
122
123
|
# File 'lib/arjdbc/mysql/adapter.rb', line 121
def adapter_name
ADAPTER_NAME
end
|
#add_column(table_name, column_name, type, options = {}) ⇒ Object
382
383
384
385
386
387
|
# File 'lib/arjdbc/mysql/adapter.rb', line 382
def add_column(table_name, column_name, type, options = {})
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(add_column_sql, options)
add_column_position!(add_column_sql, options)
execute(add_column_sql)
end
|
#add_column_position!(sql, options) ⇒ Object
unless const_defined? :SchemaCreation
437
438
439
440
441
442
443
|
# File 'lib/arjdbc/mysql/adapter.rb', line 437
def add_column_position!(sql, options)
if options[:first]
sql << " FIRST"
elsif options[:after]
sql << " AFTER #{quote_column_name(options[:after])}"
end
end
|
#add_limit_offset!(sql, options) ⇒ Object
Note:
Only used with (non-AREL) ActiveRecord 2.3.
447
448
449
450
451
452
453
454
455
456
457
|
# File 'lib/arjdbc/mysql/adapter.rb', line 447
def add_limit_offset!(sql, options)
limit, offset = options[:limit], options[:offset]
if limit && offset
sql << " LIMIT #{offset.to_i}, #{sanitize_limit(limit)}"
elsif limit
sql << " LIMIT #{sanitize_limit(limit)}"
elsif offset
sql << " OFFSET #{offset.to_i}"
end
sql
end
|
#case_sensitive_equality_operator ⇒ Object
135
136
137
|
# File 'lib/arjdbc/mysql/adapter.rb', line 135
def case_sensitive_equality_operator
"= BINARY"
end
|
#case_sensitive_modifier(node) ⇒ Object
139
140
141
|
# File 'lib/arjdbc/mysql/adapter.rb', line 139
def case_sensitive_modifier(node)
Arel::Nodes::Bin.new(node)
end
|
#change_column(table_name, column_name, type, options = {}) ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
# File 'lib/arjdbc/mysql/adapter.rb', line 405
def change_column(table_name, column_name, type, options = {})
column = column_for(table_name, column_name)
unless options_include_default?(options)
options[:default] = column.default
end
unless options.has_key?(:null)
options[:null] = column.null
end
change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(change_column_sql, options)
add_column_position!(change_column_sql, options)
execute(change_column_sql)
end
|
#change_column_default(table_name, column_name, default) ⇒ Object
389
390
391
392
|
# File 'lib/arjdbc/mysql/adapter.rb', line 389
def change_column_default(table_name, column_name, default)
column = column_for(table_name, column_name)
change_column table_name, column_name, column.sql_type, :default => default
end
|
#change_column_null(table_name, column_name, null, default = nil) ⇒ Object
unless const_defined? :SchemaCreation
394
395
396
397
398
399
400
401
402
|
# File 'lib/arjdbc/mysql/adapter.rb', line 394
def change_column_null(table_name, column_name, null, default = nil)
column = column_for(table_name, column_name)
unless null || default.nil?
execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
end
change_column table_name, column_name, column.sql_type, :null => null
end
|
#charset ⇒ Object
487
488
489
|
# File 'lib/arjdbc/mysql/adapter.rb', line 487
def charset
show_variable("character_set_database")
end
|
#collation ⇒ Object
491
492
493
|
# File 'lib/arjdbc/mysql/adapter.rb', line 491
def collation
show_variable("collation_database")
end
|
#columns(table_name, name = nil) ⇒ Object
Returns an array of Column objects for the table specified.
329
330
331
332
333
334
335
336
337
338
|
# File 'lib/arjdbc/mysql/adapter.rb', line 329
def columns(table_name, name = nil)
sql = "SHOW FULL COLUMNS FROM #{quote_table_name(table_name)}"
column = ::ActiveRecord::ConnectionAdapters::MysqlAdapter::Column
columns = execute(sql, name || 'SCHEMA')
columns.map! do |field|
column.new(field['Field'], field['Default'], field['Type'],
field['Null'] == "YES", field['Collation'], field['Extra'])
end
columns
end
|
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
|
# File 'lib/arjdbc/mysql/adapter.rb', line 33
def configure_connection
variables = config[:variables] || {}
variables[:sql_auto_is_null] = 0
wait_timeout = config[:wait_timeout]
wait_timeout = self.class.type_cast_config_to_integer(wait_timeout)
variables[:wait_timeout] = wait_timeout.is_a?(Fixnum) ? wait_timeout : 2147483
if strict_mode? && ! variables.has_key?(:sql_mode)
variables[:sql_mode] = 'STRICT_ALL_TABLES'
end
encoding = "NAMES #{config[:encoding]}, " if config[:encoding]
variable_assignments = variables.map do |k, v|
if v == ':default' || v == :default
"@@SESSION.#{k.to_s} = DEFAULT"
elsif ! v.nil?
"@@SESSION.#{k.to_s} = #{quote(v)}"
end
end.compact.join(', ')
execute("SET #{encoding} #{variable_assignments}", :skip_logging)
end
|
#create_database(name, options = {}) ⇒ Object
347
348
349
350
351
352
353
|
# File 'lib/arjdbc/mysql/adapter.rb', line 347
def create_database(name, options = {})
if options[:collation]
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
else
execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
end
end
|
#create_savepoint(name = current_savepoint_name(true)) ⇒ Object
217
218
219
|
# File 'lib/arjdbc/mysql/adapter.rb', line 217
def create_savepoint(name = current_savepoint_name(true))
log("SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#create_table(name, options = {}) ⇒ Object
365
366
367
|
# File 'lib/arjdbc/mysql/adapter.rb', line 365
def create_table(name, options = {})
super(name, {:options => "ENGINE=InnoDB DEFAULT CHARSET=utf8"}.merge(options))
end
|
#current_database ⇒ Object
360
361
362
|
# File 'lib/arjdbc/mysql/adapter.rb', line 360
def current_database
select_one("SELECT DATABASE() as db")["db"]
end
|
#disable_referential_integrity ⇒ Object
231
232
233
234
235
236
237
238
239
|
# File 'lib/arjdbc/mysql/adapter.rb', line 231
def disable_referential_integrity
fk_checks = select_value("SELECT @@FOREIGN_KEY_CHECKS")
begin
update("SET FOREIGN_KEY_CHECKS = 0")
yield
ensure
update("SET FOREIGN_KEY_CHECKS = #{fk_checks}")
end
end
|
#drop_database(name) ⇒ Object
356
357
358
|
# File 'lib/arjdbc/mysql/adapter.rb', line 356
def drop_database(name)
execute "DROP DATABASE IF EXISTS `#{name}`"
end
|
#empty_insert_statement_value ⇒ Object
527
528
529
|
# File 'lib/arjdbc/mysql/adapter.rb', line 527
def empty_insert_statement_value
"VALUES ()"
end
|
#indexes(table_name, name = nil) ⇒ Object
Returns an array of indexes for the given table.
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
|
# File 'lib/arjdbc/mysql/adapter.rb', line 301
def indexes(table_name, name = nil)
indexes = []
current_index = nil
result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name || 'SCHEMA')
result.each do |row|
key_name = row['Key_name']
if current_index != key_name
next if key_name == 'PRIMARY'
current_index = key_name
indexes <<
if self.class.const_defined?(:INDEX_TYPES)
mysql_index_type = row['Index_type'].downcase.to_sym
index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
index_using = INDEX_USINGS.include?(mysql_index_type) ? mysql_index_type : nil
IndexDefinition.new(row['Table'], key_name, row['Non_unique'].to_i == 0, [], [], nil, nil, index_type, index_using)
else
IndexDefinition.new(row['Table'], key_name, row['Non_unique'].to_i == 0, [], [])
end
end
indexes.last.columns << row["Column_name"]
indexes.last.lengths << row["Sub_part"]
end
indexes
end
|
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
143
144
145
|
# File 'lib/arjdbc/mysql/adapter.rb', line 143
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
|
#native_database_types ⇒ Object
114
115
116
|
# File 'lib/arjdbc/mysql/adapter.rb', line 114
def native_database_types
NATIVE_DATABASE_TYPES
end
|
#pk_and_sequence_for(table) ⇒ Object
Note:
Not used, only here for potential compatibility with native adapter.
Returns a table's primary key and belonging sequence.
279
280
281
282
283
284
285
286
287
|
# File 'lib/arjdbc/mysql/adapter.rb', line 279
def pk_and_sequence_for(table)
result = execute("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA').first
if result['Create Table'].to_s =~ /PRIMARY KEY\s+(?:USING\s+\w+\s+)?\((.+)\)/
keys = $1.split(","); keys.map! { |key| key.gsub(/[`"]/, "") }
return keys.length == 1 ? [ keys.first, nil ] : nil
else
return nil
end
end
|
#primary_key(table) ⇒ Object
Returns just a table's primary key.
270
271
272
273
274
|
# File 'lib/arjdbc/mysql/adapter.rb', line 270
def primary_key(table)
@connection.primary_keys(table).first
end
|
#quote(value, column = nil) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/arjdbc/mysql/adapter.rb', line 150
def quote(value, column = nil)
return value.quoted_id if value.respond_to?(:quoted_id)
return value if sql_literal?(value)
return value.to_s if column && column.type == :primary_key
if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
"x'#{column.class.string_to_binary(value).unpack("H*")[0]}'"
elsif value.kind_of?(BigDecimal)
value.to_s("F")
else
super
end
end
|
#quote_column_name(name) ⇒ Object
165
166
167
|
# File 'lib/arjdbc/mysql/adapter.rb', line 165
def quote_column_name(name)
"`#{name.to_s.gsub('`', '``')}`"
end
|
#quote_table_name(name) ⇒ Object
170
171
172
|
# File 'lib/arjdbc/mysql/adapter.rb', line 170
def quote_table_name(name)
quote_column_name(name).gsub('.', '`.`')
end
|
#quoted_columns_for_index(column_names, options = {}) ⇒ Object
532
533
534
535
536
537
538
539
540
541
542
543
|
# File 'lib/arjdbc/mysql/adapter.rb', line 532
def quoted_columns_for_index(column_names, options = {})
length = options[:length] if options.is_a?(Hash)
case length
when Hash
column_names.map { |name| length[name] ? "#{quote_column_name(name)}(#{length[name]})" : quote_column_name(name) }
when Fixnum
column_names.map { |name| "#{quote_column_name(name)}(#{length})" }
else
column_names.map { |name| quote_column_name(name) }
end
end
|
#release_savepoint(name = current_savepoint_name) ⇒ Object
227
228
229
|
# File 'lib/arjdbc/mysql/adapter.rb', line 227
def release_savepoint(name = current_savepoint_name)
log("RELEASE SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#remove_index!(table_name, index_name) ⇒ Object
376
377
378
379
|
# File 'lib/arjdbc/mysql/adapter.rb', line 376
def remove_index!(table_name, index_name)
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
end
|
#rename_column(table_name, column_name, new_column_name) ⇒ Object
423
424
425
426
427
428
429
430
431
432
433
434
435
|
# File 'lib/arjdbc/mysql/adapter.rb', line 423
def rename_column(table_name, column_name, new_column_name)
options = {}
if column = columns(table_name).find { |c| c.name == column_name.to_s }
options[:default] = column.default; options[:null] = column.null
else
raise ActiveRecord::ActiveRecordError, "No such column: #{table_name}.#{column_name}"
end
current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
add_column_options!(rename_column_sql, options)
execute(rename_column_sql)
rename_column_indexes(table_name, column_name, new_column_name) if respond_to?(:rename_column_indexes)
end
|
#rename_table(table_name, new_name) ⇒ Object
370
371
372
373
|
# File 'lib/arjdbc/mysql/adapter.rb', line 370
def rename_table(table_name, new_name)
execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
rename_table_indexes(table_name, new_name) if respond_to?(:rename_table_indexes)
end
|
#rollback_to_savepoint(name = current_savepoint_name) ⇒ Object
222
223
224
|
# File 'lib/arjdbc/mysql/adapter.rb', line 222
def rollback_to_savepoint(name = current_savepoint_name)
log("ROLLBACK TO SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#show_variable(var) ⇒ Object
481
482
483
484
485
|
# File 'lib/arjdbc/mysql/adapter.rb', line 481
def show_variable(var)
res = execute("show variables like '#{var}'")
result_row = res.detect {|row| row["Variable_name"] == var }
result_row && result_row["Value"]
end
|
#strict_mode? ⇒ Boolean
strict_mode is default since AR 4.0
69
70
71
72
73
|
# File 'lib/arjdbc/mysql/adapter.rb', line 69
def strict_mode?
config.key?(:strict) ?
self.class.type_cast_config_to_boolean(config[:strict]) :
::ActiveRecord::VERSION::MAJOR > 3
end
|
#structure_dump ⇒ Object
Deprecated. no longer used - handled with (AR built-in) Rake tasks
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'lib/arjdbc/mysql/adapter.rb', line 249
def structure_dump
if supports_views?
sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
else
sql = "SHOW TABLES"
end
@connection.execute_query_raw(sql).map do |table|
table.delete('Table_type')
table_name = table.to_a.first.last
create_table = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")
"#{create_table['Create Table']};\n\n"
end.join
end
|
#supports_index_sort_order? ⇒ Boolean
185
186
187
188
189
|
# File 'lib/arjdbc/mysql/adapter.rb', line 185
def supports_index_sort_order?
true
end
|
#supports_migrations? ⇒ Boolean
175
176
177
|
# File 'lib/arjdbc/mysql/adapter.rb', line 175
def supports_migrations?
true
end
|
#supports_primary_key? ⇒ Boolean
180
181
182
|
# File 'lib/arjdbc/mysql/adapter.rb', line 180
def supports_primary_key?
true
end
|
#supports_savepoints? ⇒ Boolean
212
213
214
|
# File 'lib/arjdbc/mysql/adapter.rb', line 212
def supports_savepoints?
true
end
|
#supports_transaction_isolation?(level = nil) ⇒ Boolean
192
193
194
195
196
197
|
# File 'lib/arjdbc/mysql/adapter.rb', line 192
def supports_transaction_isolation?
version[0] && version[0] >= 5
end
|
#supports_views? ⇒ Boolean
200
201
202
|
# File 'lib/arjdbc/mysql/adapter.rb', line 200
def supports_views?
version[0] && version[0] >= 5
end
|
#translate_exception(exception, message) ⇒ Object
546
547
548
549
550
551
552
553
554
555
556
557
|
# File 'lib/arjdbc/mysql/adapter.rb', line 546
def translate_exception(exception, message)
return super unless exception.respond_to?(:errno)
case exception.errno
when 1062
::ActiveRecord::RecordNotUnique.new(message, exception)
when 1452
::ActiveRecord::InvalidForeignKey.new(message, exception)
else
super
end
end
|
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
# File 'lib/arjdbc/mysql/adapter.rb', line 495
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
case type.to_s
when 'binary'
case limit
when 0..0xfff; "varbinary(#{limit})"
when nil; "blob"
when 0x1000..0xffffffff; "blob(#{limit})"
else raise(ActiveRecordError, "No binary type has character length #{limit}")
end
when 'integer'
case limit
when 1; 'tinyint'
when 2; 'smallint'
when 3; 'mediumint'
when nil, 4, 11; 'int(11)'
when 5..8; 'bigint'
else raise(ActiveRecordError, "No integer type has byte size #{limit}")
end
when 'text'
case limit
when 0..0xff; 'tinytext'
when nil, 0x100..0xffff; 'text'
when 0x10000..0xffffff; 'mediumtext'
when 0x1000000..0xffffffff; 'longtext'
else raise(ActiveRecordError, "No text type has character length #{limit}")
end
else
super
end
end
|
#update_sql(sql, name = nil) ⇒ Object
242
243
244
|
# File 'lib/arjdbc/mysql/adapter.rb', line 242
def update_sql(sql, name = nil)
super
end
|