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.
AUTO_VACUUM | = | [:none, :full, :incremental].freeze |
PRIMARY_KEY_INDEX_RE | = | /\Asqlite_autoindex_/.freeze |
SYNCHRONOUS | = | [:off, :normal, :full].freeze |
TABLES_FILTER | = | Sequel.~(:name=>'sqlite_sequence'.freeze) & {:type => 'table'.freeze} |
TEMP_STORE | = | [:default, :file, :memory].freeze |
VIEWS_FILTER | = | {:type => 'view'.freeze}.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, /\ASQLITE ERROR \d+ \(\) : CHECK constraint failed: / => CheckConstraintViolation |
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. |
A symbol signifying the value of the auto_vacuum PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 39 39: def auto_vacuum 40: AUTO_VACUUM[pragma_get(:auto_vacuum).to_i] 41: 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 46 46: def auto_vacuum=(value) 47: value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.") 48: pragma_set(:auto_vacuum, value) 49: 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 54 54: def case_sensitive_like=(value) 55: pragma_set(:case_sensitive_like, !!value ? 'on' : 'off') if sqlite_version >= 30203 56: 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 90 90: def foreign_key_list(table, opts=OPTS) 91: m = output_identifier_meth 92: h = {} 93: metadata_dataset.with_sql("PRAGMA foreign_key_list(?)", input_identifier_meth.call(table)).each do |row| 94: if r = h[row[:id]] 95: r[:columns] << m.call(row[:from]) 96: r[:key] << m.call(row[:to]) if r[:key] 97: else 98: 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])} 99: end 100: end 101: h.values 102: 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 77 77: def foreign_keys 78: pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619 79: 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 84 84: def foreign_keys=(value) 85: pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619 86: end
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
# File lib/sequel/adapters/shared/sqlite.rb, line 105 105: def indexes(table, opts=OPTS) 106: m = output_identifier_meth 107: im = input_identifier_meth 108: indexes = {} 109: metadata_dataset.with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| 110: # :only_autocreated internal option can be used to get only autocreated indexes 111: next if (!!(r[:name] =~ PRIMARY_KEY_INDEX_RE) ^ !!opts[:only_autocreated]) 112: indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1} 113: end 114: indexes.each do |k, v| 115: v[:columns] = metadata_dataset.with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)} 116: end 117: indexes 118: end
Get the value of the given PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 121 121: def pragma_get(name) 122: self["PRAGMA #{name}"].single_value 123: 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 131 131: def pragma_set(name, value) 132: execute_ddl("PRAGMA #{name} = #{value}") 133: end
Set the integer_booleans option using the passed in :integer_boolean option.
# File lib/sequel/adapters/shared/sqlite.rb, line 136 136: def set_integer_booleans 137: @integer_booleans = @opts.has_key?(:integer_booleans) ? typecast_value_boolean(@opts[:integer_booleans]) : true 138: 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 142 142: def sqlite_version 143: return @sqlite_version if defined?(@sqlite_version) 144: @sqlite_version = begin 145: v = fetch('SELECT sqlite_version()').single_value 146: [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])} 147: rescue 148: 0 149: end 150: end
A symbol signifying the value of the synchronous PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 184 184: def synchronous 185: SYNCHRONOUS[pragma_get(:synchronous).to_i] 186: 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 190 190: def synchronous=(value) 191: value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.") 192: pragma_set(:synchronous, value) 193: end
A symbol signifying the value of the temp_store PRAGMA.
# File lib/sequel/adapters/shared/sqlite.rb, line 204 204: def temp_store 205: TEMP_STORE[pragma_get(:temp_store).to_i] 206: 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 210 210: def temp_store=(value) 211: value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.") 212: pragma_set(:temp_store, value) 213: end
Set the default transaction mode.
# File lib/sequel/adapters/shared/sqlite.rb, line 62 62: def transaction_mode=(value) 63: if TRANSACTION_MODE.include?(value) 64: @transaction_mode = value 65: else 66: raise Error, "Invalid value for transaction_mode. Please specify one of :deferred, :immediate, :exclusive, nil" 67: end 68: 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 179 179: def use_timestamp_timezones? 180: defined?(@use_timestamp_timezones) ? @use_timestamp_timezones : (@use_timestamp_timezones = false) 181: end
Creates a dataset that uses the VALUES clause:
DB.values([[1, 2], [3, 4]]) VALUES ((1, 2), (3, 4))
# File lib/sequel/adapters/shared/sqlite.rb, line 219 219: def values(v) 220: @default_dataset.clone(:values=>v) 221: end