Module: Sequel::SQLite::DatabaseMethods

Included in:
JDBC::SQLite::DatabaseMethods, Database
Defined in:
lib/sequel_core/adapters/shared/sqlite.rb

Constant Summary collapse

AUTO_VACUUM =
{'0' => :none, '1' => :full, '2' => :incremental}.freeze
SYNCHRONOUS =
{'0' => :off, '1' => :normal, '2' => :full}.freeze
TABLES_FILTER =
"type = 'table' AND NOT name = 'sqlite_sequence'"
TEMP_STORE =
{'0' => :default, '1' => :file, '2' => :memory}.freeze

Instance Method Summary collapse

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.



11
12
13
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 11

def alter_table(name, generator=nil, &block)
  transaction{super}
end

#alter_table_sql(table, op) ⇒ Object

SQLite supports limited table modification. You can add a column or an index. Dropping columns is supported by copying the table into a temporary table, dropping the table, and creating a new table without the column inside of a transaction.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 19

def alter_table_sql(table, op)
  case op[:op]
  when :add_column
    super
  when :add_index
    index_definition_sql(table, op)
  when :drop_column
    columns_str = (schema_parse_table(table, {}).map{|c| c[0]} - Array(op[:name])).join(",")
     ["CREATE TEMPORARY TABLE #{table}_backup(#{columns_str})",
     "INSERT INTO #{table}_backup SELECT #{columns_str} FROM #{table}",
     "DROP TABLE #{table}",
     "CREATE TABLE #{table}(#{columns_str})",
     "INSERT INTO #{table} SELECT #{columns_str} FROM #{table}_backup",
     "DROP TABLE #{table}_backup"]
  else
    raise Error, "Unsupported ALTER TABLE operation"
  end
end

#auto_vacuumObject

A symbol signifying the value of the auto_vacuum PRAGMA.



39
40
41
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 39

def auto_vacuum
  AUTO_VACUUM[pragma_get(:auto_vacuum).to_s]
end

#auto_vacuum=(value) ⇒ Object

Set the auto_vacuum PRAGMA using the given symbol (:none, :full, or :incremental).



45
46
47
48
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 45

def auto_vacuum=(value)
  value = AUTO_VACUUM.key(value) || (raise Error, "Invalid value for auto_vacuum option. Please specify one of :none, :full, :incremental.")
  pragma_set(:auto_vacuum, value)
end

#pragma_get(name) ⇒ Object

Get the value of the given PRAGMA.



51
52
53
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 51

def pragma_get(name)
  self["PRAGMA #{name}"].single_value
end

#pragma_set(name, value) ⇒ Object

Set the value of the given PRAGMA to value.



56
57
58
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 56

def pragma_set(name, value)
  execute_ddl("PRAGMA #{name} = #{value}")
end

#synchronousObject

A symbol signifying the value of the synchronous PRAGMA.



61
62
63
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 61

def synchronous
  SYNCHRONOUS[pragma_get(:synchronous).to_s]
end

#synchronous=(value) ⇒ Object

Set the synchronous PRAGMA using the given symbol (:off, :normal, or :full).



66
67
68
69
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 66

def synchronous=(value)
  value = SYNCHRONOUS.key(value) || (raise Error, "Invalid value for synchronous option. Please specify one of :off, :normal, :full.")
  pragma_set(:synchronous, value)
end

#tablesObject

Array of symbols specifying the table names in the current database.



72
73
74
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 72

def tables
  self[:sqlite_master].filter(TABLES_FILTER).map {|r| r[:name].to_sym}
end

#temp_storeObject

A symbol signifying the value of the temp_store PRAGMA.



77
78
79
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 77

def temp_store
  TEMP_STORE[pragma_get(:temp_store).to_s]
end

#temp_store=(value) ⇒ Object

Set the temp_store PRAGMA using the given symbol (:default, :file, or :memory).



82
83
84
85
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 82

def temp_store=(value)
  value = TEMP_STORE.key(value) || (raise Error, "Invalid value for temp_store option. Please specify one of :default, :file, :memory.")
  pragma_set(:temp_store, value)
end