-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_api.c
365 lines (285 loc) · 11.1 KB
/
json_api.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "json_api.h"
#include <string.h>
#include <errno.h>
enum json_api_action json_api_get_action(struct json_object * object) {
json_object_object_foreach(object, key, val) {
if (strcmp("action", key) == 0) {
return (enum json_api_action) json_object_get_int(val);
}
}
return -1;
}
struct json_api_create_table_request json_api_to_create_table_request(struct json_object * object) {
struct json_api_create_table_request request;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
continue;
}
if (strcmp("columns", key) == 0) {
request.columns.amount = json_object_array_length(val);
request.columns.columns = malloc(sizeof(*request.columns.columns) * request.columns.amount);
for (int i = 0; i < request.columns.amount; ++i) {
struct json_object * elem = json_object_array_get_idx(val, i);
json_object_object_foreach(elem, elem_key, elem_val) {
if (strcmp("name", elem_key) == 0) {
request.columns.columns[i].name = strdup(json_object_get_string(elem_val));
continue;
}
if (strcmp("type", elem_key) == 0) {
request.columns.columns[i].type = (enum storage_column_type) json_object_get_int(elem_val);
continue;
}
}
}
continue;
}
}
return request;
}
struct json_api_drop_table_request json_api_to_drop_table_request(struct json_object * object) {
struct json_api_drop_table_request request;
request.table_name = NULL;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
break;
}
}
return request;
}
static struct storage_value * json_to_storage_value(struct json_object * object) {
struct storage_value * value;
switch (json_object_get_type(object)) {
case json_type_boolean:
case json_type_object:
case json_type_array:
errno = EINVAL;
case json_type_null:
return NULL;
case json_type_double:
value = malloc(sizeof(*value));
value->type = STORAGE_COLUMN_TYPE_NUM;
value->value.num = json_object_get_double(object);
break;
case json_type_int:
value = malloc(sizeof(*value));
value->value._int = json_object_get_int64(object);
if (value->value._int < 0) {
value->type = STORAGE_COLUMN_TYPE_INT;
break;
}
value->type = STORAGE_COLUMN_TYPE_UINT;
value->value.uint = json_object_get_uint64(object);
break;
case json_type_string:
value = malloc(sizeof(*value));
value->type = STORAGE_COLUMN_TYPE_STR;
value->value.str = strdup(json_object_get_string(object));
break;
}
return value;
}
struct json_api_insert_request json_api_to_insert_request(struct json_object * object) {
struct json_api_insert_request request;
request.columns.amount = 0;
request.columns.columns = NULL;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
continue;
}
if (strcmp("columns", key) == 0) {
request.columns.amount = json_object_array_length(val);
request.columns.columns = malloc(sizeof(*request.columns.columns) * request.columns.amount);
for (int i = 0; i < request.columns.amount; ++i) {
request.columns.columns[i] = strdup(json_object_get_string(json_object_array_get_idx(val, i)));
}
continue;
}
if (strcmp("values", key) == 0) {
request.values.amount = json_object_array_length(val);
request.values.values = malloc(sizeof(struct storage_value *) * request.values.amount);
for (int i = 0; i < request.values.amount; ++i) {
request.values.values[i] = json_to_storage_value(json_object_array_get_idx(val, i));
}
continue;
}
}
return request;
}
static struct json_api_where * json_api_to_where(struct json_object * object) {
struct json_api_where * where = malloc(sizeof(*where));
{
json_object_object_foreach(object, key, val) {
if (strcmp("op", key) == 0) {
where->op = (enum json_api_operator) json_object_get_int(val);
break;
}
}
}
switch (where->op) {
case JSON_API_OPERATOR_EQ:
case JSON_API_OPERATOR_NE:
case JSON_API_OPERATOR_LT:
case JSON_API_OPERATOR_GT:
case JSON_API_OPERATOR_LE:
case JSON_API_OPERATOR_GE:
{
json_object_object_foreach(object, key, val) {
if (strcmp("column", key) == 0) {
where->column = strdup(json_object_get_string(val));
continue;
}
if (strcmp("value", key) == 0) {
where->value = json_to_storage_value(val);
continue;
}
}
break;
}
case JSON_API_OPERATOR_AND:
case JSON_API_OPERATOR_OR:
{
json_object_object_foreach(object, key, val) {
if (strcmp("left", key) == 0) {
where->left = json_api_to_where(val);
continue;
}
if (strcmp("right", key) == 0) {
where->right = json_api_to_where(val);
continue;
}
}
break;
}
}
return where;
}
struct json_api_delete_request json_api_to_delete_request(struct json_object * object) {
struct json_api_delete_request request;
request.where = NULL;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
continue;
}
if (strcmp("where", key) == 0) {
request.where = json_api_to_where(val);
continue;
}
}
return request;
}
struct json_api_select_request json_api_to_select_request(struct json_object * object) {
struct json_api_select_request request;
request.columns.amount = 0;
request.columns.columns = NULL;
request.joins.amount = 0;
request.joins.joins = NULL;
request.where = NULL;
request.offset = 0;
request.limit = 10;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
continue;
}
if (strcmp("columns", key) == 0) {
request.columns.amount = json_object_array_length(val);
request.columns.columns = malloc(sizeof(*request.columns.columns) * request.columns.amount);
for (int i = 0; i < request.columns.amount; ++i) {
request.columns.columns[i] = strdup(json_object_get_string(json_object_array_get_idx(val, i)));
}
continue;
}
if (strcmp("where", key) == 0) {
request.where = json_api_to_where(val);
continue;
}
if (strcmp("offset", key) == 0) {
request.offset = json_object_get_int(val);
continue;
}
if (strcmp("limit", key) == 0) {
request.limit = json_object_get_int(val);
continue;
}
if (strcmp("joins", key) == 0) {
request.joins.amount = json_object_array_length(val);
request.joins.joins = malloc(sizeof(*request.joins.joins) * request.joins.amount);
for (int i = 0; i < request.joins.amount; ++i) {
struct json_object * elem = json_object_array_get_idx(val, i);
json_object_object_foreach(elem, elem_key, elem_val) {
if (strcmp("table", elem_key) == 0) {
request.joins.joins[i].table = strdup(json_object_get_string(elem_val));
}
if (strcmp("t_column", elem_key) == 0) {
request.joins.joins[i].t_column = strdup(json_object_get_string(elem_val));
}
if (strcmp("s_column", elem_key) == 0) {
request.joins.joins[i].s_column = strdup(json_object_get_string(elem_val));
}
}
}
continue;
}
}
return request;
}
struct json_api_update_request json_api_to_update_request(struct json_object * object) {
struct json_api_update_request request;
request.where = NULL;
json_object_object_foreach(object, key, val) {
if (strcmp("table", key) == 0) {
request.table_name = strdup(json_object_get_string(val));
continue;
}
if (strcmp("columns", key) == 0) {
request.columns.amount = json_object_array_length(val);
request.columns.columns = malloc(sizeof(*request.columns.columns) * request.columns.amount);
for (int i = 0; i < request.columns.amount; ++i) {
request.columns.columns[i] = strdup(json_object_get_string(json_object_array_get_idx(val, i)));
}
continue;
}
if (strcmp("values", key) == 0) {
request.values.amount = json_object_array_length(val);
request.values.values = malloc(sizeof(struct storage_value *) * request.values.amount);
for (int i = 0; i < request.values.amount; ++i) {
request.values.values[i] = json_to_storage_value(json_object_array_get_idx(val, i));
}
continue;
}
if (strcmp("where", key) == 0) {
request.where = json_api_to_where(val);
continue;
}
}
return request;
}
struct json_object * json_api_make_success(struct json_object * answer) {
struct json_object * object = json_object_new_object();
json_object_object_add(object, "success", answer);
return object;
}
struct json_object * json_api_make_error(const char * msg) {
struct json_object * object = json_object_new_object();
json_object_object_add(object, "error", json_object_new_string(msg));
return object;
}
struct json_object * json_api_from_value(struct storage_value * value) {
if (value == NULL) {
return NULL;
}
switch (value->type) {
case STORAGE_COLUMN_TYPE_INT:
return json_object_new_int64(value->value._int);
case STORAGE_COLUMN_TYPE_UINT:
return json_object_new_uint64(value->value.uint);
case STORAGE_COLUMN_TYPE_NUM:
return json_object_new_double(value->value.num);
case STORAGE_COLUMN_TYPE_STR:
return json_object_new_string(value->value.str);
}
}