Skip to content

Commit 356fa9b

Browse files
author
Daniele Briggi
committed
fix(fetch): sync fetch and single quotes
1 parent 6568290 commit 356fa9b

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

src/cloudsync.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,7 @@ char *db_version_build_query (sqlite3 *db) {
360360

361361
// the good news is that the query can be computed in SQLite without the need to do any extra computation from the host language
362362
const char *sql = "WITH table_names AS ("
363-
#ifdef SQLITE_WASM_EXTRA_INIT
364363
"SELECT format('%w', name) as tbl_name "
365-
#else
366-
"SELECT format(\"%w\", name) as tbl_name "
367-
#endif
368364
"FROM sqlite_master "
369365
"WHERE type='table' "
370366
"AND name LIKE '%_cloudsync'"
@@ -3284,6 +3280,7 @@ APIEXPORT int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite
32843280

32853281
// register eponymous only changes virtual table
32863282
rc = cloudsync_vtab_register_changes (db, data);
3283+
fprintf(stderr, "clousync.c init - rc: %d\n", rc);
32873284
if (rc != SQLITE_OK) return rc;
32883285

32893286
// load config, if exists

src/dbutils.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,7 @@ bool dbutils_table_sanity_check (sqlite3 *db, sqlite3_context *context, const ch
419419
// the affinity of a column is determined by the declared type of the column,
420420
// according to the following rules in the order shown:
421421
// 1. If the declared type contains the string "INT" then it is assigned INTEGER affinity.
422-
#ifdef SQLITE_WASM_EXTRA_INIT
423422
sql = sqlite3_snprintf((int)blen, buffer, "SELECT count(*) FROM pragma_table_info('%w') WHERE pk=1 AND \"type\" LIKE '%%INT%%';", name);
424-
#else
425-
sql = sqlite3_snprintf((int)blen, buffer, "SELECT count(*) FROM pragma_table_info('%w') WHERE pk=1 AND \"type\" LIKE \"%%INT%%\";", name);
426-
#endif
427423
sqlite3_int64 count2 = dbutils_int_select(db, sql);
428424
if (count == count2) {
429425
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);

src/network.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@ NETWORK_RESULT network_receive_buffer (network_data *data, const char *endpoint,
145145
} else {
146146
strcpy(attr.requestMethod, "GET");
147147
}
148-
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
149-
148+
attr.onerror = NULL; // No progress callback
149+
attr.onsuccess = NULL; // No success callback
150+
attr.onprogress = NULL; // No progress callback
151+
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE;
152+
150153
// Prepare header array (alternating key, value, NULL-terminated)
151154
const char *headers[11];
152155
int h = 0;
@@ -195,10 +198,27 @@ NETWORK_RESULT network_receive_buffer (network_data *data, const char *endpoint,
195198
attr.requestData = "";
196199
attr.requestDataSize = 0;
197200
}
198-
201+
//endpoint = "https://echo.free.beeceptor.com";
199202
emscripten_fetch_t *fetch = emscripten_fetch(&attr, endpoint); // Blocks here until the operation is complete.
200203
NETWORK_RESULT result = {0, NULL, 0, NULL, NULL};
204+
fprintf(stderr, "network_receive_buffer: %s %s\n", attr.requestMethod, endpoint);
205+
fprintf(stderr, "emscripten_fetch returned, fetch pointer: %p\n", fetch);
206+
fprintf(stderr, "network_receive_buffer: status %u, numBytes %zu\n", fetch->status, fetch->numBytes);
207+
fprintf(stderr, "network_receive_buffer: statusText %s\n", fetch->statusText ? fetch->statusText : "NULL");
208+
fprintf(stderr, "network_receive_buffer: readyState %u\n", fetch->readyState);
209+
fprintf(stderr, "network_receive_buffer: data pointer %p\n", fetch->data);
210+
211+
if (fetch->readyState != 4) {
212+
fprintf(stderr, "ERROR: fetch not completed, readyState=%u\n", fetch->readyState);
213+
result.code = CLOUDSYNC_NETWORK_ERROR;
214+
result.buffer = strdup("Network request did not complete");
215+
emscripten_fetch_close(fetch);
216+
if (custom_key) free(custom_key);
217+
return result;
218+
}
219+
201220
if (fetch->status == 200) {
221+
fprintf(stderr, "status is 200 OK\n");
202222
result.code = (fetch->numBytes > 0) ? CLOUDSYNC_NETWORK_BUFFER : CLOUDSYNC_NETWORK_OK;
203223
result.buffer = (char *)malloc(fetch->numBytes + 1);
204224
if (result.buffer && fetch->numBytes > 0) {
@@ -822,7 +842,7 @@ void cloudsync_network_set_apikey (sqlite3_context *context, int argc, sqlite3_v
822842
void cloudsync_network_has_unsent_changes (sqlite3_context *context, int argc, sqlite3_value **argv) {
823843
sqlite3 *db = sqlite3_context_db_handle(context);
824844

825-
char *sql = "SELECT max(db_version), hex(site_id) FROM cloudsync_changes() WHERE site_id == (SELECT site_id FROM cloudsync_site_id WHERE rowid=0)";
845+
char *sql = "SELECT max(db_version), hex(site_id) FROM cloudsync_changes WHERE site_id == (SELECT site_id FROM cloudsync_site_id WHERE rowid=0)";
826846
int last_local_change = (int)dbutils_int_select(db, sql);
827847
if (last_local_change == 0) {
828848
sqlite3_result_int(context, 0);

src/vtab.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,33 +137,18 @@ char *build_changes_sql (sqlite3 *db, const char *idxs) {
137137
"changes_query AS ( "
138138
" SELECT "
139139
" 'SELECT "
140-
#ifdef SQLITE_WASM_EXTRA_INIT
141-
" ''' || table_name || ''' AS tbl, "
140+
" ''' || \"table_name\" || ''' AS tbl, "
142141
" t1.pk AS pk, "
143142
" t1.col_name AS col_name, "
144-
" cloudsync_col_value(''' || table_name || ''', t1.col_name, t1.pk) AS col_value, "
143+
" cloudsync_col_value(''' || \"table_name\" || ''', t1.col_name, t1.pk) AS col_value, "
145144
" t1.col_version AS col_version, "
146145
" t1.db_version AS db_version, "
147146
" site_tbl.site_id AS site_id, "
148147
" t1.seq AS seq, "
149148
" COALESCE(t2.col_version, 1) AS cl "
150-
" FROM ''' || table_meta || ''' AS t1 "
149+
" FROM ''' || \"table_meta\" || ''' AS t1 "
151150
" LEFT JOIN cloudsync_site_id AS site_tbl ON t1.site_id = site_tbl.rowid "
152-
" LEFT JOIN ''' || table_meta || ''' AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "'' "
153-
#else
154-
" \"' || \"table_name\" || '\" AS tbl, "
155-
" t1.pk AS pk, "
156-
" t1.col_name AS col_name, "
157-
" cloudsync_col_value(\"' || \"table_name\" || '\", t1.col_name, t1.pk) AS col_value, "
158-
" t1.col_version AS col_version, "
159-
" t1.db_version AS db_version, "
160-
" site_tbl.site_id AS site_id, "
161-
" t1.seq AS seq, "
162-
" COALESCE(t2.col_version, 1) AS cl "
163-
" FROM \"' || \"table_meta\" || '\" AS t1 "
164-
" LEFT JOIN cloudsync_site_id AS site_tbl ON t1.site_id = site_tbl.rowid "
165-
" LEFT JOIN \"' || \"table_meta\" || '\" AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "'' "
166-
#endif
151+
" LEFT JOIN ''' || \"table_meta\" || ''' AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "'' "
167152
" WHERE col_value IS NOT ''" CLOUDSYNC_RLS_RESTRICTED_VALUE "''' "
168153
" AS query_string FROM table_names "
169154
"), "

0 commit comments

Comments
 (0)