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 31271 : int dbutils_value_compare (dbvalue_t *lvalue, dbvalue_t *rvalue) {
27 31271 : if (lvalue == rvalue) return 0;
28 31271 : if (!lvalue) return -1;
29 31271 : if (!rvalue) return 1;
30 :
31 31269 : int l_type = (lvalue) ? database_value_type(lvalue) : DBTYPE_NULL;
32 31269 : int r_type = database_value_type(rvalue);
33 :
34 : // early exit if types differ, null is less than all types
35 31269 : if (l_type != r_type) return (r_type - l_type);
36 :
37 : // at this point lvalue and rvalue are of the same type
38 31262 : switch (l_type) {
39 : case DBTYPE_INTEGER: {
40 10563 : int64_t l_int = database_value_int(lvalue);
41 10563 : int64_t r_int = database_value_int(rvalue);
42 10563 : 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 20026 : const char *l_text = database_value_text(lvalue);
56 20026 : const char *r_text = database_value_text(rvalue);
57 20026 : if (l_text == NULL && r_text == NULL) return 0;
58 20026 : if (l_text == NULL && r_text != NULL) return -1;
59 20026 : if (l_text != NULL && r_text == NULL) return 1;
60 20026 : 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 31271 : }
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 328 : int dbutils_binary_comparison (int x, int y) {
108 328 : return (x == y) ? 0 : (x > y ? 1 : -1);
109 : }
110 :
111 2361 : 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 2361 : size_t buffer_len = 0;
116 2361 : if (intvalue) {
117 2046 : *intvalue = 0;
118 2046 : } else {
119 315 : if (!buffer || !blen || *blen == 0) return DBRES_MISUSE;
120 315 : buffer[0] = 0;
121 315 : buffer_len = *blen;
122 315 : *blen = 0;
123 : }
124 :
125 2361 : dbvm_t *vm = NULL;
126 2361 : int rc = databasevm_prepare(data, SQL_SETTINGS_GET_VALUE, (void **)&vm, 0);
127 2361 : if (rc != DBRES_OK) goto finalize_get_value;
128 :
129 2361 : rc = databasevm_bind_text(vm, 1, key, -1);
130 2361 : if (rc != DBRES_OK) goto finalize_get_value;
131 :
132 2361 : rc = databasevm_step(vm);
133 2361 : if (rc == DBRES_DONE) rc = DBRES_OK;
134 1385 : else if (rc != DBRES_ROW) goto finalize_get_value;
135 :
136 : // SQLITE_ROW case
137 2676 : if (rc == DBRES_ROW) {
138 1385 : rc = DBRES_OK;
139 :
140 : // NULL case
141 1385 : if (database_column_type(vm, 0) == DBTYPE_NULL) {
142 0 : goto finalize_get_value;
143 : }
144 :
145 : // INT case
146 1385 : if (intvalue) {
147 1070 : *intvalue = database_column_int(vm, 0);
148 1070 : goto finalize_get_value;
149 : }
150 :
151 : // buffer case
152 315 : const char *value = database_column_text(vm, 0);
153 315 : size_t size = (size_t)database_column_bytes(vm, 0);
154 315 : if (!value || size == 0) goto finalize_get_value;
155 315 : if (size + 1 > buffer_len) {
156 0 : rc = DBRES_NOMEM;
157 0 : } else {
158 315 : memcpy(buffer, value, size);
159 315 : buffer[size] = '\0';
160 315 : *blen = size;
161 : }
162 315 : }
163 :
164 : finalize_get_value:
165 2361 : if (rc != DBRES_OK) {
166 0 : DEBUG_ALWAYS("dbutils_settings_get_value error %s", database_errmsg(data));
167 0 : }
168 :
169 2361 : if (vm) databasevm_finalize(vm);
170 2361 : return rc;
171 2361 : }
172 :
173 913 : int dbutils_settings_set_key_value (cloudsync_context *data, const char *key, const char *value) {
174 913 : if (!key) return DBRES_MISUSE;
175 : DEBUG_SETTINGS("dbutils_settings_set_key_value key: %s value: %s", key, value);
176 :
177 913 : int rc = DBRES_OK;
178 913 : if (value) {
179 913 : const char *values[] = {key, value};
180 913 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT};
181 913 : int lens[] = {-1, -1};
182 913 : rc = database_write(data, SQL_SETTINGS_SET_KEY_VALUE_REPLACE, values, types, lens, 2);
183 913 : } 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 913 : if (rc == DBRES_OK && data) cloudsync_sync_key(data, key, value);
191 913 : return rc;
192 913 : }
193 :
194 1362 : int dbutils_settings_get_int_value (cloudsync_context *data, const char *key) {
195 : DEBUG_SETTINGS("dbutils_settings_get_int_value key: %s", key);
196 1362 : int64_t value = 0;
197 1362 : if (dbutils_settings_get_value(data, key, NULL, NULL, &value) != DBRES_OK) return -1;
198 :
199 1362 : return (int)value;
200 1362 : }
201 :
202 684 : 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 684 : int64_t value = 0;
205 684 : if (dbutils_settings_get_value(data, key, NULL, NULL, &value) != DBRES_OK) return -1;
206 :
207 684 : return value;
208 684 : }
209 :
210 315 : int dbutils_settings_check_version (cloudsync_context *data, const char *version) {
211 : DEBUG_SETTINGS("dbutils_settings_check_version");
212 : char buffer[256];
213 315 : size_t len = sizeof(buffer);
214 315 : 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 315 : int count1 = sscanf(buffer, "%d.%d.%d", &major1, &minor1, &patch1);
219 315 : int count2 = sscanf((version == NULL ? CLOUDSYNC_VERSION : version), "%d.%d.%d", &major2, &minor2, &patch2);
220 :
221 315 : if (count1 != 3 || count2 != 3) return -666;
222 :
223 315 : int res = 0;
224 315 : if ((res = dbutils_binary_comparison(major1, major2)) == 0) {
225 4 : if ((res = dbutils_binary_comparison(minor1, minor2)) == 0) {
226 4 : return dbutils_binary_comparison(patch1, patch2);
227 : }
228 0 : }
229 :
230 : DEBUG_SETTINGS(" %s %s (%d)", buffer, CLOUDSYNC_VERSION, res);
231 311 : return res;
232 315 : }
233 :
234 925 : 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 925 : if (!buffer || blen == 0) return DBRES_MISUSE;
238 925 : buffer[0] = 0;
239 :
240 925 : dbvm_t *vm = NULL;
241 925 : int rc = databasevm_prepare(data, SQL_TABLE_SETTINGS_GET_VALUE, (void **)&vm, 0);
242 925 : if (rc != DBRES_OK) goto finalize_get_value;
243 :
244 925 : rc = databasevm_bind_text(vm, 1, table, -1);
245 925 : if (rc != DBRES_OK) goto finalize_get_value;
246 :
247 925 : rc = databasevm_bind_text(vm, 2, (column_name) ? column_name : "*", -1);
248 925 : if (rc != DBRES_OK) goto finalize_get_value;
249 :
250 925 : rc = databasevm_bind_text(vm, 3, key, -1);
251 925 : if (rc != DBRES_OK) goto finalize_get_value;
252 :
253 925 : rc = databasevm_step(vm);
254 925 : if (rc == DBRES_DONE) rc = DBRES_OK;
255 96 : else if (rc != DBRES_ROW) goto finalize_get_value;
256 :
257 : // SQLITE_ROW case
258 1021 : if (rc == DBRES_ROW) {
259 96 : rc = DBRES_OK;
260 :
261 : // NULL case
262 96 : if (database_column_type(vm, 0) == DBTYPE_NULL) {
263 0 : goto finalize_get_value;
264 : }
265 :
266 96 : const char *value = database_column_text(vm, 0);
267 96 : size_t size = (size_t)database_column_bytes(vm, 0);
268 96 : if (size + 1 > blen) {
269 0 : rc = DBRES_NOMEM;
270 0 : } else {
271 96 : memcpy(buffer, value, size);
272 96 : buffer[size] = '\0';
273 : }
274 96 : }
275 :
276 : finalize_get_value:
277 925 : if (rc != DBRES_OK) {
278 0 : DEBUG_ALWAYS("cloudsync_table_settings error %s", database_errmsg(data));
279 0 : }
280 925 : if (vm) databasevm_finalize(vm);
281 925 : return rc;
282 925 : }
283 :
284 363 : 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 363 : int rc = DBRES_OK;
288 :
289 : // sanity check tbl_name
290 363 : 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 363 : if (column_name == NULL) column_name = "*";
296 :
297 : // remove all table_name entries
298 363 : if (key == NULL) {
299 4 : const char *values[] = {table_name};
300 4 : DBTYPE types[] = {DBTYPE_TEXT};
301 4 : int lens[] = {-1};
302 4 : rc = database_write(data, SQL_TABLE_SETTINGS_DELETE_ALL_FOR_TABLE, values, types, lens, 1);
303 4 : return rc;
304 : }
305 :
306 359 : if (key && value) {
307 355 : const char *values[] = {table_name, column_name, key, value};
308 355 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT};
309 355 : int lens[] = {-1, -1, -1, -1};
310 355 : rc = database_write(data, SQL_TABLE_SETTINGS_REPLACE, values, types, lens, 4);
311 355 : }
312 :
313 359 : if (value == NULL) {
314 4 : const char *values[] = {table_name, column_name, key};
315 4 : DBTYPE types[] = {DBTYPE_TEXT, DBTYPE_TEXT, DBTYPE_TEXT};
316 4 : int lens[] = {-1, -1, -1};
317 4 : rc = database_write(data, SQL_TABLE_SETTINGS_DELETE_ONE, values, types, lens, 3);
318 4 : }
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 359 : return rc;
324 363 : }
325 :
326 891 : int64_t dbutils_table_settings_count_tables (cloudsync_context *data) {
327 : DEBUG_SETTINGS("dbutils_table_settings_count_tables");
328 :
329 891 : int64_t count = 0;
330 891 : int rc = database_select_int(data, SQL_TABLE_SETTINGS_COUNT_TABLES, &count);
331 891 : return (rc == DBRES_OK) ? count : 0;
332 : }
333 :
334 330 : 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 330 : int rc = dbutils_table_settings_get_value(data, table_name, "*", "algo", buffer, sizeof(buffer));
339 330 : return (rc == DBRES_OK) ? cloudsync_algo_from_name(buffer) : table_algo_none;
340 : }
341 :
342 456 : int dbutils_settings_load_callback (void *xdata, int ncols, char **values, char **names) {
343 456 : cloudsync_context *data = (cloudsync_context *)xdata;
344 :
345 912 : for (int i=0; i+1<ncols; i+=2) {
346 456 : const char *key = values[i];
347 456 : const char *value = values[i+1];
348 456 : cloudsync_sync_key(data, key, value);
349 : DEBUG_SETTINGS("key: %s value: %s", key, value);
350 456 : }
351 :
352 456 : return 0;
353 : }
354 :
355 8 : int dbutils_settings_table_load_callback (void *xdata, int ncols, char **values, char **names) {
356 8 : cloudsync_context *data = (cloudsync_context *)xdata;
357 :
358 16 : for (int i=0; i+3<ncols; i+=4) {
359 8 : const char *table_name = values[i];
360 8 : const char *col_name = values[i+1];
361 8 : const char *key = values[i+2];
362 8 : const char *value = values[i+3];
363 :
364 : // Table-level algo setting (col_name == "*")
365 8 : if (strcmp(key, "algo") == 0 && col_name && strcmp(col_name, "*") == 0) {
366 : // Skip stale settings for tables that no longer exist
367 6 : if (!database_table_exists(data, table_name, cloudsync_schema(data))) {
368 : DEBUG_SETTINGS("skipping stale settings for dropped table: %s", table_name);
369 2 : continue;
370 : }
371 4 : table_algo algo = cloudsync_algo_from_name(value);
372 : char fbuf[2048];
373 4 : int frc = dbutils_table_settings_get_value(data, table_name, "*", "filter", fbuf, sizeof(fbuf));
374 4 : const char *filt = (frc == DBRES_OK && fbuf[0]) ? fbuf : NULL;
375 4 : if (database_create_triggers(data, table_name, algo, filt) != DBRES_OK) return DBRES_MISUSE;
376 4 : if (table_add_to_context(data, algo, table_name) == false) return DBRES_MISUSE;
377 : DEBUG_SETTINGS("load tbl_name: %s value: %s", key, value);
378 4 : continue;
379 : }
380 :
381 : // Column-level algo=block setting (col_name != "*")
382 3 : if (strcmp(key, "algo") == 0 && value && strcmp(value, "block") == 0 &&
383 1 : col_name && strcmp(col_name, "*") != 0) {
384 : // Read optional delimiter
385 : char dbuf[256];
386 1 : int drc = dbutils_table_settings_get_value(data, table_name, col_name, "delimiter", dbuf, sizeof(dbuf));
387 1 : const char *delim = (drc == DBRES_OK && dbuf[0]) ? dbuf : NULL;
388 1 : cloudsync_setup_block_column(data, table_name, col_name, delim, false);
389 : DEBUG_SETTINGS("load block column: %s.%s delimiter: %s", table_name, col_name, delim ? delim : "(default)");
390 1 : continue;
391 : }
392 1 : }
393 :
394 8 : return 0;
395 8 : }
396 :
397 0 : bool dbutils_settings_migrate (cloudsync_context *data) {
398 : // dbutils_settings_check_version comparison failed
399 : // so check for logic migration here (if necessary)
400 0 : return true;
401 : }
402 :
403 228 : int dbutils_settings_load (cloudsync_context *data) {
404 : DEBUG_SETTINGS("dbutils_settings_load %p", data);
405 :
406 : // load global settings
407 228 : const char *sql = SQL_SETTINGS_LOAD_GLOBAL;
408 228 : int rc = database_exec_callback(data, sql, dbutils_settings_load_callback, data);
409 228 : if (rc != DBRES_OK) DEBUG_ALWAYS("cloudsync_load_settings error: %s", database_errmsg(data));
410 :
411 : // load table-specific settings
412 228 : sql = SQL_SETTINGS_LOAD_TABLE;
413 228 : rc = database_exec_callback(data, sql, dbutils_settings_table_load_callback, data);
414 228 : if (rc != DBRES_OK) DEBUG_ALWAYS("cloudsync_load_settings error: %s", database_errmsg(data));
415 :
416 228 : return DBRES_OK;
417 : }
418 :
419 228 : int dbutils_settings_init (cloudsync_context *data) {
420 : DEBUG_SETTINGS("dbutils_settings_init %p", data);
421 :
422 : // check if cloudsync_settings table exists
423 228 : int rc = DBRES_OK;
424 228 : bool settings_exists = database_internal_table_exists(data, CLOUDSYNC_SETTINGS_NAME);
425 228 : if (settings_exists == false) {
426 : DEBUG_SETTINGS("cloudsync_settings does not exist (creating a new one)");
427 :
428 : // create table and fill-in initial data
429 222 : rc = database_exec(data, SQL_CREATE_SETTINGS_TABLE);
430 222 : if (rc != DBRES_OK) return rc;
431 :
432 : // library version
433 222 : char *sql = cloudsync_memory_mprintf(SQL_INSERT_SETTINGS_STR_FORMAT, CLOUDSYNC_KEY_LIBVERSION, CLOUDSYNC_VERSION);
434 222 : if (!sql) return DBRES_NOMEM;
435 222 : rc = database_exec(data, sql);
436 222 : cloudsync_memory_free(sql);
437 222 : if (rc != DBRES_OK) return rc;
438 :
439 : // schema version
440 : char sql_int[1024];
441 222 : snprintf(sql_int, sizeof(sql_int), SQL_INSERT_SETTINGS_INT_FORMAT, CLOUDSYNC_KEY_SCHEMAVERSION, (long long)database_schema_version(data));
442 222 : rc = database_exec(data, sql_int);
443 222 : if (rc != DBRES_OK) return rc;
444 222 : }
445 :
446 228 : if (database_internal_table_exists(data, CLOUDSYNC_SITEID_NAME) == false) {
447 : DEBUG_SETTINGS("cloudsync_site_id does not exist (creating a new one)");
448 :
449 : // create table and fill-in initial data
450 : // site_id is implicitly indexed
451 : // the rowid column is the primary key
452 222 : rc = database_exec(data, SQL_CREATE_SITE_ID_TABLE);
453 222 : if (rc != DBRES_OK) return rc;
454 :
455 : // siteid (to uniquely identify this local copy of the database)
456 : uint8_t site_id[UUID_LEN];
457 222 : if (cloudsync_uuid_v7(site_id) == -1) return DBRES_ERROR;
458 :
459 : // rowid 0 means local site_id
460 222 : const char *values[] = {"0", (const char *)&site_id};
461 222 : DBTYPE types[] = {DBTYPE_INTEGER, DBTYPE_BLOB};
462 222 : int lens[] = {-1, UUID_LEN};
463 222 : rc = database_write(data, SQL_INSERT_SITE_ID_ROWID, values, types, lens, 2);
464 222 : if (rc != DBRES_OK) return rc;
465 222 : }
466 :
467 : // check if cloudsync_table_settings table exists
468 228 : if (database_internal_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == false) {
469 : DEBUG_SETTINGS("cloudsync_table_settings does not exist (creating a new one)");
470 :
471 222 : rc = database_exec(data, SQL_CREATE_TABLE_SETTINGS_TABLE);
472 222 : if (rc != DBRES_OK) return rc;
473 222 : }
474 :
475 : // check if cloudsync_settings table exists
476 228 : bool schema_versions_exists = database_internal_table_exists(data, CLOUDSYNC_SCHEMA_VERSIONS_NAME);
477 228 : if (schema_versions_exists == false) {
478 : DEBUG_SETTINGS("cloudsync_schema_versions does not exist (creating a new one)");
479 :
480 : // create table
481 222 : rc = database_exec(data, SQL_CREATE_SCHEMA_VERSIONS_TABLE);
482 222 : if (rc != DBRES_OK) return rc;
483 222 : }
484 :
485 : // cloudsync_settings table exists so load it
486 228 : dbutils_settings_load(data);
487 :
488 : // check if some process changed schema outside of the lib
489 : /*
490 : if ((settings_exists == true) && (data->schema_version != database_schema_version(data))) {
491 : // SOMEONE CHANGED SCHEMAs SO WE NEED TO RECHECK AUGMENTED TABLES and RELATED TRIGGERS
492 : assert(0);
493 : }
494 : */
495 :
496 228 : return DBRES_OK;
497 228 : }
498 :
499 2 : int dbutils_settings_cleanup (cloudsync_context *data) {
500 2 : return database_exec(data, SQL_SETTINGS_CLEANUP_DROP_ALL);
501 : }
|