Module: ActiveRecord::ConnectionAdapters::MySQL::DatabaseStatements

Included in:
ActiveRecord::ConnectionAdapters::Mysql2Adapter
Defined in:
activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#exec_delete(sql, name = nil, binds = []) ⇒ Object Also known as: exec_update



73
74
75
76
77
78
79
80
81
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 73

def exec_delete(sql, name = nil, binds = [])
  if without_prepared_statement?(binds)
    @lock.synchronize do
      execute_and_free(sql, name) { @connection.affected_rows }
    end
  else
    exec_stmt_and_free(sql, name, binds) { |stmt| stmt.affected_rows }
  end
end

#exec_query(sql, name = "SQL", binds = [], prepare: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 53

def exec_query(sql, name = "SQL", binds = [], prepare: false)
  if without_prepared_statement?(binds)
    execute_and_free(sql, name) do |result|
      if result
        ActiveRecord::Result.new(result.fields, result.to_a)
      else
        ActiveRecord::Result.new([], [])
      end
    end
  else
    exec_stmt_and_free(sql, name, binds, cache_stmt: prepare) do |_, result|
      if result
        ActiveRecord::Result.new(result.fields, result.to_a)
      else
        ActiveRecord::Result.new([], [])
      end
    end
  end
end

#execute(sql, name = nil) ⇒ Object

Executes the SQL statement in the context of this connection.



41
42
43
44
45
46
47
48
49
50
51
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 41

def execute(sql, name = nil)
  if preventing_writes? && write_query?(sql)
    raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
  end

  # make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
  # made since we established the connection
  @connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone

  super
end

#explain(arel, binds = []) ⇒ Object



31
32
33
34
35
36
37
38
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 31

def explain(arel, binds = [])
  sql     = "EXPLAIN #{to_sql(arel, binds)}"
  start   = Concurrent.monotonic_time
  result  = exec_query(sql, "EXPLAIN", binds)
  elapsed = Concurrent.monotonic_time - start

  MySQL::ExplainPrettyPrinter.new.pp(result, elapsed)
end

#query(sql, name = nil) ⇒ Object

:nodoc:



18
19
20
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 18

def query(sql, name = nil) # :nodoc:
  execute(sql, name).to_a
end

#select_allObject

Returns an ActiveRecord::Result instance.



8
9
10
11
12
13
14
15
16
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 8

def select_all(*, **) # :nodoc:
  result = if ExplainRegistry.collect? && prepared_statements
    unprepared_statement { super }
  else
    super
  end
  @connection.abandon_results!
  result
end

#write_query?(sql) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


27
28
29
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 27

def write_query?(sql) # :nodoc:
  !READ_QUERY.match?(sql)
end