Module: ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements
- Included in:
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb
Instance Method Summary collapse
-
#_exec_insert(intent, pk = nil, sequence_name = nil, returning: nil) ⇒ Object
:nodoc:.
-
#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_restart_db_transaction ⇒ Object
:nodoc:.
-
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
-
#execute ⇒ Object
Executes an SQL statement, returning a PG::Result object on success or raising a PG::Error exception otherwise.
-
#execute_batch(statements, name = nil, **kwargs) ⇒ Object
:nodoc:.
- #explain(arel, binds = [], options = []) ⇒ Object
- #high_precision_current_timestamp ⇒ Object
-
#query_rows(sql, name = "SCHEMA", allow_retry: true, materialize_transactions: false) ⇒ Object
Queries the database and returns the results in an Array-like object.
-
#set_constraints(deferred, *constraints) ⇒ Object
Set when constraints will be checked for the current transaction.
-
#write_query?(sql) ⇒ Boolean
:nodoc:.
Instance Method Details
#_exec_insert(intent, pk = nil, sequence_name = nil, returning: nil) ⇒ Object
:nodoc:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 47 def _exec_insert(intent, pk = nil, sequence_name = nil, returning: nil) # :nodoc: if use_insert_returning? || pk == false super else intent.execute! result = intent.cast_result unless sequence_name table_ref = extract_table_ref_from_insert_sql(intent.raw_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 query_all("SELECT currval(#{quote(sequence_name)})", "SQL") end end |
#begin_db_transaction ⇒ Object
Begins a transaction.
68 69 70 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 68 def begin_db_transaction # :nodoc: query_command("BEGIN", "TRANSACTION", allow_retry: true, materialize_transactions: false) end |
#begin_isolated_db_transaction(isolation) ⇒ Object
:nodoc:
72 73 74 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 72 def begin_isolated_db_transaction(isolation) # :nodoc: query_command("BEGIN ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}", "TRANSACTION", allow_retry: true, materialize_transactions: false) end |
#build_explain_clause(options = []) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 100 def build_explain_clause( = []) return "EXPLAIN" if .empty? = .flat_map do |option| option.is_a?(Hash) ? option.to_a.map { |nested| nested.join(" ") } : option end "EXPLAIN (#{.join(", ").upcase})" end |
#commit_db_transaction ⇒ Object
Commits a transaction.
77 78 79 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 77 def commit_db_transaction # :nodoc: query_command("COMMIT", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#exec_restart_db_transaction ⇒ Object
:nodoc:
87 88 89 90 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 87 def exec_restart_db_transaction # :nodoc: cancel_any_running_query query_command("ROLLBACK AND CHAIN", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#exec_rollback_db_transaction ⇒ Object
Aborts a transaction.
82 83 84 85 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 82 def exec_rollback_db_transaction # :nodoc: cancel_any_running_query query_command("ROLLBACK", "TRANSACTION", allow_retry: false, materialize_transactions: true) end |
#execute ⇒ Object
Executes an SQL statement, returning a PG::Result object on success or raising a PG::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 PG::Result object is manually memory managed; if you don’t need it specifically, you may want consider the exec_query wrapper.
41 42 43 44 45 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 41 def execute(...) # :nodoc: super ensure @notice_receiver_sql_warnings = [] end |
#execute_batch(statements, name = nil, **kwargs) ⇒ Object
:nodoc:
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 131 def execute_batch(statements, name = nil, **kwargs) # :nodoc: intent = QueryIntent.new( adapter: self, processed_sql: combine_multi_statements(statements), name: name, batch: true, binds: kwargs[:binds] || [], prepare: kwargs[:prepare] || false, allow_async: kwargs[:async] || false, allow_retry: kwargs[:allow_retry] || false, materialize_transactions: kwargs[:materialize_transactions] != false ) intent.execute! intent.finish end |
#explain(arel, binds = [], options = []) ⇒ Object
7 8 9 10 11 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 7 def explain(arel, binds = [], = []) sql = build_explain_clause() + " " + to_sql(arel, binds) result = select_all(sql, "EXPLAIN", binds) PostgreSQL::ExplainPrettyPrinter.new.pp(result) end |
#high_precision_current_timestamp ⇒ Object
96 97 98 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 96 def HIGH_PRECISION_CURRENT_TIMESTAMP end |
#query_rows(sql, name = "SCHEMA", allow_retry: true, materialize_transactions: false) ⇒ Object
Queries the database and returns the results in an Array-like object
14 15 16 17 18 19 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 14 def query_rows(sql, name = "SCHEMA", allow_retry: true, materialize_transactions: false) # :nodoc: intent = internal_build_intent(sql, name, allow_retry:, materialize_transactions:) intent.execute! result = intent.raw_result result.map_types!(@type_map_for_results).values end |
#set_constraints(deferred, *constraints) ⇒ Object
Set when constraints will be checked for the current transaction.
Not passing any specific constraint names will set the value for all deferrable constraints.
deferred-
Valid values are
:deferredor:immediate.
See www.postgresql.org/docs/current/sql-set-constraints.html
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 118 def set_constraints(deferred, *constraints) unless %i[deferred immediate].include?(deferred) raise ArgumentError, "deferred must be :deferred or :immediate" end constraints = if constraints.empty? "ALL" else constraints.map { |c| quote_table_name(c) }.join(", ") end execute("SET CONSTRAINTS #{constraints} #{deferred.to_s.upcase}") end |
#write_query?(sql) ⇒ Boolean
:nodoc:
26 27 28 29 30 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb', line 26 def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) rescue ArgumentError # Invalid encoding !READ_QUERY.match?(sql.b) end |