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

Defined in:
activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#build_explain_clause(options = []) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 86

def build_explain_clause(options = [])
  return "EXPLAIN" if options.empty?

  explain_clause = "EXPLAIN #{options.join(" ").upcase}"

  if analyze_without_explain? && explain_clause.include?("ANALYZE")
    explain_clause.sub("EXPLAIN ", "")
  else
    explain_clause
  end
end

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

:nodoc:



65
66
67
68
69
70
71
72
73
74
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 65

def exec_delete(sql, name = nil, binds = []) # :nodoc:
  if without_prepared_statement?(binds)
    with_raw_connection do |conn|
      @affected_rows_before_warnings = nil
      execute_and_free(sql, name) { @affected_rows_before_warnings || conn.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, async: false) ⇒ Object

:nodoc:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 45

def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) # :nodoc:
  if without_prepared_statement?(binds)
    execute_and_free(sql, name, async: async) do |result|
      if result
        build_result(columns: result.fields, rows: result.to_a)
      else
        build_result(columns: [], rows: [])
      end
    end
  else
    exec_stmt_and_free(sql, name, binds, cache_stmt: prepare, async: async) do |_, result|
      if result
        build_result(columns: result.fields, rows: result.to_a)
      else
        build_result(columns: [], rows: [])
      end
    end
  end
end

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



36
37
38
39
40
41
42
43
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 36

def explain(arel, binds = [], options = [])
  sql     = build_explain_clause(options) + " " + 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_timestampObject



82
83
84
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 82

def high_precision_current_timestamp
  HIGH_PRECISION_CURRENT_TIMESTAMP
end

#query(sql, name = nil) ⇒ Object

:nodoc:



21
22
23
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 21

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
17
18
19
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 8

def select_all(*, **) # :nodoc:
  result = nil
  with_raw_connection do |conn|
    result = if ExplainRegistry.collect? && prepared_statements
      unprepared_statement { super }
    else
      super
    end
    conn.abandon_results!
  end
  result
end

#write_query?(sql) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


30
31
32
33
34
# File 'activerecord/lib/active_record/connection_adapters/mysql/database_statements.rb', line 30

def write_query?(sql) # :nodoc:
  !READ_QUERY.match?(sql)
rescue ArgumentError # Invalid encoding
  !READ_QUERY.match?(sql.b)
end