-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsqlite3.odin
More file actions
210 lines (201 loc) · 8.81 KB
/
sqlite3.odin
File metadata and controls
210 lines (201 loc) · 8.81 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
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
package sqlite3_wrapper
import sqlite "bindings"
import "base:intrinsics"
import "base:runtime"
import "core:reflect"
import "core:fmt"
import "core:mem"
DB :: sqlite.Sqlite3
Query :: sqlite.Stmt
Status :: sqlite.Status
status_explain :: proc(status: Status) -> cstring {
return sqlite.errstr(status)
}
open :: proc(filename: cstring) -> (^DB, Status) {
db: ^DB
status := sqlite.open_v2(filename, &db, {.Read_Write, .Create}, nil)
if status != nil {
return nil, status
}
return db, nil
}
close :: proc(db: ^DB) -> (status: Status) {
return sqlite.close_v2(db)
}
sql_exec :: proc(db: ^DB, sql: string, args: ..any) -> (Status) {
query, status := sql_bind(db, sql, ..args)
if status != nil {
return status
}
for _ in sql_row(db, query, struct {}) {
}
return .Ok
}
sql_bind :: proc(db: ^DB, sql: string, args: ..any) -> (^Query, Status) {
query: ^Query
unused: [^]u8
status := sqlite.prepare_v2(db, raw_data(sql), cast(i32) len(sql), &query, &unused)
if status != nil {
return nil, status
}
for arg, arg_idx in args {
arg_idx := cast(i32) arg_idx + 1
arg_info := runtime.type_info_base(type_info_of(arg.id))
if arg == nil {
status = sqlite.bind_null(query, arg_idx)
if status == nil {
fmt.panicf("Unable to bind argument %v: %s", arg, status_explain(status))
}
}
status = Status.Ok
#partial switch arg_variant in arg_info.variant {
case runtime.Type_Info_Integer:
value, ok := reflect.as_i64(arg)
assert(ok)
status = sqlite.bind_int64(query, arg_idx, value)
case runtime.Type_Info_Float:
value, ok := reflect.as_f64(arg)
assert(ok)
status = sqlite.bind_double(query, arg_idx, value)
case runtime.Type_Info_String:
value, ok := reflect.as_string(arg)
assert(ok)
status = sqlite.bind_text(query, arg_idx, raw_data(value), cast(i32) len(value), nil)
case runtime.Type_Info_Boolean:
value, ok := reflect.as_bool(arg)
assert(ok)
status = sqlite.bind_int(query, arg_idx, cast(i32) value)
case runtime.Type_Info_Array:
if arg_variant.elem.id != u8 { fmt.panicf("Unsupported bind type", arg_variant) }
value := reflect.as_bytes(arg)
status = sqlite.bind_blob(query, arg_idx, raw_data(value), cast(i32) len(value), nil)
}
}
return query, nil
}
sql_row :: proc(db: ^DB, query: ^Query, $T: typeid) -> (T, bool)
where
intrinsics.type_is_struct(T)
{
struct_info := runtime.type_info_base(type_info_of(T)).variant.(runtime.Type_Info_Struct)
if struct_info.soa_kind != .None {
fmt.panicf("#soa structs not accepted.")
}
if .raw_union in struct_info.flags {
fmt.panicf("Can not select into raw union: %v", typeid_of(T))
}
status := sqlite.step(query)
if status != .Row {
sqlite.finalize(query)
return {}, false
}
t := T {}
t_bytes := transmute([^]u8) &t
for field, field_idx in struct_info.types[:struct_info.field_count] {
col_idx := cast(i32) field_idx
col_type := sqlite.column_type(query, col_idx)
field_base := runtime.type_info_base(field)
field_offs := struct_info.offsets[field_idx]
if un, ok := field_base.variant.(runtime.Type_Info_Union); ok {
if !un.no_nil || len(un.variants) != 1 {
fmt.panicf("Only Maybe(T) is supported as union argument, %v not accepted", typeid_of(type_of(un)))
}
field_base = un.variants[0]
}
#partial switch field_variant in field_base.variant {
case runtime.Type_Info_Any:
case runtime.Type_Info_Boolean:
if col_type != .Integer {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
value := sqlite.column_int64(query, col_idx)
switch field.size {
case 1: (transmute(^b8) &t_bytes[field_offs])^ = value != 0
case 2: (transmute(^b16) &t_bytes[field_offs])^ = value != 0
case 4: (transmute(^b32) &t_bytes[field_offs])^ = value != 0
case 8: (transmute(^b64) &t_bytes[field_offs])^ = value != 0
case:
panic("Only bool sizes of 1, 2, 4 and 8 bytes are supported")
}
case runtime.Type_Info_Enum:
if col_type == .Integer {
value := sqlite.column_int64(query, col_idx)
switch field.size {
case 1: (transmute(^i8) &t_bytes[field_offs])^ = cast(i8) value
case 2: (transmute(^i16) &t_bytes[field_offs])^ = cast(i16) value
case 4: (transmute(^i32) &t_bytes[field_offs])^ = cast(i32) value
case 8: (transmute(^i64) &t_bytes[field_offs])^ = value
case:
panic("Only bool sizes of 1, 2, 4 and 8 bytes are supported")
}
} else if col_type == .Text {
name := sqlite.column_text(query, col_idx)
value_idx := -1
for enum_name, idx in field_variant.names {
if enum_name == cast(string) name {
value_idx = idx
}
}
if value_idx == -1 {
panic("Enum value extracted from SQL query is not part of enum")
}
value := field_variant.values[value_idx]
switch field.size {
case 1: (transmute(^i8) &t_bytes[field_offs])^ = cast(i8) value
case 2: (transmute(^i16) &t_bytes[field_offs])^ = cast(i16) value
case 4: (transmute(^i32) &t_bytes[field_offs])^ = cast(i32) value
case 8: (transmute(^i64) &t_bytes[field_offs])^ = cast(i64) value
case:
panic("Only enum integer sizes of 1, 2, 4 and 8 bytes are supported")
}
} else {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
case runtime.Type_Info_Float:
if col_type != .Float {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
value := sqlite.column_double(query, col_idx)
switch field.size {
case 2: (transmute(^f16) &t_bytes[field_offs])^ = cast(f16) value
case 4: (transmute(^f32) &t_bytes[field_offs])^ = cast(f32) value
case 8: (transmute(^f64) &t_bytes[field_offs])^ = value
case:
panic("Only float sizes of 2, 4 and 8 bytes are supported")
}
case runtime.Type_Info_Integer:
if col_type != .Integer {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
value := sqlite.column_int64(query, col_idx)
switch field.size {
case 1: (transmute(^i8) &t_bytes[field_offs])^ = cast(i8) value
case 2: (transmute(^i16) &t_bytes[field_offs])^ = cast(i16) value
case 4: (transmute(^i32) &t_bytes[field_offs])^ = cast(i32) value
case 8: (transmute(^i64) &t_bytes[field_offs])^ = value
case:
panic("Only enum integer sizes of 1, 2, 4 and 8 bytes are supported")
}
case runtime.Type_Info_String:
if col_type != .Text {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
value := sqlite.column_text(query, col_idx)
if field_variant.is_cstring {
(transmute(^cstring) &t_bytes[field_offs])^ = value
} else {
(transmute(^string) &t_bytes[field_offs])^ = cast(string) value
}
case runtime.Type_Info_Array:
if col_type != .Blob {
fmt.panicf("Type mismatch: %v <- %v", typeid_of(type_of(field_variant)), col_type)
}
len := int(sqlite.column_bytes(query, col_idx))
value := sqlite.column_blob(query, col_idx)
mem.copy((transmute(^rawptr) &t_bytes[field_offs]), value, len)
case:
panic("Unsupported type for accepting SQL values in the given struct")
}
}
return t, true
}