Parent

Files

Class Index [+]

Quicksearch

ActiveRecord::ConnectionAdapters::MysqlAdapter

The MySQL adapter will work with both Ruby/MySQL, which is a Ruby-based MySQL adapter that comes bundled with Active Record, and with the faster C-based MySQL/Ruby adapter (available both as a gem and from www.tmtm.org/en/mysql/ruby/).

Options:

Constants

ADAPTER_NAME
ENCODINGS

Taken from here:

  https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb

Author: TOMITA Masahiro

ENCODINGS

Public Class Methods

new(connection, logger, connection_options, config) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 126
126:       def initialize(connection, logger, connection_options, config)
127:         super
128:         @statements = StatementPool.new(@connection,
129:                                         config.fetch(:statement_limit) { 1000 })
130:         @client_encoding = nil
131:         connect
132:       end

Public Instance Methods

active?() click to toggle source

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

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 175
175:       def active?
176:         if @connection.respond_to?(:stat)
177:           @connection.stat
178:         else
179:           @connection.query 'select 1'
180:         end
181: 
182:         # mysql-ruby doesn't raise an exception when stat fails.
183:         if @connection.respond_to?(:errno)
184:           @connection.errno.zero?
185:         else
186:           true
187:         end
188:       rescue Mysql::Error
189:         false
190:       end
clear_cache!() click to toggle source

Clears the prepared statements cache.

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 223
223:       def clear_cache!
224:         @statements.clear
225:       end
client_encoding() click to toggle source

Get the client encoding for this database

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 275
275:       def client_encoding
276:         return @client_encoding if @client_encoding
277: 
278:         result = exec_query(
279:           "SHOW VARIABLES WHERE Variable_name = 'character_set_client'",
280:           'SCHEMA')
281:         @client_encoding = ENCODINGS[result.rows.last.last]
282:       end
disconnect!() click to toggle source

Disconnects from the database if already connected. Otherwise, this method does nothing.

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 200
200:       def disconnect!
201:         @connection.close rescue nil
202:       end
exec_delete(sql, name, binds) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 336
336:       def exec_delete(sql, name, binds)
337:         affected_rows = 0
338: 
339:         exec_query(sql, name, binds) do |n|
340:           affected_rows = n
341:         end
342: 
343:         affected_rows
344:       end
Also aliased as: exec_update
exec_query(sql, name = 'SQL', binds = []) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 284
284:       def exec_query(sql, name = 'SQL', binds = [])
285:         # If the configuration sets prepared_statements:false, binds will
286:         # always be empty, since the bind variables will have been already
287:         # substituted and removed from binds by BindVisitor, so this will
288:         # effectively disable prepared statement usage completely.
289:         if binds.empty?
290:           result_set, affected_rows = exec_without_stmt(sql, name)
291:         else
292:           result_set, affected_rows = exec_stmt(sql, name, binds)
293:         end
294: 
295:         yield affected_rows if block_given?
296: 
297:         result_set
298:       end
exec_update(sql, name, binds) click to toggle source
Alias for: exec_delete
execute_and_free(sql, name = nil) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 323
323:       def execute_and_free(sql, name = nil)
324:         result = execute(sql, name)
325:         ret = yield result
326:         result.free
327:         ret
328:       end
last_inserted_id(result) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 300
300:       def last_inserted_id(result)
301:         @connection.insert_id
302:       end
reconnect!() click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 192
192:       def reconnect!
193:         disconnect!
194:         clear_cache!
195:         connect
196:       end
reset!() click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 204
204:       def reset!
205:         if @connection.respond_to?(:change_user)
206:           # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to
207:           # reset the connection is to change the user to the same user.
208:           @connection.change_user(@config[:username], @config[:password], @config[:database])
209:           configure_connection
210:         end
211:       end
select_rows(sql, name = nil) click to toggle source

