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.
- A
- C
- D
- E
- F
- I
- L
- N
- R
- S
- V
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" }, } |
# 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
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
Returns 62. SQLite supports index names up to 64 characters. The rest is used by Rails internally to perform temporary rename operations
Clears the prepared statements cache.
Disconnects from the database if already connected. Otherwise, this method does nothing.
Returns the current database encoding format as a string, eg: 'UTF-8'
# 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
# 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
# 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
# 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
Renames a table.
Example:
rename_table('octopuses', 'octopi')