Skip to content

Commit 9979cc7

Browse files
committed
Updated static statements in cloudsync.c (WP 1)
1 parent 623be6b commit 9979cc7

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

src/cloudsync.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "cloudsync_private.h"
2121
#include "lz4.h"
2222
#include "pk.h"
23+
#include "sql.h"
2324
#include "utils.h"
2425
#include "dbutils.h"
2526

@@ -325,22 +326,9 @@ char *cloudsync_dbversion_build_query (db_t *db) {
325326
*/
326327

327328
// the good news is that the query can be computed in SQLite without the need to do any extra computation from the host language
328-
const char *sql = "WITH table_names AS ("
329-
"SELECT format('%w', name) as tbl_name "
330-
"FROM sqlite_master "
331-
"WHERE type='table' "
332-
"AND name LIKE '%_cloudsync'"
333-
"), "
334-
"query_parts AS ("
335-
"SELECT 'SELECT max(db_version) as version FROM \"' || tbl_name || '\"' as part FROM table_names"
336-
"), "
337-
"combined_query AS ("
338-
"SELECT GROUP_CONCAT(part, ' UNION ALL ') || ' UNION SELECT value as version FROM cloudsync_settings WHERE key = ''pre_alter_dbversion''' as full_query FROM query_parts"
339-
") "
340-
"SELECT 'SELECT max(version) as version FROM (' || full_query || ');' FROM combined_query;";
341329

342330
char *value = NULL;
343-
int rc = database_select_text(db, sql, &value);
331+
int rc = database_select_text(db, SQL_DBVERSION_BUILD_QUERY, &value);
344332
return (rc == DBRES_OK) ? value : NULL;
345333
}
346334

