Class: Swift::Adapter::Sqlite3

Inherits:
Sql show all
Defined in:
lib/swift/adapter/sqlite3.rb

Constant Summary

Constants inherited from Swift::Adapter

EM

Instance Attribute Summary

Attributes inherited from Swift::Adapter

#db

Instance Method Summary collapse

Methods inherited from Sql

#fields, #transaction

Methods inherited from Swift::Adapter

#create, #delete, #execute, #get, #identity_map, #log_command, #prepare, #trace, #trace?, #update

Constructor Details

#initialize(options = {}) ⇒ Sqlite3

Returns a new instance of Sqlite3


7
8
9
# File 'lib/swift/adapter/sqlite3.rb', line 7

def initialize options = {}
  super Swift::DB::Sqlite3.new(options)
end

Instance Method Details

#field_type(attribute) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/swift/adapter/sqlite3.rb', line 25

def field_type attribute
  case attribute
    when Type::String     then 'text'
    when Type::Integer    then attribute.serial ? 'integer primary key' : 'integer'
    when Type::Float      then 'float'
    when Type::BigDecimal then 'numeric'
    when Type::Time       then 'timestamp' # deprecated
    when Type::DateTime   then 'timestamp'
    when Type::Date       then 'date'
    when Type::Boolean    then 'boolean'
    when Type::IO         then 'blob'
    else 'text'
  end
end

#migrate!(record) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/swift/adapter/sqlite3.rb', line 15

def migrate! record
  keys   =  record.header.keys
  serial =  record.header.find(&:serial)
  fields =  record.header.map{|p| field_definition(p)}.join(', ')
  fields += ", primary key (#{keys.join(', ')})" unless serial or keys.empty?

  execute("drop table if exists #{record.store}")
  execute("create table #{record.store} (#{fields})")
end

#returning?Boolean

Returns:

  • (Boolean)

11
12
13
# File 'lib/swift/adapter/sqlite3.rb', line 11

def returning?
  false
end

#tablesObject


40
41
42
# File 'lib/swift/adapter/sqlite3.rb', line 40

def tables
  execute('select name from sqlite_master where type = ?', 'table').map(&:values).flatten
end

#write(table, fields = nil, io) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/swift/adapter/sqlite3.rb', line 44

def write table, fields = nil, io
  fields    = execute("select * from #{table} limit 0").fields if fields.nil? or fields.empty?
  statement = prepare("insert into #{table}(#{fields.join(',')}) values (%s)" % (['?'] * fields.size).join(','))

  r  = 0
  io = io.read if io.respond_to?(:read)
  io.split(/\n+/).each do |line|
    r += statement.execute(*line.split(/\t/).map {|value| value == '\N' ? nil : value}).affected_rows
  end

  # TODO: a better way to return a pretend result
  Struct.new(:affected_rows).new(r)
end