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'"
TEMP_STORE = [:default, :file, :memory].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 15
15:       def alter_table(name, generator=nil, &block)
16:         remove_cached_schema(name)
17:         generator ||= Schema::AlterTableGenerator.new(self, &block)
18:         transaction{generator.operations.each{|op| alter_table_sql_list(name, [op]).flatten.each{|sql| execute_ddl(sql)}}}
19:       end

A symbol signifying the value of the auto_vacuum PRAGMA.

[Source]

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

SQLite uses the :sqlite database type.

[Source]

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

Get the value of the given PRAGMA.

[Source]

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

SQLite 3.6.8+ supports savepoints.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 100
100:       def supports_savepoints?
101:         sqlite_version >= 30608
102:       end

A symbol signifying the value of the synchronous PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 105
105:       def synchronous
106:         SYNCHRONOUS[pragma_get(:synchronous).to_i]
107:       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 111
111:       def synchronous=(value)
112:         value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.")
113:         pragma_set(:synchronous, value)
114:       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 120
120:       def tables(opts={})
121:         m = output_identifier_meth
122:         metadata_dataset.from(:sqlite_master).server(opts[:server]).filter(TABLES_FILTER).map{|r| m.call(r[:name])}
123:       end

A symbol signifying the value of the temp_store PRAGMA.

[Source]

     # File lib/sequel/adapters/shared/sqlite.rb, line 126
126:       def temp_store
127:         TEMP_STORE[pragma_get(:temp_store).to_i]
128:       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 132
132:       def temp_store=(value)
133:         value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.")
134:         pragma_set(:temp_store, value)
135:       end

[Validate]