class ActiveRecord::ConnectionAdapters::SQLite3Adapter
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.
Constants
- ADAPTER_NAME
- COLLATE_REGEX
- NATIVE_DATABASE_TYPES
Public Class Methods
# 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
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 92 class_attribute :represent_boolean_as_integer, default: false
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
Public Instance Methods
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 146 def active? @active end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 170 def allowed_index_name_length index_name_length - 2 end
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 159 def clear_cache! @statements.clear end
Clears the prepared statements cache.
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 152 def disconnect! super @active = false @connection.close rescue nil end
Disconnects from the database if already connected. Otherwise, this method does nothing.
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 179 def encoding @connection.encoding.to_s end
Returns the current database encoding format as a string, eg: 'UTF-8'
# 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
# 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 204 def explain(arel, binds = []) sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}" SQLite3::ExplainPrettyPrinter.new.pp(exec_query(sql, "EXPLAIN", [])) 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
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 248 def last_inserted_id(result) @connection.last_insert_row_id end
# 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
Renames a table.
Example:
rename_table('octopuses', 'octopi')
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 122 def requires_reloading? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 134 def supports_datetime_with_precision? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 110 def supports_ddl_transactions? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 183 def supports_explain? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 126 def supports_foreign_keys_in_create? sqlite_version >= "3.6.19" end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 163 def supports_index_sort_order? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 138 def supports_json? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 142 def supports_multi_insert? sqlite_version >= "3.7.11" end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 118 def supports_partial_index? sqlite_version >= "3.8.0" end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 114 def supports_savepoints? true end
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 130 def supports_views? true end
# 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
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.