Class: Rigrate::Sqlite
Instance Attribute Summary
Attributes inherited from Driver
#db
Instance Method Summary
collapse
Methods inherited from Driver
#connect, #extract_conn_param, #extract_tbl_from_sql, #method_missing, #to_native_row
Constructor Details
#initialize(url = nil) ⇒ Sqlite
Returns a new instance of Sqlite.
7
8
9
10
11
12
13
14
|
# File 'lib/rigrate/interface/sqlite.rb', line 7
def initialize(url = nil)
url ||= "sqlite://memory"
opts = (URI.parse(url))
file = (url)
@db = ::SQLite3::Database.new(file, opts)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Rigrate::Driver
Instance Method Details
#commit ⇒ Object
102
103
104
|
# File 'lib/rigrate/interface/sqlite.rb', line 102
def commit
@db.commit
end
|
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/rigrate/interface/sqlite.rb', line 84
def (path)
result = ":memory:"
if path =~ /sqlite:\/\/(.*)/
if $1 == 'memory'
result = ":memory:"
else
result = $1
end
end
result
end
|
#primary_key(tbl_name) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/rigrate/interface/sqlite.rb', line 57
def primary_key(tbl_name)
(@db.table_info(tbl_name).select do |col_hash|
col_hash["pk"] == 1
end).map do |col_hash|
col_hash["name"]
end
end
|
#save(resultset) ⇒ Object
34
35
36
37
38
|
# File 'lib/rigrate/interface/sqlite.rb', line 34
def save(resultset)
resultset.db = self
resultset.save!
end
|
#select(sql, *args) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/rigrate/interface/sqlite.rb', line 16
def select(sql, *args)
target_tbl_name = (sql)
ResultSet.new.tap do |rs|
stm = @db.prepare sql, *args
rs.db = self
rs.target_tbl_name = target_tbl_name
rs.column_info = statement_fields(stm.columns, stm.types)
rs.rows = []
stm.execute.each do |row|
new_row = Row.new(to_rb_row(row.to_a))
yield new_row if block_given?
rs.rows << new_row
end
end
end
|
#statement_fields(names, types) ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/rigrate/interface/sqlite.rb', line 75
def statement_fields(names, types)
cols = []
names.each_with_index do |name, idx|
cols << Column.new(name, types[idx])
end
cols
end
|
#to_rb_row(row) ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/rigrate/interface/sqlite.rb', line 65
def to_rb_row(row)
row.map do |field|
if field.nil?
field.to_s
else
field
end
end
end
|
#transaction ⇒ Object
98
99
100
|
# File 'lib/rigrate/interface/sqlite.rb', line 98
def transaction
@db.transaction
end
|
#update(sql, *args) ⇒ Object
Also known as:
insert, delete
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/rigrate/interface/sqlite.rb', line 40
def update(sql, *args)
begin
stm = @db.prepare sql
args.each do |row|
if Rigrate::Row === row
row = row.data
end
stm.execute(*row)
end
rescue Exception => e
Rigrate.logger.error("execute SQL [#{sql}] ARGS [#{args.size}] -> #{e.backtrace.join('\n')}")
raise DriverError.new("execute error #{e.message}")
end
end
|