edge badge

The SQLite3 adapter works SQLite 3.6.16 or newer with the sqlite3-ruby drivers (available as gem from rubygems.org/gems/sqlite3).

Options:

  • :database - Path to the database file.

Namespace
Methods
A
C
D
E
I
L
N
Q
R
S
T
V
Constants
ADAPTER_NAME = 'SQLite'.freeze
 
NATIVE_DATABASE_TYPES = { primary_key: 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL', string: { name: "varchar" }, text: { name: "text" }, integer: { name: "integer" }, float: { name: "float" }, decimal: { name: "decimal" }, datetime: { name: "datetime" }, time: { name: "time" }, date: { name: "date" }, binary: { name: "blob" }, boolean: { name: "boolean" } }
 
Class Public methods
new(connection, logger, connection_options, config)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 134
def initialize(connection, logger, connection_options, config)
  super(connection, logger)

  @active     = nil
  @statements = StatementPool.new(@connection,
                                  self.class.type_cast_config_to_integer(config.fetch(:statement_limit) { 1000 }))
  @config = config

  @visitor = Arel::Visitors::SQLite.new self

  if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
    @prepared_statements = true
  else
    @prepared_statements = false
  end
end
Instance Public methods
active?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 190
def active?
  @active != false
end
allowed_index_name_length()

Returns 62. SQLite supports index names up to 64 characters. The rest is used by rails internally to perform temporary rename operations

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 214
def allowed_index_name_length
  index_name_length - 2
end
change_column_null(table_name, column_name, null, default = nil)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 480
def change_column_null(table_name, column_name, null, default = nil)
  unless null || default.nil?
    exec_query("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
  end
  alter_table(table_name) do |definition|
    definition[column_name].null = null
  end
end
clear_cache!()

Clears the prepared statements cache.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 203
def clear_cache!
  @statements.clear
end
disconnect!()

Disconnects from the database if already connected. Otherwise, this method does nothing.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 196
def disconnect!
  super
  @active = false
  @connection.close rescue nil
end
encoding()

Returns the current database encoding format as a string, eg: 'UTF-8'

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 223
def encoding
  @connection.encoding.to_s
end
exec_delete(sql, name = 'SQL', binds = [])
Also aliased as: exec_update
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 324
def exec_delete(sql, name = 'SQL', binds = [])
  exec_query(sql, name, binds)
  @connection.changes
end
exec_query(sql, name = nil, binds = [])
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 294
def exec_query(sql, name = nil, binds = [])
  type_casted_binds = binds.map { |col, val|
    [col, type_cast(val, col)]
  }

  log(sql, name, type_casted_binds) do
    # Don't cache statements if they are not prepared
    if without_prepared_statement?(binds)
      stmt    = @connection.prepare(sql)
      begin
        cols    = stmt.columns
        records = stmt.to_a
      ensure
        stmt.close
      end
      stmt = records
    else
      cache = @statements[sql] ||= {
        :stmt => @connection.prepare(sql)
      }
      stmt = cache[:stmt]
      cols = cache[:cols] ||= stmt.columns
      stmt.reset!
      stmt.bind_params type_casted_binds.map { |_, val| val }
    end

    ActiveRecord::Result.new(cols, stmt.to_a)
  end
end
exec_update(sql, name = 'SQL', binds = [])
Alias for: exec_delete
explain(arel, binds = [])

DATABASE STATEMENTS ======================================

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 275
def explain(arel, binds = [])
  sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
  ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', []))
end
last_inserted_id(result)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 330
def last_inserted_id(result)
  @connection.last_insert_row_id
end
quote_table_name_for_assignment(table, attr)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 255
def quote_table_name_for_assignment(table, attr)
  quote_column_name(attr)
end
rename_table(table_name, new_name)

Renames a table.

Example:

rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 447
def rename_table(table_name, new_name)
  exec_query "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
  rename_table_indexes(table_name, new_name)
end
requires_reloading?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 178
def requires_reloading?
  true
end
select_rows(sql, name = nil, binds = [])
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 354
def select_rows(sql, name = nil, binds = [])
  exec_query(sql, name, binds).rows
end
supports_add_column?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 182
def supports_add_column?
  true
end
supports_ddl_transactions?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 151
def supports_ddl_transactions?
  true
end
supports_explain?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 227
def supports_explain?
  true
end
supports_index_sort_order?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 207
def supports_index_sort_order?
  true
end
supports_partial_index?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 159
def supports_partial_index?
  sqlite_version >= '3.8.0'
end
supports_savepoints?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 155
def supports_savepoints?
  true
end
supports_statement_cache?()

Returns true, since this connection adapter supports prepared statement caching.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 165
def supports_statement_cache?
  true
end
supports_views?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 186
def supports_views?
  true
end
table_exists?(table_name)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 385
def table_exists?(table_name)
  table_name && tables(nil, table_name).any?
end
valid_alter_table_options( type, options)

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 454
def valid_alter_table_options( type, options)
  type.to_sym != :primary_key
end
Instance Protected methods
initialize_type_map(m)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 511
def initialize_type_map(m)
  super
  m.register_type(/binary/i, SQLite3Binary.new)
  register_class_with_limit m, %r(char)i, SQLite3String
end
sqlite_version()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 616
def sqlite_version
  @sqlite_version ||= SQLite3Adapter::Version.new(select_value('select sqlite_version(*)'))
end
table_structure(table_name)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 521
def table_structure(table_name)
  structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA').to_hash
  raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
  structure
end
translate_exception(exception, message)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 620
def translate_exception(exception, message)
  case exception.message
  # SQLite 3.8.2 returns a newly formatted error message:
  #   UNIQUE constraint failed: *table_name*.*column_name*
  # Older versions of SQLite return:
  #   column *column_name* is not unique
  when /column(s)? .* (is|are) not unique/, /UNIQUE constraint failed: .*/
    RecordNotUnique.new(message, exception)
  else
    super
  end
end