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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
module sqlite
$if freebsd || openbsd {
#flag -I/usr/local/include
#flag -L/usr/local/lib
}
$if windows {
#flag windows -I@VEXEROOT/thirdparty/sqlite
#flag windows -L@VEXEROOT/thirdparty/sqlite
#flag windows @VEXEROOT/thirdparty/sqlite/sqlite3.o
} $else {
#flag -lsqlite3
}
#include "sqlite3.h"
const (
sqlite_ok = 0
sqlite_error = 1
sqlite_row = 100
sqlite_done = 101
)
struct C.sqlite3 {
}
struct C.sqlite3_stmt {
}
struct Stmt {
stmt &C.sqlite3_stmt
db &DB
}
struct SQLError {
msg string
code int
}
//
pub struct DB {
pub mut:
is_open bool
mut:
conn &C.sqlite3
}
pub fn (db DB) str() string {
return 'sqlite.DB{ conn: ' + ptr_str(db.conn) + ' }'
}
pub struct Row {
pub mut:
vals []string
}
//
fn C.sqlite3_open(&char, &&C.sqlite3) int
fn C.sqlite3_close(&C.sqlite3) int
fn C.sqlite3_last_insert_rowid(&C.sqlite3) i64
//
fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int
fn C.sqlite3_step(&C.sqlite3_stmt) int
fn C.sqlite3_finalize(&C.sqlite3_stmt) int
//
fn C.sqlite3_column_name(&C.sqlite3_stmt, int) &char
fn C.sqlite3_column_text(&C.sqlite3_stmt, int) &byte
fn C.sqlite3_column_int(&C.sqlite3_stmt, int) int
fn C.sqlite3_column_int64(&C.sqlite3_stmt, int) i64
fn C.sqlite3_column_double(&C.sqlite3_stmt, int) f64
fn C.sqlite3_column_count(&C.sqlite3_stmt) int
//
fn C.sqlite3_errstr(int) &char
fn C.sqlite3_errmsg(&C.sqlite3) &char
fn C.sqlite3_free(voidptr)
// connect Opens the connection with a database.
pub fn connect(path string) ?DB {
db := &C.sqlite3(0)
code := C.sqlite3_open(&char(path.str), &db)
if code != 0 {
return IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
}
return DB{
conn: db
is_open: true
}
}
// close Closes the DB.
// TODO: For all functions, determine whether the connection is
// closed first, and determine what to do if it is
pub fn (mut db DB) close() ?bool {
code := C.sqlite3_close(db.conn)
if code == 0 {
db.is_open = false
} else {
return IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
}
return true // successfully closed
}
// Only for V ORM
fn get_int_from_stmt(stmt &C.sqlite3_stmt) int {
x := C.sqlite3_step(stmt)
if x != C.SQLITE_OK && x != C.SQLITE_DONE {
C.puts(C.sqlite3_errstr(x))
}
res := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
return res
}
// Returns last insert rowid
// https://www.sqlite.org/c3ref/last_insert_rowid.html
pub fn (db DB) last_insert_rowid() i64 {
return C.sqlite3_last_insert_rowid(db.conn)
}
// Returns a single cell with value int.
pub fn (db DB) q_int(query string) int {
stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
C.sqlite3_step(stmt)
res := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
return res
}
// Returns a single cell with value string.
pub fn (db DB) q_string(query string) string {
stmt := &C.sqlite3_stmt(0)
defer {
C.sqlite3_finalize(stmt)
}
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
C.sqlite3_step(stmt)
val := unsafe { &byte(C.sqlite3_column_text(stmt, 0)) }
return if val != &byte(0) { unsafe { tos_clone(val) } } else { '' }
}
// Execute the query on db, return an array of all the results, alongside any result code.
// Result codes: https://www.sqlite.org/rescode.html
pub fn (db DB) exec(query string) ([]Row, int) {
stmt := &C.sqlite3_stmt(0)
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
nr_cols := C.sqlite3_column_count(stmt)
mut res := 0
mut rows := []Row{}
for {
res = C.sqlite3_step(stmt)
// Result Code SQLITE_ROW; Another row is available
if res != 100 {
// C.puts(C.sqlite3_errstr(res))
break
}
mut row := Row{}
for i in 0 .. nr_cols {
val := unsafe { &byte(C.sqlite3_column_text(stmt, i)) }
if val == &byte(0) {
row.vals << ''
} else {
row.vals << unsafe { tos_clone(val) }
}
}
rows << row
}
C.sqlite3_finalize(stmt)
return rows, res
}
// Execute a query, handle error code
// Return the first row from the resulting table
pub fn (db DB) exec_one(query string) ?Row {
rows, code := db.exec(query)
if rows.len == 0 {
return IError(&SQLError{
msg: 'No rows'
code: code
})
} else if code != 101 {
return IError(&SQLError{
msg: unsafe { cstring_to_vstring(&char(C.sqlite3_errstr(code))) }
code: code
})
}
return rows[0]
}
pub fn (db DB) error_message(code int, query string) IError {
msg := unsafe { cstring_to_vstring(&char(C.sqlite3_errmsg(db.conn))) }
return IError(&SQLError{
msg: '$msg ($code) ($query)'
code: code
})
}
// In case you don't expect any result, but still want an error code
// e.g. INSERT INTO ... VALUES (...)
pub fn (db DB) exec_none(query string) int {
_, code := db.exec(query)
return code
}
/*
TODO
pub fn (db DB) exec_param(query string, param string) []Row {
}
*/
pub fn (db DB) create_table(table_name string, columns []string) {
db.exec('create table if not exists $table_name (' + columns.join(',\n') + ')')
}
|