Class: ActiveRecord::ConnectionAdapters::RubyMysqlAdapter
- Inherits:
-
AbstractMysqlAdapter
- Object
- AbstractMysqlAdapter
- ActiveRecord::ConnectionAdapters::RubyMysqlAdapter
- Defined in:
- lib/active_record/connection_adapters/ruby_mysql_adapter.rb
Overview
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 <encoding>” after connection. -
: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.
Defined Under Namespace
Classes: Column, StatementPool
Constant Summary collapse
- ADAPTER_NAME =
'RubyMysql'- ENCODINGS =
Taken from here:
https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rbAuthor: TOMITA Masahiro <[email protected]>
Hash.new { |h,k| h[k] = k }
Instance Method Summary collapse
-
#active? ⇒ Boolean
CONNECTION MANAGEMENT ====================================.
-
#begin_db_transaction ⇒ Object
:nodoc:.
-
#clear_cache! ⇒ Object
Clears the prepared statements cache.
-
#client_encoding ⇒ Object
Get the client encoding for this database.
-
#disconnect! ⇒ Object
Disconnects from the database if already connected.
-
#each_hash(result) ⇒ Object
HELPER METHODS ===========================================.
-
#error_number(exception) ⇒ Object
:nodoc:.
- #exec_delete(sql, name, binds) ⇒ Object (also: #exec_update)
- #exec_query(sql, name = 'SQL', binds = []) ⇒ Object
-
#exec_without_stmt(sql, name = 'SQL') ⇒ Object
:nodoc:.
- #execute_and_free(sql, name = nil) ⇒ Object
-
#initialize(connection, logger, connection_options, config) ⇒ RubyMysqlAdapter
constructor
A new instance of RubyMysqlAdapter.
-
#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
(also: #create)
:nodoc:.
- #last_inserted_id(result) ⇒ Object
-
#new_column(field, default, type, null, collation) ⇒ Object
:nodoc:.
-
#quote_string(string) ⇒ Object
:nodoc:.
- #reconnect! ⇒ Object
- #reset! ⇒ Object
-
#select_rows(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================.
-
#supports_statement_cache? ⇒ Boolean
Returns true, since this connection adapter supports prepared statement caching.
-
#type_cast(value, column) ⇒ Object
QUOTING ==================================================.
Constructor Details
#initialize(connection, logger, connection_options, config) ⇒ RubyMysqlAdapter
Returns a new instance of RubyMysqlAdapter.
125 126 127 128 129 130 131 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 125 def initialize(connection, logger, , config) super @statements = StatementPool.new(@connection, config.fetch(:statement_limit) { 1000 }) @client_encoding = nil connect end |
Instance Method Details
#active? ⇒ Boolean
CONNECTION MANAGEMENT ====================================
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 174 def active? if @connection.respond_to?(:stat) @connection.stat else @connection.query 'select 1' end # mysql-ruby doesn't raise an exception when stat fails. if @connection.respond_to?(:errno) @connection.errno.zero? else true end rescue Mysql::Error false end |
#begin_db_transaction ⇒ Object
:nodoc:
334 335 336 337 338 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 334 def begin_db_transaction #:nodoc: exec_without_stmt "BEGIN" rescue Mysql::Error # Transactions aren't supported end |
#clear_cache! ⇒ Object
Clears the prepared statements cache.
222 223 224 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 222 def clear_cache! @statements.clear end |
#client_encoding ⇒ Object
Get the client encoding for this database
274 275 276 277 278 279 280 281 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 274 def client_encoding return @client_encoding if @client_encoding result = exec_query( "SHOW VARIABLES WHERE Variable_name = 'character_set_client'", 'SCHEMA') @client_encoding = ENCODINGS[result.rows.last.last] end |
#disconnect! ⇒ Object
Disconnects from the database if already connected. Otherwise, this method does nothing.
199 200 201 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 199 def disconnect! @connection.close rescue nil end |
#each_hash(result) ⇒ Object
HELPER METHODS ===========================================
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 141 def each_hash(result) # :nodoc: if block_given? result.each_hash do |row| row.symbolize_keys! yield row end else to_enum(:each_hash, result) end end |
#error_number(exception) ⇒ Object
:nodoc:
156 157 158 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 156 def error_number(exception) # :nodoc: exception.errno if exception.respond_to?(:errno) end |
#exec_delete(sql, name, binds) ⇒ Object Also known as: exec_update
325 326 327 328 329 330 331 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 325 def exec_delete(sql, name, binds) log(sql, name, binds) do exec_stmt(sql, name, binds) do |cols, stmt| stmt.affected_rows end end end |
#exec_query(sql, name = 'SQL', binds = []) ⇒ Object
283 284 285 286 287 288 289 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 283 def exec_query(sql, name = 'SQL', binds = []) log(sql, name, binds) do exec_stmt(sql, name, binds) do |cols, stmt| ActiveRecord::Result.new(cols, stmt.to_a) if cols end end end |
#exec_without_stmt(sql, name = 'SQL') ⇒ Object
:nodoc:
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 295 def exec_without_stmt(sql, name = 'SQL') # :nodoc: # Some queries, like SHOW CREATE TABLE don't work through the prepared # statement API. For those queries, we need to use this method. :'( log(sql, name) do result = @connection.query(sql) cols = [] rows = [] if result cols = result.fetch_fields.map { |field| field.name } rows = result.to_a result.free end ActiveRecord::Result.new(cols, rows) end end |
#execute_and_free(sql, name = nil) ⇒ Object
312 313 314 315 316 317 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 312 def execute_and_free(sql, name = nil) result = execute(sql, name) ret = yield result result.free ret end |
#insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object Also known as: create
:nodoc:
319 320 321 322 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 319 def insert_sql(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc: super sql, name id_value || @connection.insert_id end |
#last_inserted_id(result) ⇒ Object
291 292 293 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 291 def last_inserted_id(result) @connection.insert_id end |
#new_column(field, default, type, null, collation) ⇒ Object
:nodoc:
152 153 154 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 152 def new_column(field, default, type, null, collation) # :nodoc: Column.new(field, default, type, null, collation) end |
#quote_string(string) ⇒ Object
:nodoc:
168 169 170 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 168 def quote_string(string) #:nodoc: @connection.quote(string) end |
#reconnect! ⇒ Object
191 192 193 194 195 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 191 def reconnect! disconnect! clear_cache! connect end |
#reset! ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 203 def reset! if @connection.respond_to?(:change_user) # See http://bugs.mysql.com/bug.php?id=33540 -- the workaround way to # reset the connection is to change the user to the same user. @connection.change_user(@config[:username], @config[:password], @config[:database]) configure_connection end end |
#select_rows(sql, name = nil) ⇒ Object
DATABASE STATEMENTS ======================================
214 215 216 217 218 219 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 214 def select_rows(sql, name = nil) @connection.query_with_result = true rows = exec_without_stmt(sql, name).rows @connection.more_results && @connection.next_result # invoking stored procedures with CLIENT_MULTI_RESULTS requires this to tidy up else connection will be dropped rows end |
#supports_statement_cache? ⇒ Boolean
Returns true, since this connection adapter supports prepared statement caching.
135 136 137 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 135 def supports_statement_cache? true end |
#type_cast(value, column) ⇒ Object
QUOTING ==================================================
162 163 164 165 166 |
# File 'lib/active_record/connection_adapters/ruby_mysql_adapter.rb', line 162 def type_cast(value, column) return super unless value == true || value == false value ? 1 : 0 end |