Module Sequel::SQLite::DatabaseMethods
In: lib/sequel/adapters/shared/sqlite.rb

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

Constants

AUTO_VACUUM = [:none, :full, :incremental].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
VIEWS_FILTER = "type = 'view'".freeze
TRANSACTION_MODE = { :deferred => "BEGIN DEFERRED TRANSACTION".freeze, :immediate => "BEGIN IMMEDIATE TRANSACTION".freeze, :exclusive => "BEGIN EXCLUSIVE TRANSACTION".freeze, nil => Sequel::Database::SQL_BEGIN, }.freeze
DATABASE_ERROR_REGEXPS = { /(is|are) not unique\z|PRIMARY KEY must be unique\z|UNIQUE constraint failed: .+\z/ => UniqueConstraintViolation, /foreign key constraint failed\z/i => ForeignKeyConstraintViolation, /\ACHECK constraint failed/ => CheckConstraintViolation, /\A(SQLITE ERROR 19 \(CONSTRAINT\) : )?constraint failed\z/ => ConstraintViolation, /may not be NULL\z|NOT NULL constraint failed: .+\z/ => NotNullConstraintViolation, }.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. It is set to false by default, as SQLite‘s date/time methods do not support timezones in timestamps.

Public Instance methods

A symbol signifying the value of the auto_vacuum PRAGMA.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 29
29:       def auto_vacuum
30:         AUTO_VACUUM[pragma_get(:auto_vacuum).to_i]
31:       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.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 36
36:       def auto_vacuum=(value)
37:         value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.")
38:         pragma_set(:auto_vacuum, value)
39:       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.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 44
44:       def case_sensitive_like=(value)
45:         pragma_set(:case_sensitive_like, !!value ? 'on' : 'off') if sqlite_version >= 30203
46:       end

SQLite uses the :sqlite database type.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 61
61:       def database_type
62:         :sqlite
63:       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.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 80
80:       def foreign_key_list(table, opts=OPTS)
81:         m = output_identifier_meth
82:         h = {}
83:         metadata_dataset.with_sql("PRAGMA foreign_key_list(?)", input_identifier_meth.call(table)).each do |row|
84:           if r = h[row[:id]]
85:             r[:columns] << m.call(row[:from])
86:             r[:key] << m.call(row[:to]) if r[:key]
87:           else
88:             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])}
89:           end
90:         end
91:         h.values
92:       end

Boolean signifying the value of the foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 67
67:       def foreign_keys
68:         pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619
69:       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.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 74
74:       def foreign_keys=(value)
75:         pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619
76:       end

Use the index_list and index_info PRAGMAs to determine the indexes on the table.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 95
 95:       def indexes(table, opts=OPTS)
 96:         m = output_identifier_meth
 97:         im = input_identifier_meth
 98:         indexes = {}
 99:         metadata_dataset.with_sql("PRAGMA index_list(?)", im.call(table)).each do |r|
100:           # :only_autocreated internal option can be used to get only autocreated indexes
101:           next if (!!(r[:name] =~ PRIMARY_KEY_INDEX_RE) ^ !!opts[:only_autocreated])
102:           indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1}
103:         end
104:         indexes.each do |k, v|
105:           v[:columns] = metadata_dataset.with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)}
106:         end
107:         indexes
108:       end

Get the value of the given PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 111
111:       def pragma_get(name)
112:         self["PRAGMA #{name}"].single_value
113:       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.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 121
121:       def pragma_set(name, value)
122:         execute_ddl("PRAGMA #{name} = #{value}")
123:       end

Set the integer_booleans option using the passed in :integer_boolean option.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 126
126:       def set_integer_booleans
127:         @integer_booleans = @opts.has_key?(:integer_booleans) ? typecast_value_boolean(@opts[:integer_booleans]) : true
128:       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.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 132
132:       def sqlite_version
133:         return @sqlite_version if defined?(@sqlite_version)
134:         @sqlite_version = begin
135:           v = fetch('SELECT sqlite_version()').single_value
136:           [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])}
137:         rescue
138:           0
139:         end
140:       end

SQLite supports CREATE TABLE IF NOT EXISTS syntax since 3.3.0.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 143
143:       def supports_create_table_if_not_exists?
144:         sqlite_version >= 30300
145:       end

SQLite 3.6.19+ supports deferrable foreign key constraints.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 148
148:       def supports_deferrable_foreign_key_constraints?
149:         sqlite_version >= 30619
150:       end

SQLite 3.8.0+ supports partial indexes.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 153
153:       def supports_partial_indexes?
154:         sqlite_version >= 30800
155:       end

SQLite 3.6.8+ supports savepoints.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 158
158:       def supports_savepoints?
159:         sqlite_version >= 30608
160:       end

A symbol signifying the value of the synchronous PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 174
174:       def synchronous
175:         SYNCHRONOUS[pragma_get(:synchronous).to_i]
176:       end

Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full). See pragma_set. Consider using the :synchronous Database option instead.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 180
180:       def synchronous=(value)
181:         value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.")
182:         pragma_set(:synchronous, value)
183:       end

Array of symbols specifying the table names in the current database.

Options:

:server :Set the server to use.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 189
189:       def tables(opts=OPTS)
190:         tables_and_views(TABLES_FILTER, opts)
191:       end

A symbol signifying the value of the temp_store PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 194
194:       def temp_store
195:         TEMP_STORE[pragma_get(:temp_store).to_i]
196:       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.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 200
200:       def temp_store=(value)
201:         value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.")
202:         pragma_set(:temp_store, value)
203:       end

Set the default transaction mode.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 52
52:       def transaction_mode=(value)
53:         if TRANSACTION_MODE.include?(value)
54:           @transaction_mode = value
55:         else
56:           raise Error, "Invalid value for transaction_mode.  Please specify one of :deferred, :immediate, :exclusive, nil"
57:         end
58:       end

SQLite supports timezones in timestamps, since it just stores them as strings, but it breaks the usage of SQLite‘s datetime functions.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 169
169:       def use_timestamp_timezones?
170:         defined?(@use_timestamp_timezones) ? @use_timestamp_timezones : (@use_timestamp_timezones = false)
171:       end

Array of symbols specifying the view names in the current database.

Options:

:server :Set the server to use.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 209
209:       def views(opts=OPTS)
210:         tables_and_views(VIEWS_FILTER, opts)
211:       end

[Validate]