-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDatabase.v
More file actions
103 lines (83 loc) · 2.32 KB
/
Database.v
File metadata and controls
103 lines (83 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module database
import db.sqlite
import console
pub struct Database {
pub:
connection DatabaseConnection
connections map[string]DatabaseConnection
mut:
handle sqlite.DB
handles map[string]sqlite.DB
}
pub fn (mut db Database) get_connection() !sqlite.DB {
if !db.handle.is_open {
db.handle = sqlite.connect(db.connection.database)!
}
return db.handle
}
pub fn (mut db Database) close() ! {
// Closing default connection
if db.handle.is_open {
db.handle.close()!
}
// Closing named connections
for _, mut handle in db.handles {
if handle.is_open {
handle.close()!
}
}
db.handles.clear()
}
pub fn (mut db Database) run(query string) ! {
conn := db.get_connection()!
conn.exec(query)!
}
pub fn (mut db Database) all[T](query string) ![]T {
conn := db.get_connection()!
// Execute query and map results
rows := conn.exec(query)!
mut results := []T{}
for row in rows {
mut instance := T{}
mut index := 0
// Iterate through struct fields, assuming same order as query
$for field in T.fields {
value := row.vals[index]
$if field.typ is string {
instance.$(field.name) = value
} $else $if field.typ is int {
instance.$(field.name) = value.int()
} $else $if field.typ is u64 {
instance.$(field.name) = value.u64()
}
index = index + 1
}
results << instance
}
return results
}
pub fn (db Database) exec(query string) ! {
mut connection_name := ''
for name, conn in db.connections {
if conn.by_default {
connection_name = name
break
}
}
if connection_name == '' {
return error('No default database connection configured')
}
connection := db.connections[connection_name] or {
return error('Database connection "${connection_name}" not found')
}
mut sqlite_conn := sqlite.connect(connection.database)!
defer {
sqlite_conn.close() or {
/**
* @todo Use app.report(err) instead.
*/
console.error("Could not close Sqlite connection (${err.msg()}).")
}
}
sqlite_conn.exec(query)!
}