2222#include "pk.h"
2323#include "dbutils.h"
2424#include "cloudsync.h"
25+ #include "cloudsync_private.h"
2526
2627// declared only if macro CLOUDSYNC_UNITTEST is defined
2728extern char * OUT_OF_MEMORY_BUFFER ;
@@ -337,39 +338,52 @@ bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_
337338 * xdata = s ;
338339 }
339340
341+ // extract context info
342+ int64_t colname_len = 0 ;
343+ char * colname = cloudsync_pk_context_colname (d , & colname_len );
344+
345+ int64_t tbl_len = 0 ;
346+ char * tbl = cloudsync_pk_context_tbl (d , & tbl_len );
347+
348+ int64_t pk_len = 0 ;
349+ void * pk = cloudsync_pk_context_pk (d , & pk_len );
350+
351+ int64_t cl = cloudsync_pk_context_cl (d );
352+ int64_t db_version = cloudsync_pk_context_dbversion (d );
353+
340354 switch (step ) {
341355 case CLOUDSYNC_PAYLOAD_APPLY_WILL_APPLY : {
342356 // if the tbl name or the prikey has changed, then verify if the row is valid
343357 // must use strncmp because strings in xdata are not zero-terminated
344- bool tbl_changed = (s -> last_tbl && (strlen (s -> last_tbl ) != (size_t )d -> tbl_len || strncmp (s -> last_tbl , d -> tbl , (size_t )d -> tbl_len ) != 0 ));
345- bool pk_changed = (s -> last_pk && d -> pk && cloudsync_blob_compare (s -> last_pk , s -> last_pk_len , d -> pk , d -> pk_len ) != 0 );
358+ bool tbl_changed = (s -> last_tbl && (strlen (s -> last_tbl ) != (size_t )tbl_len || strncmp (s -> last_tbl , tbl , (size_t )tbl_len ) != 0 ));
359+ bool pk_changed = (s -> last_pk && pk && cloudsync_blob_compare (s -> last_pk , s -> last_pk_len , pk , pk_len ) != 0 );
346360 if (s -> is_approved
347361 && !s -> last_is_delete
348362 && (tbl_changed || pk_changed )) {
349363 s -> is_approved = unittest_validate_changed_row (db , data , s -> last_tbl , s -> last_pk , s -> last_pk_len );
350364 }
351365
352- s -> last_is_delete = ((size_t )d -> col_name_len == strlen (CLOUDSYNC_TOMBSTONE_VALUE ) &&
353- strncmp (d -> col_name , CLOUDSYNC_TOMBSTONE_VALUE , (size_t )d -> col_name_len ) == 0
354- ) && d -> cl % 2 == 0 ;
366+ s -> last_is_delete = ((size_t )colname_len == strlen (CLOUDSYNC_TOMBSTONE_VALUE ) &&
367+ strncmp (colname , CLOUDSYNC_TOMBSTONE_VALUE , (size_t )colname_len ) == 0
368+ ) && cl % 2 == 0 ;
355369
356370 // update the last_tbl value, if needed
357371 if (!s -> last_tbl ||
358- !d -> tbl ||
359- (strlen (s -> last_tbl ) != (size_t )d -> tbl_len ) ||
360- strncmp (s -> last_tbl , d -> tbl , (size_t )d -> tbl_len ) != 0 ) {
372+ !tbl ||
373+ (strlen (s -> last_tbl ) != (size_t )tbl_len ) ||
374+ strncmp (s -> last_tbl , tbl , (size_t )tbl_len ) != 0 ) {
361375 if (s -> last_tbl ) cloudsync_memory_free (s -> last_tbl );
362- if (d -> tbl && d -> tbl_len > 0 ) s -> last_tbl = cloudsync_string_ndup (d -> tbl , d -> tbl_len , false);
376+ if (tbl && tbl_len > 0 ) s -> last_tbl = cloudsync_string_ndup (tbl , tbl_len , false);
363377 else s -> last_tbl = NULL ;
364378 }
365379
366380 // update the last_prikey and len values, if needed
367- if (!s -> last_pk || !d -> pk || cloudsync_blob_compare (s -> last_pk , s -> last_pk_len , d -> pk , d -> pk_len ) != 0 ) {
381+ if (!s -> last_pk || !pk || cloudsync_blob_compare (s -> last_pk , s -> last_pk_len , pk , pk_len ) != 0 ) {
368382 if (s -> last_pk ) cloudsync_memory_free (s -> last_pk );
369- if (d -> pk && d -> pk_len > 0 ) {
370- s -> last_pk = cloudsync_memory_alloc (d -> pk_len );
371- memcpy (s -> last_pk , d -> pk , d -> pk_len );
372- s -> last_pk_len = d -> pk_len ;
383+ if (pk && pk_len > 0 ) {
384+ s -> last_pk = cloudsync_memory_alloc (pk_len );
385+ memcpy (s -> last_pk , pk , pk_len );
386+ s -> last_pk_len = pk_len ;
373387 } else {
374388 s -> last_pk = NULL ;
375389 s -> last_pk_len = 0 ;
@@ -378,12 +392,12 @@ bool unittest_payload_apply_rls_callback(void **xdata, cloudsync_pk_decode_bind_
378392
379393 // commit the previous transaction, if any
380394 // begin new transacion, if needed
381- if (s -> last_db_version != d -> db_version ) {
395+ if (s -> last_db_version != db_version ) {
382396 rc = unittest_payload_apply_reset_transaction (db , s , true);
383397 if (rc != SQLITE_OK ) printf ("unittest_payload_apply error in reset_transaction: (%d) %s\n" , rc , sqlite3_errmsg (db ));
384398
385399 // reset local variables
386- s -> last_db_version = d -> db_version ;
400+ s -> last_db_version = db_version ;
387401 s -> is_approved = true;
388402 }
389403 break ;
0 commit comments