module ActiveRecord::ConnectionAdapters::SQLite3::DatabaseStatements

Public Instance Methods

begin_isolated_db_transaction(isolation) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 81
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?

  Thread.current.thread_variable_set("read_uncommitted", @connection.get_first_value("PRAGMA read_uncommitted"))
  @connection.read_uncommitted = true
  begin_db_transaction
end
exec_delete(sql, name = "SQL", binds = []) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 75
def exec_delete(sql, name = "SQL", binds = [])
  exec_query(sql, name, binds)
  @connection.changes
end
Also aliased as: exec_update
exec_query(sql, name = nil, binds = [], prepare: false) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 38
def exec_query(sql, name = nil, binds = [], prepare: false)
  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)

  type_casted_binds = type_casted_binds(binds)

  log(sql, name, binds, type_casted_binds) do
    ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
      # Don't cache statements if they are not prepared
      unless prepare
        stmt = @connection.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] ||= @connection.prepare(sql)
        cols = stmt.columns
        stmt.reset!
        stmt.bind_params(type_casted_binds)
        records = stmt.to_a
      end

      build_result(columns: cols, rows: records)
    end
  end
end
exec_update(sql, name = "SQL", binds = [])
Alias for: exec_delete
explain(arel, binds = []) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 18
def explain(arel, binds = [])
  sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
  SQLite3::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", []))
end

Private Instance Methods

build_fixture_statements(fixture_set) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 133
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) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 140
def build_truncate_statement(table_name)
  "DELETE FROM #{quote_table_name(table_name)}"
end
execute_batch(statements, name = nil) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 112
def execute_batch(statements, name = nil)
  sql = combine_multi_statements(statements)

  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.execute_batch2(sql)
    end
  end
end
last_inserted_id(result) click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 129
def last_inserted_id(result)
  @connection.last_insert_row_id
end
reset_read_uncommitted() click to toggle source
# File lib/active_record/connection_adapters/sqlite3/database_statements.rb, line 105
def reset_read_uncommitted
  read_uncommitted = Thread.current.thread_variable_get("read_uncommitted")
  return unless read_uncommitted

  @connection.read_uncommitted = read_uncommitted
end