active?()
click to toggle source
CONNECTION MANAGEMENT ====================================
240: def active?
241: return false unless @connection
242: @connection.query 'select 1'
243: true
244: rescue Mysql2::Error
245: false
246: end
adapter_name()
click to toggle source
172: def adapter_name
173: ADAPTER_NAME
174: end
add_column(table_name, column_name, type, options = {})
click to toggle source
477: def add_column(table_name, column_name, type, options = {})
478: 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])}"
479: add_column_options!(add_column_sql, options)
480: add_column_position!(add_column_sql, options)
481: execute(add_column_sql)
482: end
add_column_position!(sql, options)
click to toggle source
544: def add_column_position!(sql, options)
545: if options[:first]
546: sql << " FIRST"
547: elsif options[:after]
548: sql << " AFTER #{quote_column_name(options[:after])}"
549: end
550: end
add_limit_offset!(sql, options)
click to toggle source
365: def add_limit_offset!(sql, options)
366: limit, offset = options[:limit], options[:offset]
367: if limit && offset
368: sql << " LIMIT #{offset.to_i}, #{sanitize_limit(limit)}"
369: elsif limit
370: sql << " LIMIT #{sanitize_limit(limit)}"
371: elsif offset
372: sql << " OFFSET #{offset.to_i}"
373: end
374: sql
375: end
begin_db_transaction()
click to toggle source
335: def begin_db_transaction
336: execute "BEGIN"
337: rescue Exception
338:
339: end
case_sensitive_equality_operator()
click to toggle source
572: def case_sensitive_equality_operator
573: "= BINARY"
574: end
change_column(table_name, column_name, type, options = {})
click to toggle source
499: def change_column(table_name, column_name, type, options = {})
500: column = column_for(table_name, column_name)
501:
502: unless options_include_default?(options)
503: options[:default] = column.default
504: end
505:
506: unless options.has_key?(:null)
507: options[:null] = column.null
508: end
509:
510: 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])}"
511: add_column_options!(change_column_sql, options)
512: add_column_position!(change_column_sql, options)
513: execute(change_column_sql)
514: end
change_column_default(table_name, column_name, default)
click to toggle source
484: def change_column_default(table_name, column_name, default)
485: column = column_for(table_name, column_name)
486: change_column table_name, column_name, column.sql_type, :default => default
487: end
change_column_null(table_name, column_name, null, default = nil)
click to toggle source
489: def change_column_null(table_name, column_name, null, default = nil)
490: column = column_for(table_name, column_name)
491:
492: unless null || default.nil?
493: execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
494: end
495:
496: change_column table_name, column_name, column.sql_type, :null => null
497: end
charset()
click to toggle source
Returns the database character set.
421: def charset
422: show_variable 'character_set_database'
423: end
collation()
click to toggle source
Returns the database collation strategy.
426: def collation
427: show_variable 'collation_database'
428: end
columns(table_name, name = nil)
click to toggle source
459: def columns(table_name, name = nil)
460: sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
461: columns = []
462: result = execute(sql, :skip_logging)
463: result.each(:symbolize_keys => true, :as => :hash) { |field|
464: columns << Mysql2Column.new(field[:Field], field[:Default], field[:Type], field[:Null] == "YES")
465: }
466: columns
467: end
commit_db_transaction()
click to toggle source
341: def commit_db_transaction
342: execute "COMMIT"
343: rescue Exception
344:
345: end
create(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
click to toggle source
create_database(name, options = {})
click to toggle source
Create a new MySQL database with optional :charset and
:collation. Charset defaults to utf8.
Example:
create_database 'charset_test', :charset => 'latin1', :collation => 'latin1_bin'
create_database 'matt_development'
create_database 'matt_development', :charset => :big5
404: def create_database(name, options = {})
405: if options[:collation]
406: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}` COLLATE `#{options[:collation]}`"
407: else
408: execute "CREATE DATABASE `#{name}` DEFAULT CHARACTER SET `#{options[:charset] || 'utf8'}`"
409: end
410: end
create_savepoint()
click to toggle source
353: def create_savepoint
354: execute("SAVEPOINT #{current_savepoint_name}")
355: end
create_table(table_name, options = {})
click to toggle source
469: def create_table(table_name, options = {})
470: super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB"))
471: end
current_database()
click to toggle source
416: def current_database
417: select_value 'SELECT DATABASE() as db'
418: end
disconnect!()
click to toggle source
258: def disconnect!
259: unless @connection.nil?
260: @connection.close
261: @connection = nil
262: end
263: end
drop_table(table_name, options = {})
click to toggle source
438: def drop_table(table_name, options = {})
439: super(table_name, options)
440: end
execute(sql, name = nil)
click to toggle source
Executes the SQL statement in the context of this connection.
307: def execute(sql, name = nil)
308:
309:
310: @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
311: if name == :skip_logging
312: @connection.query(sql)
313: else
314: log(sql, name) { @connection.query(sql) }
315: end
316: rescue ActiveRecord::StatementInvalid => exception
317: if exception.message.split(":").first =~ /Packets out of order/
318: raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
319: else
320: raise
321: end
322: end
indexes(table_name, name = nil)
click to toggle source
442: def indexes(table_name, name = nil)
443: indexes = []
444: current_index = nil
445: result = execute("SHOW KEYS FROM #{quote_table_name(table_name)}", name)
446: result.each(:symbolize_keys => true, :as => :hash) do |row|
447: if current_index != row[:Key_name]
448: next if row[:Key_name] == PRIMARY
449: current_index = row[:Key_name]
450: indexes << IndexDefinition.new(row[:Table], row[:Key_name], row[:Non_unique] == 0, [], [])
451: end
452:
453: indexes.last.columns << row[:Column_name]
454: indexes.last.lengths << row[:Sub_part]
455: end
456: indexes
457: end
insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
click to toggle source
324: def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
325: super
326: id_value || @connection.last_id
327: end
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
click to toggle source
576: def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
577: where_sql
578: end
native_database_types()
click to toggle source
188: def native_database_types
189: NATIVE_DATABASE_TYPES
190: end
pk_and_sequence_for(table)
click to toggle source
557: def pk_and_sequence_for(table)
558: keys = []
559: result = execute("describe #{quote_table_name(table)}")
560: result.each(:symbolize_keys => true, :as => :hash) do |row|
561: keys << row[:Field] if row[:Key] == "PRI"
562: end
563: keys.length == 1 ? [keys.first, nil] : nil
564: end
primary_key(table)
click to toggle source
Returns just a table’s primary key
567: def primary_key(table)
568: pk_and_sequence = pk_and_sequence_for(table)
569: pk_and_sequence && pk_and_sequence.first
570: end
quote(value, column = nil)
click to toggle source
QUOTING ==================================================
194: def quote(value, column = nil)
195: if value.kind_of?(String) && column && column.type == :binary && column.class.respond_to?(:string_to_binary)
196: s = column.class.string_to_binary(value).unpack("H*")[0]
197: "x'#{s}'"
198: elsif value.kind_of?(BigDecimal)
199: value.to_s("F")
200: else
201: super
202: end
203: end
quote_string(string)
click to toggle source
213: def quote_string(string)
214: @connection.escape(string)
215: end
quoted_false()
click to toggle source
221: def quoted_false
222: QUOTED_FALSE
223: end
quoted_true()
click to toggle source
217: def quoted_true
218: QUOTED_TRUE
219: end
reconnect!()
click to toggle source
248: def reconnect!
249: disconnect!
250: connect
251: end
recreate_database(name, options = {})
click to toggle source
392: def recreate_database(name, options = {})
393: drop_database(name)
394: create_database(name, options)
395: end
release_savepoint()
click to toggle source
361: def release_savepoint
362: execute("RELEASE SAVEPOINT #{current_savepoint_name}")
363: end
rename_column(table_name, column_name, new_column_name)
click to toggle source
516: def rename_column(table_name, column_name, new_column_name)
517: options = {}
518: if column = columns(table_name).find { |c| c.name == column_name.to_s }
519: options[:default] = column.default
520: options[:null] = column.null
521: else
522: raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
523: end
524: current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
525: rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
526: add_column_options!(rename_column_sql, options)
527: execute(rename_column_sql)
528: end
rename_table(table_name, new_name)
click to toggle source
473: def rename_table(table_name, new_name)
474: execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
475: end
requires_reloading?()
click to toggle source
this is set to true in 2.3, but we don’t want it to be
254: def requires_reloading?
255: false
256: end
reset!()
click to toggle source
265: def reset!
266: disconnect!
267: connect
268: end
rollback_db_transaction()
click to toggle source
347: def rollback_db_transaction
348: execute "ROLLBACK"
349: rescue Exception
350:
351: end
rollback_to_savepoint()
click to toggle source
357: def rollback_to_savepoint
358: execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
359: end
select_rows(sql, name = nil)
click to toggle source
Returns an array of arrays containing the field values. Order is the same
as that returned by columns.
302: def select_rows(sql, name = nil)
303: execute(sql, name).to_a
304: end
show_variable(name)
click to toggle source
552: def show_variable(name)
553: variables = select_all("SHOW VARIABLES LIKE '#{name}'")
554: variables.first['Value'] unless variables.empty?
555: end
structure_dump()
click to toggle source
SCHEMA STATEMENTS ========================================
379: def structure_dump
380: if supports_views?
381: sql = "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'"
382: else
383: sql = "SHOW TABLES"
384: end
385:
386: select_all(sql).inject("") do |structure, table|
387: table.delete('Table_type')
388: structure += select_one("SHOW CREATE TABLE #{quote_table_name(table.to_a.first.last)}")["Create Table"] + ";\n\n"
389: end
390: end
supports_migrations?()
click to toggle source
176: def supports_migrations?
177: true
178: end
supports_primary_key?()
click to toggle source
180: def supports_primary_key?
181: true
182: end
supports_savepoints?()
click to toggle source
184: def supports_savepoints?
185: true
186: end
tables(name = nil)
click to toggle source
430: def tables(name = nil)
431: tables = []
432: execute("SHOW TABLES", name).each do |field|
433: tables << field.first
434: end
435: tables
436: end
type_to_sql(type, limit = nil, precision = nil, scale = nil)
click to toggle source
Maps logical Rails types to MySQL-specific data types.
531: def type_to_sql(type, limit = nil, precision = nil, scale = nil)
532: return super unless type.to_s == 'integer'
533:
534: case limit
535: when 1; 'tinyint'
536: when 2; 'smallint'
537: when 3; 'mediumint'
538: when nil, 4, 11; 'int(11)'
539: when 5..8; 'bigint'
540: else raise(ActiveRecordError, "No integer type has byte size #{limit}")
541: end
542: end
update_sql(sql, name = nil)
click to toggle source
330: def update_sql(sql, name = nil)
331: super
332: @connection.affected_rows
333: end