Module: ActiveRecord::ConnectionAdapters::CipherStashPG::DatabaseStatements
- Defined in:
- lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb,
lib/active_record/connection_adapters/7.0/cipherstash_pg/database_statements.rb,
lib/active_record/connection_adapters/7.1/cipherstash_pg/database_statements.rb
Instance Method Summary collapse
-
#begin_db_transaction ⇒ Object
Begins a transaction.
-
#begin_isolated_db_transaction(isolation) ⇒ Object
:nodoc:.
- #build_explain_clause(options = []) ⇒ Object
-
#commit_db_transaction ⇒ Object
Commits a transaction.
-
#exec_delete(sql, name = nil, binds = []) ⇒ Object
(also: #exec_update)
:nodoc:.
-
#exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, returning: nil) ⇒ Object
:nodoc:.
-
#exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) ⇒ Object
:nodoc:.
-
#exec_restart_db_transaction ⇒ Object
:nodoc:.
-
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
-
#execute ⇒ Object
Executes an SQL statement, returning a ::CipherStashPG::Result object on success or raising a ::CipherStashPG::Error exception otherwise.
- #explain(arel, binds = [], options = []) ⇒ Object
- #high_precision_current_timestamp ⇒ Object
-
#internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
:nodoc:.
-
#query(sql, name = nil) ⇒ Object
Queries the database and returns the results in an Array-like object.
- #raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
-
#write_query?(sql) ⇒ Boolean
:nodoc:.
Instance Method Details
#begin_db_transaction ⇒ Object
Begins a transaction.
110 111 112 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 110 def begin_db_transaction # :nodoc: execute("BEGIN", "TRANSACTION") end |
#begin_isolated_db_transaction(isolation) ⇒ Object
:nodoc:
114 115 116 117 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 114 def begin_isolated_db_transaction(isolation) # :nodoc: begin_db_transaction execute "SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}" end |
#build_explain_clause(options = []) ⇒ Object
145 146 147 148 149 |
# File 'lib/active_record/connection_adapters/7.1/cipherstash_pg/database_statements.rb', line 145 def build_explain_clause( = []) return "EXPLAIN" if .empty? "EXPLAIN (#{.join(", ").upcase})" end |
#commit_db_transaction ⇒ Object
Commits a transaction.
120 121 122 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 120 def commit_db_transaction # :nodoc: execute("COMMIT", "TRANSACTION") end |
#exec_delete(sql, name = nil, binds = []) ⇒ Object Also known as: exec_update
:nodoc:
66 67 68 |
# File 'lib/active_record/connection_adapters/7.0/cipherstash_pg/database_statements.rb', line 66 def exec_delete(sql, name = nil, binds = []) execute_and_clear(sql, name, binds) { |result| result.cmd_tuples } end |
#exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, returning: nil) ⇒ Object
:nodoc:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 91 def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil) # :nodoc: if use_insert_returning? || pk == false super else result = exec_query(sql, name, binds) unless sequence_name table_ref = extract_table_ref_from_insert_sql(sql) if table_ref pk = primary_key(table_ref) if pk.nil? pk = suppress_composite_primary_key(pk) sequence_name = default_sequence_name(table_ref, pk) end return result unless sequence_name end last_insert_id_result(sequence_name) end end |
#exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) ⇒ Object
:nodoc:
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_record/connection_adapters/7.0/cipherstash_pg/database_statements.rb', line 53 def exec_query(sql, name = "SQL", binds = [], prepare: false) execute_and_clear(sql, name, binds, prepare: prepare) do |result| types = {} fields = result.fields fields.each_with_index do |fname, i| ftype = result.ftype i fmod = result.fmod i case type = get_oid_type(ftype, fmod, fname) when Type::Integer, Type::Float, OID::Decimal, Type::String, Type::DateTime, Type::Boolean # skip if a column has already been type casted by pg decoders else types[fname] = type end end build_result(columns: fields, rows: result.values, column_types: types) end end |
#exec_restart_db_transaction ⇒ Object
:nodoc:
132 133 134 135 |
# File 'lib/active_record/connection_adapters/7.1/cipherstash_pg/database_statements.rb', line 132 def exec_restart_db_transaction # :nodoc: cancel_any_running_query internal_execute("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
125 126 127 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 125 def exec_rollback_db_transaction # :nodoc: execute("ROLLBACK", "TRANSACTION") end |
#execute ⇒ Object
Executes an SQL statement, returning a ::CipherStashPG::Result object on success or raising a ::CipherStashPG::Error exception otherwise.
Setting allow_retry
to true causes the db to reconnect and retry executing the SQL statement in case of a connection-related exception. This option should only be enabled for known idempotent queries.
Note: the ::CipherStashPG::Result object is manually memory managed; if you don’t need it specifically, you may want consider the exec_query
wrapper.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 39 def execute(sql, name = nil) if preventing_writes? && write_query?(sql) raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}" end materialize_transactions mark_transaction_written_if_write(sql) log(sql, name) do ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @connection.async_exec(sql) end end end |
#explain(arel, binds = [], options = []) ⇒ Object
7 8 9 10 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 7 def explain(arel, binds = []) sql = "EXPLAIN #{to_sql(arel, binds)}" CipherStashPG::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", binds)) end |
#high_precision_current_timestamp ⇒ Object
128 129 130 |
# File 'lib/active_record/connection_adapters/7.0/cipherstash_pg/database_statements.rb', line 128 def HIGH_PRECISION_CURRENT_TIMESTAMP end |
#internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
:nodoc:
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_record/connection_adapters/7.1/cipherstash_pg/database_statements.rb', line 60 def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true) # :nodoc: execute_and_clear(sql, name, binds, prepare: prepare, async: async, allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |result| types = {} fields = result.fields fields.each_with_index do |fname, i| ftype = result.ftype i fmod = result.fmod i types[fname] = types[i] = get_oid_type(ftype, fmod, fname) end build_result(columns: fields, rows: result.values, column_types: types) end end |
#query(sql, name = nil) ⇒ Object
Queries the database and returns the results in an Array-like object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 13 def query(sql, name = nil) # :nodoc: materialize_transactions mark_transaction_written_if_write(sql) log(sql, name) do ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @connection.async_exec(sql).map_types!(@type_map_for_results).values end end end |
#raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/connection_adapters/7.1/cipherstash_pg/database_statements.rb', line 50 def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true) log(sql, name, async: async) do with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn| result = conn.async_exec(sql) handle_warnings(result) result end end end |
#write_query?(sql) ⇒ Boolean
:nodoc:
29 30 31 32 33 |
# File 'lib/active_record/connection_adapters/6.1/cipherstash_pg/database_statements.rb', line 29 def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) rescue ArgumentError # Invalid encoding !READ_QUERY.match?(sql.b) end |