Module: ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements
- Included in:
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/active_record/connection_adapters/postgresql/database_statements.rb
Instance Method Summary collapse
-
#begin_db_transaction ⇒ Object
Begins a transaction.
-
#begin_isolated_db_transaction(isolation) ⇒ Object
:nodoc:.
-
#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) ⇒ Object
:nodoc:.
-
#exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) ⇒ Object
:nodoc:.
-
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
-
#execute(sql, name = nil) ⇒ Object
Executes an SQL statement, returning a PG::Result object on success or raising a PG::Error exception otherwise.
- #explain(arel, binds = []) ⇒ Object
- #high_precision_current_timestamp ⇒ Object
-
#query(sql, name = nil) ⇒ Object
Queries the database and returns the results in an Array-like object.
-
#write_query?(sql) ⇒ Boolean
:nodoc:.
Instance Method Details
#begin_db_transaction ⇒ Object
Begins a transaction.
105 106 107 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 105 def begin_db_transaction # :nodoc: execute("BEGIN", "TRANSACTION") end |
#begin_isolated_db_transaction(isolation) ⇒ Object
:nodoc:
109 110 111 112 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 109 def begin_isolated_db_transaction(isolation) # :nodoc: begin_db_transaction execute "SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}" end |
#commit_db_transaction ⇒ Object
Commits a transaction.
115 116 117 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 115 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/postgresql/database_statements.rb', line 66 def exec_delete(sql, name = nil, binds = []) # :nodoc: execute_and_clear(sql, name, binds) { |result| result.cmd_tuples } end |
#exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil) ⇒ Object
:nodoc:
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 86 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 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 53 def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) # :nodoc: execute_and_clear(sql, name, binds, prepare: prepare, async: async) 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 |
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
120 121 122 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 120 def exec_rollback_db_transaction # :nodoc: execute("ROLLBACK", "TRANSACTION") end |
#execute(sql, name = nil) ⇒ Object
Executes an SQL statement, returning a PG::Result object on success or raising a PG::Error exception otherwise. Note: the PG::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 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 39 def execute(sql, name = nil) sql = transform_query(sql) check_if_write_query(sql) 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 = []) ⇒ Object
7 8 9 10 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 7 def explain(arel, binds = []) sql = "EXPLAIN #{to_sql(arel, binds)}" PostgreSQL::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", binds)) end |
#high_precision_current_timestamp ⇒ Object
128 129 130 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 128 def HIGH_PRECISION_CURRENT_TIMESTAMP 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/postgresql/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 |
#write_query?(sql) ⇒ Boolean
:nodoc:
29 30 31 32 33 |
# File 'lib/active_record/connection_adapters/postgresql/database_statements.rb', line 29 def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) rescue ArgumentError # Invalid encoding !READ_QUERY.match?(sql.b) end |