Skip to content

Commit c2e11cd

Browse files
committed
fix: conditional SQLITE_WASM_EXTRA_INIT escape single quotes instead of double quotes in query strings for wasm support
1 parent 97357f2 commit c2e11cd

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/cloudsync.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,11 @@ 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
364+
"SELECT format('%w', name) as tbl_name "
365+
#else
363366
"SELECT format(\"%w\", name) as tbl_name "
367+
#endif
364368
"FROM sqlite_master "
365369
"WHERE type='table' "
366370
"AND name LIKE '%_cloudsync'"

src/dbutils.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,11 @@ 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
423+
sql = sqlite3_snprintf((int)blen, buffer, "SELECT count(*) FROM pragma_table_info('%w') WHERE pk=1 AND \"type\" LIKE '%%INT%%';", name);
424+
#else
422425
sql = sqlite3_snprintf((int)blen, buffer, "SELECT count(*) FROM pragma_table_info('%w') WHERE pk=1 AND \"type\" LIKE \"%%INT%%\";", name);
426+
#endif
423427
sqlite3_int64 count2 = dbutils_int_select(db, sql);
424428
if (count == count2) {
425429
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/vtab.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ 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, "
142+
" t1.pk AS pk, "
143+
" t1.col_name AS col_name, "
144+
" cloudsync_col_value(''' || table_name || ''', t1.col_name, t1.pk) AS col_value, "
145+
" t1.col_version AS col_version, "
146+
" t1.db_version AS db_version, "
147+
" site_tbl.site_id AS site_id, "
148+
" t1.seq AS seq, "
149+
" COALESCE(t2.col_version, 1) AS cl "
150+
" FROM ''' || table_meta || ''' AS t1 "
151+
" 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
140154
" \"' || \"table_name\" || '\" AS tbl, "
141155
" t1.pk AS pk, "
142156
" t1.col_name AS col_name, "
@@ -149,6 +163,7 @@ char *build_changes_sql (sqlite3 *db, const char *idxs) {
149163
" FROM \"' || \"table_meta\" || '\" AS t1 "
150164
" LEFT JOIN cloudsync_site_id AS site_tbl ON t1.site_id = site_tbl.rowid "
151165
" LEFT JOIN \"' || \"table_meta\" || '\" AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "'' "
166+
#endif
152167
" WHERE col_value IS NOT ''" CLOUDSYNC_RLS_RESTRICTED_VALUE "''' "
153168
" AS query_string FROM table_names "
154169
"), "

0 commit comments

Comments
 (0)