-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.odin
272 lines (244 loc) · 7.3 KB
/
result.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
package main
import "base:builtin"
import "base:intrinsics"
import "base:runtime"
import "core:log"
import "core:slice"
import pq "./shared/odin-postgresql"
exec_bin :: proc(conn: pq.Conn, cmd: cstring) -> pq.Result {
return pq.exec_params(conn, cmd, 0, nil, nil, nil, nil, .Binary)
}
results :: proc(
$T: typeid,
res: pq.Result,
allocator := context.allocator,
loc := #caller_location,
) -> []T {
context.allocator = allocator
row_info := type_info_of(builtin.typeid_of(T))
#partial switch row_variant in row_info.variant {
case runtime.Type_Info_Named:
row_info = row_variant.base
}
rows := pq.n_tuples(res)
cols, ok_cols := row_info.variant.(runtime.Type_Info_Struct)
if !ok_cols do unimplemented("only structs allowed")
if cast(int)pq.n_fields(res) != len(cols.names) {
log.panicf(
"query contained %d cols, expected %d (%v)",
cast(int)pq.n_fields(res),
len(cols.names),
cols.names,
)
}
items := make([]T, rows, loc=loc)
for &item, row in items {
item_bytes := cast([^]byte)&item
for col in 0 ..< len(cols.names) {
col_info := cols.types[col]
col_size := col_info.size
col_offset := cast(int)cols.offsets[col]
col_bytes_len := pq.get_length(res, cast(i32)row, cast(i32)col)
col_src := pq.get_value(
res,
cast(i32)row,
cast(i32)col,
)[:col_bytes_len]
col_dst := item_bytes[col_offset:col_offset + col_size]
#partial switch col_variant in col_info.variant {
case runtime.Type_Info_Integer,
runtime.Type_Info_Float,
runtime.Type_Info_Bit_Set:
if cast(int)col_bytes_len == 0 {
log.warnf("col %d was skipped because it size was 0 bytes", col)
continue
}
if cast(int)col_bytes_len != col_size {
log.panicf(
"col %d was %d bytes, expected col of type %v to be %d bytes",
col,
col_bytes_len,
col_info.id,
col_size,
)
}
copy(col_dst, col_src)
slice.reverse(col_dst) // flip endian
case runtime.Type_Info_String:
str_dst := make([]byte, col_bytes_len, loc=loc)
copy(str_dst, col_src) // buffer
raw := transmute(runtime.Raw_Slice)str_dst
src := (cast([^]byte)&raw)[:size_of(raw)]
copy(col_dst, src) // ptr and size
case runtime.Type_Info_Slice:
elem_info := col_variant.elem
#partial switch elem_variant in col_variant.elem.variant {
case runtime.Type_Info_Named:
elem_info = elem_variant.base
}
log.info(elem_info.variant)
// maybe should col_src
src := pq.get_value(res, cast(i32)row, cast(i32)col)
Array_Header :: struct #packed {
num_dims, flags, elem_oid, dim_size, lower_bound: i32be,
}
array_header := (cast(^Array_Header)src[0:])^
log.info(array_header)
fields, ok_fields := elem_info.variant.(runtime.Type_Info_Struct)
if !ok_fields do unimplemented("only structs allowed")
log.info("fields", fields)
Field_Header :: struct #packed {
oid, len: i32be,
}
first_elem_len := (cast(^i32be)src[size_of(Array_Header):])^
min_elem_len := size_of(i32be) * 3 + size_of(Field_Header)
if cast(int)first_elem_len == min_elem_len {
log.warn("first was skipped by odd calculation")
continue
}
// this is were id and name go
fields_dst := make(
[]byte,
elem_info.size * cast(int)array_header.dim_size,
loc=loc,
)
n_dst := 0
log.infof(
"bytes: %d, e_size: %d",
len(fields_dst),
elem_info.size,
)
for elem in 0 ..< array_header.dim_size {
log.info(elem)
log.info(n_dst)
elem_start := size_of(Array_Header) + n_dst
log.info(elem_start)
elem_len := (cast(^i32be)src[elem_start:])^
log.warn(elem_len)
n_dst += size_of(i32be)
if elem_len == -1 {
log.panicf(
"row %d col %d expected no null elements in",
row,
col,
)
}
num_fields := (cast(^i32be)src[elem_start +
size_of(i32be):])^
n_dst += size_of(i32be)
log.info(cast(int)num_fields, len(fields.names))
assert(cast(int)num_fields == len(fields.names))
n_field := 0
for field in 0 ..< len(fields.names) {
field_start :=
elem_start +
size_of(i32be) +
size_of(i32be) +
n_field
field_info := fields.types[field]
field_size := field_info.size
field_offset := cast(int)fields.offsets[field]
field_header := (cast(^Field_Header)src[field_start:])^
if cast(int)field_header.len == -1 {
continue // change this its creating 1 empty
}
n_field += size_of(Field_Header)
// n_dst += size_of(Field_Header)
log.info(field_header)
field_dst_start :=
elem_info.size * cast(int)elem + field_offset
field_dst := fields_dst[field_dst_start:field_dst_start +
field_size]
field_src_start := field_start + size_of(Field_Header)
#partial switch field_variant in field_info.variant {
case runtime.Type_Info_Integer,
runtime.Type_Info_Float,
runtime.Type_Info_Bit_Set:
if cast(int)field_header.len != field_size {
log.panicf(
"row %d col %d array contained %d fields, expected %d (%v)",
row,
col,
field_header.len,
len(fields.names),
fields.names,
)
}
field_src := src[field_src_start:field_src_start +
field_size]
copy(field_dst, field_src)
slice.reverse(field_dst) // flip endian
case runtime.Type_Info_String:
str_dst := make([]byte, field_header.len)
field_src := src[field_src_start:field_src_start +
cast(int)field_header.len]
copy(str_dst, field_src)
raw := transmute(runtime.Raw_Slice)str_dst
src := (cast([^]byte)&raw)[:size_of(raw)]
copy(field_dst, src) // ptr and size
case:
unimplemented()
}
n_field += cast(int)field_header.len
}
n_dst += n_field
}
// TODO: the fields_dst should aready be short enough
raw := transmute(runtime.Raw_Slice)fields_dst[:array_header.dim_size]
src_raw := (cast([^]byte)&raw)[:size_of(raw)]
copy(col_dst, src_raw)
case:
unimplemented()
}
}
}
return items
}
result_number :: proc(
$T: typeid,
res: pq.Result,
row_number, column_number: i32,
) -> T where intrinsics.type_is_integer(T) ||
intrinsics.type_is_float(T) ||
intrinsics.type_is_bit_set(T) {
assert(pq.get_length(res, row_number, column_number) == size_of(T))
ptr := pq.get_value(res, row_number, column_number)
bytes := ptr[:size_of(T)]
// flip endian
slice.reverse(bytes)
return (cast(^T)(ptr))^
}
result_slice :: proc(
$T: typeid,
res: pq.Result,
row_number, column_number: i32,
allocator := context.allocator,
loc := #caller_location,
) -> T where intrinsics.type_is_slice(T) {
context.allocator = allocator
len := pq.get_length(res, row_number, column_number)
old_slice := pq.get_value(res, row_number, column_number)[:len]
return slice.clone(old_slice, loc = loc)
}
result_string :: proc(
$T: typeid/string,
res: pq.Result,
row_number, column_number: i32,
allocator := context.allocator,
loc := #caller_location,
) -> string {
bytes := result_slice(
[]byte,
res,
row_number,
column_number,
allocator,
loc,
)
return transmute(string)bytes
}
result :: proc {
result_number,
result_slice,
result_string,
}