@@ -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
271237static 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
835805bool 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
839813bool 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 -
0 commit comments