Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To be consistent with other adapters.
- Loading branch information
Showing
2 changed files
with
91 additions
and
83 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
activerecord/lib/active_record/connection_adapters/trilogy/database_statements.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveRecord | ||
module ConnectionAdapters | ||
module Trilogy | ||
module DatabaseStatements | ||
READ_QUERY = AbstractAdapter.build_read_query_regexp( | ||
:desc, :describe, :set, :show, :use | ||
) # :nodoc: | ||
private_constant :READ_QUERY | ||
|
||
HIGH_PRECISION_CURRENT_TIMESTAMP = Arel.sql("CURRENT_TIMESTAMP(6)").freeze # :nodoc: | ||
private_constant :HIGH_PRECISION_CURRENT_TIMESTAMP | ||
|
||
def select_all(*, **) # :nodoc: | ||
result = nil | ||
with_raw_connection do |conn| | ||
result = super | ||
conn.next_result while conn.more_results_exist? | ||
end | ||
result | ||
end | ||
|
||
def write_query?(sql) # :nodoc: | ||
!READ_QUERY.match?(sql) | ||
rescue ArgumentError # Invalid encoding | ||
!READ_QUERY.match?(sql.b) | ||
end | ||
|
||
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 | ||
|
||
def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) | ||
sql = transform_query(sql) | ||
check_if_write_query(sql) | ||
|
||
result = raw_execute(sql, name, async: async) | ||
ActiveRecord::Result.new(result.fields, result.to_a) | ||
end | ||
|
||
alias exec_without_stmt exec_query | ||
|
||
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil) | ||
sql = transform_query(sql) | ||
check_if_write_query(sql) | ||
|
||
raw_execute(to_sql(sql, binds), name) | ||
end | ||
|
||
def exec_delete(sql, name = nil, binds = []) | ||
sql = transform_query(sql) | ||
check_if_write_query(sql) | ||
|
||
result = raw_execute(to_sql(sql, binds), name) | ||
result.affected_rows | ||
end | ||
|
||
alias :exec_update :exec_delete | ||
|
||
def high_precision_current_timestamp | ||
HIGH_PRECISION_CURRENT_TIMESTAMP | ||
end | ||
|
||
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 | ||
|
||
private | ||
def last_inserted_id(result) | ||
result.last_insert_id | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters