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_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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 13

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(",")
    ["BEGIN TRANSACTION",
     "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",
     "COMMIT"]
  else
    raise Error, "Unsupported ALTER TABLE operation"
  end
end

#auto_vacuumObject

A symbol signifying the value of the auto_vacuum PRAGMA.



35
36
37
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 35

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).



41
42
43
44
# File 'lib/sequel_core/adapters/shared/sqlite.rb', line 41

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.



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

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

#pragma_set(name, value) ⇒ Object

Set the value of the given PRAGMA to value.



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

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

#synchronousObject

A symbol signifying the value of the synchronous PRAGMA.



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

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

#synchronous=(value) ⇒ Object

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



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

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.



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

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.



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

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).



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

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