-
Notifications
You must be signed in to change notification settings - Fork 3
/
sql_helpers.odin
289 lines (239 loc) · 6.49 KB
/
sql_helpers.odin
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import "core:fmt"
import "core:strings"
import "base:runtime"
import "core:reflect"
import "core:mem"
import sql "odin-sqlite"
Result_Code :: sql.ResultCode
Stmt :: sql.Stmt
db: ^sql.sqlite3
db_cache: map[string]^Stmt
db_init :: proc(name: cstring) -> (err: Result_Code) {
sql.open(name, &db) or_return
return
}
db_destroy :: proc() -> (err: Result_Code) {
sql.close(db) or_return
return
}
db_check :: proc(err: Result_Code, loc := #caller_location) {
if err == .ERROR || err == .CONSTRAINT || err == .MISUSE {
text := fmt.tprintf("%s %v %s", err, sql.errmsg(db), loc)
panic(text)
}
}
// does not do caching
db_execute_simple :: proc(cmd: string) -> (err: Result_Code) {
data := transmute([^]u8)strings.unsafe_string_to_cstring(cmd)
stmt: ^Stmt
sql.prepare_v2(db, data, i32(len(cmd)), &stmt, nil) or_return
db_run(stmt) or_return
sql.finalize(stmt) or_return
return
}
// execute cached with args
db_execute :: proc(cmd: string, args: ..any) -> (err: Result_Code) {
stmt := db_cache_prepare(cmd) or_return
db_bind(stmt, ..args) or_return
db_bind_run(stmt) or_return
return
}
// simple run through statement
db_run :: proc(stmt: ^Stmt) -> (err: Result_Code) {
for {
result := sql.step(stmt)
if result == .DONE {
break
} else if result != .ROW {
return result
}
}
return
}
// set a cap to the cache
db_cache_cap :: proc(cap: int) {
db_cache = make(map[string]^Stmt, cap)
}
// return cached stmt or create one
db_cache_prepare :: proc(cmd: string) -> (stmt: ^Stmt, err: Result_Code) {
if existing_stmt := db_cache[cmd]; existing_stmt != nil {
stmt = existing_stmt
} else {
data := transmute([^]u8)strings.unsafe_string_to_cstring(cmd)
sql.prepare_v2(db, data, i32(len(cmd)), &stmt, nil);
db_cache[cmd] = stmt
}
return
}
// strings are not deleted
db_cache_destroy :: proc() {
for _, stmt in db_cache {
sql.finalize(stmt)
}
clear(&db_cache)
}
// simple execute -> no cache
// bindings -> maybe cache
// step once ->
// struct getters
db_bind_run :: proc(stmt: ^Stmt) -> (err: Result_Code) {
db_run(stmt) or_return
sql.reset(stmt) or_return
sql.clear_bindings(stmt) or_return
return
}
// bind primitive arguments to input statement
db_bind :: proc(stmt: ^Stmt, args: ..any) -> (err: Result_Code) {
for arg, index in args {
// index starts at 1 in binds
index := index + 1
ti := runtime.type_info_base(type_info_of(arg.id))
if arg == nil {
sql.bind_null(stmt, i32(index)) or_return
continue
}
// only allow slice of bytes
if arg.id == []byte {
slice := cast(^mem.Raw_Slice) arg.data
sql.bind_blob(
stmt,
i32(index),
cast(^u8) arg.data,
i32(slice.len),
sql.STATIC,
) or_return
continue
}
#partial switch info in ti.variant {
case runtime.Type_Info_Integer: {
// TODO actually use int64 for i64
value, valid := reflect.as_i64(arg)
if valid {
sql.bind_int(stmt, i32(index), i32(value)) or_return
} else {
return .ERROR
}
}
case runtime.Type_Info_Float: {
value, valid := reflect.as_f64(arg)
if valid {
sql.bind_double(stmt, i32(index), f64(value)) or_return
} else {
return .ERROR
}
}
case runtime.Type_Info_String: {
text, valid := reflect.as_string(arg)
if valid {
data := transmute([^]u8)strings.unsafe_string_to_cstring(text)
sql.bind_text(stmt, i32(index), data, i32(len(text)), sql.STATIC) or_return
} else {
return .ERROR
}
}
}
// fmt.println(stmt, arg, index)
}
return
}
// data from the struct has to match wanted column names
// changes the cmd string to the arg which should be a struct
db_select :: proc(cmd_end: string, struct_arg: any, args: ..any) -> (err: Result_Code) {
b := strings.builder_make_len_cap(0, 128)
defer strings.builder_destroy(&b)
strings.write_string(&b, "SELECT ")
ti := runtime.type_info_base(type_info_of(struct_arg.id))
struct_info := ti.variant.(runtime.Type_Info_Struct)
for name, i in struct_info.names[:struct_info.field_count] {
strings.write_string(&b, name)
if i != int(struct_info.field_count) - 1 {
strings.write_byte(&b, ',')
} else {
strings.write_byte(&b, ' ')
}
}
strings.write_string(&b, cmd_end)
full_cmd := strings.to_string(b)
// fmt.println(full_cmd)
stmt := db_cache_prepare(full_cmd) or_return
db_bind(stmt, ..args) or_return
for {
result := sql.step(stmt)
if result == .DONE {
break
} else if result != .ROW {
return result
}
// get column data per struct field
for i in 0..<int(struct_info.field_count) {
type := struct_info.types[i].id
offset := struct_info.offsets[i]
struct_value := any { rawptr(uintptr(struct_arg.data) + offset), type }
db_any_column(stmt, i32(i), struct_value) or_return
}
}
return
}
db_any_column :: proc(stmt: ^Stmt, column_index: i32, arg: any) -> (err: Result_Code) {
ti := runtime.type_info_base(type_info_of(arg.id))
#partial switch info in ti.variant {
case runtime.Type_Info_Integer: {
value := sql.column_int(stmt, column_index)
// TODO proper i64
switch arg.id {
case i8: (cast(^i8) arg.data)^ = i8(value)
case i16: (cast(^i16) arg.data)^ = i16(value)
case i32: (cast(^i32) arg.data)^ = value
case i64: (cast(^i64) arg.data)^ = i64(value)
}
}
case runtime.Type_Info_Float: {
value := sql.column_double(stmt, column_index)
switch arg.id {
case f32: (cast(^f32) arg.data)^ = f32(value)
case f64: (cast(^f64) arg.data)^ = value
}
}
case runtime.Type_Info_String: {
value := sql.column_text(stmt, column_index)
switch arg.id {
case string: {
(cast(^string) arg.data)^ = strings.clone(
string(value),
context.temp_allocator,
)
}
case cstring: {
(cast(^cstring) arg.data)^ = strings.clone_to_cstring(
string(value),
context.temp_allocator,
)
}
}
}
}
return
}
// auto insert INSERT INTO cmd_names VALUES (...)
db_insert :: proc(cmd_names: string, args: ..any) -> (err: Result_Code) {
b := strings.builder_make_len_cap(0, 128)
defer strings.builder_destroy(&b)
strings.write_string(&b, "INSERT INTO ")
strings.write_string(&b, cmd_names)
strings.write_string(&b, " VALUES ")
strings.write_byte(&b, '(')
for arg, i in args {
fmt.sbprintf(&b, "?%d", i + 1)
if i != len(args) - 1 {
strings.write_byte(&b, ',')
}
}
strings.write_byte(&b, ')')
full_cmd := strings.to_string(b)
// fmt.println(full_cmd)
stmt := db_cache_prepare(full_cmd) or_return
db_bind(stmt, ..args) or_return
db_bind_run(stmt) or_return
return
}