DATABASE STATEMENTS ======================================

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 215
215:       def select_rows(sql, name = nil)
216:         @connection.query_with_result = true
217:         rows = exec_query(sql, name).rows
218:         @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
219:         rows
220:       end
supports_statement_cache?() click to toggle source

Returns true, since this connection adapter supports prepared statement caching.

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 136
136:       def supports_statement_cache?
137:         true
138:       end
type_cast(value, column) click to toggle source

QUOTING ==================================================

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 163
163:       def type_cast(value, column)
164:         return super unless value == true || value == false
165: 
166:         value ? 1 : 0
167:       end

Private Instance Methods

configure_connection() click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 419
419:       def configure_connection
420:         encoding = @config[:encoding]
421:         execute("SET NAMES '#{encoding}'", :skip_logging) if encoding
422: 
423:         # By default, MySQL 'where id is null' selects the last inserted id.
424:         # Turn this off. http://dev.rubyonrails.org/ticket/6778
425:         execute("SET SQL_AUTO_IS_NULL=0", :skip_logging)
426:       end
connect() click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 397
397:       def connect
398:         encoding = @config[:encoding]
399:         if encoding
400:           @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil
401:         end
402: 
403:         if @config[:sslca] || @config[:sslkey]
404:           @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher])
405:         end
406: 
407:         @connection.options(Mysql::OPT_CONNECT_TIMEOUT, @config[:connect_timeout]) if @config[:connect_timeout]
408:         @connection.options(Mysql::OPT_READ_TIMEOUT, @config[:read_timeout]) if @config[:read_timeout]
409:         @connection.options(Mysql::OPT_WRITE_TIMEOUT, @config[:write_timeout]) if @config[:write_timeout]
410: 
411:         @connection.real_connect(*@connection_options)
412: 
413:         # reconnect must be set after real_connect is called, because real_connect sets it to false internally
414:         @connection.reconnect = !!@config[:reconnect] if @connection.respond_to?(:reconnect=)
415: 
416:         configure_connection
417:       end
exec_stmt(sql, name, binds) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 355
355:       def exec_stmt(sql, name, binds)
356:         cache = {}
357:         log(sql, name, binds) do
358:           if binds.empty?
359:             stmt = @connection.prepare(sql)
360:           else
361:             cache = @statements[sql] ||= {
362:               :stmt => @connection.prepare(sql)
363:             }
364:             stmt = cache[:stmt]
365:           end
366: 
367:           begin
368:             stmt.execute(*binds.map { |col, val| type_cast(val, col) })
369:           rescue Mysql::Error => e
370:             # Older versions of MySQL leave the prepared statement in a bad
371:             # place when an error occurs. To support older mysql versions, we
372:             # need to close the statement and delete the statement from the
373:             # cache.
374:             stmt.close
375:             @statements.delete sql
376:             raise e
377:           end
378: 
379:           cols = nil
380:           if metadata = stmt.result_metadata
381:             cols = cache[:cols] ||= metadata.fetch_fields.map { |field|
382:               field.name
383:             }
384:           end
385: 
386:           result_set = ActiveRecord::Result.new(cols, stmt.to_a) if cols
387:           affected_rows = stmt.affected_rows
388: 
389:           stmt.result_metadata.free if cols
390:           stmt.free_result
391:           stmt.close if binds.empty?
392: 
393:           [result_set, affected_rows]
394:         end
395:       end
select(sql, name = nil, binds = []) click to toggle source
     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 428
428:       def select(sql, name = nil, binds = [])
429:         @connection.query_with_result = true
430:         rows = exec_query(sql, name, binds).to_a
431:         @connection.more_results && @connection.next_result    # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped
432:         rows
433:       end
version() click to toggle source

Returns the version of the connected MySQL server.

     # File lib/active_record/connection_adapters/mysql_adapter.rb, line 436
436:       def version
437:         @version ||= @connection.server_info.scan(/^(\d+)\.(\d+)\.(\d+)/).flatten.map { |v| v.to_i }
438:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.