No matter how you connect to SQLite, the following Database options can be used to set PRAGMAs on connections in a thread-safe manner: :#auto_vacuum, :#foreign_keys, :synchronous, and :temp_store.
Methods
Public Instance
- auto_vacuum
- auto_vacuum=
- case_sensitive_like=
- database_type
- foreign_key_list
- foreign_keys
- foreign_keys=
- indexes
- integer_booleans
- pragma_get
- pragma_set
- set_integer_booleans
- sqlite_version
- supports_create_table_if_not_exists?
- supports_deferrable_foreign_key_constraints?
- supports_partial_indexes?
- supports_savepoints?
- synchronous
- synchronous=
- tables
- temp_store
- temp_store=
- transaction_mode
- transaction_mode=
- use_timestamp_timezones
- use_timestamp_timezones?
- views
Constants
AUTO_VACUUM | = | [:none, :full, :incremental].freeze | ||
DATABASE_ERROR_REGEXPS | = | { /is not unique\z/ => UniqueConstraintViolation, /foreign key constraint failed\z/ => ForeignKeyConstraintViolation, /\A(SQLITE ERROR 19 \(CONSTRAINT\) : )?constraint failed\z/ => ConstraintViolation, /may not be NULL\z/ => NotNullConstraintViolation, }.freeze | ||
PRIMARY_KEY_INDEX_RE | = | /\Asqlite_autoindex_/.freeze | ||
SYNCHRONOUS | = | [:off, :normal, :full].freeze | ||
TABLES_FILTER | = | "type = 'table' AND NOT name = 'sqlite_sequence'".freeze | ||
TEMP_STORE | = | [:default, :file, :memory].freeze | ||
TRANSACTION_MODE | = | { :deferred => "BEGIN DEFERRED TRANSACTION".freeze, :immediate => "BEGIN IMMEDIATE TRANSACTION".freeze, :exclusive => "BEGIN EXCLUSIVE TRANSACTION".freeze, nil => Sequel::Database::SQL_BEGIN, }.freeze | ||
VIEWS_FILTER | = | "type = 'view'".freeze |
Attributes
integer_booleans | [RW] |
Whether to use integers for booleans in the database. SQLite recommends booleans be stored as integers, but historically Sequel has used ‘t’/‘f’. |
transaction_mode | [R] |
A symbol signifying the value of the default transaction mode |
use_timestamp_timezones | [W] |
Override the default setting for whether to use timezones in timestamps.
For backwards compatibility, it is set to |
Public Instance methods
A symbol signifying the value of the #auto_vacuum PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 29 def auto_vacuum AUTO_VACUUM[pragma_get(:auto_vacuum).to_i] end
Set the #auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental). See pragma_set. Consider using the :#auto_vacuum Database option instead.
# File lib/sequel/adapters/shared/sqlite.rb, line 36 def auto_vacuum=(value) value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.") pragma_set(:auto_vacuum, value) end
Set the case_sensitive_like PRAGMA using the given boolean value, if using SQLite 3.2.3+. If not using 3.2.3+, no error is raised. See pragma_set. Consider using the :case_sensitive_like Database option instead.
# File lib/sequel/adapters/shared/sqlite.rb, line 44 def case_sensitive_like=(value) pragma_set(:case_sensitive_like, !!value ? 'on' : 'off') if sqlite_version >= 30203 end
SQLite uses the :sqlite database type.
# File lib/sequel/adapters/shared/sqlite.rb, line 61 def database_type :sqlite end
Return the array of foreign key info hashes using the #foreign_key_list PRAGMA, including information for the :on_update and :on_delete entries.
# File lib/sequel/adapters/shared/sqlite.rb, line 80 def foreign_key_list(table, opts=OPTS) m = output_identifier_meth h = {} metadata_dataset.with_sql("PRAGMA foreign_key_list(?)", input_identifier_meth.call(table)).each do |row| if r = h[row[:id]] r[:columns] << m.call(row[:from]) r[:key] << m.call(row[:to]) if r[:key] else h[row[:id]] = {:columns=>[m.call(row[:from])], :table=>m.call(row[:table]), :key=>([m.call(row[:to])] if row[:to]), :on_update=>on_delete_sql_to_sym(row[:on_update]), :on_delete=>on_delete_sql_to_sym(row[:on_delete])} end end h.values end
Boolean signifying the value of the #foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.
# File lib/sequel/adapters/shared/sqlite.rb, line 67 def foreign_keys pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619 end
Set the #foreign_keys PRAGMA using the given boolean value, if using SQLite 3.6.19+. If not using 3.6.19+, no error is raised. See pragma_set. Consider using the :#foreign_keys Database option instead.
# File lib/sequel/adapters/shared/sqlite.rb, line 74 def foreign_keys=(value) pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619 end
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
# File lib/sequel/adapters/shared/sqlite.rb, line 95 def indexes(table, opts=OPTS) m = output_identifier_meth im = input_identifier_meth indexes = {} metadata_dataset.with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| next if r[:name] =~ PRIMARY_KEY_INDEX_RE indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1} end indexes.each do |k, v| v[:columns] = metadata_dataset.with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)} end indexes end
Get the value of the given PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 110 def pragma_get(name) self["PRAGMA #{name}"].single_value end
Set the value of the given PRAGMA to value.
This method is not thread safe, and will not work correctly if there are multiple connections in the Database’s connection pool. PRAGMA modifications should be done when the connection is created, using an option provided when creating the Database object.
# File lib/sequel/adapters/shared/sqlite.rb, line 120 def pragma_set(name, value) execute_ddl("PRAGMA #{name} = #{value}") end
Set the #integer_booleans option using the passed in :integer_boolean option.
# File lib/sequel/adapters/shared/sqlite.rb, line 125 def set_integer_booleans @integer_booleans = @opts.has_key?(:integer_booleans) ? typecast_value_boolean(@opts[:integer_booleans]) : true end
The version of the server as an integer, where 3.6.19 = 30619. If the server version can’t be determined, 0 is used.
# File lib/sequel/adapters/shared/sqlite.rb, line 131 def sqlite_version return @sqlite_version if defined?(@sqlite_version) @sqlite_version = begin v = get{sqlite_version{}} [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])} rescue 0 end end
SQLite supports CREATE TABLE IF NOT EXISTS syntax since 3.3.0.
# File lib/sequel/adapters/shared/sqlite.rb, line 142 def supports_create_table_if_not_exists? sqlite_version >= 30300 end
SQLite 3.6.19+ supports deferrable foreign key constraints.
# File lib/sequel/adapters/shared/sqlite.rb, line 147 def supports_deferrable_foreign_key_constraints? sqlite_version >= 30619 end
SQLite 3.8.0+ supports partial indexes.
# File lib/sequel/adapters/shared/sqlite.rb, line 152 def supports_partial_indexes? sqlite_version >= 30800 end
SQLite 3.6.8+ supports savepoints.
# File lib/sequel/adapters/shared/sqlite.rb, line 157 def supports_savepoints? sqlite_version >= 30608 end
A symbol signifying the value of the synchronous PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 175 def synchronous SYNCHRONOUS[pragma_get(:synchronous).to_i] end
Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full). See pragma_set. Consider using the :synchronous Database option instead.
# File lib/sequel/adapters/shared/sqlite.rb, line 181 def synchronous=(value) value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.") pragma_set(:synchronous, value) end
Array of symbols specifying the table names in the current database.
Options:
-
:server - Set the server to use.
# File lib/sequel/adapters/shared/sqlite.rb, line 190 def tables(opts=OPTS) tables_and_views(TABLES_FILTER, opts) end
A symbol signifying the value of the #temp_store PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 195 def temp_store TEMP_STORE[pragma_get(:temp_store).to_i] end
Set the #temp_store PRAGMA using the given symbol (:default, :file, or :memory). See pragma_set. Consider using the :#temp_store Database option instead.
# File lib/sequel/adapters/shared/sqlite.rb, line 201 def temp_store=(value) value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.") pragma_set(:temp_store, value) end
Set the default transaction mode.
# File lib/sequel/adapters/shared/sqlite.rb, line 52 def transaction_mode=(value) if TRANSACTION_MODE.include?(value) @transaction_mode = value else raise Error, "Invalid value for transaction_mode. Please specify one of :deferred, :immediate, :exclusive, nil" end end
SQLite supports timezones in timestamps, since it just stores them as strings, but it breaks the usage of SQLite's datetime functions.
# File lib/sequel/adapters/shared/sqlite.rb, line 170 def use_timestamp_timezones? defined?(@use_timestamp_timezones) ? @use_timestamp_timezones : (@use_timestamp_timezones = false) end
Array of symbols specifying the view names in the current database.
Options:
-
:server - Set the server to use.
# File lib/sequel/adapters/shared/sqlite.rb, line 210 def views(opts=OPTS) tables_and_views(VIEWS_FILTER, opts) end