@@ -23,7 +23,7 @@ const SIMPLE_NAME: &CStr = c"powersync_crud";
2323
2424// Structure:
2525// CREATE TABLE powersync_crud_(data TEXT, options INT HIDDEN);
26- // CREATE TABLE powersync_crud(op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT);
26+ // CREATE TABLE powersync_crud(op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT, options INT HIDDEN );
2727//
2828// This is a insert-only virtual table. It generates transaction ids in ps_tx, and inserts data in
2929// ps_crud(tx_id, data).
@@ -98,11 +98,24 @@ impl VirtualTable {
9898 set_updated_rows,
9999 update_local_bucket,
100100 } => {
101- // Columns are (op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT)
102- let metadata = args[ 5 ] ;
101+ // Columns are (op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT, options INT HIDDEN)
102+ let flags = match args[ 6 ] . value_type ( ) {
103+ sqlite_nostd:: ColumnType :: Null => TableInfoFlags :: default ( ) ,
104+ _ => TableInfoFlags ( args[ 1 ] . int ( ) as u32 ) ,
105+ } ;
103106 let op = args[ 0 ] . text ( ) ;
104107 let id = args[ 1 ] . text ( ) ;
105108 let row_type = args[ 2 ] . text ( ) ;
109+ let metadata = args[ 5 ] ;
110+ let data = Self :: value_to_json ( & args[ 3 ] ) ;
111+
112+ if flags. include_old_only_when_changed ( )
113+ && op == "PATCH"
114+ && data. map ( |r| r. get ( ) ) == Some ( "{}" )
115+ {
116+ // Ignore this empty update
117+ return Ok ( ( ) ) ;
118+ }
106119
107120 #[ derive( Serialize ) ]
108121 struct CrudEntry < ' a > {
@@ -121,11 +134,12 @@ impl VirtualTable {
121134 // First, we insert into ps_crud like the manual vtab would too. We have to create
122135 // the JSON out of the individual components for that.
123136 stmt. bind_int64 ( 1 , current_tx. tx_id ) ?;
137+
124138 let serialized = serde_json:: to_string ( & CrudEntry {
125139 op,
126140 id,
127141 row_type,
128- data : Self :: value_to_json ( & args [ 3 ] ) ,
142+ data : data ,
129143 old : Self :: value_to_json ( & args[ 4 ] ) ,
130144 metadata : if metadata. value_type ( ) == ColumnType :: Text {
131145 Some ( metadata. text ( ) )
@@ -214,7 +228,7 @@ extern "C" fn connect(
214228 let is_simple = name == SIMPLE_NAME ;
215229
216230 let sql = if is_simple {
217- "CREATE TABLE powersync_crud(op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT);"
231+ "CREATE TABLE powersync_crud(op TEXT, id TEXT, type TEXT, data TEXT, old_values TEXT, metadata TEXT, options INT HIDDEN );"
218232 } else {
219233 "CREATE TABLE powersync_crud_(data TEXT, options INT HIDDEN);"
220234 } ;
0 commit comments