class ActiveRecord::ConnectionAdapters::SQLite3Adapter

Parent:
ActiveRecord::ConnectionAdapters::AbstractAdapter

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

new(connection, logger, connection_options, config) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 86
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

Public Instance Methods

active?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 136
def active?
  @active != false
end
allowed_index_name_length() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 164
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

clear_cache!() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 149
def clear_cache!
  @statements.clear
end

Clears the prepared statements cache.

data_source_exists?(table_name) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 279
def data_source_exists?(table_name)
  return false unless table_name.present?

  sql = "SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name <> 'sqlite_sequence'"
  sql << " AND name = #{quote(table_name)}"

  select_values(sql, 'SCHEMA').any?
end
data_sources() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 265
def data_sources
  select_values("SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name <> 'sqlite_sequence'", 'SCHEMA')
end
disconnect!() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 142
def disconnect!
  super
  @active = false
  @connection.close rescue nil
end

Disconnects from the database if already connected. Otherwise, this method does nothing.

encoding() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 173
def encoding
  @connection.encoding.to_s
end

Returns the current database encoding format as a string, eg: 'UTF-8'

exec_delete(sql, name = 'SQL', binds = []) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 221
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 = [], prepare: false) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 190
def exec_query(sql, name = nil, binds = [], prepare: false)
  type_casted_binds = type_casted_binds(binds)

  log(sql, name, binds, type_casted_binds) 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
exec_update(sql, name = 'SQL', binds = [])
Alias for: exec_delete
explain(arel, binds = []) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 185
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) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 227
def last_inserted_id(result)
  @connection.last_insert_row_id
end
rename_table(table_name, new_name) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 360
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')
requires_reloading?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 120
def requires_reloading?
  true
end
supports_datetime_with_precision?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 128
def supports_datetime_with_precision?
  true
end
supports_ddl_transactions?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 93
def supports_ddl_transactions?
  true
end
supports_explain?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 177
def supports_explain?
  true
end
supports_index_sort_order?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 153
def supports_index_sort_order?
  true
end
supports_multi_insert?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 132
def supports_multi_insert?
  sqlite_version >= '3.7.11'
end
supports_partial_index?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 101
def supports_partial_index?
  sqlite_version >= '3.8.0'
end
supports_savepoints?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 97
def supports_savepoints?
  true
end
supports_statement_cache?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 107
def supports_statement_cache?
  true
end

Returns true, since this connection adapter supports prepared statement caching.

supports_views?() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 124
def supports_views?
  true
end
table_exists?(table_name) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 269
      def table_exists?(table_name)
        ActiveSupport::Deprecation.warn("          #table_exists? currently checks both tables and views.
          This behavior is deprecated and will be changed with Rails 5.1 to only check tables.
          Use #data_source_exists? instead.
".squish)

        data_source_exists?(table_name)
      end
valid_alter_table_type?(type) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 367
def valid_alter_table_type?(type)
  type.to_sym != :primary_key
end

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement

valid_type?(type) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 157
def valid_type?(type)
  true
end

Protected Instance Methods

sqlite_version() Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 511
def sqlite_version
  @sqlite_version ||= SQLite3Adapter::Version.new(select_value('select sqlite_version(*)'))
end
table_structure(table_name) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 427
def table_structure(table_name)
  structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA')
  raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
  table_structure_with_collation(table_name, structure)
end
translate_exception(exception, message) Show source
# File activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb, line 515
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)
  else
    super
  end
end

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.