module ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements
Public Instance Methods
execute(sql, name = nil, async: false)
click to toggle source
Executes the SQL statement in the context of this connection.
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 43 def execute(sql, name = nil, async: false) sql = transform_query(sql) check_if_write_query(sql) raw_execute(sql, name, async: async) end
explain(arel, binds = [])
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 33 def explain(arel, binds = []) sql = "EXPLAIN #{to_sql(arel, binds)}" start = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = exec_query(sql, "EXPLAIN", binds) elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start MySQL::ExplainPrettyPrinter.new.pp(result, elapsed) end
high_precision_current_timestamp()
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 86 def high_precision_current_timestamp HIGH_PRECISION_CURRENT_TIMESTAMP end
Private Instance Methods
combine_multi_statements(total_sql)
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 139 def combine_multi_statements(total_sql) total_sql.each_with_object([]) do |sql, total_sql_chunks| previous_packet = total_sql_chunks.last if max_allowed_packet_reached?(sql, previous_packet) total_sql_chunks << +sql else previous_packet << ";\n" previous_packet << sql end end end
default_insert_value(column)
click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 107 def default_insert_value(column) super unless column.auto_increment? end
exec_stmt_and_free(sql, name, binds, cache_stmt: false, async: false) { |stmt, result| ... }
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 166 def exec_stmt_and_free(sql, name, binds, cache_stmt: false, async: false) sql = transform_query(sql) check_if_write_query(sql) materialize_transactions mark_transaction_written_if_write(sql) # make sure we carry over any changes to ActiveRecord.default_timezone that have been # made since we established the connection @connection.query_options[:database_timezone] = ActiveRecord.default_timezone type_casted_binds = type_casted_binds(binds) log(sql, name, binds, type_casted_binds, async: async) do if cache_stmt stmt = @statements[sql] ||= @connection.prepare(sql) else stmt = @connection.prepare(sql) end begin result = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do stmt.execute(*type_casted_binds) end rescue Mysql2::Error => e if cache_stmt @statements.delete(sql) else stmt.close end raise e end ret = yield stmt, result result.free if result stmt.close unless cache_stmt ret end end
execute_batch(statements, name = nil)
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 99 def execute_batch(statements, name = nil) statements = statements.map { |sql| transform_query(sql) } combine_multi_statements(statements).each do |statement| raw_execute(statement, name) end @connection.abandon_results! end
last_inserted_id(result)
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 111 def last_inserted_id(result) @connection.last_id end
max_allowed_packet()
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 162 def max_allowed_packet @max_allowed_packet ||= show_variable("max_allowed_packet") end
max_allowed_packet_reached?(current_packet, previous_packet)
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 151 def max_allowed_packet_reached?(current_packet, previous_packet) if current_packet.bytesize > max_allowed_packet raise ActiveRecordError, "Fixtures set is too large #{current_packet.bytesize}. Consider increasing the max_allowed_packet variable." elsif previous_packet.nil? true else (current_packet.bytesize + previous_packet.bytesize + 2) > max_allowed_packet end end
multi_statements_enabled?()
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 115 def multi_statements_enabled? flags = @config[:flags] if flags.is_a?(Array) flags.include?("MULTI_STATEMENTS") else flags.anybits?(Mysql2::Client::MULTI_STATEMENTS) end end
raw_execute(sql, name, async: false)
click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 91 def raw_execute(sql, name, async: false) # make sure we carry over any changes to ActiveRecord.default_timezone that have been # made since we established the connection @connection.query_options[:database_timezone] = ActiveRecord.default_timezone super end
with_multi_statements() { || ... }
click to toggle source
# File lib/active_record/connection_adapters/mysql/database_statements.rb, line 125 def with_multi_statements multi_statements_was = multi_statements_enabled? unless multi_statements_was @connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_ON) end yield ensure unless multi_statements_was @connection.set_server_option(Mysql2::Client::OPTION_MULTI_STATEMENTS_OFF) end end