Debian Codegraph @

src/hoel-simple-json.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /**
2 *
3 * Hoel database abstraction library
4 *
5 * hoel-simple-json.c: hoel simple Json query functions
6 *
7 * Copyright 2015-2020 Nicolas Mora <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation;
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU GENERAL PUBLIC LICENSE for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24 #include <string.h>
25 #include <ctype.h>
26
27 #include "hoel.h"
28 #include "h-private.h"
29
30 static char * h_get_insert_values_from_json_object(const struct _h_connection * conn, json_t * data) {
31 char * new_data = NULL, * escape, * insert_data = NULL, * tmp;
32 const char * key = NULL;
33 json_t * value = NULL, * raw;
34 int i = 0;
35
36 json_object_foreach((json_t *)data, key, value) {
37 switch (json_typeof(value)) {
38 case JSON_STRING:
39 escape = h_escape_string_with_quotes(conn, json_string_value(value));
40 if (escape == NULL) {
41 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error escape");
42 h_free(new_data);
43 new_data = NULL;
44 } else {
45 new_data = o_strdup(escape);
46 h_free(escape);
47 }
48 break;
49 case JSON_INTEGER:
50 new_data = msprintf("%"JSON_INTEGER_FORMAT, json_integer_value(value));
51 break;
52 case JSON_REAL:
53 new_data = msprintf("%f", json_real_value(value));
54 break;
55 case JSON_TRUE:
56 new_data = o_strdup("1");
57 break;
58 case JSON_FALSE:
59 new_data = o_strdup("0");
60 break;
61 case JSON_NULL:
62 new_data = o_strdup("NULL");
63 break;
64 case JSON_OBJECT:
65 raw = json_object_get(value, "raw");
66 if (raw != NULL && json_is_string(raw)) {
67 new_data = o_strdup(json_string_value(raw));
68 } else {
69 new_data = o_strdup("NULL");
70 }
71 break;
72 default:
73 tmp = json_dumps(value, JSON_ENCODE_ANY);
74 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error decoding value %s, inserting NULL value", tmp);
75 h_free(tmp);
76 new_data = o_strdup("NULL");
77 break;
78 }
79 if (new_data == NULL) {
80 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error allocating memory for new_data");
81 return NULL;
82 }
83 if (i == 0) {
84 insert_data = msprintf("(%s", new_data);
85 if (insert_data == NULL) {
86 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error allocating insert_data");
87 }
88 i = 1;
89 h_free(new_data);
90 } else {
91 tmp = msprintf("%s,%s", insert_data, new_data);
92 if (tmp == NULL) {
93 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error allocating tmp");
94 h_free(insert_data);
95 h_free(new_data);
96 return NULL;
97 }
98 h_free(insert_data);
99 h_free(new_data);
100 insert_data = tmp;
101 }
102 }
103 tmp = msprintf("%s)", insert_data);
104 h_free(insert_data);
105 if (tmp == NULL) {
106 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_values_from_json_object - Error final allocation for tmp");
107 }
108 return tmp;
109 }
110
111 static char * h_get_insert_columns_from_json_object(json_t * data) {
112 char * insert_cols = NULL, * tmp;
113 const char * key = NULL;
114 json_t * value = NULL;
115 int i = 0;
116
117 json_object_foreach((json_t *)data, key, value) {
118 if (i == 0) {
119 insert_cols = msprintf("%s", key);
120 if (insert_cols == NULL) {
121 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_columns_from_json_object - Error allocating insert_cols");
122 return NULL;
123 }
124 i = 1;
125 } else {
126 tmp = msprintf("%s,%s", insert_cols, key);
127 h_free(insert_cols);
128 if (tmp == NULL) {
129 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_columns_from_json_object - Error allocating insert_cols");
130 return NULL;
131 }
132 insert_cols = tmp;
133 }
134 }
135
136 return insert_cols;
137 }
138
139 /**
140 * Builds an insert query from a json object and a table name
141 * Returned value must be h_free'd after use
142 */
143 static char * h_get_insert_query_from_json_object(const struct _h_connection * conn, json_t * data, const char * table) {
144 char * to_return = NULL, * insert_cols, * insert_data;
145
146 insert_cols = h_get_insert_columns_from_json_object(data);
147 insert_data = h_get_insert_values_from_json_object(conn, data);
148 if (insert_cols == NULL || insert_data == NULL) {
149 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_query_from_json_object - Error h_get_insert_columns_from_json_object or h_get_insert_values_from_json_object");
150 } else {
151 to_return = msprintf("INSERT INTO %s (%s) VALUES %s", table, insert_cols, insert_data);
152 }
153 h_free(insert_cols);
154 h_free(insert_data);
155 if (to_return == NULL) {
156 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_query_from_json_object - Error allocating memory for h_get_insert_query_from_json_object");
157 }
158 return to_return;
159 }
160
161 /**
162 * Builds an insert query from a json object and a table name
163 * Returned value must be h_free'd after use
164 */
165 static char * h_get_insert_query_from_json_array(const struct _h_connection * conn, json_t * j_array, const char * table) {
166 json_t * j_row = NULL;
167 size_t index = 0;
168 char * to_return = NULL, * insert_cols, * insert_data, * tmp;
169
170 json_array_foreach(j_array, index, j_row) {
171 insert_data = h_get_insert_values_from_json_object(conn, j_row);
172 if (!index) {
173 insert_cols = h_get_insert_columns_from_json_object(j_row);
174 to_return = msprintf("INSERT INTO %s (%s) VALUES %s", table, insert_cols, insert_data);
175 h_free(insert_cols);
176 if (to_return == NULL) {
177 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_query_from_json_array - Error allocating to_return");
178 h_free(insert_data);
179 return NULL;
180 }
181 } else {
182 tmp = msprintf("%s,%s", to_return, insert_data);
183 if (tmp == NULL) {
184 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel/h_get_insert_query_from_json_array - Error allocating tmp");
185 h_free(insert_data);
186 return NULL;
187 }
188 h_free(to_return);
189 to_return = tmp;
190 }
191 h_free(insert_data);
192 }
193 return to_return;
194 }
195
196 /**
197 * Generates a where clause based on a json object
198 * the where object is a simple object like
199 * {
200 * col1: "value1",
201 * col2: "value2"
202 * }
203 * the output is a WHERE query will use only '=' and 'AND' keywords
204 * col1='value1' AND col2='value2'
205 * return a char * containing the WHERE clause, NULL on error
206 * the returned value must be h_free'd after use
207 */
208 static char * h_get_where_clause_from_json_object(const struct _h_connection * conn, const json_t * where) {
209 const char * key = NULL;
210 json_t * value = NULL, * ope, * val, * j_element;
211 char * where_clause = NULL, * dump = NULL, * escape = NULL, * tmp, * clause = NULL, * dump2 = NULL;
212 int i = 0;
213 size_t index = 0;
214
215 if (conn == NULL) {
216 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_where_clause_from_json_object - Error conn is NULL");
217 return NULL;
218 } else if (where == NULL || (json_is_object(where) && json_object_size(where) == 0)) {
219 return o_strdup("1=1");
220 } else {
221 json_object_foreach((json_t *)where, key, value) {
222 if (!json_is_string(value) && !json_is_real(value) && !json_is_integer(value) && !json_is_object(value) && !json_is_null(value)) {
223 dump = json_dumps(value, JSON_ENCODE_ANY);
224 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_where_clause_from_json_object - Error where value is invalid: %s", dump);
225 h_free(dump);
226 return NULL;
227 } else {
228 if (json_is_object(value)) {
229 ope = json_object_get(value, "operator");
230 val = json_object_get(value, "value");
231 if (ope == NULL ||
232 !json_is_string(ope) ||
233 (val == NULL && 0 != o_strcasecmp("NOT NULL", json_string_value(ope))) ||
234 (!json_is_string(val) && !json_is_real(val) && !json_is_integer(val) && 0 != o_strcasecmp("NOT NULL", json_string_value(ope)) && 0 != o_strcasecmp("IN", json_string_value(ope)))) {
235 dump = json_dumps(val, JSON_ENCODE_ANY);
236 dump2 = json_dumps(ope, JSON_ENCODE_ANY);
237 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_where_clause_from_json_object - Error where object value is invalid: %s %s", dump, dump2);
238 h_free(dump);
239 h_free(dump2);
240 h_free(where_clause);
241 return NULL;
242 } else {
243 if (0 == o_strcasecmp("NOT NULL", json_string_value(ope))) {
244 clause = msprintf("%s IS NOT NULL", key);
245 } else if (0 == o_strcasecmp("raw", json_string_value(ope)) && json_is_string(val)) {
246 clause = msprintf("%s %s", key, json_string_value(val));
247 } else if (0 == o_strcasecmp("IN", json_string_value(ope))) {
248 if (json_is_array(val) && json_array_size(val) > 0) {
249 clause = NULL, tmp = NULL;
250 json_array_foreach(val, index, j_element) {
251 if (!json_is_string(j_element) && !json_is_real(j_element) && !json_is_integer(j_element)) {
252 h_free(clause);
253 h_free(where_clause);
254 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error element value in IN statement array must be real, integer or string");
255 return NULL;
256 } else {
257 if (json_is_string(j_element)) {
258 escape = h_escape_string_with_quotes(conn, json_string_value(j_element));
259 if (escape == NULL) {
260 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error escape");
261 h_free(clause);
262 h_free(where_clause);
263 return NULL;
264 }
265 dump = msprintf("%s", escape);
266 h_free(escape);
267 } else if (json_is_real(j_element)) {
268 dump = msprintf("%f", json_real_value(j_element));
269 } else {
270 dump = msprintf("%" JSON_INTEGER_FORMAT, json_integer_value(j_element));
271 }
272 if (clause == NULL) {
273 clause = msprintf("%s IN (%s", key, dump);
274 } else {
275 tmp = msprintf("%s,%s", clause, dump);
276 h_free(clause);
277 clause = tmp;
278 }
279 h_free(dump);
280 }
281 }
282 tmp = msprintf("%s)", clause);
283 h_free(clause);
284 clause = tmp;
285 } else {
286 h_free(where_clause);
287 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error value in IN statement must be a non empty JSON array");
288 return NULL;
289 }
290 } else {
291 if (json_is_real(val)) {
292 clause = msprintf("%s %s %f", key, json_string_value(ope), json_real_value(val));
293 } else if (json_is_integer(val)) {
294 clause = msprintf("%s %s %" JSON_INTEGER_FORMAT, key, json_string_value(ope), json_integer_value(val));
295 } else {
296 escape = h_escape_string_with_quotes(conn, json_string_value(val));
297 if (escape == NULL) {
298 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error escape");
299 h_free(where_clause);
300 return NULL;
301 }
302 clause = msprintf("%s %s %s", key, json_string_value(ope), escape);
303 h_free(escape);
304 }
305 }
306 if (clause == NULL) {
307 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for clause");
308 h_free(where_clause);
309 return NULL;
310 }
311 }
312 } else {
313 if (json_is_null(value)) {
314 clause = msprintf("%s IS NULL", key);
315 } else if (json_is_string(value)) {
316 escape = h_escape_string_with_quotes(conn, json_string_value(value));
317 if (escape == NULL) {
318 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error escape");
319 h_free(where_clause);
320 return NULL;
321 }
322 clause = msprintf("%s=%s", key, escape);
323 h_free(escape);
324 } else if (json_is_integer(value)) {
325 clause = msprintf("%s='%"JSON_INTEGER_FORMAT"'", key, json_integer_value(value));
326 } else if (json_is_real(value)) {
327 clause = msprintf("%s='%f'", key, json_real_value(value));
328 } else if (json_is_true(value)) {
329 clause = msprintf("%s=1");
330 } else if (json_is_false(value)) {
331 clause = msprintf("%s=0");
332 }
333 if (clause == NULL) {
334 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for clause");
335 h_free(where_clause);
336 return NULL;
337 }
338 }
339 if (i == 0) {
340 where_clause = o_strdup(clause);
341 if (where_clause == NULL) {
342 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_where_clause_from_json_object - Error where_clause");
343 h_free(clause);
344 h_free(where_clause);
345 return NULL;
346 }
347 h_free(clause);
348 i = 1;
349 } else {
350 tmp = msprintf("%s AND %s", where_clause, clause);
351 h_free(where_clause);
352 if (tmp == NULL) {
353 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_where_clause_from_json_object - Error tmp where_clause");
354 h_free(clause);
355 h_free(where_clause);
356 return NULL;
357 }
358 h_free(clause);
359 where_clause = tmp;
360 }
361 }
362 }
363 return where_clause;
364 }
365 }
366
367 /**
368 * Generates a set clause based on a json object
369 * the where object is a simple object like
370 * {
371 * col1: "value1",
372 * col2: "value2"
373 * }
374 * the output is a WHERE query will use only '=' and 'AND' keywords
375 * col1='value1', col2='value2'
376 * return a char * containing the WHERE clause, NULL on error
377 * the returned value must be h_free'd after use
378 */
379 static char * h_get_set_clause_from_json_object(const struct _h_connection * conn, const json_t * set) {
380 const char * key = NULL;
381 json_t * value = NULL, * raw;
382 char * where_clause = NULL, * escape = NULL, * tmp;
383 int i = 0;
384
385 if (conn == NULL || set == NULL || !json_is_object(set)) {
386 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_set_clause_from_json_object - Error null input parameters");
387 return NULL;
388 } else {
389 json_object_foreach((json_t *)set, key, value) {
390 if (!json_is_string(value) && !json_is_real(value) && !json_is_integer(value) && !json_is_null(value) && !json_is_object(value)) {
391 tmp = json_dumps(value, JSON_ENCODE_ANY);
392 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_set_clause_from_json_object - Error value invalid: %s", tmp);
393 h_free(tmp);
394 h_free(where_clause);
395 return NULL;
396 } else {
397 if (json_is_string(value)) {
398 tmp = h_escape_string_with_quotes(conn, json_string_value(value));
399 escape = msprintf("%s", tmp);
400 h_free(tmp);
401 } else if (json_is_real(value)) {
402 escape = msprintf("%f", json_real_value(value));
403 } else if (json_is_integer(value)) {
404 escape = msprintf("%" JSON_INTEGER_FORMAT, json_integer_value(value));
405 } else if (json_is_object(value)) {
406 raw = json_object_get(value, "raw");
407 if (raw != NULL && json_is_string(raw)) {
408 escape = o_strdup(json_string_value(raw));
409 } else {
410 escape = o_strdup("NULL");
411 }
412 } else {
413 escape = o_strdup("");
414 }
415 if (escape == NULL) {
416 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error escape");
417 return NULL;
418 }
419 if (i == 0) {
420 if (!json_is_null(value)) {
421 where_clause = msprintf("%s=%s", key, escape);
422 } else {
423 where_clause = msprintf("%s=NULL", key);
424 }
425 if (where_clause == NULL) {
426 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_set_clause_from_json_object - Error where_clause");
427 return NULL;
428 }
429 i = 1;
430 } else {
431 if (!json_is_null(value)) {
432 tmp = msprintf("%s, %s=%s", where_clause, key, escape);
433 } else {
434 tmp = msprintf("%s, %s=NULL", where_clause, key);
435 }
436 h_free(where_clause);
437 if (tmp == NULL) {
438 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_get_set_clause_from_json_object - Error tmp where_clause");
439 return NULL;
440 }
441 where_clause = tmp;
442 }
443 h_free(escape);
444 }
445 }
446 return where_clause;
447 }
448 }
449
450 /**
451 * h_select
452 * Execute a select query
453 * Uses a json_t * parameter for the query parameters
454 * Store the result of the query in j_result if specified. j_result must be decref'd after use
455 * Duplicate the generated query in generated_query if specified, must be h_free'd after use
456 * return H_OK on success
457 */
458 int h_select(const struct _h_connection * conn, const json_t * j_query, json_t ** j_result, char ** generated_query) {
459 const char * table;
460 const json_t * cols, * where, * order_by, * group_by;
461 json_int_t limit, offset;
462 char * query = NULL, * columns = NULL, * where_clause = NULL, * tmp = NULL, * str_where_limit = NULL, * str_order_by = NULL, * str_group_by = NULL;
463 const char * col;
464 size_t index = 0;
465 json_t * value;
466 int res;
467
468 if (conn == NULL || j_result == NULL || j_query == NULL || !json_is_object(j_query) || json_object_get(j_query, "table") == NULL || !json_is_string(json_object_get(j_query, "table")) || o_strnullempty(json_string_value(json_object_get(j_query, "table")))) {
469 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error invalid input parameters");
470 return H_ERROR_PARAMS;
471 }
472
473 table = json_string_value((const json_t *)json_object_get(j_query, "table"));
474 cols = json_object_get(j_query, "columns");
475 where = json_object_get(j_query, "where");
476 order_by = json_object_get(j_query, "order_by");
477 group_by = json_object_get(j_query, "group_by");
478 limit = json_is_integer(json_object_get(j_query, "limit"))?json_integer_value(json_object_get(j_query, "limit")):0;
479 offset = json_is_integer(json_object_get(j_query, "offset"))?json_integer_value(json_object_get(j_query, "offset")):0;
480
481 where_clause = h_get_where_clause_from_json_object(conn, (json_t *)where);
482 if (where_clause == NULL) {
483 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error where_clause construction");
484 return H_ERROR_PARAMS;
485 }
486
487 if (cols == NULL) {
488 columns = o_strdup("*");
489 } else if (json_is_array(cols)) {
490 json_array_foreach(cols, index, value) {
491 if (json_is_string(value)) {
492 col = json_string_value(value);
493 if (col == NULL) {
494 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error col");
495 h_free(where_clause);
496 h_free(columns);
497 return H_ERROR_MEMORY;
498 }
499 } else {
500 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error column not string");
501 h_free(where_clause);
502 return H_ERROR_PARAMS;
503 }
504 if (index == 0) {
505 columns = o_strdup(col);
506 if (columns == NULL) {
507 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error allocating columns");
508 h_free(where_clause);
509 return H_ERROR_MEMORY;
510 }
511 } else {
512 tmp = msprintf("%s, %s", columns, col);
513 if (tmp == NULL) {
514 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error allocating clause");
515 h_free(where_clause);
516 h_free(columns);
517 return H_ERROR_MEMORY;
518 }
519 h_free(columns);
520 columns = tmp;
521 }
522 }
523 } else {
524 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error cols not array");
525 h_free(where_clause);
526 return H_ERROR_PARAMS;
527 }
528
529 if (columns == NULL) {
530 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for columns");
531 h_free(where_clause);
532 return H_ERROR_MEMORY;
533 }
534
535 if (limit > 0) {
536 if (offset > 0) {
537 str_where_limit = msprintf(" LIMIT %" JSON_INTEGER_FORMAT " OFFSET %" JSON_INTEGER_FORMAT, limit, offset);
538 } else {
539 str_where_limit = msprintf(" LIMIT %" JSON_INTEGER_FORMAT, limit);
540 }
541 } else {
542 str_where_limit = o_strdup("");
543 }
544 if (str_where_limit == NULL) {
545 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for str_where_limit");
546 h_free(columns);
547 h_free(where_clause);
548 return H_ERROR_MEMORY;
549 }
550
551 if (order_by != NULL && json_is_string(order_by) && !o_strnullempty(json_string_value(order_by))) {
552 str_order_by = msprintf(" ORDER BY %s", json_string_value(order_by));
553 } else {
554 str_order_by = o_strdup("");
555 }
556 if (str_order_by == NULL) {
557 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for str_order_by");
558 h_free(columns);
559 h_free(where_clause);
560 h_free(str_where_limit);
561 return H_ERROR_MEMORY;
562 }
563
564 if (group_by != NULL && json_is_string(group_by) && !o_strnullempty(json_string_value(group_by))) {
565 str_group_by = msprintf(" GROUP BY %s", json_string_value(group_by));
566 } else {
567 str_group_by = o_strdup("");
568 }
569 if (str_group_by == NULL) {
570 y_log_message(Y_LOG_LEVEL_ERROR, "Hoel - Error allocating memory for str_order_by");
571 h_free(columns);
572 h_free(where_clause);
573 h_free(str_where_limit);
574 h_free(str_order_by);
575 return H_ERROR_MEMORY;
576 }
577
578 query = msprintf("SELECT %s FROM %s WHERE %s%s%s%s", columns, table, where_clause, str_group_by, str_order_by, str_where_limit);
579 h_free(columns);
580 h_free(where_clause);
581 h_free(str_where_limit);
582 h_free(str_order_by);
583 h_free(str_group_by);
584 if (query == NULL) {
585 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_select Error allocating query");
586 return H_ERROR_MEMORY;
587 } else {
588 if (generated_query != NULL) {
589 *generated_query = o_strdup(query);
590 }
591 res = h_query_select_json(conn, query, j_result);
592 h_free(query);
593 return res;
594 }
595 }
596
597 /**
598 * h_insert
599 * Execute an insert query
600 * Uses a json_t * parameter for the query parameters
601 * Duplicate the generated query in generated_query if specified, must be h_free'd after use
602 * return H_OK on success
603 */
604 int h_insert(const struct _h_connection * conn, const json_t * j_query, char ** generated_query) {
605 const char * table;
606 char * query;
607 json_t * values;
608 int res;
609
610 if (conn != NULL && j_query != NULL && json_is_object(j_query) && json_is_string(json_object_get(j_query, "table")) && (json_is_object(json_object_get(j_query, "values")) || json_is_array(json_object_get(j_query, "values")))) {
611 /* Construct query */
612 table = json_string_value((const json_t *)json_object_get(j_query, "table"));
613 values = json_object_get(j_query, "values");
614 switch (json_typeof(values)) {
615 case JSON_OBJECT:
616 query = h_get_insert_query_from_json_object(conn, values, table);
617 if (query != NULL) {
618 if (generated_query != NULL) {
619 *generated_query = o_strdup(query);
620 }
621 res = h_query_insert(conn, query);
622 h_free(query);
623 if (res != H_OK) {
624 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error executing query (1)");
625 return H_ERROR_QUERY;
626 }
627 return res;
628 } else {
629 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error allocating query (1)");
630 return H_ERROR_MEMORY;
631 }
632 break;
633 case JSON_ARRAY:
634 if (json_array_size(values)) {
635 query = h_get_insert_query_from_json_array(conn, values, table);
636 if (query != NULL) {
637 if (generated_query != NULL) {
638 /* Export just the first query */
639 *generated_query = o_strdup(query);
640 }
641 res = h_query_insert(conn, query);
642 h_free(query);
643 if (res != H_OK) {
644 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error executing query (2)");
645 return H_ERROR_QUERY;
646 }
647 return res;
648 } else {
649 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error allocating query (2)");
650 return H_ERROR_MEMORY;
651 }
652 } else {
653 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error no values to insert");
654 return H_ERROR_QUERY;
655 }
656 break;
657 default:
658 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error unknown object type for values");
659 return H_ERROR_PARAMS;
660 break;
661 }
662 } else {
663 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_insert - Error null input parameters");
664 return H_ERROR_PARAMS;
665 }
666 }
667
668 /**
669 * h_last_insert_id
670 * return the id of the last inserted value
671 * return a pointer to json_t * on success, NULL otherwise.
672 * The returned value is of type JSON_INTEGER
673 */
674 json_t * h_last_insert_id(const struct _h_connection * conn) {
675 json_t * j_data = NULL;
676 if (conn != NULL && conn->connection != NULL) {
677 if (0) {
678 /* Not happening */
679 #ifdef _HOEL_SQLITE
680 } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
681 long long int last_id = h_last_insert_id_sqlite(conn);
682 if (last_id > 0) {
683 j_data = json_integer(last_id);
684 }
685 #endif
686 #ifdef _HOEL_MARIADB
687 } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
688 long long int last_id = h_last_insert_id_mariadb(conn);
689 if (last_id > 0) {
690 j_data = json_integer(last_id);
691 }
692 #endif
693 #ifdef _HOEL_PGSQL
694 } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
695 long long int last_id = h_last_insert_id_pgsql(conn);
696 if (last_id > 0) {
697 j_data = json_integer(last_id);
698 }
699 #endif
700 }
701 }
702 return j_data;
703 }
704
705 /**
706 * h_update
707 * Execute an update query
708 * Uses a json_t * parameter for the query parameters
709 * Duplicate the generated query in generated_query if specified, must be h_free'd after use
710 * return H_OK on success
711 */
712 int h_update(const struct _h_connection * conn, const json_t * j_query, char ** generated_query) {
713 char * set_clause, * where_clause, * query;
714 const char * table;
715 int res;
716 json_t * set, * where;
717
718 if (j_query == NULL || !json_is_object(j_query) || !json_is_string(json_object_get(j_query, "table")) || !json_is_object(json_object_get(j_query, "set"))) {
719 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_update - Error invalid input parameters");
720 return H_ERROR_PARAMS;
721 }
722
723 table = json_string_value((const json_t *)json_object_get(j_query, "table"));
724
725 set = json_object_get(j_query, "set");
726 set_clause = h_get_set_clause_from_json_object(conn, set);
727
728 if (set_clause == NULL) {
729 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_update - Error generating set clause");
730 return H_ERROR_PARAMS;
731 }
732
733 if (json_is_object(json_object_get(j_query, "where")) && json_object_size(json_object_get(j_query, "where")) > 0) {
734 where = json_object_get(j_query, "where");
735 where_clause = h_get_where_clause_from_json_object(conn, where);
736 query = msprintf("UPDATE %s SET %s WHERE %s", table, set_clause, where_clause);
737 h_free(where_clause);
738 } else {
739 query = msprintf("UPDATE %s SET %s", table, set_clause);
740 }
741
742 h_free(set_clause);
743 if (query == NULL) {
744 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_update - Error allocating query");
745 return H_ERROR_MEMORY;
746 }
747 if (generated_query != NULL) {
748 *generated_query = o_strdup(query);
749 }
750 res = h_query_update(conn, query);
751 h_free(query);
752 return res;
753 }
754
755 /**
756 * h_delete
757 * Execute a delete query
758 * Uses a json_t * parameter for the query parameters
759 * Duplicate the generated query in generated_query if specified, must be h_free'd after use
760 * return H_OK on success
761 */
762 int h_delete(const struct _h_connection * conn, const json_t * j_query, char ** generated_query) {
763 char * where_clause, * query;
764 const char * table;
765 int res;
766 json_t * where;
767
768 if (j_query == NULL || !json_is_object(j_query) || !json_is_string(json_object_get(j_query, "table"))) {
769 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_delete - Error invalid input parameters");
770 return H_ERROR_PARAMS;
771 }
772
773 table = json_string_value((json_t *)json_object_get(j_query, "table"));
774
775 if (json_is_object(json_object_get(j_query, "where")) && json_object_size(json_object_get(j_query, "where")) > 0) {
776 where = json_object_get(j_query, "where");
777 where_clause = h_get_where_clause_from_json_object(conn, where);
778
779 if (where_clause == NULL) {
780 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_delete - Error invalid input parameters");
781 return H_ERROR_PARAMS;
782 }
783 query = msprintf("DELETE FROM %s WHERE %s", table, where_clause);
784 h_free(where_clause);
785 } else {
786 query = msprintf("DELETE FROM %s", table);
787 }
788
789 if (query == NULL) {
790 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_delete - Error allocating query");
791 return H_ERROR_MEMORY;
792 }
793 if (generated_query != NULL) {
794 *generated_query = o_strdup(query);
795 }
796 res = h_query_delete(conn, query);
797 h_free(query);
798 return res;
799 }
800
801 /**
802 * h_build_where_clause
803 * Generates a where clause based on the pattern and the values given
804 * return a heap-allocated string
805 * returned value must be h_free'd after use
806 */
807 char * h_build_where_clause(const struct _h_connection * conn, const char * pattern, ...) {
808 char * where_clause = NULL, * pattern_pos = NULL, * escaped = NULL, * prefix = NULL;
809 const char * pattern_save = pattern, * unescaped = NULL;
810 int has_error = 0;
811 json_t * j_value = NULL;
812 json_int_t i_value = 0;
813 double d_value = 0.0;
814 va_list vl;
815 size_t prefix_len;
816
817 if (conn == NULL || o_strnullempty(pattern)) {
818 y_log_message(Y_LOG_LEVEL_DEBUG, "Hoel/h_build_where_clause - Error invalid input parameters");
819 return NULL;
820 }
821
822 va_start(vl, pattern);
823 while ((pattern_pos = o_strchr(pattern_save, '%')) != NULL && !has_error) {
824 if (*(pattern_pos+1) != '\0') {
825 prefix_len = (size_t)(pattern_pos - pattern_save);
826 prefix = o_strndup(pattern_save, prefix_len);
827 where_clause = mstrcatf(where_clause, "%s", prefix);
828 h_free(prefix);
829 pattern_save += prefix_len;
830 switch (*(pattern_pos+1)) {
831 case 's':
832 unescaped = va_arg(vl, const char *);
833 if ((escaped = h_escape_string_with_quotes(conn, unescaped)) != NULL) {
834 if ((where_clause = mstrcatf(where_clause, "%s", escaped)) != NULL) {
835 h_free(escaped);
836 pattern_save += 2;
837 } else {
838 has_error = 1;
839 }
840 } else {
841 has_error = 1;
842 }
843 break;
844 case 'S':
845 unescaped = va_arg(vl, const char *);
846 if ((escaped = h_escape_string(conn, unescaped)) != NULL) {
847 if ((where_clause = mstrcatf(where_clause, "%s", escaped)) != NULL) {
848 h_free(escaped);
849 pattern_save += 2;
850 } else {
851 has_error = 1;
852 }
853 } else {
854 has_error = 1;
855 }
856 break;
857 case 'c':
858 unescaped = va_arg(vl, const char *);
859 if ((where_clause = mstrcatf(where_clause, "'%s'", unescaped)) != NULL) {
860 pattern_save += 2;
861 } else {
862 has_error = 1;
863 }
864 break;
865 case 'C':
866 unescaped = va_arg(vl, const char *);
867 if ((where_clause = mstrcatf(where_clause, "%s", unescaped)) != NULL) {
868 pattern_save += 2;
869 } else {
870 has_error = 1;
871 }
872 break;
873 case 'd':
874 i_value = va_arg(vl, json_int_t);
875 if ((where_clause = mstrcatf(where_clause, "%"JSON_INTEGER_FORMAT, i_value)) != NULL) {
876 pattern_save += 2;
877 } else {
878 has_error = 1;
879 }
880 break;
881 case 'f':
882 d_value = va_arg(vl, double);
883 if ((where_clause = mstrcatf(where_clause, "%f", d_value)) != NULL) {
884 pattern_save += 2;
885 } else {
886 has_error = 1;
887 }
888 break;
889 case 'j':
890 j_value = va_arg(vl, json_t *);
891 if (j_value != NULL) {
892 if (json_is_string(j_value)) {
893 if ((escaped = h_escape_string_with_quotes(conn, json_string_value(j_value))) != NULL) {
894 if ((where_clause = mstrcatf(where_clause, "%s", escaped)) != NULL) {
895 h_free(escaped);
896 pattern_save += 2;
897 } else {
898 has_error = 1;
899 }
900 } else {
901 has_error = 1;
902 }
903 } else if (json_is_integer(j_value)) {
904 if ((where_clause = mstrcatf(where_clause, "%"JSON_INTEGER_FORMAT, json_integer_value(j_value))) != NULL) {
905 pattern_save += 2;
906 } else {
907 has_error = 1;
908 }
909 } else if (json_is_real(j_value)) {
910 if ((where_clause = mstrcatf(where_clause, "%f", json_real_value(j_value))) != NULL) {
911 pattern_save += 2;
912 } else {
913 has_error = 1;
914 }
915 } else {
916 has_error = 1;
917 }
918 } else {
919 has_error = 1;
920 }
921 break;
922 case '%':
923 if ((where_clause = mstrcatf(where_clause, "%%")) != NULL) {
924 pattern_save += 2;
925 } else {
926 has_error = 1;
927 }
928 break;
929 default:
930 has_error = 1;
931 break;
932 }
933 escaped = NULL;
934 j_value = NULL;
935 } else {
936 has_error = 1;
937 }
938 }
939 va_end(vl);
940
941 if (!has_error) {
942 where_clause = mstrcatf(where_clause, "%s", pattern_save);
943 } else {
944 h_free(where_clause);
945 where_clause = NULL;
946 }
947 return where_clause;
948 }