Parent

ActiveRecord::ConnectionAdapters::Mysql2Adapter

Constants

ADAPTER_NAME
PRIMARY
LOST_CONNECTION_ERROR_MESSAGES
QUOTED_FALSE
NATIVE_DATABASE_TYPES

Public Class Methods

new(connection, logger, connection_options, config) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 165
165:       def initialize(connection, logger, connection_options, config)
166:         super(connection, logger)
167:         @connection_options, @config = connection_options, config
168:         @quoted_column_names, @quoted_table_names = {}, {}
169:         configure_connection
170:       end

Public Instance Methods

active?() click to toggle source

CONNECTION MANAGEMENT ====================================

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 240
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 172
172:       def adapter_name
173:         ADAPTER_NAME
174:       end
add_column(table_name, column_name, type, options = {}) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 477
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 544
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 365
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 335
335:       def begin_db_transaction
336:         execute "BEGIN"
337:       rescue Exception
338:         # Transactions aren't supported
339:       end
case_sensitive_equality_operator() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 572
572:       def case_sensitive_equality_operator
573:         "= BINARY"
574:       end
change_column(table_name, column_name, type, options = {}) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 499
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 484
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 489
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.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 421
421:       def charset
422:         show_variable 'character_set_database'
423:       end
collation() click to toggle source

Returns the database collation strategy.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 426
426:       def collation
427:         show_variable 'collation_database'
428:       end
columns(table_name, name = nil) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 459
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 341
341:       def commit_db_transaction
342:         execute "COMMIT"
343:       rescue Exception
344:         # Transactions aren't supported
345:       end
create(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) click to toggle source
Alias for: insert_sql
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 404
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 353
353:       def create_savepoint
354:         execute("SAVEPOINT #{current_savepoint_name}")
355:       end
create_table(table_name, options = {}) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 469
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 416
416:       def current_database
417:         select_value 'SELECT DATABASE() as db'
418:       end
disconnect!() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 258
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 438
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.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 307
307:       def execute(sql, name = nil)
308:         # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
309:         # made since we established the connection
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 442
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 # skip the primary key
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 324
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
Also aliased as: create
limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 576
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 188
188:       def native_database_types
189:         NATIVE_DATABASE_TYPES
190:       end
pk_and_sequence_for(table) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 557
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

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 567
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 ==================================================

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 194
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 213
213:       def quote_string(string)
214:         @connection.escape(string)
215:       end
quoted_false() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 221
221:       def quoted_false
222:         QUOTED_FALSE
223:       end
quoted_true() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 217
217:       def quoted_true
218:         QUOTED_TRUE
219:       end
reconnect!() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 248
248:       def reconnect!
249:         disconnect!
250:         connect
251:       end
recreate_database(name, options = {}) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 392
392:       def recreate_database(name, options = {})
393:         drop_database(name)
394:         create_database(name, options)
395:       end
release_savepoint() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 361
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 516
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 473
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

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 254
254:       def requires_reloading?
255:         false
256:       end
reset!() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 265
265:       def reset!
266:         disconnect!
267:         connect
268:       end
rollback_db_transaction() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 347
347:       def rollback_db_transaction
348:         execute "ROLLBACK"
349:       rescue Exception
350:         # Transactions aren't supported
351:       end
rollback_to_savepoint() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 357
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.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 302
302:       def select_rows(sql, name = nil)
303:         execute(sql, name).to_a
304:       end
show_variable(name) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 552
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 ========================================

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 379
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 176
176:       def supports_migrations?
177:         true
178:       end
supports_primary_key?() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 180
180:       def supports_primary_key?
181:         true
182:       end
supports_savepoints?() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 184
184:       def supports_savepoints?
185:         true
186:       end
tables(name = nil) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 430
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.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 531
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)'  # compatibility with MySQL default
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
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 330
330:       def update_sql(sql, name = nil)
331:         super
332:         @connection.affected_rows
333:       end

Protected Instance Methods

quoted_columns_for_index(column_names, options = {}) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 581
581:         def quoted_columns_for_index(column_names, options = {})
582:           length = options[:length] if options.is_a?(Hash)
583: 
584:           quoted_column_names = case length
585:           when Hash
586:             column_names.map {|name| length[name] ? "#{quote_column_name(name)}(#{length[name]})" : quote_column_name(name) }
587:           when Fixnum
588:             column_names.map {|name| "#{quote_column_name(name)}(#{length})"}
589:           else
590:             column_names.map {|name| quote_column_name(name) }
591:           end
592:         end
translate_exception(exception, message) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 594
594:         def translate_exception(exception, message)
595:           return super unless exception.respond_to?(:error_number)
596: 
597:           case exception.error_number
598:           when 1062
599:             RecordNotUnique.new(message, exception)
600:           when 1452
601:             InvalidForeignKey.new(message, exception)
602:           else
603:             super
604:           end
605:         end

Private Instance Methods

column_for(table_name, column_name) click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 646
646:         def column_for(table_name, column_name)
647:           unless column = columns(table_name).find { |c| c.name == column_name.to_s }
648:             raise "No such column: #{table_name}.#{column_name}"
649:           end
650:           column
651:         end
configure_connection() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 613
613:         def configure_connection
614:           @connection.query_options.merge!(:as => :array)
615: 
616:           # By default, MySQL 'where id is null' selects the last inserted id.
617:           # Turn this off. http://dev.rubyonrails.org/ticket/6778
618:           variable_assignments = ['SQL_AUTO_IS_NULL=0']
619:           encoding = @config[:encoding]
620: 
621:           # make sure we set the encoding
622:           variable_assignments << "NAMES '#{encoding}'" if encoding
623: 
624:           # increase timeout so mysql server doesn't disconnect us
625:           wait_timeout = @config[:wait_timeout]
626:           wait_timeout = 2592000 unless wait_timeout.is_a?(Fixnum)
627:           variable_assignments << "@@wait_timeout = #{wait_timeout}"
628: 
629:           execute("SET #{variable_assignments.join(', ')}", :skip_logging)
630:         end
connect() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 608
608:         def connect
609:           @connection = Mysql2::Client.new(@config)
610:           configure_connection
611:         end
select(sql, name = nil) click to toggle source

Returns an array of record hashes with the column names as keys and column values as values.

     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 634
634:         def select(sql, name = nil)
635:           execute(sql, name).each(:as => :hash)
636:         end
supports_views?() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 638
638:         def supports_views?
639:           version[0] >= 5
640:         end
version() click to toggle source
     # File lib/active_record/connection_adapters/mysql2_adapter.rb, line 642
642:         def version
643:           @version ||= @connection.info[:version].scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
644:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.