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
- NATIVE_DATABASE_TYPES
Public Class Methods
new(connection, logger, connection_options, config)
click to toggle source
Calls superclass method
ActiveRecord::ConnectionAdapters::QueryCache.new
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 124 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
Public Instance Methods
active?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 176 def active? @active != false end
allowed_index_name_length()
click to toggle source
Returns 62. SQLite supports index names up to 64 characters. The rest is used by rails internally to perform temporary rename operations
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 200 def allowed_index_name_length index_name_length - 2 end
change_column_null(table_name, column_name, null, default = nil)
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 473 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!()
click to toggle source
Clears the prepared statements cache.
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 189 def clear_cache! @statements.clear end
disconnect!()
click to toggle source
Disconnects from the database if already connected. Otherwise, this method does nothing.
Calls superclass method
ActiveRecord::ConnectionAdapters::AbstractAdapter#disconnect!
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 182 def disconnect! super @active = false @connection.close rescue nil end
encoding()
click to toggle source
Returns the current database encoding format as a string, eg: 'UTF-8'
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 209 def encoding @connection.encoding.to_s end
exec_delete(sql, name = 'SQL', binds = [])
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 318 def exec_delete(sql, name = 'SQL', binds = []) exec_query(sql, name, binds) @connection.changes end
Also aliased as: exec_update
exec_query(sql, name = nil, binds = [])
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 288 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
explain(arel, binds = [])
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 269 def explain(arel, binds = []) sql = "EXPLAIN QUERY PLAN #{to_sql(arel, binds)}" ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', [])) end
last_inserted_id(result)
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 324 def last_inserted_id(result) @connection.last_insert_row_id end
quote_table_name_for_assignment(table, attr)
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 247 def quote_table_name_for_assignment(table, attr) quote_column_name(attr) end
rename_table(table_name, new_name)
click to toggle source
Renames a table.
Example:
rename_table('octopuses', 'octopi')
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 440 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?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 168 def requires_reloading? true end
select_rows(sql, name = nil, binds = [])
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 348 def select_rows(sql, name = nil, binds = []) exec_query(sql, name, binds).rows end
supports_ddl_transactions?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 141 def supports_ddl_transactions? true end
supports_explain?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 213 def supports_explain? true end
supports_index_sort_order?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 193 def supports_index_sort_order? true end
supports_partial_index?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 149 def supports_partial_index? sqlite_version >= '3.8.0' end
supports_savepoints?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 145 def supports_savepoints? true end
supports_statement_cache?()
click to toggle source
Returns true, since this connection adapter supports prepared statement caching.
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 155 def supports_statement_cache? true end
supports_views?()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 172 def supports_views? true end
table_exists?(table_name)
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 379 def table_exists?(table_name) table_name && tables(nil, table_name).any? end
valid_alter_table_type?(type)
click to toggle source
See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 447 def valid_alter_table_type?(type) type.to_sym != :primary_key end
Protected Instance Methods
initialize_type_map(m)
click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 504 def initialize_type_map(m) super m.register_type(/binary/i, SQLite3Binary.new) end
sqlite_version()
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 604 def sqlite_version @sqlite_version ||= SQLite3Adapter::Version.new(select_value('select sqlite_version(*)')) end
table_structure(table_name)
click to toggle source
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 509 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)
click to toggle source
Calls superclass method
ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception
# File lib/active_record/connection_adapters/sqlite3_adapter.rb, line 608 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