Module: ActiveRecord::ConnectionAdapters::SQLite3::DatabaseStatements

Defined in:
activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb

Instance Method Summary collapse

Instance Method Details

#begin_db_transactionObject

:nodoc:



89
90
91
92
93
94
95
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 89

def begin_db_transaction # :nodoc:
  log("begin transaction", "TRANSACTION") do
    with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
      conn.transaction
    end
  end
end

#begin_isolated_db_transaction(isolation) ⇒ Object

:nodoc:



78
79
80
81
82
83
84
85
86
87
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 78

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, uses_transaction: 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

#commit_db_transactionObject

:nodoc:



97
98
99
100
101
102
103
104
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 97

def commit_db_transaction # :nodoc:
  log("commit transaction", "TRANSACTION") do
    with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
      conn.commit
    end
  end
  reset_read_uncommitted
end

#exec_delete(sql, name = "SQL", binds = []) ⇒ Object Also known as: exec_update

:nodoc:



72
73
74
75
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 72

def exec_delete(sql, name = "SQL", binds = []) # :nodoc:
  exec_query(sql, name, binds)
  @raw_connection.changes
end

#exec_query(sql, name = nil, binds = [], prepare: false, async: false) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 37

def exec_query(sql, name = nil, binds = [], prepare: false, async: 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
    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

      build_result(columns: cols, rows: records)
    end
  end
end

#exec_rollback_db_transactionObject

:nodoc:



106
107
108
109
110
111
112
113
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 106

def exec_rollback_db_transaction # :nodoc:
  log("rollback transaction", "TRANSACTION") do
    with_raw_connection(allow_retry: true, uses_transaction: false) do |conn|
      conn.rollback
    end
  end
  reset_read_uncommitted
end

#execute(sql, name = nil, allow_retry: false) ⇒ Object

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 24

def execute(sql, name = nil, allow_retry: false) # :nodoc:
  sql = transform_query(sql)
  check_if_write_query(sql)

  mark_transaction_written_if_write(sql)

  log(sql, name) do
    with_raw_connection(allow_retry: allow_retry) do |conn|
      conn.execute(sql)
    end
  end
end

#explain(arel, binds = [], _options = []) ⇒ Object



18
19
20
21
22
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 18

def explain(arel, binds = [], _options = [])
  sql    = "EXPLAIN QUERY PLAN " + to_sql(arel, binds)
  result = exec_query(sql, "EXPLAIN", [])
  SQLite3::ExplainPrettyPrinter.new.pp(result)
end

#high_precision_current_timestampObject



120
121
122
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 120

def high_precision_current_timestamp
  HIGH_PRECISION_CURRENT_TIMESTAMP
end

#write_query?(sql) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


12
13
14
15
16
# 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