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.
Methods
- active?
- allowed_index_name_length
- clear_cache!
- disconnect!
- encoding
- exec_delete
- exec_query
- exec_update
- explain
- foreign_keys
- insert_fixtures
- insert_fixtures_set
- last_inserted_id
- new
- rename_table
- represent_boolean_as_integer
- requires_reloading?
- supports_datetime_with_precision?
- supports_ddl_transactions?
- supports_explain?
- supports_foreign_keys_in_create?
- supports_index_sort_order?
- supports_json?
- supports_multi_insert?
- supports_partial_index?
- supports_savepoints?
- supports_views?
- valid_alter_table_type?
Constants
ADAPTER_NAME | = | "SQLite".freeze |
COLLATE_REGEX | = | /.*\"(\w+)\".*collate\s+\"(\w+)\".*/i.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" }, json: { name: "json" }, } |
Class Public methods
new(connection, logger, connection_options, config)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 101
def initialize(connection, logger, connection_options, config)
super(connection, logger, config)
@active = true
@statements = StatementPool.new(self.class.type_cast_config_to_integer(config[:statement_limit]))
configure_connection
end
🔎 See on GitHub
represent_boolean_as_integer
Indicates whether boolean values are stored in sqlite3 databases as 1 and 0 or 't' and 'f'. Leaving ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer
set to false is deprecated. SQLite databases have used 't' and 'f' to serialize boolean values and must have old data converted to 1 and 0 (its native boolean serialization) before setting this flag to true. Conversion can be accomplished by setting up a rake task which runs
ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
for all models and all boolean columns, after which the flag must be set to true by adding the following to your application.rb
file:
Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 92
class_attribute :represent_boolean_as_integer, default: false
🔎 See on GitHub
Instance Public methods
active?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 146
def active?
@active
end
🔎 See on GitHub
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
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 170
def allowed_index_name_length
index_name_length - 2
end
🔎 See on GitHub
clear_cache!()
Clears the prepared statements cache.
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 159
def clear_cache!
@statements.clear
end
🔎 See on GitHub
disconnect!()
Disconnects from the database if already connected. Otherwise, this method does nothing.
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 152
def disconnect!
super
@active = false
@connection.close rescue nil
end
🔎 See on GitHub
encoding()
Returns the current database encoding format as a string, eg: 'UTF-8'
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 179
def encoding
@connection.encoding.to_s
end
🔎 See on GitHub
exec_delete(sql, name = "SQL", binds = [])
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 242
def exec_delete(sql, name = "SQL", binds = [])
exec_query(sql, name, binds)
@connection.changes
end
🔎 See on GitHub
exec_query(sql, name = nil, binds = [], prepare: false)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 209
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
🔎 See on GitHub
explain(arel, binds = [])
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 204
def explain(arel, binds = [])
sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}"
SQLite3::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", []))
end
🔎 See on GitHub
foreign_keys(table_name)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 356
def foreign_keys(table_name)
fk_info = exec_query("PRAGMA foreign_key_list(#{quote(table_name)})", "SCHEMA")
fk_info.map do |row|
options = {
column: row["from"],
primary_key: row["to"],
on_delete: extract_foreign_key_action(row["on_delete"]),
on_update: extract_foreign_key_action(row["on_update"])
}
ForeignKeyDefinition.new(table_name, row["table"], options)
end
end
🔎 See on GitHub
insert_fixtures(rows, table_name)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 369
def insert_fixtures(rows, table_name)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
`insert_fixtures` is deprecated and will be removed in the next version of Rails.
Consider using `insert_fixtures_set` for performance improvement.
MSG
insert_fixtures_set(table_name => rows)
end
🔎 See on GitHub
insert_fixtures_set(fixture_set, tables_to_delete = [])
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 377
def insert_fixtures_set(fixture_set, tables_to_delete = [])
disable_referential_integrity do
transaction(requires_new: true) do
tables_to_delete.each { |table| delete "DELETE FROM #{quote_table_name(table)}", "Fixture Delete" }
fixture_set.each do |table_name, rows|
rows.each { |row| insert_fixture(row, table_name) }
end
end
end
end
🔎 See on GitHub
last_inserted_id(result)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 248
def last_inserted_id(result)
@connection.last_insert_row_id
end
🔎 See on GitHub
rename_table(table_name, new_name)
Renames a table.
Example:
rename_table('octopuses', 'octopi')
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 288
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
🔎 See on GitHub
requires_reloading?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 122
def requires_reloading?
true
end
🔎 See on GitHub
supports_datetime_with_precision?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 134
def supports_datetime_with_precision?
true
end
🔎 See on GitHub
supports_ddl_transactions?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 110
def supports_ddl_transactions?
true
end
🔎 See on GitHub
supports_explain?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 183
def supports_explain?
true
end
🔎 See on GitHub
supports_foreign_keys_in_create?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 126
def supports_foreign_keys_in_create?
sqlite_version >= "3.6.19"
end
🔎 See on GitHub
supports_index_sort_order?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 163
def supports_index_sort_order?
true
end
🔎 See on GitHub
supports_json?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 138
def supports_json?
true
end
🔎 See on GitHub
supports_multi_insert?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 142
def supports_multi_insert?
sqlite_version >= "3.7.11"
end
🔎 See on GitHub
supports_partial_index?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 118
def supports_partial_index?
sqlite_version >= "3.8.0"
end
🔎 See on GitHub
supports_savepoints?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 114
def supports_savepoints?
true
end
🔎 See on GitHub
supports_views?()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 130
def supports_views?
true
end
🔎 See on GitHub
valid_alter_table_type?(type, options = {})
📝 Source code
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 293
def valid_alter_table_type?(type, options = {})
!invalid_alter_table_type?(type, options)
end
🔎 See on GitHub