Class: RDBI::Driver::SQLite3::Database

Inherits:
RDBI::Database
  • Object
show all
Extended by:
MethLab
Defined in:
lib/rdbi/driver/sqlite3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Database

Returns a new instance of Database.



19
20
21
22
23
24
25
26
# File 'lib/rdbi/driver/sqlite3.rb', line 19

def initialize(*args)
  super
  self.database_name = @connect_args[:database]
  sqlite3_connect
  @preprocess_quoter = proc do |x, named, indexed|
    ::SQLite3::Database.quote((named[x] || indexed[x]).to_s)
  end
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



17
18
19
# File 'lib/rdbi/driver/sqlite3.rb', line 17

def handle
  @handle
end

Instance Method Details

#commitObject

Raises:

  • (RDBI::TransactionError)


87
88
89
90
91
# File 'lib/rdbi/driver/sqlite3.rb', line 87

def commit
  raise RDBI::TransactionError, "not in a transaction during commit" unless in_transaction?
  @handle.commit
  super()
end

#disconnectObject



33
34
35
36
# File 'lib/rdbi/driver/sqlite3.rb', line 33

def disconnect
  super
  @handle.close
end

#new_statement(query) ⇒ Object



44
45
46
47
# File 'lib/rdbi/driver/sqlite3.rb', line 44

def new_statement(query)
  sth = Statement.new(query, self)
  return sth
end

#reconnectObject



28
29
30
31
# File 'lib/rdbi/driver/sqlite3.rb', line 28

def reconnect
  super
  sqlite3_connect
end

#rollbackObject

Raises:

  • (RDBI::TransactionError)


81
82
83
84
85
# File 'lib/rdbi/driver/sqlite3.rb', line 81

def rollback
  raise RDBI::TransactionError, "not in a transaction during rollback" unless in_transaction?
  @handle.rollback
  super()
end

#schemaObject



49
50
51
52
53
54
55
56
# File 'lib/rdbi/driver/sqlite3.rb', line 49

def schema
  sch = { }
  execute("SELECT name, type FROM sqlite_master WHERE type='table' or type='view'").fetch(:all).each do |row|
    table_name_sym, table_name, table_type_sym = row[0].to_sym, row[0], row[1].to_sym
    sch[table_name_sym] = table_schema(table_name, table_type_sym)
  end
  return sch
end

#table_schema(table_name, type = nil) ⇒ Object

overloaded for performance



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rdbi/driver/sqlite3.rb', line 58

def table_schema(table_name, type = nil) # overloaded for performance
  sch = RDBI::Schema.new([], [], type)
  sch.tables << table_name.to_sym

  unless sch.type
    sch.type = execute("select type from sqlite_master where (type='table' or type='view') and name=?", table_name.to_s).fetch(:first)[0].to_sym rescue nil
    return nil unless sch.type
  end

  @handle.table_info(table_name) do |hash|
    col = RDBI::Column.new
    col.name       = hash['name'].to_sym
    col.type       = hash['type'].to_sym
    col.ruby_type  = hash['type'].to_sym
    col.nullable   = !(hash['notnull'] == "0")
    sch.columns << col
  end

  return sch
end

#transaction(&block) ⇒ Object

Raises:

  • (RDBI::TransactionError)


38
39
40
41
42
# File 'lib/rdbi/driver/sqlite3.rb', line 38

def transaction(&block)
  raise RDBI::TransactionError, "already in a transaction" if in_transaction?
  @handle.transaction
  super
end