-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmt.c.v
98 lines (82 loc) · 2.35 KB
/
stmt.c.v
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
module sqlite
fn C.sqlite3_bind_null(&C.sqlite3_stmt, int) int
fn C.sqlite3_bind_double(&C.sqlite3_stmt, int, f64) int
fn C.sqlite3_bind_int(&C.sqlite3_stmt, int, int) int
fn C.sqlite3_bind_int64(&C.sqlite3_stmt, int, i64) int
fn C.sqlite3_bind_text(&C.sqlite3_stmt, int, &char, int, voidptr) int
// Only for V ORM
fn (db &DB) init_stmt(query string) (&C.sqlite3_stmt, int) {
// println('init_stmt("$query")')
stmt := &C.sqlite3_stmt(unsafe { nil })
err := C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
return stmt, err
}
fn (db &DB) new_init_stmt(query string) !Stmt {
stmt, err := db.init_stmt(query)
if err != sqlite_ok {
return db.error_message(err, query)
}
return Stmt{stmt, db}
}
fn (stmt &Stmt) bind_null(idx int) int {
return C.sqlite3_bind_null(stmt.stmt, idx)
}
fn (stmt &Stmt) bind_int(idx int, v int) int {
return C.sqlite3_bind_int(stmt.stmt, idx, v)
}
fn (stmt &Stmt) bind_i64(idx int, v i64) int {
return C.sqlite3_bind_int64(stmt.stmt, idx, v)
}
fn (stmt &Stmt) bind_f64(idx int, v f64) int {
return C.sqlite3_bind_double(stmt.stmt, idx, v)
}
fn (stmt &Stmt) bind_text(idx int, s string) int {
return C.sqlite3_bind_text(stmt.stmt, idx, voidptr(s.str), s.len, 0)
}
fn (stmt &Stmt) get_int(idx int) ?int {
if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL {
return none
} else {
return C.sqlite3_column_int(stmt.stmt, idx)
}
}
fn (stmt &Stmt) get_i64(idx int) ?i64 {
if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL {
return none
} else {
return C.sqlite3_column_int64(stmt.stmt, idx)
}
}
fn (stmt &Stmt) get_f64(idx int) ?f64 {
if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL {
return none
} else {
return C.sqlite3_column_double(stmt.stmt, idx)
}
}
fn (stmt &Stmt) get_text(idx int) ?string {
if C.sqlite3_column_type(stmt.stmt, idx) == C.SQLITE_NULL {
return none
} else {
b := &char(C.sqlite3_column_text(stmt.stmt, idx))
if b == &char(0) {
return ''
}
return unsafe { b.vstring() }
}
}
fn (stmt &Stmt) get_count() int {
return C.sqlite3_column_count(stmt.stmt)
}
fn (stmt &Stmt) step() int {
return C.sqlite3_step(stmt.stmt)
}
fn (stmt &Stmt) orm_step(query string) ! {
res := stmt.step()
if res != sqlite_ok && res != sqlite_done && res != sqlite_row {
return stmt.db.error_message(res, query)
}
}
fn (stmt &Stmt) finalize() {
C.sqlite3_finalize(stmt.stmt)
}