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
L
N
R
S
V
Included Modules
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" } }
 
COLLATE_REGEX = /.*\"(\w+)\".*collate\s+\"(\w+)\".*/i.freeze
 
Class Public methods
new(connection, logger, connection_options, config)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 93
def initialize(connection, logger, connection_options, config)
  super(connection, logger, config)

  @active     = nil
  @statements = StatementPool.new(self.class.type_cast_config_to_integer(config[:statement_limit]))
end
Instance Public methods
active?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 143
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 171
def allowed_index_name_length
  index_name_length - 2
end
clear_cache!()

Clears the prepared statements cache.

# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 156
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 149
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 180
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 230
def exec_delete(sql, name = "SQL", binds = [])
  exec_query(sql, name, binds)
  @connection.changes
end
exec_query(sql, name = nil, binds = [], prepare: false)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 197
def exec_query(sql, name = nil, binds = [], prepare: false)
  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
        cache = @statements[sql] ||= {
          stmt: @connection.prepare(sql)
        }
        stmt = cache[:stmt]
        cols = cache[:cols] ||= stmt.columns
        stmt.reset!
        stmt.bind_params(type_casted_binds)
        records = stmt.to_a
      end

      ActiveRecord::Result.new(cols, records)
    end
  end
end
exec_update(sql, name = "SQL", binds = [])
Alias for: exec_delete
explain(arel, binds = [])
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 192
def explain(arel, binds = [])
  sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
  SQLite3::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", []))
end
last_inserted_id(result)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 236
def last_inserted_id(result)
  @connection.last_insert_row_id
end
new_column_from_field(table_name, field)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 301
def new_column_from_field(table_name, field) # :nondoc:
  case field["dflt_value"]
  when /^null$/i
    field["dflt_value"] = nil
  when /^'(.*)'$/m
    field["dflt_value"] = $1.gsub("''", "'")
  when /^"(.*)"$/m
    field["dflt_value"] = $1.gsub('""', '"')
  end

  collation = field["collation"]
  sql_type = field["type"]
  type_metadata = fetch_type_metadata(sql_type)
  new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
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 362
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 127
def requires_reloading?
  true
end
supports_datetime_with_precision?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 135
def supports_datetime_with_precision?
  true
end
supports_ddl_transactions?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 100
def supports_ddl_transactions?
  true
end
supports_explain?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 184
def supports_explain?
  true
end
supports_index_sort_order?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 160
def supports_index_sort_order?
  true
end
supports_multi_insert?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 139
def supports_multi_insert?
  sqlite_version >= "3.7.11"
end
supports_partial_index?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 108
def supports_partial_index?
  sqlite_version >= "3.8.0"
end
supports_savepoints?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 104
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 114
def supports_statement_cache?
  true
end
supports_views?()
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 131
def supports_views?
  true
end
valid_alter_table_type?(type)

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 369
def valid_alter_table_type?(type)
  type.to_sym != :primary_key
end
valid_type?(type)
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 164
def valid_type?(type)
  true
end