Skip to content

Commit d4c15c5

Browse files
committed
Simplified cloudsync_init_all
1 parent ea47957 commit d4c15c5

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/cloudsync.c

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ typedef struct {
104104
int index;
105105
} cloudsync_pk_decode_context;
106106

107-
typedef struct {
108-
sqlite3_context *context;
109-
bool skip_int_pk_check;
110-
} cloudsync_init_all_context;
111-
112107
#define SYNCBIT_SET(_data) _data->insync = 1
113108
#define SYNCBIT_RESET(_data) _data->insync = 0
114109
#define BUMP_SEQ(_data) ((_data)->seq += 1, (_data)->seq - 1)
@@ -2927,32 +2922,29 @@ int cloudsync_init_internal (sqlite3_context *context, const char *table_name, c
29272922
return SQLITE_OK;
29282923
}
29292924

2930-
int cloudsync_init_all_callback (void *xdata, int ncols, char **values, char **names) {
2931-
cloudsync_init_all_context *init_ctx = (cloudsync_init_all_context *)xdata;
2932-
sqlite3_context *context = init_ctx->context;
2933-
bool skip_int_pk_check = init_ctx->skip_int_pk_check;
2934-
2935-
for (int i=0; i<ncols; i+=2) {
2936-
const char *table = values[i];
2937-
const char *algo = values[i+1];
2938-
2939-
int rc = cloudsync_init_internal(context, table, algo, skip_int_pk_check);
2940-
if (rc != SQLITE_OK) {
2941-
cloudsync_cleanup_internal(context, table);
2942-
return rc;
2943-
}
2944-
}
2945-
2946-
return SQLITE_OK;
2947-
}
2948-
29492925
int cloudsync_init_all (sqlite3_context *context, const char *algo_name, bool skip_int_pk_check) {
29502926
char sql[1024];
29512927
snprintf(sql, sizeof(sql), "SELECT name, '%s' FROM sqlite_master WHERE type='table' and name NOT LIKE 'sqlite_%%' AND name NOT LIKE 'cloudsync_%%' AND name NOT LIKE '%%_cloudsync';", (algo_name) ? algo_name : CLOUDSYNC_DEFAULT_ALGO);
29522928

29532929
sqlite3 *db = sqlite3_context_db_handle(context);
2954-
cloudsync_init_all_context init_ctx = {.context = context, .skip_int_pk_check = skip_int_pk_check};
2955-
int rc = sqlite3_exec(db, sql, cloudsync_init_all_callback, &init_ctx, NULL);
2930+
sqlite3_stmt *vm = NULL;
2931+
int rc = sqlite3_prepare_v2(db, sql, -1, &vm, NULL);
2932+
if (rc != SQLITE_OK) goto abort_init_all;
2933+
2934+
while (1) {
2935+
rc = sqlite3_step(vm);
2936+
if (rc == SQLITE_DONE) break;
2937+
else if (rc != SQLITE_ROW) goto abort_init_all;
2938+
2939+
const char *table = (const char *)sqlite3_column_text(vm, 0);
2940+
const char *algo = (const char *)sqlite3_column_text(vm, 1);
2941+
rc = cloudsync_init_internal(context, table, algo, skip_int_pk_check);
2942+
if (rc != SQLITE_OK) {cloudsync_cleanup_internal(context, table); goto abort_init_all;}
2943+
}
2944+
rc = SQLITE_OK;
2945+
2946+
abort_init_all:
2947+
if (vm) sqlite3_finalize(vm);
29562948
return rc;
29572949
}
29582950

0 commit comments

Comments
 (0)