Line data Source code
1 : //
2 : // dbutils.c
3 : // cloudsync
4 : //
5 : // Created by Marco Bambini on 23/09/24.
6 : //
7 :
8 : #include <stdlib.h>
9 : #include <inttypes.h>
10 :
11 : #include "sql.h"
12 : #include "utils.h"
13 : #include "dbutils.h"
14 : #include "cloudsync.h"
15 :
16 : #if CLOUDSYNC_UNITTEST
17 : char *OUT_OF_MEMORY_BUFFER = "OUT_OF_MEMORY_BUFFER";
18 : #ifndef SQLITE_MAX_ALLOCATION_SIZE
19 : #define SQLITE_MAX_ALLOCATION_SIZE 2147483391
20 : #endif
21 : #endif
22 :
23 : // MARK: - Others -
24 :
25 : // compares two SQLite values and returns an integer indicating the comparison result
26 31162 : int dbutils_value_compare (dbvalue_t *lvalue, dbvalue_t *rvalue) {
27 31162 : if (lvalue == rvalue) return 0;
28 31162 : if (!lvalue) return -1;
29 31162 : if (!rvalue) return 1;
30 :
31 31160 : int l_type = (lvalue) ? database_value_type(lvalue) : DBTYPE_NULL;
32 31160 : int r_type = database_value_type(rvalue);
33 :
34 : // early exit if types differ, null is less than all types
35 31160 : if (l_type != r_type) return (r_type - l_type);
36 :
37 : // at this point lvalue and rvalue are of the same type
38 31153 : switch (l_type) {
39 : case DBTYPE_INTEGER: {
40 10538 : int64_t l_int = database_value_int(lvalue);
41 10538 : int64_t r_int = database_value_int(rvalue);
42 10538 : return (l_int < r_int) ? -1 : (l_int > r_int);
43 : } break;
44 :
45 : case DBTYPE_FLOAT: {
46 4 : double l_double = database_value_double(lvalue);
47 4 : double r_double = database_value_double(rvalue);
48 4 : return (l_double < r_double) ? -1 : (l_double > r_double);
49 : } break;
50 :
51 : case DBTYPE_NULL:
52 634 : break;
53 :
54 : case DBTYPE_TEXT: {
55 19942 : const char *l_text = database_value_text(lvalue);
56 19942 : const char *r_text = database_value_text(rvalue);
57 19942 : if (l_text == NULL && r_text == NULL) return 0;
58 19942 : if (l_text == NULL && r_text != NULL) return -1;
59 19942 : if (l_text != NULL && r_text == NULL) return 1;
60 19942 : return strcmp((const char *)l_text, (const char *)r_text);
61 : } break;
62 :
63 : case DBTYPE_BLOB: {
64 35 : const void *l_blob = database_value_blob(lvalue);
65 35 : const void *r_blob = database_value_blob(rvalue);
66 35 : if (l_blob == NULL && r_blob == NULL) return 0;
67 35 : if (l_blob == NULL && r_blob != NULL) return -1;
68 35 : if (l_blob != NULL && r_blob == NULL) return 1;
69 35 : int l_size = database_value_bytes(lvalue);
70 35 : int r_size = database_value_bytes(rvalue);
71 35 : int cmp = memcmp(l_blob, r_blob, (l_size < r_size) ? l_size : r_size);
72 35 : return (cmp != 0) ? cmp : (l_size - r_size);
73 : } break;
74 : }
75 :
76 634 : return 0;
77 31162 : }
78 :
79 28 : void dbutils_debug_value (dbvalue_t *value) {
80 28 : switch (database_value_type(value)) {
81 : case DBTYPE_INTEGER:
82 7 : printf("\t\tINTEGER: %" PRId64 "\n", database_value_int(value));
83 7 : break;
84 : case DBTYPE_FLOAT:
85 7 : printf("\t\tFLOAT: %f\n", database_value_double(value));
86 7 : break;
87 : case DBTYPE_TEXT:
88 6 : printf("\t\tTEXT: %s (%d)\n", database_value_text(value), database_value_bytes(value));
89 6 : break;
90 : case DBTYPE_BLOB:
91 6 : printf("\t\tBLOB: %p (%d)\n", (char *)database_value_blob(value), database_value_bytes(value));
92 6 : break;
93 : case DBTYPE_NULL:
94 2 : printf("\t\tNULL\n");
95 2 : break;
96 : }
97 28 : }
98 :
99 14 : void dbutils_debug_values (dbvalue_t **argv, int argc) {
100 42 : for (int i = 0; i < argc; i++) {
101 28 : dbutils_debug_value(argv[i]);
102 28 : }
103 14 : }
104 :
105 : // MARK: - Settings -
106 :
107 497 : int dbutils_binary_comparison (int x, int y) {
108 497 : return (x == y) ? 0 : (x > y ? 1 : -1);
109 : }
110 :
111 2177 : int dbutils_settings_get_value (cloudsync_context *data, const char *key, char *buffer, size_t *blen, int64_t *intvalue) {
112 : DEBUG_SETTINGS("dbutils_settings_get_value key: %s", key);
113 :
114 : // if intvalue requested: buffer/blen optional
115 2177 : size_t buffer_len = 0;
116 2177 : if (intvalue) {
117 1931 : *intvalue = 0;
118 1931 : } else {
119 246 : if (!buffer || !blen || *blen == 0) return DBRES_MISUSE;
120 246 : buffer[0] = 0;
121 246 : buffer_len = *blen;
122 246 : *blen = 0;
123 : }
124 :
125 2177 : dbvm_t *vm = NULL;
126 2177 : int rc = databasevm_prepare(data, SQL_SETTINGS_GET_VALUE, (void **)&vm, 0);
127 2177 : if (rc != DBRES_OK) goto finalize_get_value;
128 :
129 2177 : rc = databasevm_bind_text(vm, 1, key, -1);
130 2177 : if (rc != DBRES_OK) goto finalize_get_value;
131 :
132 2177 : rc = databasevm_step(vm);
133 2177 : if (rc == DBRES_DONE) rc = DBRES_OK;
134 1294 : else if (rc != DBRES_ROW) goto finalize_get_value;
135 :
136 : // SQLITE_ROW case
137 2423 : if (rc == DBRES_ROW) {
138 1294 : rc = DBRES_OK;
139 :
140 : // NULL case
141 1294 : if (database_column_type(vm, 0) == DBTYPE_NULL) {
142 0 : goto finalize_get_value;
143 : }
144 :
145 : // INT case
146 1294 : if (intvalue) {
147 1048 : *intvalue = database_column_int(vm, 0);
148 1048 : goto finalize_get_value;
149 : }
150 :
151 : // buffer case
152 246 : const char *value = database_column_text(vm, 0);
153 246 : size_t size = (size_t)database_column_bytes(vm, 0);
154 246 : if (!value || size == 0) goto finalize_get_value;
155 246 : if (size + 1 > buffer_len) {
156 0 : rc = DBRES_NOMEM;
157 0 : } else {
158 246 : memcpy(buffer, value, size);
159 246 : buffer[size] = '\0';
160 246 : *blen = size;
161 : }
162 246 : }
163 :
164 : finalize_get_value:
165 2177 : if (rc != DBRES_OK) {
166 0 : DEBUG_ALWAYS("dbutils_settings_get_value error %s", database_errmsg(data));
167 0 : }
168 :
169 2177 : if (vm) databasevm_finalize(vm);
170 2177 : return rc;
171 2177 : }
172 :
173 862 : int dbutils_settings_set_key_value (cloudsync_context *data, const char *key, const char *value) {
174 862 : if (!key) return DBRES_MISUSE;
175 : DEBUG_SETTINGS("dbutils_settings_set_key_value key: %s value: %s", key, value);
176 :
177 862 : int rc = DBRES_OK;
178 862 : if (value) {
179 862 : const char *values[] = {key, value};
180 862 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT};
181 862 : int lens[] = {-1, -1};
182 862 : rc = database_write(data, SQL_SETTINGS_SET_KEY_VALUE_REPLACE, values, types, lens, 2);
183 862 : } else {
184 0 : const char *values[] = {key};
185 0 : DBTYPE types[] = {DBTYPE_TEXT};
186 0 : int lens[] = {-1};
187 0 : rc = database_write(data, SQL_SETTINGS_SET_KEY_VALUE_DELETE, values, types, lens, 1);
188 : }
189 :
190 862 : if (rc == DBRES_OK && data) cloudsync_sync_key(data, key, value);
191 862 : return rc;
192 862 : }
193 :
194 1286 : int dbutils_settings_get_int_value (cloudsync_context *data, const char *key) {
195 : DEBUG_SETTINGS("dbutils_settings_get_int_value key: %s", key);
196 1286 : int64_t value = 0;
197 1286 : if (dbutils_settings_get_value(data, key, NULL, NULL, &value) != DBRES_OK) return -1;
198 :
199 1286 : return (int)value;
200 1286 : }
201 :
202 645 : int64_t dbutils_settings_get_int64_value (cloudsync_context *data, const char *key) {
203 : DEBUG_SETTINGS("dbutils_settings_get_int_value key: %s", key);
204 645 : int64_t value = 0;
205 645 : if (dbutils_settings_get_value(data, key, NULL, NULL, &value) != DBRES_OK) return -1;
206 :
207 645 : return value;
208 645 : }
209 :
210 246 : int dbutils_settings_check_version (cloudsync_context *data, const char *version) {
211 : DEBUG_SETTINGS("dbutils_settings_check_version");
212 : char buffer[256];
213 246 : size_t len = sizeof(buffer);
214 246 : if (dbutils_settings_get_value(data, CLOUDSYNC_KEY_LIBVERSION, buffer, &len, NULL) != DBRES_OK) return -666;
215 :
216 : int major1, minor1, patch1;
217 : int major2, minor2, patch2;
218 246 : int count1 = sscanf(buffer, "%d.%d.%d", &major1, &minor1, &patch1);
219 246 : int count2 = sscanf((version == NULL ? CLOUDSYNC_VERSION : version), "%d.%d.%d", &major2, &minor2, &patch2);
220 :
221 246 : if (count1 != 3 || count2 != 3) return -666;
222 :
223 246 : int res = 0;
224 246 : if ((res = dbutils_binary_comparison(major1, major2)) == 0) {
225 246 : if ((res = dbutils_binary_comparison(minor1, minor2)) == 0) {
226 0 : return dbutils_binary_comparison(patch1, patch2);
227 : }
228 246 : }
229 :
230 : DEBUG_SETTINGS(" %s %s (%d)", buffer, CLOUDSYNC_VERSION, res);
231 246 : return res;
232 246 : }
233 :
234 755 : int dbutils_table_settings_get_value (cloudsync_context *data, const char *table, const char *column_name, const char *key, char *buffer, size_t blen) {
235 : DEBUG_SETTINGS("dbutils_table_settings_get_value table: %s column: %s key: %s", table, column_name, key);
236 :
237 755 : if (!buffer || blen == 0) return DBRES_MISUSE;
238 755 : buffer[0] = 0;
239 :
240 755 : dbvm_t *vm = NULL;
241 755 : int rc = databasevm_prepare(data, SQL_TABLE_SETTINGS_GET_VALUE, (void **)&vm, 0);
242 755 : if (rc != DBRES_OK) goto finalize_get_value;
243 :
244 755 : rc = databasevm_bind_text(vm, 1, table, -1);
245 755 : if (rc != DBRES_OK) goto finalize_get_value;
246 :
247 755 : rc = databasevm_bind_text(vm, 2, (column_name) ? column_name : "*", -1);
248 755 : if (rc != DBRES_OK) goto finalize_get_value;
249 :
250 755 : rc = databasevm_bind_text(vm, 3, key, -1);
251 755 : if (rc != DBRES_OK) goto finalize_get_value;
252 :
253 755 : rc = databasevm_step(vm);
254 755 : if (rc == DBRES_DONE) rc = DBRES_OK;
255 52 : else if (rc != DBRES_ROW) goto finalize_get_value;
256 :
257 : // SQLITE_ROW case
258 807 : if (rc == DBRES_ROW) {
259 52 : rc = DBRES_OK;
260 :
261 : // NULL case
262 52 : if (database_column_type(vm, 0) == DBTYPE_NULL) {
263 0 : goto finalize_get_value;
264 : }
265 :
266 52 : const char *value = database_column_text(vm, 0);
267 52 : size_t size = (size_t)database_column_bytes(vm, 0);
268 52 : if (size + 1 > blen) {
269 0 : rc = DBRES_NOMEM;
270 0 : } else {
271 52 : memcpy(buffer, value, size);
272 52 : buffer[size] = '\0';
273 : }
274 52 : }
275 :
276 : finalize_get_value:
277 755 : if (rc != DBRES_OK) {
278 0 : DEBUG_ALWAYS("cloudsync_table_settings error %s", database_errmsg(data));
279 0 : }
280 755 : if (vm) databasevm_finalize(vm);
281 755 : return rc;
282 755 : }
283 :
284 296 : int dbutils_table_settings_set_key_value (cloudsync_context *data, const char *table_name, const char *column_name, const char *key, const char *value) {
285 : DEBUG_SETTINGS("dbutils_table_settings_set_key_value table: %s column: %s key: %s", table_name, column_name, key);
286 :
287 296 : int rc = DBRES_OK;
288 :
289 : // sanity check tbl_name
290 296 : if (table_name == NULL) {
291 0 : return cloudsync_set_error(data, "cloudsync_set_table/set_column requires a non-null table parameter", DBRES_ERROR);
292 : }
293 :
294 : // sanity check column name
295 296 : if (column_name == NULL) column_name = "*";
296 :
297 : // remove all table_name entries
298 296 : if (key == NULL) {
299 3 : const char *values[] = {table_name};
300 3 : DBTYPE types[] = {DBTYPE_TEXT};
301 3 : int lens[] = {-1};
302 3 : rc = database_write(data, SQL_TABLE_SETTINGS_DELETE_ALL_FOR_TABLE, values, types, lens, 1);
303 3 : return rc;
304 : }
305 :
306 293 : if (key && value) {
307 293 : const char *values[] = {table_name, column_name, key, value};
308 293 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT};
309 293 : int lens[] = {-1, -1, -1, -1};
310 293 : rc = database_write(data, SQL_TABLE_SETTINGS_REPLACE, values, types, lens, 4);
311 293 : }
312 :
313 293 : if (value == NULL) {
314 0 : const char *values[] = {table_name, column_name, key};
315 0 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT};
316 0 : int lens[] = {-1, -1, -1};
317 0 : rc = database_write(data, SQL_TABLE_SETTINGS_DELETE_ONE, values, types, lens, 3);
318 0 : }
319 :
320 : // unused in this version
321 : // cloudsync_context *data = (context) ? (cloudsync_context *)sqlite3_user_data(context) : NULL;
322 : // if (rc == DBRES_OK && data) cloudsync_sync_table_key(data, table, column, key, value);
323 293 : return rc;
324 296 : }
325 :
326 743 : int64_t dbutils_table_settings_count_tables (cloudsync_context *data) {
327 : DEBUG_SETTINGS("dbutils_table_settings_count_tables");
328 :
329 743 : int64_t count = 0;
330 743 : int rc = database_select_int(data, SQL_TABLE_SETTINGS_COUNT_TABLES, &count);
331 743 : return (rc == DBRES_OK) ? count : 0;
332 : }
333 :
334 268 : table_algo dbutils_table_settings_get_algo (cloudsync_context *data, const char *table_name) {
335 : DEBUG_SETTINGS("dbutils_table_settings_get_algo %s", table_name);
336 :
337 : char buffer[512];
338 268 : int rc = dbutils_table_settings_get_value(data, table_name, "*", "algo", buffer, sizeof(buffer));
339 268 : return (rc == DBRES_OK) ? cloudsync_algo_from_name(buffer) : table_algo_none;
340 : }
341 :
342 374 : int dbutils_settings_load_callback (void *xdata, int ncols, char **values, char **names) {
343 374 : cloudsync_context *data = (cloudsync_context *)xdata;
344 :
345 748 : for (int i=0; i+1<ncols; i+=2) {
346 374 : const char *key = values[i];
347 374 : const char *value = values[i+1];
348 374 : cloudsync_sync_key(data, key, value);
349 : DEBUG_SETTINGS("key: %s value: %s", key, value);
350 374 : }
351 :
352 374 : return 0;
353 : }
354 :
355 1 : int dbutils_settings_table_load_callback (void *xdata, int ncols, char **values, char **names) {
356 1 : cloudsync_context *data = (cloudsync_context *)xdata;
357 :
358 2 : for (int i=0; i+3<ncols; i+=4) {
359 1 : const char *table_name = values[i];
360 1 : const char *col_name = values[i+1];
361 1 : const char *key = values[i+2];
362 1 : const char *value = values[i+3];
363 :
364 : // Table-level algo setting (col_name == "*")
365 1 : if (strcmp(key, "algo") == 0 && col_name && strcmp(col_name, "*") == 0) {
366 1 : table_algo algo = cloudsync_algo_from_name(value);
367 : char fbuf[2048];
368 1 : int frc = dbutils_table_settings_get_value(data, table_name, "*", "filter", fbuf, sizeof(fbuf));
369 1 : const char *filt = (frc == DBRES_OK && fbuf[0]) ? fbuf : NULL;
370 1 : if (database_create_triggers(data, table_name, algo, filt) != DBRES_OK) return DBRES_MISUSE;
371 1 : if (table_add_to_context(data, algo, table_name) == false) return DBRES_MISUSE;
372 : DEBUG_SETTINGS("load tbl_name: %s value: %s", key, value);
373 1 : continue;
374 : }
375 :
376 : // Column-level algo=block setting (col_name != "*")
377 0 : if (strcmp(key, "algo") == 0 && value && strcmp(value, "block") == 0 &&
378 0 : col_name && strcmp(col_name, "*") != 0) {
379 : // Read optional delimiter
380 : char dbuf[256];
381 0 : int drc = dbutils_table_settings_get_value(data, table_name, col_name, "delimiter", dbuf, sizeof(dbuf));
382 0 : const char *delim = (drc == DBRES_OK && dbuf[0]) ? dbuf : NULL;
383 0 : cloudsync_setup_block_column(data, table_name, col_name, delim);
384 : DEBUG_SETTINGS("load block column: %s.%s delimiter: %s", table_name, col_name, delim ? delim : "(default)");
385 0 : continue;
386 : }
387 0 : }
388 :
389 1 : return 0;
390 1 : }
391 :
392 0 : bool dbutils_settings_migrate (cloudsync_context *data) {
393 : // dbutils_settings_check_version comparison failed
394 : // so check for logic migration here (if necessary)
395 0 : return true;
396 : }
397 :
398 187 : int dbutils_settings_load (cloudsync_context *data) {
399 : DEBUG_SETTINGS("dbutils_settings_load %p", data);
400 :
401 : // load global settings
402 187 : const char *sql = SQL_SETTINGS_LOAD_GLOBAL;
403 187 : int rc = database_exec_callback(data, sql, dbutils_settings_load_callback, data);
404 187 : if (rc != DBRES_OK) DEBUG_ALWAYS("cloudsync_load_settings error: %s", database_errmsg(data));
405 :
406 : // load table-specific settings
407 187 : sql = SQL_SETTINGS_LOAD_TABLE;
408 187 : rc = database_exec_callback(data, sql, dbutils_settings_table_load_callback, data);
409 187 : if (rc != DBRES_OK) DEBUG_ALWAYS("cloudsync_load_settings error: %s", database_errmsg(data));
410 :
411 187 : return DBRES_OK;
412 : }
413 :
414 187 : int dbutils_settings_init (cloudsync_context *data) {
415 : DEBUG_SETTINGS("dbutils_settings_init %p", data);
416 :
417 : // check if cloudsync_settings table exists
418 187 : int rc = DBRES_OK;
419 187 : bool settings_exists = database_internal_table_exists(data, CLOUDSYNC_SETTINGS_NAME);
420 187 : if (settings_exists == false) {
421 : DEBUG_SETTINGS("cloudsync_settings does not exist (creating a new one)");
422 :
423 : // create table and fill-in initial data
424 186 : rc = database_exec(data, SQL_CREATE_SETTINGS_TABLE);
425 186 : if (rc != DBRES_OK) return rc;
426 :
427 : // library version
428 186 : char *sql = cloudsync_memory_mprintf(SQL_INSERT_SETTINGS_STR_FORMAT, CLOUDSYNC_KEY_LIBVERSION, CLOUDSYNC_VERSION);
429 186 : if (!sql) return DBRES_NOMEM;
430 186 : rc = database_exec(data, sql);
431 186 : cloudsync_memory_free(sql);
432 186 : if (rc != DBRES_OK) return rc;
433 :
434 : // schema version
435 : char sql_int[1024];
436 186 : snprintf(sql_int, sizeof(sql_int), SQL_INSERT_SETTINGS_INT_FORMAT, CLOUDSYNC_KEY_SCHEMAVERSION, (long long)database_schema_version(data));
437 186 : rc = database_exec(data, sql_int);
438 186 : if (rc != DBRES_OK) return rc;
439 186 : }
440 :
441 187 : if (database_internal_table_exists(data, CLOUDSYNC_SITEID_NAME) == false) {
442 : DEBUG_SETTINGS("cloudsync_site_id does not exist (creating a new one)");
443 :
444 : // create table and fill-in initial data
445 : // site_id is implicitly indexed
446 : // the rowid column is the primary key
447 186 : rc = database_exec(data, SQL_CREATE_SITE_ID_TABLE);
448 186 : if (rc != DBRES_OK) return rc;
449 :
450 : // siteid (to uniquely identify this local copy of the database)
451 : uint8_t site_id[UUID_LEN];
452 186 : if (cloudsync_uuid_v7(site_id) == -1) return DBRES_ERROR;
453 :
454 : // rowid 0 means local site_id
455 186 : const char *values[] = {"0", (const char *)&site_id};
456 186 : DBTYPE types[] = {DBTYPE_INTEGER, DBTYPE_BLOB};
457 186 : int lens[] = {-1, UUID_LEN};
458 186 : rc = database_write(data, SQL_INSERT_SITE_ID_ROWID, values, types, lens, 2);
459 186 : if (rc != DBRES_OK) return rc;
460 186 : }
461 :
462 : // check if cloudsync_table_settings table exists
463 187 : if (database_internal_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == false) {
464 : DEBUG_SETTINGS("cloudsync_table_settings does not exist (creating a new one)");
465 :
466 186 : rc = database_exec(data, SQL_CREATE_TABLE_SETTINGS_TABLE);
467 186 : if (rc != DBRES_OK) return rc;
468 186 : }
469 :
470 : // check if cloudsync_settings table exists
471 187 : bool schema_versions_exists = database_internal_table_exists(data, CLOUDSYNC_SCHEMA_VERSIONS_NAME);
472 187 : if (schema_versions_exists == false) {
473 : DEBUG_SETTINGS("cloudsync_schema_versions does not exist (creating a new one)");
474 :
475 : // create table
476 186 : rc = database_exec(data, SQL_CREATE_SCHEMA_VERSIONS_TABLE);
477 186 : if (rc != DBRES_OK) return rc;
478 186 : }
479 :
480 : // cloudsync_settings table exists so load it
481 187 : dbutils_settings_load(data);
482 :
483 : // check if some process changed schema outside of the lib
484 : /*
485 : if ((settings_exists == true) && (data->schema_version != database_schema_version(data))) {
486 : // SOMEONE CHANGED SCHEMAs SO WE NEED TO RECHECK AUGMENTED TABLES and RELATED TRIGGERS
487 : assert(0);
488 : }
489 : */
490 :
491 187 : return DBRES_OK;
492 187 : }
493 :
494 1 : int dbutils_settings_cleanup (cloudsync_context *data) {
495 1 : return database_exec(data, SQL_SETTINGS_CLEANUP_DROP_ALL);
496 : }
|