@@ -164,10 +164,11 @@ int dbutils_write_simple (sqlite3 *db, const char *sql) {
164164}
165165
166166sqlite3_int64 dbutils_int_select (sqlite3 * db , const char * sql ) {
167+ // used only for cound(*), hash, or 1, so return -1 to signal an error
167168 DATABASE_RESULT results [1 ] = {0 };
168169 int expected_types [1 ] = {SQLITE_INTEGER };
169170 dbutils_exec (NULL , db , sql , NULL , NULL , NULL , 0 , results , expected_types , 1 );
170- return results [0 ].value .intValue ;
171+ return ( results [0 ].rc == SQLITE_OK ) ? results [ 0 ]. value .intValue : -1 ;
171172}
172173
173174char * dbutils_text_select (sqlite3 * db , const char * sql ) {
@@ -177,11 +178,12 @@ char *dbutils_text_select (sqlite3 *db, const char *sql) {
177178 return results [0 ].value .stringValue ;
178179}
179180
180- char * dbutils_blob_select (sqlite3 * db , const char * sql , int * size ) {
181+ char * dbutils_blob_select (sqlite3 * db , const char * sql , int * size , sqlite3_context * context , int * rc ) {
181182 DATABASE_RESULT results [1 ] = {0 };
182183 int expected_types [1 ] = {SQLITE_BLOB };
183- dbutils_exec (NULL , db , sql , NULL , NULL , NULL , 0 , results , expected_types , 1 );
184+ dbutils_exec (context , db , sql , NULL , NULL , NULL , 0 , results , expected_types , 1 );
184185 * size = results [0 ].len ;
186+ * rc = results [0 ].rc ;
185187 return results [0 ].value .stringValue ;
186188}
187189
@@ -196,13 +198,6 @@ int dbutils_blob_int_int_select (sqlite3 *db, const char *sql, char **blob, int
196198 return results [0 ].rc ;
197199}
198200
199- sqlite3_int64 dbutils_select (sqlite3 * db , const char * sql , const char * * values , int types [], int lens [], int count , int expected_type ) {
200- DATABASE_RESULT results [1 ] = {0 };
201- int expected_types [1 ] = {expected_type };
202- dbutils_exec (NULL , db , sql , values , types , lens , count , results , expected_types , 1 );
203- return results [0 ].value .intValue ;
204- }
205-
206201// MARK: -
207202
208203// compares two SQLite values and returns an integer indicating the comparison result
@@ -396,6 +391,9 @@ bool dbutils_table_sanity_check (sqlite3 *db, sqlite3_context *context, const ch
396391 if (count > 128 ) {
397392 dbutils_context_result_error (context , "No more than 128 columns can be used to form a composite primary key" );
398393 return false;
394+ } else if (count == -1 ) {
395+ dbutils_context_result_error (context , "%s" , sqlite3_errmsg (db ));
396+ return false;
399397 }
400398
401399 #if CLOUDSYNC_DISABLE_ROWIDONLY_TABLES
@@ -417,13 +415,21 @@ bool dbutils_table_sanity_check (sqlite3 *db, sqlite3_context *context, const ch
417415 dbutils_context_result_error (context , "Table %s uses an single-column INTEGER primary key. For CRDT replication, primary keys must be globally unique. Consider using a TEXT primary key with UUIDs or ULID to avoid conflicts across nodes. If you understand the risk and still want to use this INTEGER primary key, set the third argument of the cloudsync_init function to 1 to skip this check." , name );
418416 return false;
419417 }
418+ if (count2 == -1 ) {
419+ dbutils_context_result_error (context , "%s" , sqlite3_errmsg (db ));
420+ return false;
421+ }
420422 }
421423 }
422424
423425 // if user declared explicit primary key(s) then make sure they are all declared as NOT NULL
424426 if (count > 0 ) {
425427 sql = sqlite3_snprintf ((int )blen , buffer , "SELECT count(*) FROM pragma_table_info('%w') WHERE pk>0 AND \"notnull\"=1;" , name );
426428 sqlite3_int64 count2 = dbutils_int_select (db , sql );
429+ if (count2 == -1 ) {
430+ dbutils_context_result_error (context , "%s" , sqlite3_errmsg (db ));
431+ return false;
432+ }
427433 if (count != count2 ) {
428434 dbutils_context_result_error (context , "All primary keys must be explicitly declared as NOT NULL (table %s)" , name );
429435 return false;
@@ -434,6 +440,10 @@ bool dbutils_table_sanity_check (sqlite3 *db, sqlite3_context *context, const ch
434440 // Otherwise, col_merge_stmt would fail if changes to other columns are inserted first.
435441 sql = sqlite3_snprintf ((int )blen , buffer , "SELECT count(*) FROM pragma_table_info('%w') WHERE pk=0 AND \"notnull\"=1 AND \"dflt_value\" IS NULL;" , name );
436442 sqlite3_int64 count3 = dbutils_int_select (db , sql );
443+ if (count3 == -1 ) {
444+ dbutils_context_result_error (context , "%s" , sqlite3_errmsg (db ));
445+ return false;
446+ }
437447 if (count3 > 0 ) {
438448 dbutils_context_result_error (context , "All non-primary key columns declared as NOT NULL must have a DEFAULT value. (table %s)" , name );
439449 return false;
@@ -1026,7 +1036,7 @@ int dbutils_settings_init (sqlite3 *db, void *cloudsync_data, sqlite3_context *c
10261036 // check if some process changed schema outside of the lib
10271037 /*
10281038 if ((settings_exists == true) && (data->schema_version != dbutils_schema_version(db))) {
1029- // TODO: SOMEONE CHANGED SCHEMAs SO WE NEED TO RECHECK AUGMENTED TABLES and RELATED TRIGGERS
1039+ // SOMEONE CHANGED SCHEMAs SO WE NEED TO RECHECK AUGMENTED TABLES and RELATED TRIGGERS
10301040 assert(0);
10311041 }
10321042 */
@@ -1072,7 +1082,7 @@ bool dbutils_check_schema_hash (sqlite3 *db, sqlite3_uint64 hash) {
10721082 char sql [1024 ];
10731083 snprintf (sql , sizeof (sql ), "SELECT 1 FROM cloudsync_schema_versions WHERE hash = (%lld)" , hash );
10741084
1075- return dbutils_int_select (db , sql ) == 1 ;
1085+ return ( dbutils_int_select (db , sql ) == 1 ) ;
10761086}
10771087
10781088
0 commit comments