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
-
#add_limit_offset!(sql, options) ⇒ Object
-
#case_insensitive_comparison(table, attribute, column, value) ⇒ Object
-
#case_sensitive_comparison(table, attribute, column, value) ⇒ Object
-
#case_sensitive_equality_operator ⇒ Object
-
#case_sensitive_modifier(node, table_attribute) ⇒ 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
-
#clear_cache! ⇒ 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
-
#drop_table(table_name, options = {}) ⇒ Object
-
#empty_insert_statement_value ⇒ Object
-
#foreign_keys(table_name) ⇒ Object
-
#index_algorithms ⇒ Object
-
#indexes(table_name, name = nil) ⇒ Object
Returns an array of indexes for the given table.
-
#initialize_schema_migrations_table ⇒ Object
-
#jdbc_column_class ⇒ Object
-
#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_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_index(table_name, old_name, new_name) ⇒ Object
-
#rename_table(table_name, new_name) ⇒ Object
-
#rollback_to_savepoint(name = current_savepoint_name) ⇒ Object
-
#schema_creation ⇒ Object
-
#show_variable(var) ⇒ Object
-
#strict_mode? ⇒ Boolean
-
#structure_dump ⇒ Object
deprecated
Deprecated.
no longer used - handled with (AR built-in) Rake tasks
-
#subquery_for(key, select) ⇒ Object
MySQL is too stupid to create a temporary table for use subquery, so we have to give it some prompting in the form of a subsubquery.
-
#supports_foreign_keys? ⇒ Boolean
-
#supports_index_sort_order? ⇒ Boolean
-
#supports_indexes_in_create? ⇒ Boolean
-
#supports_migrations? ⇒ Boolean
-
#supports_primary_key? ⇒ Boolean
-
#supports_rename_index? ⇒ Boolean
-
#supports_savepoints? ⇒ Boolean
-
#supports_transaction_isolation?(level = nil) ⇒ Boolean
-
#supports_views? ⇒ Boolean
-
#translate_exception(exception, message) ⇒ Object
protected
-
#truncate(table_name, name = nil) ⇒ Object
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Maps logical Rails types to MySQL-specific data types.
-
#update_sql(sql, name = nil) ⇒ Object
-
#valid_type?(type) ⇒ Boolean
#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
136
137
138
|
# File 'lib/arjdbc/mysql/adapter.rb', line 136
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.
96
|
# File 'lib/arjdbc/mysql/adapter.rb', line 96
def self.emulate_booleans; @@emulate_booleans; end
|
.emulate_booleans=(emulate) ⇒ Object
98
|
# File 'lib/arjdbc/mysql/adapter.rb', line 98
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
94
|
# File 'lib/arjdbc/mysql/adapter.rb', line 94
def self.emulate_booleans?; @@emulate_booleans; end
|
.jdbc_connection_class ⇒ Object
Instance Method Details
#adapter_name ⇒ Object
132
133
134
|
# File 'lib/arjdbc/mysql/adapter.rb', line 132
def adapter_name
ADAPTER_NAME
end
|
#add_column(table_name, column_name, type, options = {}) ⇒ Object
593
594
595
596
597
598
|
# File 'lib/arjdbc/mysql/adapter.rb', line 593
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
659
660
661
662
663
664
665
|
# File 'lib/arjdbc/mysql/adapter.rb', line 659
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.
669
670
671
672
673
674
675
676
677
678
679
|
# File 'lib/arjdbc/mysql/adapter.rb', line 669
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_insensitive_comparison(table, attribute, column, value) ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/arjdbc/mysql/adapter.rb', line 167
def case_insensitive_comparison(table, attribute, column, value)
if column.case_sensitive?
super
else
table[attribute].eq(value)
end
end
|
#case_sensitive_comparison(table, attribute, column, value) ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/arjdbc/mysql/adapter.rb', line 159
def case_sensitive_comparison(table, attribute, column, value)
if column.case_sensitive?
table[attribute].eq(value)
else
super
end
end
|
#case_sensitive_equality_operator ⇒ Object
146
147
148
|
# File 'lib/arjdbc/mysql/adapter.rb', line 146
def case_sensitive_equality_operator
"= BINARY"
end
|
#case_sensitive_modifier(node, table_attribute) ⇒ Object
150
151
152
|
# File 'lib/arjdbc/mysql/adapter.rb', line 150
def case_sensitive_modifier(node)
Arel::Nodes::Bin.new(node)
end
|
#change_column_default(table_name, column_name, default) ⇒ Object
600
601
602
603
|
# File 'lib/arjdbc/mysql/adapter.rb', line 600
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
605
606
607
608
609
610
611
612
613
|
# File 'lib/arjdbc/mysql/adapter.rb', line 605
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
709
710
711
|
# File 'lib/arjdbc/mysql/adapter.rb', line 709
def charset
show_variable("character_set_database")
end
|
#clear_cache! ⇒ Object
766
767
768
769
|
# File 'lib/arjdbc/mysql/adapter.rb', line 766
def clear_cache!
super
reload_type_map
end
|
#collation ⇒ Object
713
714
715
|
# File 'lib/arjdbc/mysql/adapter.rb', line 713
def collation
show_variable("collation_database")
end
|
#columns(table_name, name = nil) ⇒ Object
Returns an array of Column
objects for the table specified.
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
# File 'lib/arjdbc/mysql/adapter.rb', line 405
def columns(table_name, name = nil)
sql = "SHOW FULL #{AR40 ? 'FIELDS' : 'COLUMNS'} FROM #{quote_table_name(table_name)}"
columns = execute(sql, name || 'SCHEMA')
strict = strict_mode?
pass_cast_type = respond_to?(:lookup_cast_type)
columns.map! do |field|
sql_type = field['Type']
null = field['Null'] == "YES"
if pass_cast_type
cast_type = lookup_cast_type(sql_type)
jdbc_column_class.new(field['Field'], field['Default'], cast_type, sql_type, null, field['Collation'], strict, field['Extra'])
else
jdbc_column_class.new(field['Field'], field['Default'], sql_type, null, field['Collation'], strict, field['Extra'])
end
end
columns
end
|
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
|
# File 'lib/arjdbc/mysql/adapter.rb', line 44
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
501
502
503
504
505
506
507
|
# File 'lib/arjdbc/mysql/adapter.rb', line 501
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
297
298
299
|
# File 'lib/arjdbc/mysql/adapter.rb', line 297
def create_savepoint(name = current_savepoint_name(true))
log("SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#create_table(name, options = {}) ⇒ Object
523
524
525
|
# File 'lib/arjdbc/mysql/adapter.rb', line 523
def create_table(name, options = {})
super(name, { :options => "ENGINE=InnoDB" }.merge(options))
end
|
#current_database ⇒ Object
514
515
516
|
# File 'lib/arjdbc/mysql/adapter.rb', line 514
def current_database
select_one("SELECT DATABASE() as db")['db']
end
|
#disable_referential_integrity ⇒ Object
311
312
313
314
315
316
317
318
319
|
# File 'lib/arjdbc/mysql/adapter.rb', line 311
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
510
511
512
|
# File 'lib/arjdbc/mysql/adapter.rb', line 510
def drop_database(name)
execute "DROP DATABASE IF EXISTS `#{name}`"
end
|
#drop_table(table_name, options = {}) ⇒ Object
527
528
529
|
# File 'lib/arjdbc/mysql/adapter.rb', line 527
def drop_table(table_name, options = {})
execute "DROP#{' TEMPORARY' if options[:temporary]} TABLE #{quote_table_name(table_name)}"
end
|
#empty_insert_statement_value ⇒ Object
757
758
759
|
# File 'lib/arjdbc/mysql/adapter.rb', line 757
def empty_insert_statement_value
"VALUES ()"
end
|
#foreign_keys(table_name) ⇒ Object
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
# File 'lib/arjdbc/mysql/adapter.rb', line 558
def foreign_keys(table_name)
fk_info = select_all "" <<
"SELECT fk.referenced_table_name as 'to_table' " <<
",fk.referenced_column_name as 'primary_key' " <<
",fk.column_name as 'column' " <<
",fk.constraint_name as 'name' " <<
"FROM information_schema.key_column_usage fk " <<
"WHERE fk.referenced_column_name is not null " <<
"AND fk.table_schema = '#{current_database}' " <<
"AND fk.table_name = '#{table_name}'"
create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]
fk_info.map! do |row|
options = {
:column => row['column'], :name => row['name'], :primary_key => row['primary_key']
}
options[:on_update] = (create_table_info, row['name'], "UPDATE")
options[:on_delete] = (create_table_info, row['name'], "DELETE")
ForeignKeyDefinition.new(table_name, row['to_table'], options)
end
end
|
#index_algorithms ⇒ Object
280
281
282
|
# File 'lib/arjdbc/mysql/adapter.rb', line 280
def index_algorithms
{ :default => 'ALGORITHM = DEFAULT', :copy => 'ALGORITHM = COPY', :inplace => 'ALGORITHM = INPLACE' }
end
|
#indexes(table_name, name = nil) ⇒ Object
Returns an array of indexes for the given table.
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
|
# File 'lib/arjdbc/mysql/adapter.rb', line 377
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
|
#initialize_schema_migrations_table ⇒ Object
179
180
181
182
183
184
185
|
# File 'lib/arjdbc/mysql/adapter.rb', line 179
def initialize_schema_migrations_table
if @config[:encoding] == 'utf8mb4'
ActiveRecord::SchemaMigration.create_table(191)
else
ActiveRecord::SchemaMigration.create_table
end
end
|
#jdbc_column_class ⇒ Object
#limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) ⇒ Object
175
176
177
|
# File 'lib/arjdbc/mysql/adapter.rb', line 175
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end
|
#native_database_types ⇒ Object
125
126
127
|
# File 'lib/arjdbc/mysql/adapter.rb', line 125
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.
359
360
361
362
363
364
365
366
367
|
# File 'lib/arjdbc/mysql/adapter.rb', line 359
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.
350
351
352
353
354
|
# File 'lib/arjdbc/mysql/adapter.rb', line 350
def primary_key(table)
@connection.primary_keys(table).first
end
|
#quote_column_name(name) ⇒ Object
231
232
233
|
# File 'lib/arjdbc/mysql/adapter.rb', line 231
def quote_column_name(name)
"`#{name.to_s.gsub('`', '``')}`"
end
|
#quote_table_name(name) ⇒ Object
236
237
238
|
# File 'lib/arjdbc/mysql/adapter.rb', line 236
def quote_table_name(name)
quote_column_name(name).gsub('.', '`.`')
end
|
#quoted_columns_for_index(column_names, options = {}) ⇒ Object
846
847
848
849
850
851
852
853
854
855
856
857
|
# File 'lib/arjdbc/mysql/adapter.rb', line 846
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
307
308
309
|
# File 'lib/arjdbc/mysql/adapter.rb', line 307
def release_savepoint(name = current_savepoint_name)
log("RELEASE SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#remove_index!(table_name, index_name) ⇒ Object
538
539
540
541
|
# File 'lib/arjdbc/mysql/adapter.rb', line 538
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
# File 'lib/arjdbc/mysql/adapter.rb', line 640
def rename_column(table_name, column_name, new_column_name)
options = {}
if column = columns(table_name).find { |c| c.name == column_name.to_s }
type = column.type
options[:default] = column.default if type != :text && type != :binary
options[:null] = column.null
else
raise 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_index(table_name, old_name, new_name) ⇒ Object
544
545
546
547
548
549
550
551
|
# File 'lib/arjdbc/mysql/adapter.rb', line 544
def rename_index(table_name, old_name, new_name)
if supports_rename_index?
validate_index_length!(table_name, new_name) if respond_to?(:validate_index_length!)
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME INDEX #{quote_table_name(old_name)} TO #{quote_table_name(new_name)}"
else
super
end
end
|
#rename_table(table_name, new_name) ⇒ Object
532
533
534
535
|
# File 'lib/arjdbc/mysql/adapter.rb', line 532
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
302
303
304
|
# File 'lib/arjdbc/mysql/adapter.rb', line 302
def rollback_to_savepoint(name = current_savepoint_name)
log("ROLLBACK TO SAVEPOINT #{name}", 'Savepoint') { super }
end
|
#schema_creation ⇒ Object
489
|
# File 'lib/arjdbc/mysql/adapter.rb', line 489
def schema_creation; SchemaCreation.new self end
|
#show_variable(var) ⇒ Object
703
704
705
706
707
|
# File 'lib/arjdbc/mysql/adapter.rb', line 703
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
80
81
82
83
84
|
# File 'lib/arjdbc/mysql/adapter.rb', line 80
def strict_mode?
config.key?(:strict) ?
self.class.type_cast_config_to_boolean(config[:strict]) :
AR40 end
|
#structure_dump ⇒ Object
Deprecated. no longer used - handled with (AR built-in) Rake tasks
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/arjdbc/mysql/adapter.rb', line 329
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
|
#subquery_for(key, select) ⇒ Object
MySQL is too stupid to create a temporary table for use subquery, so we have
to give it some prompting in the form of a subsubquery. Ugh!
837
838
839
840
841
842
843
844
|
# File 'lib/arjdbc/mysql/adapter.rb', line 837
def subquery_for(key, select)
subsubselect = select.clone
subsubselect.projections = [key]
subselect = Arel::SelectManager.new(select.engine)
subselect.project Arel.sql(key.name)
subselect.from subsubselect.as('__active_record_temp')
end
|
#supports_foreign_keys? ⇒ Boolean
556
|
# File 'lib/arjdbc/mysql/adapter.rb', line 556
def supports_foreign_keys?; true end
|
#supports_index_sort_order? ⇒ Boolean
251
252
253
254
255
|
# File 'lib/arjdbc/mysql/adapter.rb', line 251
def supports_index_sort_order?
true
end
|
#supports_indexes_in_create? ⇒ Boolean
258
259
260
|
# File 'lib/arjdbc/mysql/adapter.rb', line 258
def supports_indexes_in_create?
true
end
|
#supports_migrations? ⇒ Boolean
241
242
243
|
# File 'lib/arjdbc/mysql/adapter.rb', line 241
def supports_migrations?
true
end
|
#supports_primary_key? ⇒ Boolean
246
247
248
|
# File 'lib/arjdbc/mysql/adapter.rb', line 246
def supports_primary_key?
true
end
|
#supports_rename_index? ⇒ Boolean
275
276
277
278
|
# File 'lib/arjdbc/mysql/adapter.rb', line 275
def supports_rename_index?
return false if mariadb? || ! version[0]
(version[0] == 5 && version[1] >= 7) || version[0] >= 6
end
|
#supports_savepoints? ⇒ Boolean
292
293
294
|
# File 'lib/arjdbc/mysql/adapter.rb', line 292
def supports_savepoints?
true
end
|
#supports_transaction_isolation?(level = nil) ⇒ Boolean
263
264
265
266
267
268
|
# File 'lib/arjdbc/mysql/adapter.rb', line 263
def supports_transaction_isolation?
version[0] && version[0] >= 5
end
|
#supports_views? ⇒ Boolean
271
272
273
|
# File 'lib/arjdbc/mysql/adapter.rb', line 271
def supports_views?
version[0] && version[0] >= 5
end
|
#translate_exception(exception, message) ⇒ Object
860
861
862
863
864
865
866
867
868
869
870
871
|
# File 'lib/arjdbc/mysql/adapter.rb', line 860
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
|
#truncate(table_name, name = nil) ⇒ Object
518
519
520
|
# File 'lib/arjdbc/mysql/adapter.rb', line 518
def truncate(table_name, name = nil)
execute "TRUNCATE TABLE #{quote_table_name(table_name)}", name
end
|
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
Maps logical Rails types to MySQL-specific data types.
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
# File 'lib/arjdbc/mysql/adapter.rb', line 718
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
when 'datetime'
return super unless precision
case precision
when 0..6; "datetime(#{precision})"
else raise(ActiveRecordError, "No datetime type has precision of #{precision}. The allowed range of precision is from 0 to 6.")
end
else
super
end
end
|
#update_sql(sql, name = nil) ⇒ Object
322
323
324
|
# File 'lib/arjdbc/mysql/adapter.rb', line 322
def update_sql(sql, name = nil)
super
end
|
#valid_type?(type) ⇒ Boolean
762
763
764
|
# File 'lib/arjdbc/mysql/adapter.rb', line 762
def valid_type?(type)
! native_database_types[type].nil?
end
|