AbstractMysqlAdapter
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:
:host - Defaults to “localhost”.
:port - Defaults to 3306.
:socket - Defaults to “/tmp/mysql.sock“.
:username - Defaults to “root“
:password - Defaults to nothing.
:database - The name of the database. No default, must be provided.
:encoding - (Optional) Sets the client encoding by executing
“SET NAMES
:reconnect - Defaults to false (See MySQL documentation: dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html).
:sslca - Necessary to use MySQL with an SSL connection.
:sslkey - Necessary to use MySQL with an SSL connection.
:sslcert - Necessary to use MySQL with an SSL connection.
:sslcapath - Necessary to use MySQL with an SSL connection.
:sslcipher - Necessary to use MySQL with an SSL connection.
Taken from here:
https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
Author: TOMITA Masahiro
# 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
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
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
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
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
# 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
# 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
# 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
# File lib/active_record/connection_adapters/mysql_adapter.rb, line 300 300: def last_inserted_id(result) 301: @connection.insert_id 302: end
# File lib/active_record/connection_adapters/mysql_adapter.rb, line 192 192: def reconnect! 193: disconnect! 194: clear_cache! 195: connect 196: end
# 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
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
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
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
# 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
# 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
# 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
# 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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.