Module | Sequel::SQLite::DatabaseMethods |
In: |
lib/sequel/adapters/shared/sqlite.rb
|
AUTO_VACUUM | = | [:none, :full, :incremental].freeze |
SYNCHRONOUS | = | [:off, :normal, :full].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 => "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. |
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 60 60: def foreign_key_list(table, opts=OPTS) 61: m = output_identifier_meth 62: h = {} 63: metadata_dataset.with_sql("PRAGMA foreign_key_list(?)", input_identifier_meth.call(table)).each do |row| 64: if r = h[row[:id]] 65: r[:columns] << m.call(row[:from]) 66: r[:key] << m.call(row[:to]) if r[:key] 67: else 68: 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])} 69: end 70: end 71: h.values 72: end
# File lib/sequel/adapters/shared/sqlite.rb, line 74 74: def freeze 75: sqlite_version 76: use_timestamp_timezones? 77: super 78: end
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
# File lib/sequel/adapters/shared/sqlite.rb, line 81 81: def indexes(table, opts=OPTS) 82: m = output_identifier_meth 83: im = input_identifier_meth 84: indexes = {} 85: metadata_dataset.with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| 86: if opts[:only_autocreated] 87: # If specifically asked for only autocreated indexes, then return those an only those 88: next unless r[:name] =~ /\Asqlite_autoindex_/ 89: elsif r.has_key?(:origin) 90: # If origin is set, then only exclude primary key indexes and partial indexes 91: next if r[:origin] == 'pk' 92: next if r[:partial].to_i == 1 93: else 94: # When :origin key not present, assume any autoindex could be a primary key one and exclude it 95: next if r[:name] =~ /\Asqlite_autoindex_/ 96: end 97: 98: indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1} 99: end 100: indexes.each do |k, v| 101: v[:columns] = metadata_dataset.with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)} 102: end 103: indexes 104: end
Set the integer_booleans option using the passed in :integer_boolean option.
# File lib/sequel/adapters/shared/sqlite.rb, line 54 54: def set_integer_booleans 55: @integer_booleans = @opts.has_key?(:integer_booleans) ? typecast_value_boolean(@opts[:integer_booleans]) : true 56: 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 108 108: def sqlite_version 109: return @sqlite_version if defined?(@sqlite_version) 110: @sqlite_version = begin 111: v = fetch('SELECT sqlite_version()').single_value 112: [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])} 113: rescue 114: 0 115: end 116: end
Set the default transaction mode.
# File lib/sequel/adapters/shared/sqlite.rb, line 40 40: def transaction_mode=(value) 41: if TRANSACTION_MODE.include?(value) 42: @transaction_mode = value 43: else 44: raise Error, "Invalid value for transaction_mode. Please specify one of :deferred, :immediate, :exclusive, nil" 45: end 46: 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 145 145: def use_timestamp_timezones? 146: defined?(@use_timestamp_timezones) ? @use_timestamp_timezones : (@use_timestamp_timezones = false) 147: 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 161 161: def values(v) 162: @default_dataset.clone(:values=>v) 163: end