123456789_123456789_123456789_123456789_123456789_

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

Instance Method Summary

Instance Method Details

#affected_rows(result) (private)

[ GitHub ]

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

def affected_rows(result)
  @last_affected_rows
end

#begin_db_transaction

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 32

def begin_db_transaction # :nodoc:
  internal_begin_transaction(:immediate, nil)
end

#begin_deferred_transaction(isolation = nil)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 24

def begin_deferred_transaction(isolation = nil) # :nodoc:
  internal_begin_transaction(:deferred, isolation)
end

#begin_isolated_db_transaction(isolation)

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 28

def begin_isolated_db_transaction(isolation) # :nodoc:
  internal_begin_transaction(:deferred, isolation)
end

#build_truncate_statement(table_name) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 131

def build_truncate_statement(table_name)
  "DELETE FROM #{quote_table_name(table_name)}"
end

#cast_result(result) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 116

def cast_result(result)
  # Given that SQLite3 doesn't really a Result type, raw_execute already return an ActiveRecord::Result
  # and we have nothing to cast here.
  result
end

#commit_db_transaction

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 36

def commit_db_transaction # :nodoc:
  internal_execute("COMMIT TRANSACTION", "TRANSACTION", allow_retry: true, materialize_transactions: false)
end

#default_insert_value(column) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 139

def default_insert_value(column)
  column.default
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 40

def exec_rollback_db_transaction # :nodoc:
  internal_execute("ROLLBACK TRANSACTION", "TRANSACTION", allow_retry: true, materialize_transactions: false)
end

#execute

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 53

def execute(...) # :nodoc:
  # SQLite3Adapter was refactored to use ActiveRecord::Result internally
  # but for backward compatibility we have to keep returning arrays of hashes here
  super&.to_a
end

#execute_batch(statements, name = nil, **kwargs) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 126

def execute_batch(statements, name = nil, **kwargs)
  sql = combine_multi_statements(statements)
  raw_execute(sql, name, batch: true, **kwargs)
end

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

[ GitHub ]

  
# 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 = 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 49

def high_precision_current_timestamp
  HIGH_PRECISION_CURRENT_TIMESTAMP
end

#internal_begin_transaction(mode, isolation) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 65

def internal_begin_transaction(mode, isolation)
  if isolation
    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?
  end

  internal_execute("BEGIN #{mode} TRANSACTION", "TRANSACTION", allow_retry: true, materialize_transactions: false)
  if isolation
    @previous_read_uncommitted = query_value("PRAGMA read_uncommitted")
    internal_execute("PRAGMA read_uncommitted=ON", "TRANSACTION", allow_retry: true, materialize_transactions: false)
  end
end

#perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 78

def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch: false)
  if batch
    raw_connection.execute_batch2(sql)
  elsif prepare
    stmt = @statements[sql] ||= raw_connection.prepare(sql)
    stmt.reset!
    stmt.bind_params(type_casted_binds)

    result = if stmt.column_count.zero? # No return
      stmt.step
      ActiveRecord::Result.empty
    else
      ActiveRecord::Result.new(stmt.columns, stmt.to_a)
    end
  else
    # Don't cache statements if they are not prepared.
    stmt = raw_connection.prepare(sql)
    begin
      unless binds.nil? || binds.empty?
        stmt.bind_params(type_casted_binds)
      end
      result = if stmt.column_count.zero? # No return
        stmt.step
        ActiveRecord::Result.empty
      else
        ActiveRecord::Result.new(stmt.columns, stmt.to_a)
      end
    ensure
      stmt.close
    end
  end
  @last_affected_rows = raw_connection.changes
  verified!

  notification_payload[:row_count] = result&.length || 0
  result
end

#reset_isolation_level

This method is for internal use only.
[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 59

def reset_isolation_level # :nodoc:
  internal_execute("PRAGMA read_uncommitted=#{@previous_read_uncommitted}", "TRANSACTION", allow_retry: true, materialize_transactions: false)
  @previous_read_uncommitted = nil
end

#returning_column_values(result) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/sqlite3/database_statements.rb', line 135

def returning_column_values(result)
  result.rows.first
end

#write_query?(sql) ⇒ Boolean

This method is for internal use only.
[ GitHub ]

  
# 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