-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
280 lines (197 loc) · 7.13 KB
/
util.c
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
#include <stdlib.h>
#include <assert.h>
#include "internal.h"
#include "lib/log.h"
#include "lib/misc.h"
#define _PARAM_TYPE_CASE(typenam) case EVSQL_TYPE_ ## typenam: return #typenam
#define _PARAM_VAL_BUF_MAX 120
#define _PARAM_VAL_CASE(typenam, ...) case EVSQL_TYPE_ ## typenam: if (item->bytes) ret = snprintf(buf, _PARAM_VAL_BUF_MAX, __VA_ARGS__); else return "(null)"; break
const char *evsql_item_type (const struct evsql_item_info *item_info) {
switch (item_info->type) {
_PARAM_TYPE_CASE (INVALID );
_PARAM_TYPE_CASE (NULL_ );
_PARAM_TYPE_CASE (BINARY );
_PARAM_TYPE_CASE (STRING );
_PARAM_TYPE_CASE (UINT16 );
_PARAM_TYPE_CASE (UINT32 );
_PARAM_TYPE_CASE (UINT64 );
default: return "???";
}
}
static const char *evsql_item_val (const struct evsql_item *item) {
static char buf[_PARAM_VAL_BUF_MAX];
int ret;
switch (item->info.type) {
_PARAM_VAL_CASE (INVALID, "???" );
_PARAM_VAL_CASE (NULL_, "(null)" );
_PARAM_VAL_CASE (BINARY, "%zu:%s", item->length, "... " );
_PARAM_VAL_CASE (STRING, "%s", item->bytes );
_PARAM_VAL_CASE (UINT16, "%hu", (unsigned short int) ntohs(item->value.uint16) );
_PARAM_VAL_CASE (UINT32, "%lu", (unsigned long int) ntohl(item->value.uint32) );
_PARAM_VAL_CASE (UINT64, "%llu", (unsigned long long int) ntohq(item->value.uint64) );
default: return "???";
}
return buf;
}
int evsql_params_clear (struct evsql_query_params *params) {
struct evsql_item *param;
for (param = params->list; param->info.type; param++) {
param->bytes = NULL;
param->flags.has_value = 0;
}
return 0;
}
int evsql_param_null (struct evsql_query_params *params, size_t param) {
struct evsql_item *p = ¶ms->list[param];
p->bytes = NULL;
p->flags.has_value = 0;
return 0;
}
int evsql_param_binary (struct evsql_query_params *params, size_t param, const char *ptr, size_t len) {
struct evsql_item *p = ¶ms->list[param];
assert(p->info.type == EVSQL_TYPE_BINARY);
p->bytes = ptr;
p->length = len;
return 0;
}
int evsql_param_string (struct evsql_query_params *params, size_t param, const char *ptr) {
struct evsql_item *p = ¶ms->list[param];
assert(p->info.type == EVSQL_TYPE_STRING);
// XXX: hmm...
p->info.format = EVSQL_FMT_TEXT;
p->bytes = ptr;
p->length = 0;
return 0;
}
int evsql_param_uint16 (struct evsql_query_params *params, size_t param, uint16_t uval) {
struct evsql_item *p = ¶ms->list[param];
assert(p->info.type == EVSQL_TYPE_UINT16);
p->value.uint16 = htons(uval);
p->length = sizeof(uval);
p->flags.has_value = 1;
return 0;
}
int evsql_param_uint32 (struct evsql_query_params *params, size_t param, uint32_t uval) {
struct evsql_item *p = ¶ms->list[param];
assert(p->info.type == EVSQL_TYPE_UINT32);
p->value.uint32 = htonl(uval);
p->length = sizeof(uval);
p->flags.has_value = 1;
return 0;
}
void evsql_query_debug (const char *sql, const struct evsql_query_params *params) {
const struct evsql_item *param;
size_t param_count = 0, idx = 0;
// count the params
for (param = params->list; param->info.type; param++)
param_count++;
DEBUG("sql: %s", sql);
DEBUG("params: %zu", param_count);
for (param = params->list; param->info.type; param++) {
DEBUG("\t%2zu : %8s = %s", ++idx, evsql_item_type(¶m->info), evsql_item_val(param));
}
}
int evsql_result_binary (const struct evsql_result *res, size_t row, size_t col, const char **ptr, size_t *size, bool nullok) {
*ptr = NULL;
switch (res->evsql->type) {
case EVSQL_EVPQ:
if (PQgetisnull(res->result.pq, row, col)) {
if (nullok)
return 0;
else
ERROR("[%zu:%zu] field is null", row, col);
}
if (PQfformat(res->result.pq, col) != 1)
ERROR("[%zu:%zu] PQfformat is not binary: %d", row, col, PQfformat(res->result.pq, col));
*size = PQgetlength(res->result.pq, row, col);
*ptr = PQgetvalue(res->result.pq, row, col);
return 0;
default:
FATAL("res->evsql->type");
}
error:
return -1;
}
int evsql_result_binlen (const struct evsql_result *res, size_t row, size_t col, const char **ptr, size_t size, int nullok) {
size_t real_size = 0;
if (evsql_result_binary(res, row, col, ptr, &real_size, nullok))
goto error;
if (*ptr == NULL) {
assert(nullok);
return 0;
}
if (size && real_size != size)
ERROR("[%zu:%zu] field size mismatch: %zu -> %zu", row, col, size, real_size);
return 0;
error:
return -1;
}
int evsql_result_string (const struct evsql_result *res, size_t row, size_t col, const char **ptr, int nullok) {
size_t real_size;
if (evsql_result_binary(res, row, col, ptr, &real_size, nullok))
goto error;
assert(real_size == strlen(*ptr));
return 0;
error:
return -1;
}
int evsql_result_uint16 (const struct evsql_result *res, size_t row, size_t col, uint16_t *uval, int nullok) {
const char *data;
int16_t sval;
if (evsql_result_binlen(res, row, col, &data, sizeof(*uval), nullok))
goto error;
if (!data)
return 0;
sval = ntohs(*((int16_t *) data));
if (sval < 0)
ERROR("negative value for unsigned: %d", sval);
*uval = sval;
return 0;
error:
return nullok ? 0 : -1;
}
int evsql_result_uint32 (const struct evsql_result *res, size_t row, size_t col, uint32_t *uval, int nullok) {
const char *data;
int32_t sval;
if (evsql_result_binlen(res, row, col, &data, sizeof(*uval), nullok))
goto error;
if (!data)
return 0;
sval = ntohl(*(int32_t *) data);
if (sval < 0)
ERROR("negative value for unsigned: %d", sval);
*uval = sval;
return 0;
error:
return nullok ? 0 : -1;
}
int evsql_result_uint64 (const struct evsql_result *res, size_t row, size_t col, uint64_t *uval, int nullok) {
const char *data;
int64_t sval;
if (evsql_result_binlen(res, row, col, &data, sizeof(*uval), nullok))
goto error;
if (!data)
return 0;
sval = ntohq(*(int64_t *) data);
if (sval < 0)
ERROR("negative value for unsigned: %ld", sval);
*uval = sval;
return 0;
error:
return nullok ? 0 : -1;
}
const char *evsql_conn_error (struct evsql_conn *conn) {
switch (conn->evsql->type) {
case EVSQL_EVPQ:
if (!conn->engine.evpq)
return "unknown error (no conn)";
return evpq_error_message(conn->engine.evpq);
default:
FATAL("res->evsql->type");
}
}
const char *evsql_trans_error (struct evsql_trans *trans) {
if (trans->conn == NULL)
return "unknown error (no trans conn)";
return evsql_conn_error(trans->conn);
}