@@ -621,9 +621,7 @@ void cloudsync_network_init (sqlite3_context *context, int argc, sqlite3_value *
621621 }
622622}
623623
624- void cloudsync_network_cleanup (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
625- DEBUG_FUNCTION ("cloudsync_network_cleanup" );
626-
624+ void cloudsync_network_cleanup_internal (sqlite3_context * context ) {
627625 network_data * data = (network_data * )cloudsync_get_auxdata (context );
628626 if (data ) {
629627 if (data -> authentication ) cloudsync_memory_free (data -> authentication );
@@ -639,6 +637,98 @@ void cloudsync_network_cleanup (sqlite3_context *context, int argc, sqlite3_valu
639637 #endif
640638}
641639
640+ void cloudsync_network_cleanup (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
641+ DEBUG_FUNCTION ("cloudsync_network_cleanup" );
642+
643+ cloudsync_network_cleanup_internal (context );
644+ }
645+
646+ /**
647+ * Cleanup all local data from cloudsync-enabled tables, so the database can be safely reused
648+ * by another user without exposing any data from the previous session.
649+ *
650+ * Warning: this function deletes all data from the tables. Use with caution.
651+ */
652+ void cloudsync_network_logout (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
653+ bool completed = false;
654+ char * errmsg = NULL ;
655+ sqlite3 * db = sqlite3_context_db_handle (context );
656+
657+ // if the network layer is enabled, remove the token or apikey
658+ sqlite3_exec (db , "SELECT cloudsync_network_set_token('');" , NULL , NULL , NULL );
659+
660+ // get the list of cloudsync-enabled tables
661+ char * sql = "SELECT tbl_name, key, value FROM cloudsync_table_settings;" ;
662+ char * * result = NULL ;
663+ int nrows , ncols ;
664+ int rc = sqlite3_get_table (db , sql , & result , & nrows , & ncols , NULL );
665+ if (rc != SQLITE_OK ) {
666+ errmsg = cloudsync_memory_mprintf ("Unable to get current cloudsync configuration. %s" , sqlite3_errmsg (db ));
667+ goto finalize ;
668+ }
669+
670+ // run everything in a savepoint
671+ rc = sqlite3_exec (db , "SAVEPOINT cloudsync_logout_sp;" , NULL , NULL , NULL );
672+ if (rc != SQLITE_OK ) {
673+ errmsg = cloudsync_memory_mprintf ("Unable to create cloudsync_logout savepoint. %s" , sqlite3_errmsg (db ));
674+ return ;
675+ }
676+
677+ // cleanup the network init configuration
678+ // the user must call cloudsync_network_init the next time a sync is needed
679+ cloudsync_network_cleanup_internal (context );
680+
681+ // disable cloudsync for all the previously enabled tables: cloudsync_cleanup('*')
682+ rc = sqlite3_exec (db , "SELECT cloudsync_cleanup('*')" , NULL , NULL , NULL );
683+ if (rc != SQLITE_OK ) {
684+ errmsg = cloudsync_memory_mprintf ("Unable to cleanup current cloudsync configuration. %s" , sqlite3_errmsg (db ));
685+ goto finalize ;
686+ }
687+
688+ // delete all the local data for each previously enabled table
689+ // re-enable cloudsync on previously enabled tables
690+ for (int i = 1 ; i <= nrows ; i ++ ) {
691+ char * tbl_name = result [i * ncols + 0 ];
692+ char * key = result [i * ncols + 1 ];
693+ char * value = result [i * ncols + 2 ];
694+
695+ if (strcmp (key , "algo" ) != 0 ) continue ;
696+
697+ sql = cloudsync_memory_mprintf ("DELETE FROM \"%w\";" , tbl_name );
698+ rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
699+ cloudsync_memory_free (sql );
700+ if (rc != SQLITE_OK ) {
701+ errmsg = cloudsync_memory_mprintf ("Unable to delete data from table %s. %s" , tbl_name , sqlite3_errmsg (db ));
702+ goto finalize ;
703+ }
704+
705+ sql = cloudsync_memory_mprintf ("SELECT cloudsync_init('%q', '%q', 1);" , tbl_name , value );
706+ rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
707+ cloudsync_memory_free (sql );
708+ if (rc != SQLITE_OK ) {
709+ errmsg = cloudsync_memory_mprintf ("Unable to enable cloudsync on table %s. %s" , tbl_name , sqlite3_errmsg (db ));
710+ goto finalize ;
711+ }
712+ }
713+
714+ completed = true;
715+
716+ finalize :
717+ if (completed ) {
718+ sqlite3_exec (db , "RELEASE cloudsync_logout_sp;" , NULL , NULL , NULL );
719+ } else {
720+ // cleanup:
721+ // ROLLBACK TO command reverts the state of the database back to what it was just after the corresponding SAVEPOINT
722+ // then RELEASE to remove the SAVEPOINT from the transaction stack
723+ sqlite3_exec (db , "ROLLBACK TO cloudsync_logout_sp;" , NULL , NULL , NULL );
724+ sqlite3_exec (db , "RELEASE cloudsync_logout_sp;" , NULL , NULL , NULL );
725+ sqlite3_result_error (context , errmsg , -1 );
726+ sqlite3_result_error_code (context , rc );
727+ }
728+ sqlite3_free_table (result );
729+ cloudsync_memory_free (errmsg );
730+ }
731+
642732// MARK: - Public -
643733
644734bool cloudsync_network_set_authentication_token (sqlite3_context * context , const char * value , bool is_token ) {
@@ -845,88 +935,6 @@ void cloudsync_network_reset_sync_version (sqlite3_context *context, int argc, s
845935 dbutils_settings_set_key_value (db , context , CLOUDSYNC_KEY_SEND_SEQ , buf );
846936}
847937
848- /**
849- * Cleanup all local data from cloudsync-enabled tables, so the database can be safely reused
850- * by another user without exposing any data from the previous session.
851- *
852- * Warning: this function deletes all data from the tables. Use with caution.
853- */
854- void cloudsync_network_logout (sqlite3_context * context , int argc , sqlite3_value * * argv ) {
855- bool completed = false;
856- char * errmsg = NULL ;
857- sqlite3 * db = sqlite3_context_db_handle (context );
858-
859- // if the network layer is enabled, remove the token or apikey
860- sqlite3_exec (db , "SELECT cloudsync_network_set_token('');" , NULL , NULL , NULL );
861-
862- // get the list of cloudsync-enabled tables
863- char * sql = "SELECT tbl_name, key, value FROM cloudsync_table_settings;" ;
864- char * * result = NULL ;
865- int nrows , ncols ;
866- int rc = sqlite3_get_table (db , sql , & result , & nrows , & ncols , NULL );
867- if (rc != SQLITE_OK ) {
868- errmsg = cloudsync_memory_mprintf ("Unable to get current cloudsync configuration. %s" , sqlite3_errmsg (db ));
869- goto finalize ;
870- }
871-
872- // run everything in a savepoint
873- rc = sqlite3_exec (db , "SAVEPOINT cloudsync_logout_sp;" , NULL , NULL , NULL );
874- if (rc != SQLITE_OK ) {
875- errmsg = cloudsync_memory_mprintf ("Unable to create cloudsync_logout savepoint. %s" , sqlite3_errmsg (db ));
876- return ;
877- }
878-
879- // disable cloudsync for all the previously enabled tables: cloudsync_cleanup('*')
880- rc = sqlite3_exec (db , "SELECT cloudsync_cleanup('*')" , NULL , NULL , NULL );
881- if (rc != SQLITE_OK ) {
882- errmsg = cloudsync_memory_mprintf ("Unable to cleanup current cloudsync configuration. %s" , sqlite3_errmsg (db ));
883- goto finalize ;
884- }
885-
886- // delete all the local data for each previously enabled table
887- // re-enable cloudsync on previously enabled tables
888- for (int i = 1 ; i <= nrows ; i ++ ) {
889- char * tbl_name = result [i * ncols + 0 ];
890- char * key = result [i * ncols + 1 ];
891- char * value = result [i * ncols + 2 ];
892-
893- if (strcmp (key , "algo" ) != 0 ) continue ;
894-
895- sql = cloudsync_memory_mprintf ("DELETE FROM \"%w\";" , tbl_name );
896- rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
897- cloudsync_memory_free (sql );
898- if (rc != SQLITE_OK ) {
899- errmsg = cloudsync_memory_mprintf ("Unable to delete data from table %s. %s" , tbl_name , sqlite3_errmsg (db ));
900- goto finalize ;
901- }
902-
903- sql = cloudsync_memory_mprintf ("SELECT cloudsync_init('%q', '%q', 1);" , tbl_name , value );
904- rc = sqlite3_exec (db , sql , NULL , NULL , NULL );
905- cloudsync_memory_free (sql );
906- if (rc != SQLITE_OK ) {
907- errmsg = cloudsync_memory_mprintf ("Unable to enable cloudsync on table %s. %s" , tbl_name , sqlite3_errmsg (db ));
908- goto finalize ;
909- }
910- }
911-
912- completed = true;
913-
914- finalize :
915- if (completed ) {
916- sqlite3_exec (db , "RELEASE cloudsync_logout_sp;" , NULL , NULL , NULL );
917- } else {
918- // cleanup:
919- // ROLLBACK TO command reverts the state of the database back to what it was just after the corresponding SAVEPOINT
920- // then RELEASE to remove the SAVEPOINT from the transaction stack
921- sqlite3_exec (db , "ROLLBACK TO cloudsync_logout_sp;" , NULL , NULL , NULL );
922- sqlite3_exec (db , "RELEASE cloudsync_logout_sp;" , NULL , NULL , NULL );
923- sqlite3_result_error (context , errmsg , -1 );
924- sqlite3_result_error_code (context , rc );
925- }
926- sqlite3_free_table (result );
927- cloudsync_memory_free (errmsg );
928- }
929-
930938// MARK: -
931939
932940int cloudsync_network_register (sqlite3 * db , char * * pzErrMsg , void * ctx ) {
0 commit comments