Module: Sequel::SQLite::DatabaseMethods
- Included in:
- Amalgalite::Database, DataObjects::SQLite::DatabaseMethods, JDBC::SQLite::DatabaseMethods, Database, Sequel::Swift::SQLite::DatabaseMethods
- Defined in:
- lib/sequel/adapters/shared/sqlite.rb
Overview
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.
Constant Summary collapse
- 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
Instance Method Summary collapse
-
#alter_table(name, generator = nil, &block) ⇒ Object
Run all alter_table commands in a transaction.
-
#auto_vacuum ⇒ Object
A symbol signifying the value of the auto_vacuum PRAGMA.
-
#auto_vacuum=(value) ⇒ Object
Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental).
-
#database_type ⇒ Object
SQLite uses the :sqlite database type.
-
#foreign_keys ⇒ Object
Boolean signifying the value of the foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.
-
#foreign_keys=(value) ⇒ Object
Set the foreign_keys PRAGMA using the given boolean value, if using SQLite 3.6.19+.
-
#indexes(table, opts = {}) ⇒ Object
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
-
#pragma_get(name) ⇒ Object
Get the value of the given PRAGMA.
-
#pragma_set(name, value) ⇒ Object
Set the value of the given PRAGMA to value.
-
#sqlite_version ⇒ Object
The version of the server as an integer, where 3.6.19 = 30619.
-
#supports_savepoints? ⇒ Boolean
SQLite 3.6.8+ supports savepoints.
-
#synchronous ⇒ Object
A symbol signifying the value of the synchronous PRAGMA.
-
#synchronous=(value) ⇒ Object
Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full).
-
#tables(opts = {}) ⇒ Object
Array of symbols specifying the table names in the current database.
-
#temp_store ⇒ Object
A symbol signifying the value of the temp_store PRAGMA.
-
#temp_store=(value) ⇒ Object
Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory).
Instance Method Details
#alter_table(name, generator = nil, &block) ⇒ Object
Run all alter_table commands in a transaction. This is technically only needed for drop column.
15 16 17 18 19 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 15 def alter_table(name, generator=nil, &block) remove_cached_schema(name) generator ||= Schema::AlterTableGenerator.new(self, &block) transaction{generator.operations.each{|op| alter_table_sql_list(name, [op]).flatten.each{|sql| execute_ddl(sql)}}} end |
#auto_vacuum ⇒ Object
A symbol signifying the value of the auto_vacuum PRAGMA.
22 23 24 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 22 def auto_vacuum AUTO_VACUUM[pragma_get(:auto_vacuum).to_i] end |
#auto_vacuum=(value) ⇒ Object
Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental). See pragma_set. Consider using the :auto_vacuum Database option instead.
29 30 31 32 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 29 def auto_vacuum=(value) value = AUTO_VACUUM.index(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.") pragma_set(:auto_vacuum, value) end |
#database_type ⇒ Object
SQLite uses the :sqlite database type.
35 36 37 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 35 def database_type :sqlite end |
#foreign_keys ⇒ Object
Boolean signifying the value of the foreign_keys PRAGMA, or nil if not using SQLite 3.6.19+.
41 42 43 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 41 def foreign_keys pragma_get(:foreign_keys).to_i == 1 if sqlite_version >= 30619 end |
#foreign_keys=(value) ⇒ Object
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.
48 49 50 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 48 def foreign_keys=(value) pragma_set(:foreign_keys, !!value ? 'on' : 'off') if sqlite_version >= 30619 end |
#indexes(table, opts = {}) ⇒ Object
Use the index_list and index_info PRAGMAs to determine the indexes on the table.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 53 def indexes(table, opts={}) m = output_identifier_meth im = input_identifier_meth indexes = {} begin .with_sql("PRAGMA index_list(?)", im.call(table)).each do |r| next if r[:name] =~ PRIMARY_KEY_INDEX_RE indexes[m.call(r[:name])] = {:unique=>r[:unique].to_i==1} end rescue Sequel::DatabaseError nil else indexes.each do |k, v| v[:columns] = .with_sql("PRAGMA index_info(?)", im.call(k)).map(:name).map{|x| m.call(x)} end end indexes end |
#pragma_get(name) ⇒ Object
Get the value of the given PRAGMA.
73 74 75 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 73 def pragma_get(name) self["PRAGMA #{name}"].single_value end |
#pragma_set(name, value) ⇒ Object
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.
83 84 85 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 83 def pragma_set(name, value) execute_ddl("PRAGMA #{name} = #{value}") end |
#sqlite_version ⇒ Object
The version of the server as an integer, where 3.6.19 = 30619. If the server version can’t be determined, 0 is used.
89 90 91 92 93 94 95 96 97 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 89 def sqlite_version return @sqlite_version if defined?(@sqlite_version) @sqlite_version = begin v = get{sqlite_version{}} [10000, 100, 1].zip(v.split('.')).inject(0){|a, m| a + m[0] * Integer(m[1])} rescue 0 end end |
#supports_savepoints? ⇒ Boolean
SQLite 3.6.8+ supports savepoints.
100 101 102 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 100 def supports_savepoints? sqlite_version >= 30608 end |
#synchronous ⇒ Object
A symbol signifying the value of the synchronous PRAGMA.
105 106 107 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 105 def synchronous SYNCHRONOUS[pragma_get(:synchronous).to_i] end |
#synchronous=(value) ⇒ Object
Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full). See pragma_set. Consider using the :synchronous Database option instead.
111 112 113 114 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 111 def synchronous=(value) value = SYNCHRONOUS.index(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.") pragma_set(:synchronous, value) end |
#tables(opts = {}) ⇒ Object
Array of symbols specifying the table names in the current database.
Options:
-
:server - Set the server to use.
120 121 122 123 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 120 def tables(opts={}) m = output_identifier_meth .from(:sqlite_master).server(opts[:server]).filter(TABLES_FILTER).map{|r| m.call(r[:name])} end |
#temp_store ⇒ Object
A symbol signifying the value of the temp_store PRAGMA.
126 127 128 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 126 def temp_store TEMP_STORE[pragma_get(:temp_store).to_i] end |
#temp_store=(value) ⇒ Object
Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory). See pragma_set. Consider using the :temp_store Database option instead.
132 133 134 135 |
# File 'lib/sequel/adapters/shared/sqlite.rb', line 132 def temp_store=(value) value = TEMP_STORE.index(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.") pragma_set(:temp_store, value) end |