@@ -447,7 +435,7 @@ int cloudsync_load_siteid (db_t *db, cloudsync_context *data) {
447435
// load site_id
448436
char *buffer = NULL;
449437
db_int64 size = 0;
450-
int rc = database_select_blob(db, "SELECT site_id FROM cloudsync_site_id WHERE rowid=0;", &buffer, &size);
438+
int rc = database_select_blob(db, SQL_SITEID_SELECT_ROWID0, &buffer, &size);
451439
if (rc != DBRES_OK) return rc;
452440
if (!buffer || size != UUID_LEN) {
453441
if (buffer) cloudsync_memory_free(buffer);
@@ -482,16 +470,14 @@ int cloudsync_add_dbvms (db_t *db, cloudsync_context *data) {
482470
DEBUG_DBFUNCTION("cloudsync_add_stmts");
483471

484472
if (data->data_version_stmt == NULL) {
485-
const char *sql = "PRAGMA data_version;";
486-
int rc = database_prepare(db, sql, (void **)&data->data_version_stmt, DBFLAG_PERSISTENT);
473+
int rc = database_prepare(db, SQL_DATA_VERSION, (void **)&data->data_version_stmt, DBFLAG_PERSISTENT);
487474
DEBUG_STMT("data_version_stmt %p", data->data_version_stmt);
488475
if (rc != DBRES_OK) return rc;
489476
DEBUG_SQL("data_version_stmt: %s", sql);
490477
}
491478

492479
if (data->schema_version_stmt == NULL) {
493-
const char *sql = "PRAGMA schema_version;";
494-
int rc = database_prepare(db, sql, (void **)&data->schema_version_stmt, DBFLAG_PERSISTENT);
480+
int rc = database_prepare(db, SQL_SCHEMA_VERSION, (void **)&data->schema_version_stmt, DBFLAG_PERSISTENT);
495481
DEBUG_STMT("schema_version_stmt %p", data->schema_version_stmt);
496482
if (rc != DBRES_OK) return rc;
497483
DEBUG_SQL("schema_version_stmt: %s", sql);
@@ -501,8 +487,7 @@ int cloudsync_add_dbvms (db_t *db, cloudsync_context *data) {
501487
// get and set index of the site_id
502488
// in SQLite, we can’t directly combine an INSERT and a SELECT to both insert a row and return an identifier (rowid) in a single statement,
503489
// however, we can use a workaround by leveraging the INSERT statement with ON CONFLICT DO UPDATE and then combining it with RETURNING rowid
504-
const char *sql = "INSERT INTO cloudsync_site_id (site_id) VALUES (?) ON CONFLICT(site_id) DO UPDATE SET site_id = site_id RETURNING rowid;";
505-
int rc = database_prepare(db, sql, (void **)&data->getset_siteid_stmt, DBFLAG_PERSISTENT);
490+
int rc = database_prepare(db, SQL_SITEID_GETSET_ROWID_BY_SITEID, (void **)&data->getset_siteid_stmt, DBFLAG_PERSISTENT);
506491
DEBUG_STMT("getset_siteid_stmt %p", data->getset_siteid_stmt);
507492
if (rc != DBRES_OK) return rc;
508493
DEBUG_SQL("getset_siteid_stmt: %s", sql);

src/database_sqlite.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "database.h"
1010
#include "dbutils.h"
1111
#include "utils.h"
12+
#include "sql.h"
1213

1314
#include <string.h>
1415
#include <stdlib.h>
@@ -513,7 +514,7 @@ int database_delete_triggers (db_t *db, const char *table) {
513514

514515
db_int64 database_schema_version (db_t *db) {
515516
db_int64 value = 0;
516-
int rc = database_select_int(db, "PRAGMA schema_version;", &value);
517+
int rc = database_select_int(db, SQL_SCHEMA_VERSION, &value);
517518
return (rc == DBRES_OK) ? value : 0;
518519
}
519520

src/sql.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,11 @@ extern const char * const SQL_CREATE_TABLE_SETTINGS_TABLE;
2828
extern const char * const SQL_CREATE_SCHEMA_VERSIONS_TABLE;
2929
extern const char * const SQL_SETTINGS_CLEANUP_DROP_ALL;
3030

31+
// CLOUDSYNC
32+
extern const char * const SQL_DBVERSION_BUILD_QUERY;
33+
extern const char * const SQL_SITEID_SELECT_ROWID0;
34+
extern const char * const SQL_DATA_VERSION;
35+
extern const char * const SQL_SCHEMA_VERSION;
36+
extern const char * const SQL_SITEID_GETSET_ROWID_BY_SITEID;
37+
3138
#endif

src/sql_sqlite.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,33 @@ const char * const SQL_SETTINGS_CLEANUP_DROP_ALL =
6767
"DROP TABLE IF EXISTS cloudsync_table_settings; "
6868
"DROP TABLE IF EXISTS cloudsync_schema_versions; ";
6969

70+
// MARK: - CloudSync -
71+
72+
const char * const SQL_DBVERSION_BUILD_QUERY =
73+
"WITH table_names AS ("
74+
"SELECT format('%w', name) as tbl_name "
75+
"FROM sqlite_master "
76+
"WHERE type='table' "
77+
"AND name LIKE '%_cloudsync'"
78+
"), "
79+
"query_parts AS ("
80+
"SELECT 'SELECT max(db_version) as version FROM \"' || tbl_name || '\"' as part FROM table_names"
81+
"), "
82+
"combined_query AS ("
83+
"SELECT GROUP_CONCAT(part, ' UNION ALL ') || ' UNION SELECT value as version FROM cloudsync_settings WHERE key = ''pre_alter_dbversion''' as full_query FROM query_parts"
84+
") "
85+
"SELECT 'SELECT max(version) as version FROM (' || full_query || ');' FROM combined_query;";
86+
87+
const char * const SQL_SITEID_SELECT_ROWID0 =
88+
"SELECT site_id FROM cloudsync_site_id WHERE rowid=0;";
89+
90+
const char * const SQL_DATA_VERSION =
91+
"PRAGMA data_version;";
92+
93+
const char * const SQL_SCHEMA_VERSION =
94+
"PRAGMA schema_version;";
95+
96+
const char * const SQL_SITEID_GETSET_ROWID_BY_SITEID =
97+
"INSERT INTO cloudsync_site_id (site_id) VALUES (?) "
98+
"ON CONFLICT(site_id) DO UPDATE SET site_id = site_id "
99+
"RETURNING rowid;";

0 commit comments

Comments
 (0)