Skip to content

Commit 2ccdcfe

Browse files
committed
Added new database_internal_table_exists function to make sure to check for system tables in the public schema (PG only)
1 parent 6e689cc commit 2ccdcfe

File tree

6 files changed

+25
-46
lines changed

6 files changed

+25
-46
lines changed

src/cloudsync.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ int merge_insert (cloudsync_context *data, cloudsync_table_context *table, const
14701470
// MARK: - Private -
14711471

14721472
bool cloudsync_config_exists (cloudsync_context *data) {
1473-
return database_table_exists(data, CLOUDSYNC_SITEID_NAME) == true;
1473+
return database_internal_table_exists(data, CLOUDSYNC_SITEID_NAME) == true;
14741474
}
14751475

14761476
cloudsync_context *cloudsync_context_create (void *db) {
@@ -1518,7 +1518,7 @@ const char *cloudsync_context_init (cloudsync_context *data) {
15181518
// The data->site_id value could exists while settings tables don't exists if the
15191519
// cloudsync_context_init was previously called in init transaction that was rolled back
15201520
// because of an error during the init process.
1521-
if (data->site_id[0] == 0 || !database_table_exists(data, CLOUDSYNC_SITEID_NAME)) {
1521+
if (data->site_id[0] == 0 || !database_internal_table_exists(data, CLOUDSYNC_SITEID_NAME)) {
15221522
if (dbutils_settings_init(data) != DBRES_OK) return NULL;
15231523
if (cloudsync_add_dbvms(data) != DBRES_OK) return NULL;
15241524
if (cloudsync_load_siteid(data) != DBRES_OK) return NULL;
@@ -2595,7 +2595,7 @@ int cloudsync_cleanup (cloudsync_context *data, const char *table_name) {
25952595
cloudsync_reset_siteid(data);
25962596
dbutils_settings_cleanup(data);
25972597
} else {
2598-
if (database_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == true) {
2598+
if (database_internal_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == true) {
25992599
cloudsync_update_schema_hash(data);
26002600
}
26012601
}

src/database.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ int database_select_blob (cloudsync_context *data, const char *sql, char **valu
6767
int database_select_blob_2int (cloudsync_context *data, const char *sql, char **value, int64_t *value_len, int64_t *value2, int64_t *value3);
6868
int database_write (cloudsync_context *data, const char *sql, const char **values, DBTYPE types[], int lens[], int count);
6969
bool database_table_exists (cloudsync_context *data, const char *table_name);
70+
bool database_internal_table_exists (cloudsync_context *data, const char *name);
7071
bool database_trigger_exists (cloudsync_context *data, const char *table_name);
7172
int database_create_metatable (cloudsync_context *data, const char *table_name);
7273
int database_create_triggers (cloudsync_context *data, const char *table_name, table_algo algo);

src/dbutils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ int dbutils_settings_init (cloudsync_context *data) {
394394

395395
// check if cloudsync_settings table exists
396396
int rc = DBRES_OK;
397-
bool settings_exists = database_table_exists(data, CLOUDSYNC_SETTINGS_NAME);
397+
bool settings_exists = database_internal_table_exists(data, CLOUDSYNC_SETTINGS_NAME);
398398
if (settings_exists == false) {
399399
DEBUG_SETTINGS("cloudsync_settings does not exist (creating a new one)");
400400

@@ -414,7 +414,7 @@ int dbutils_settings_init (cloudsync_context *data) {
414414
if (rc != DBRES_OK) return rc;
415415
}
416416

417-
if (database_table_exists(data, CLOUDSYNC_SITEID_NAME) == false) {
417+
if (database_internal_table_exists(data, CLOUDSYNC_SITEID_NAME) == false) {
418418
DEBUG_SETTINGS("cloudsync_site_id does not exist (creating a new one)");
419419

420420
// create table and fill-in initial data
@@ -436,15 +436,15 @@ int dbutils_settings_init (cloudsync_context *data) {
436436
}
437437

438438
// check if cloudsync_table_settings table exists
439-
if (database_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == false) {
439+
if (database_internal_table_exists(data, CLOUDSYNC_TABLE_SETTINGS_NAME) == false) {
440440
DEBUG_SETTINGS("cloudsync_table_settings does not exist (creating a new one)");
441441

442442
rc = database_exec(data, SQL_CREATE_TABLE_SETTINGS_TABLE);
443443
if (rc != DBRES_OK) return rc;
444444
}
445445

446446
// check if cloudsync_settings table exists
447-
bool schema_versions_exists = database_table_exists(data, CLOUDSYNC_SCHEMA_VERSIONS_NAME);
447+
bool schema_versions_exists = database_internal_table_exists(data, CLOUDSYNC_SCHEMA_VERSIONS_NAME);
448448
if (schema_versions_exists == false) {
449449
DEBUG_SETTINGS("cloudsync_schema_versions does not exist (creating a new one)");
450450

src/postgresql/cloudsync_postgresql.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ Datum pg_cloudsync_set_schema (PG_FUNCTION_ARGS) {
15201520
// Only persist if settings table exists (it may not exist before cloudsync_init).
15211521
int spi_rc = SPI_connect();
15221522
if (spi_rc == SPI_OK_CONNECT) {
1523-
if (database_table_exists(data, "cloudsync_settings")) {
1523+
if (database_internal_table_exists(data, CLOUDSYNC_SETTINGS_NAME)) {
15241524
dbutils_settings_set_key_value(data, "schema", schema);
15251525
}
15261526
SPI_finish();

src/postgresql/database_postgresql.c

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,40 +233,6 @@ char *database_build_base_ref(const char *schema, const char *table_name) {
233233

234234
// MARK: - HELPER FUNCTIONS -
235235

236-
// TODO: is this really necessary? We now control the SQL statements and so we can use the Postgres style when needed
237-
// Convert SQLite-style ? placeholders to PostgreSQL-style $1, $2, etc.
238-
/*
239-
static char* convert_placeholders(const char *sql) {
240-
if (!sql) {
241-
return NULL;
242-
}
243-
244-
// Count placeholders
245-
int count = 0;
246-
for (const char *p = sql; *p; p++) {
247-
if (*p == '?') count++;
248-
}
249-
250-
// Allocate new string (worst case: $999 for each ? = 4 chars vs 1)
251-
size_t newlen = strlen(sql) + (count * 3) + 1;
252-
char *newsql = cloudsync_memory_alloc(newlen);
253-
254-
// Convert
255-
char *dst = newsql;
256-
int param_num = 1;
257-
for (const char *src = sql; *src; src++) {
258-
if (*src == '?') {
259-
dst += sprintf(dst, "$%d", param_num++);
260-
} else {
261-
*dst++ = *src;
262-
}
263-
}
264-
*dst = '\0';
265-
266-
return newsql;
267-
}
268-
*/
269-
270236
// Map SPI result codes to DBRES
271237
static int map_spi_result (int rc) {
272238
switch (rc) {
@@ -521,15 +487,19 @@ int database_select3_values (cloudsync_context *data, const char *sql, char **va
521487
return rc;
522488
}
523489

524-
bool database_system_exists (cloudsync_context *data, const char *name, const char *type) {
490+
static bool database_system_exists (cloudsync_context *data, const char *name, const char *type, bool force_public) {
525491
if (!name || !type) return false;
526492
cloudsync_reset_error(data);
527493

528494
bool exists = false;
529495
const char *query;
530496

531497
if (strcmp(type, "table") == 0) {
532-
query = "SELECT 1 FROM pg_tables WHERE schemaname = COALESCE(cloudsync_schema(), current_schema()) AND tablename = $1";
498+
if (force_public) {
499+
query = "SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = $1";
500+
} else {
501+
query = "SELECT 1 FROM pg_tables WHERE schemaname = COALESCE(cloudsync_schema(), current_schema()) AND tablename = $1";
502+
}
533503
} else if (strcmp(type, "trigger") == 0) {
534504
query = "SELECT 1 FROM pg_trigger WHERE tgname = $1";
535505
} else {
@@ -833,11 +803,15 @@ bool database_in_transaction (cloudsync_context *data) {
833803
}
834804

835805
bool database_table_exists (cloudsync_context *data, const char *name) {
836-
return database_system_exists(data, name, "table");
806+
return database_system_exists(data, name, "table", false);
807+
}
808+
809+
bool database_internal_table_exists (cloudsync_context *data, const char *name) {
810+
return database_system_exists(data, name, "table", true);
837811
}
838812

839813
bool database_trigger_exists (cloudsync_context *data, const char *name) {
840-
return database_system_exists(data, name, "trigger");
814+
return database_system_exists(data, name, "trigger", false);
841815
}
842816

843817
// MARK: - SCHEMA INFO -

src/sqlite/database_sqlite.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ bool database_table_exists (cloudsync_context *data, const char *name) {
396396
return database_system_exists(data, name, "table");
397397
}
398398

399+
bool database_internal_table_exists (cloudsync_context *data, const char *name) {
400+
return database_table_exists(data, name);
401+
}
402+
399403
bool database_trigger_exists (cloudsync_context *data, const char *name) {
400404
return database_system_exists(data, name, "trigger");
401405
}

0 commit comments

Comments
 (0)