Skip to content

Commit b937272

Browse files
committed
Merge branch 'main' into update-build-pipeline
2 parents 76c58f9 + cc35c4c commit b937272

File tree

6 files changed

+84
-5
lines changed

6 files changed

+84
-5
lines changed

src/cloudsync.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,6 +2991,17 @@ void cloudsync_commit_alter (sqlite3_context *context, int argc, sqlite3_value *
29912991
}
29922992
}
29932993

2994+
// MARK: -
2995+
2996+
void cloudsync_uuid (sqlite3_context *context, int argc, sqlite3_value **argv) {
2997+
DEBUG_FUNCTION("cloudsync_uuid");
2998+
2999+
char value[UUID_STR_MAXLEN];
3000+
char *uuid = cloudsync_uuid_v7_string(value, true);
3001+
sqlite3_result_text(context, uuid, -1, SQLITE_TRANSIENT);
3002+
}
3003+
3004+
29943005
// MARK: - Main Entrypoint -
29953006

29963007
APIEXPORT int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
@@ -3101,6 +3112,9 @@ APIEXPORT int sqlite3_cloudsync_init (sqlite3 *db, char **pzErrMsg, const sqlite
31013112
rc = dbutils_register_function(db, "cloudsync_commit_alter", cloudsync_commit_alter, 1, pzErrMsg, ctx, NULL);
31023113
if (rc != SQLITE_OK) return rc;
31033114

3115+
rc = dbutils_register_function(db, "cloudsync_uuid", cloudsync_uuid, 0, pzErrMsg, ctx, NULL);
3116+
if (rc != SQLITE_OK) return rc;
3117+
31043118
// NETWORK LAYER
31053119
#ifndef CLOUDSYNC_OMIT_NETWORK
31063120
rc = cloudsync_network_register(db, pzErrMsg, ctx);

src/dbutils.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,7 @@ int dbutils_update_schema_hash(sqlite3 *db, uint64_t *hash) {
10121012
if (hash && *hash == h) return SQLITE_CONSTRAINT;
10131013

10141014
char sql[1024];
1015-
//
1016-
snprintf(sql, sizeof(sql), "INSERT INTO %s (hash) VALUES (%lld);", CLOUDSYNC_SCHEMA_VERSIONS_NAME, h);
1015+
snprintf(sql, sizeof(sql), "INSERT INTO %s (hash) VALUES (%lld);", CLOUDSYNC_SCHEMA_VERSIONS_NAME, (sqlite3_int64)h);
10171016
int rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
10181017

10191018
if (hash) *hash = h;

src/network.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ int extract_query_param(const char *query, const char *key, char *output, size_t
334334
return -3; // Key not found
335335
}
336336

337-
bool network_compute_endpoints (sqlite3_context *context, network_data *data, const char *connection_string) {
337+
bool network_compute_endpoints (sqlite3_context *context, network_data *data, const char *conn_string) {
338338
// compute endpoints
339339
bool result = false;
340340

@@ -348,12 +348,16 @@ bool network_compute_endpoints (sqlite3_context *context, network_data *data, co
348348
char *check_endpoint = NULL;
349349
char *upload_endpoint = NULL;
350350

351+
char *conn_string_https = NULL;
352+
351353
CURLUcode rc = CURLUE_OUT_OF_MEMORY;
352354
CURLU *url = curl_url();
353355
if (!url) goto finalize;
354356

357+
conn_string_https = cloudsync_string_replace_prefix(conn_string, "sqlitecloud://", "https://");
358+
355359
// set URL: https://UUID.g5.sqlite.cloud:443/chinook.sqlite?apikey=hWDanFolRT9WDK0p54lufNrIyfgLZgtMw6tb6fbPmpo
356-
rc = curl_url_set(url, CURLUPART_URL, connection_string, 0);
360+
rc = curl_url_set(url, CURLUPART_URL, conn_string_https, 0);
357361
if (rc != CURLE_OK) goto finalize;
358362

359363
// https (MANDATORY)
@@ -421,6 +425,7 @@ bool network_compute_endpoints (sqlite3_context *context, network_data *data, co
421425
if (database) curl_free(database);
422426
if (query) curl_free(query);
423427
if (url) curl_url_cleanup(url);
428+
if (conn_string_https && conn_string_https != conn_string) cloudsync_memory_free(conn_string_https);
424429

425430
return result;
426431
}
@@ -658,6 +663,15 @@ void cloudsync_network_check_changes (sqlite3_context *context, int argc, sqlite
658663
cloudsync_network_check_internal(context, argc, argv);
659664
}
660665

666+
void cloudsync_network_reset_check_version (sqlite3_context *context, int argc, sqlite3_value **argv) {
667+
DEBUG_FUNCTION("cloudsync_network_reset_check_version");
668+
669+
sqlite3 *db = sqlite3_context_db_handle(context);
670+
char *buf = "0";
671+
dbutils_settings_set_key_value(db, context, CLOUDSYNC_KEY_CHECK_DBVERSION, buf);
672+
dbutils_settings_set_key_value(db, context, CLOUDSYNC_KEY_CHECK_SEQ, buf);
673+
}
674+
661675
// MARK: -
662676

663677
int cloudsync_network_register (sqlite3 *db, char **pzErrMsg, void *ctx) {
@@ -681,7 +695,7 @@ int cloudsync_network_register (sqlite3 *db, char **pzErrMsg, void *ctx) {
681695
rc = dbutils_register_function(db, "cloudsync_network_check_changes", cloudsync_network_check_changes, 0, pzErrMsg, ctx, NULL);
682696
if (rc != SQLITE_OK) return rc;
683697

684-
rc = dbutils_register_function(db, "cloudsync_network_check_changes_sync", cloudsync_network_check_changes_sync, 2, pzErrMsg, ctx, NULL);
698+
rc = dbutils_register_function(db, "cloudsync_network_reset_check_version", cloudsync_network_reset_check_version, 0, pzErrMsg, ctx, NULL);
685699
if (rc != SQLITE_OK) return rc;
686700

687701
return rc;

src/utils.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ void cloudsync_rowid_decode (sqlite3_int64 rowid, sqlite3_int64 *db_version, sql
165165
*db_version = (sqlite3_int64)(urowid >> 30);
166166
}
167167

168+
char *cloudsync_string_replace_prefix(const char *input, char *prefix, char *replacement) {
169+
//const char *prefix = "sqlitecloud://";
170+
//const char *replacement = "https://";
171+
size_t prefix_len = strlen(prefix);
172+
size_t replacement_len = strlen(replacement);
173+
174+
if (strncmp(input, prefix, prefix_len) == 0) {
175+
// Allocate memory for new string
176+
size_t input_len = strlen(input);
177+
size_t new_len = input_len - prefix_len + replacement_len;
178+
char *result = cloudsync_memory_alloc(new_len + 1); // +1 for null terminator
179+
if (!result) return NULL;
180+
181+
// Copy replacement and the rest of the input string
182+
strcpy(result, replacement);
183+
strcpy(result + replacement_len, input + prefix_len);
184+
return result;
185+
}
186+
187+
// If no match, return the original string
188+
return (char *)input;
189+
}
190+
168191
uint64_t fnv1a_hash(const char *data, size_t len) {
169192
uint64_t hash = FNV_OFFSET_BASIS;
170193
for (size_t i = 0; i < len; ++i) {

src/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ int cloudsync_uuid_v7 (uint8_t value[UUID_LEN]);
125125
int cloudsync_uuid_v7_compare (uint8_t value1[UUID_LEN], uint8_t value2[UUID_LEN]);
126126
char *cloudsync_uuid_v7_string (char value[UUID_STR_MAXLEN], bool dash_format);
127127
char *cloudsync_uuid_v7_stringify (uint8_t uuid[UUID_LEN], char value[UUID_STR_MAXLEN], bool dash_format);
128+
char *cloudsync_string_replace_prefix(const char *input, char *prefix, char *replacement);
128129
uint64_t fnv1a_hash(const char *data, size_t len);
129130

130131
void *cloudsync_memory_zeroalloc (uint64_t size);

test/unit.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,11 @@ bool do_test_functions (sqlite3 *db, bool print_results) {
829829
rc = sqlite3_exec(db, "SELECT cloudsync_cleanup('*');", NULL, NULL, NULL);
830830
if (rc != SQLITE_OK) goto abort_test_functions;
831831

832+
char *uuid = dbutils_text_select(db, "SELECT cloudsync_uuid();");
833+
if (uuid == NULL) goto abort_test_functions;
834+
if (print_results) printf("New uuid: %s\n", uuid);
835+
cloudsync_memory_free(uuid);
836+
832837
return true;
833838

834839
abort_test_functions:
@@ -1760,6 +1765,26 @@ bool do_test_internal_functions (void) {
17601765
return result;
17611766
}
17621767

1768+
bool do_test_string_replace_prefix(void) {
1769+
char *host = "rejfwkr.sqlite.cloud";
1770+
char *prefix = "sqlitecloud://";
1771+
char *replacement = "https://";
1772+
1773+
char string[512];
1774+
snprintf(string, sizeof(string), "%s%s", prefix, host);
1775+
char expected[512];
1776+
snprintf(expected, sizeof(expected), "%s%s", replacement, host);
1777+
1778+
char *replaced = cloudsync_string_replace_prefix(string, prefix, replacement);
1779+
if (string == replaced || strcmp(replaced, expected) != 0) return false;
1780+
if (string != replaced) cloudsync_memory_free(replaced);
1781+
1782+
replaced = cloudsync_string_replace_prefix(expected, prefix, replacement);
1783+
if (expected != replaced) return false;
1784+
1785+
return true;
1786+
}
1787+
17631788
// MARK: -
17641789

17651790
bool do_compare_queries (sqlite3 *db1, const char *sql1, sqlite3 *db2, const char *sql2, int col_to_skip, int col_tombstone, bool display_column) {
@@ -3081,6 +3106,9 @@ int main(int argc, const char * argv[]) {
30813106
result += test_report("Functions Test:", do_test_functions(db, print_result));
30823107
result += test_report("Functions Test (Int):", do_test_internal_functions());
30833108

3109+
result = do_test_string_replace_prefix();
3110+
printf("%-24s %s\n", "String Func Test:", (result) ? "OK" : "FAILED");
3111+
30843112
// close local database
30853113
db = close_db(db);
30863114

0 commit comments

Comments
 (0)