Module: ActiveRecord::ConnectionAdapters::SQLite3::DatabaseStatements
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb |
Constant Summary
-
HIGH_PRECISION_CURRENT_TIMESTAMP =
private
Internal use only
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 109
Arel.sql("STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')", retryable: true).freeze
-
READ_QUERY =
private
Internal use only
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 7ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp( :pragma )
Instance Method Summary
-
#exec_update(sql, name = "SQL", binds = [])
Alias for #exec_delete.
- #explain(arel, binds = [], _options = [])
- #high_precision_current_timestamp
- #build_fixture_statements(fixture_set) private
- #build_truncate_statement(table_name) private
- #execute_batch(statements, name = nil) private
- #raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: false) private
- #reset_read_uncommitted private
- #returning_column_values(result) private
- #begin_db_transaction Internal use only
- #begin_isolated_db_transaction(isolation) Internal use only
- #commit_db_transaction Internal use only
- #exec_delete(sql, name = "SQL", binds = []) (also: #exec_update) Internal use only
- #exec_rollback_db_transaction Internal use only
- #internal_exec_query(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false) Internal use only
- #write_query?(sql) ⇒ Boolean Internal use only
Instance Method Details
#begin_db_transaction
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 79
def begin_db_transaction # :nodoc: log("begin transaction", "TRANSACTION") do with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn| result = conn.transaction verified! result end end end
#begin_isolated_db_transaction(isolation)
This method is for internal use only.
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 68
def begin_isolated_db_transaction(isolation) # :nodoc: raise TransactionIsolationError, "SQLite3 only supports the `read_uncommitted` transaction isolation level" if isolation != :read_uncommitted raise StandardError, "You need to enable the shared-cache mode in SQLite mode before attempting to change the transaction isolation level" unless shared_cache? with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn| ActiveSupport::IsolatedExecutionState[:active_record_read_uncommitted] = conn.get_first_value("PRAGMA read_uncommitted") conn.read_uncommitted = true begin_db_transaction end end
#build_fixture_statements(fixture_set) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 152
def build_fixture_statements(fixture_set) fixture_set.flat_map do |table_name, fixtures| next if fixtures.empty? fixtures.map { |fixture| build_fixture_sql([fixture], table_name) } end.compact end
#build_truncate_statement(table_name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 159
def build_truncate_statement(table_name) "DELETE FROM #{quote_table_name(table_name)}" end
#commit_db_transaction
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 89
def commit_db_transaction # :nodoc: log("commit transaction", "TRANSACTION") do with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn| conn.commit end end reset_read_uncommitted end
#exec_delete(sql, name = "SQL", binds = []) Also known as: #exec_update
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 62
def exec_delete(sql, name = "SQL", binds = []) # :nodoc: internal_exec_query(sql, name, binds) @raw_connection.changes end
#exec_rollback_db_transaction
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 98
def exec_rollback_db_transaction # :nodoc: log("rollback transaction", "TRANSACTION") do with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn| conn.rollback end end reset_read_uncommitted end
#exec_update(sql, name = "SQL", binds = [])
Alias for #exec_delete.
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 66
alias :exec_update :exec_delete
#execute_batch(statements, name = nil) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 135
def execute_batch(statements, name = nil) statements = statements.map { |sql| transform_query(sql) } sql = combine_multi_statements(statements) check_if_write_query(sql) mark_transaction_written_if_write(sql) log(sql, name) do |notification_payload| with_raw_connection do |conn| result = conn.execute_batch2(sql) verified! notification_payload[:row_count] = result.length result end end end
#explain(arel, binds = [], _options = [])
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 18
def explain(arel, binds = [], = []) sql = "EXPLAIN QUERY PLAN " + to_sql(arel, binds) result = internal_exec_query(sql, "EXPLAIN", []) SQLite3::ExplainPrettyPrinter.new.pp(result) end
#high_precision_current_timestamp
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 112
def HIGH_PRECISION_CURRENT_TIMESTAMP end
#internal_exec_query(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false)
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 24
def internal_exec_query(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false) # :nodoc: sql = transform_query(sql) check_if_write_query(sql) mark_transaction_written_if_write(sql) type_casted_binds = type_casted_binds(binds) log(sql, name, binds, type_casted_binds, async: async) do |notification_payload| with_raw_connection do |conn| # Don't cache statements if they are not prepared unless prepare stmt = conn.prepare(sql) begin cols = stmt.columns unless without_prepared_statement?(binds) stmt.bind_params(type_casted_binds) end records = stmt.to_a ensure stmt.close end else stmt = @statements[sql] ||= conn.prepare(sql) cols = stmt.columns stmt.reset! stmt.bind_params(type_casted_binds) records = stmt.to_a end verified! result = build_result(columns: cols, rows: records) notification_payload[:row_count] = result.length result end end end
#raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: false) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 117
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: false) log(sql, name, async: async) do |notification_payload| with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn| result = conn.execute(sql) verified! notification_payload[:row_count] = result.length result end end end
#reset_read_uncommitted (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 128
def reset_read_uncommitted read_uncommitted = ActiveSupport::IsolatedExecutionState[:active_record_read_uncommitted] return unless read_uncommitted @raw_connection&.read_uncommitted = read_uncommitted end
#returning_column_values(result) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 163
def returning_column_values(result) result.rows.first end
#write_query?(sql) ⇒ Boolean
This method is for internal use only.
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 12
def write_query?(sql) # :nodoc: !READ_QUERY.match?(sql) rescue ArgumentError # Invalid encoding !READ_QUERY.match?(sql.b) end