-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.xi
More file actions
99 lines (81 loc) · 3.55 KB
/
Copy pathsystem.xi
File metadata and controls
99 lines (81 loc) · 3.55 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
// xi-sqlite: the stock SQLite implementation over the system sqlite3 library.
// Resolved for the sqlite.SQLite interface automatically; bind an alternative
// in `module Test` to fake it. Column decoding is delegated to the injected
// sqlite.ColumnDecoder, so it can be rebound without touching this class.
import "std/ffi.xi"
import "std/json.xi"
class SystemSQLite implements sqlite.SQLite {
deps { decoder: sqlite.ColumnDecoder }
producer open(path: String) -> sqlite.Database! {
let handle = empty Ptr
let rc = sqlite3_open(toCString(path), &mut handle)
if rc != SQLITE_OK() {
let message = sqliteErrorOn(handle)
let ignored = sqlite3_close(handle)
return err(message)
}
return ok(sqlite.Database { handle: handle })
}
producer close(db: sqlite.Database) -> Bool! {
let rc = sqlite3_close(db.handle)
if rc != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
return ok(true)
}
producer exec(db: sqlite.Database, sql: String) -> Bool! {
let rc = sqlite3_exec(db.handle, toCString(sql), empty Ptr, empty Ptr, empty Ptr)
if rc != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
return ok(true)
}
producer query(db: sqlite.Database, sql: String) -> sqlite.Rows! {
let stmt = empty Ptr
let rc = sqlite3_prepare_v2(db.handle, toCString(sql), -1, &mut stmt, empty Ptr)
if rc != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
let rows = empty List<sqlite.Row>
let cols = sqlite3_column_count(stmt)
loop {
rc = sqlite3_step(stmt)
if rc != SQLITE_ROW() { break }
let columns = empty Map<String, sqlite.Value>
for col in 0 until cols {
let name = sqliteOwnText(sqlite3_column_name(stmt, col))
columns.put(name, decoder.decode(stmt, col))
}
rows.push(sqlite.Row { columns: columns })
}
if rc != SQLITE_DONE() {
let message = sqliteErrorOn(db.handle)
let ignored = sqlite3_finalize(stmt)
return err(message)
}
let finalized = sqlite3_finalize(stmt)
if finalized != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
return ok(sqlite.Rows { items: rows })
}
producer queryJson(db: sqlite.Database, sql: String) -> String! {
let stmt = empty Ptr
let rc = sqlite3_prepare_v2(db.handle, toCString(sql), -1, &mut stmt, empty Ptr)
if rc != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
let out = json.array()
let cols = sqlite3_column_count(stmt)
loop {
rc = sqlite3_step(stmt)
if rc != SQLITE_ROW() { break }
let obj = json.object()
for col in 0 until cols {
let name = sqliteOwnText(sqlite3_column_name(stmt, col))
obj = json.set(obj, name, decoder.toJson(decoder.decode(stmt, col)))
}
out = json.push(out, obj)
}
if rc != SQLITE_DONE() {
let message = sqliteErrorOn(db.handle)
let ignored = sqlite3_finalize(stmt)
return err(message)
}
let finalized = sqlite3_finalize(stmt)
if finalized != SQLITE_OK() { return err(sqliteErrorOn(db.handle)) }
return ok(json.stringify(out))
}
producer lastInsertId(db: sqlite.Database) -> Integer => sqlite3_last_insert_rowid(db.handle)
producer changes(db: sqlite.Database) -> Integer => sqlite3_changes(db.handle)
}