Skip to content

Commit dc2cc4f

Browse files
committed
feat: add support for RLS in cloudsync_changes virtual table
if the cloudsync_col_value stmt returns 0 rows, it means that the value from the original table related to the current row in the augmented table is not found because of a RLS restriction, otherwise it always returns one value (maybe null)
1 parent 2697de3 commit dc2cc4f

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

src/cloudsync.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ void cloudsync_col_value (sqlite3_context *context, int argc, sqlite3_value **ar
22472247
rc = sqlite3_step(vm);
22482248
if (rc == SQLITE_DONE) {
22492249
rc = SQLITE_OK;
2250-
sqlite3_result_null(context);
2250+
sqlite3_result_text(context, CLOUDSYNC_RLS_RESTRICTED_VALUE, -1, SQLITE_STATIC);
22512251
} else if (rc == SQLITE_ROW) {
22522252
// store value result
22532253
rc = SQLITE_OK;

src/cloudsync.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#define CLOUDSYNC_VERSION "0.7.1"
2020
#define CLOUDSYNC_TOMBSTONE_VALUE "__[RIP]__"
21+
#define CLOUDSYNC_RLS_RESTRICTED_VALUE "__[RLS]__"
2122
#define CLOUDSYNC_DISABLE_ROWIDONLY_TABLES 1
2223

2324
typedef struct cloudsync_context cloudsync_context;

src/vtab.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ char *build_changes_sql (sqlite3 *db, const char *idxs) {
127127
* cloud sync tables, filtered and ordered based on the `db_version` and
128128
* `seq` fields.
129129
*/
130+
131+
// tbl,pk,col_name,col_value,col_version,db_version,site_id,cl,seq
130132

131133
const char *query =
132134
"WITH table_names AS ( "
@@ -148,7 +150,7 @@ char *build_changes_sql (sqlite3 *db, const char *idxs) {
148150
" COALESCE(t2.col_version, 1) AS cl "
149151
" FROM \"' || \"table_meta\" || '\" AS t1 "
150152
" LEFT JOIN cloudsync_site_id AS site_tbl ON t1.site_id = site_tbl.rowid "
151-
" LEFT JOIN \"' || \"table_meta\" || '\" AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "''' "
153+
" LEFT JOIN \"' || \"table_meta\" || '\" AS t2 ON t1.pk = t2.pk AND t2.col_name = ''" CLOUDSYNC_TOMBSTONE_VALUE "'' WHERE col_value != ''" CLOUDSYNC_RLS_RESTRICTED_VALUE "''' "
152154
" AS query_string FROM table_names "
153155
"), "
154156
"union_query AS ( "

0 commit comments

Comments
 (0)