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

Public Instance methods

Run all alter_table commands in a transaction. This is technically only needed for drop column.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 16
16:       def alter_table(name, generator=nil, &block)
17:         remove_cached_schema(name)
18:         generator ||= Schema::AlterTableGenerator.new(self, &block)
19:         transaction{generator.operations.each{|op| alter_table_sql_list(name, [op]).flatten.each{|sql| execute_ddl(sql)}}}
20:       end

A symbol signifying the value of the auto_vacuum PRAGMA.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 23
23:       def auto_vacuum
24:         AUTO_VACUUM[pragma_get(:auto_vacuum).to_i]
25:       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 30
30:       def auto_vacuum=(value)
31:         value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.")
32:         pragma_set(:auto_vacuum, value)
33:       end

SQLite uses the :sqlite database type.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 36
36:       def database_type
37:         :sqlite
38:       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 42
42:       def foreign_keys
43:         pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619
44:       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 49
49:       def foreign_keys=(value)
50:         pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619
51:       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 54
54:       def indexes(table, opts={})
55:         m = output_identifier_meth
56:         im = input_identifier_meth
57:         indexes = {}
58:         begin
59:           metadata_dataset.with_sql("PRAGMA index_list(?)", im.call(table)).each do |r|
60:             next if r[:name] =~ PRIMARY_KEY_INDEX_RE
61:             indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1}
62:           end
63:         rescue Sequel::DatabaseError
64:           nil
65:         else
66:           indexes.each do |k, v|
67:             v[:columns] = metadata_dataset.with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)}
68:           end
69:         end
70:         indexes
71:       end

Get the value of the given PRAGMA.

[Source]

    # File lib/sequel/adapters/shared/sqlite.rb, line 74
74:       def pragma_get(name)
75:         self["PRAGMA #{name}"].single_value
76:       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 84
84:       def pragma_set(name, value)
85:         execute_ddl("PRAGMA #{name} = #{value}")
86:       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 90
90:       def sqlite_version
91:         return @sqlite_version if defined?(@sqlite_version)
92:         @sqlite_version = begin
93:           v = get{sqlite_version{}}
94:           [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])}
95:         rescue
96:           0
97:         end
98:       end

SQLite supports CREATE TABLE IF NOT EXISTS syntax since 3.3.0.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 101
101:       def supports_create_table_if_not_exists?
102:         sqlite_version >= 30300
103:       end

SQLite 3.6.8+ supports savepoints.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 106
106:       def supports_savepoints?
107:         sqlite_version >= 30608
108:       end

A symbol signifying the value of the synchronous PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 111
111:       def synchronous
112:         SYNCHRONOUS[pragma_get(:synchronous).to_i]
113:       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 117
117:       def synchronous=(value)
118:         value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.")
119:         pragma_set(:synchronous, value)
120:       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 126
126:       def tables(opts={})
127:         tables_and_views(TABLES_FILTER, opts)
128:       end

A symbol signifying the value of the temp_store PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 131
131:       def temp_store
132:         TEMP_STORE[pragma_get(:temp_store).to_i]
133:       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 137
137:       def temp_store=(value)
138:         value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.")
139:         pragma_set(:temp_store, value)
140:       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 146
146:       def views(opts={})
147:         tables_and_views(VIEWS_FILTER, opts)
148:       end

[Validate]