Module: Sequel::SQLite::DatabaseMethods

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

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

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.


12
13
14
15
16
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 12

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_vacuumObject

A symbol signifying the value of the auto_vacuum PRAGMA.


19
20
21
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 19

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


25
26
27
28
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 25

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_typeObject

SQLite uses the :sqlite database type.


31
32
33
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 31

def database_type
  :sqlite
end

#indexes(table) ⇒ Object

Return a hash containing index information. Hash keys are index name symbols. Values are subhashes with two keys, :columns and :unique. The value of :columns is an array of symbols of column names. The value of :unique is true or false depending on if the index is unique.


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 39

def indexes(table)
  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.


59
60
61
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 59

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

#pragma_set(name, value) ⇒ Object

Set the value of the given PRAGMA to value.


64
65
66
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 64

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

#supports_savepoints?Boolean

SQLite supports savepoints


69
70
71
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 69

def supports_savepoints?
  true
end

#synchronousObject

A symbol signifying the value of the synchronous PRAGMA.


74
75
76
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 74

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

#synchronous=(value) ⇒ Object

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


79
80
81
82
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 79

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.


88
89
90
91
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 88

def tables(opts={})
  m = output_identifier_meth
  .from(:sqlite_master).server(opts[:server]).filter(TABLES_FILTER).map{|r| m.call(r[:name])}
end

#temp_storeObject

A symbol signifying the value of the temp_store PRAGMA.


94
95
96
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 94

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


99
100
101
102
# File 'lib/sequel/lib/sequel/adapters/shared/sqlite.rb', line 99

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