Skip to content

Commit 9b6e35d

Browse files
committed
network: add support for connection strings with sqlitecloud schema
1 parent b26e8ad commit 9b6e35d

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

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: 7 additions & 2 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
}

src/utils.c

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

154+
char *cloudsync_string_replace_prefix(const char *input, char *prefix, char *replacement) {
155+
//const char *prefix = "sqlitecloud://";
156+
//const char *replacement = "https://";
157+
size_t prefix_len = strlen(prefix);
158+
size_t replacement_len = strlen(replacement);
159+
160+
if (strncmp(input, prefix, prefix_len) == 0) {
161+
// Allocate memory for new string
162+
size_t input_len = strlen(input);
163+
size_t new_len = input_len - prefix_len + replacement_len;
164+
char *result = cloudsync_memory_alloc(new_len + 1); // +1 for null terminator
165+
if (!result) return NULL;
166+
167+
// Copy replacement and the rest of the input string
168+
strcpy(result, replacement);
169+
strcpy(result + replacement_len, input + prefix_len);
170+
return result;
171+
}
172+
173+
// If no match, return the original string
174+
return (char *)input;
175+
}
176+
154177
uint64_t fnv1a_hash(const char *data, size_t len) {
155178
uint64_t hash = FNV_OFFSET_BASIS;
156179
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/unittest.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,26 @@ bool do_test_internal_functions (void) {
17601760
return result;
17611761
}
17621762

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

17651785
bool do_compare_queries (sqlite3 *db1, const char *sql1, sqlite3 *db2, const char *sql2, int col_to_skip, int col_tombstone, bool display_column) {
@@ -3094,6 +3114,9 @@ int main(int argc, const char * argv[]) {
30943114
result = do_test_internal_functions();
30953115
printf("%-24s %s\n", "Functions Test (Int):", (result) ? "OK" : "FAILED");
30963116

3117+
result = do_test_string_replace_prefix();
3118+
printf("%-24s %s\n", "String Func Test:", (result) ? "OK" : "FAILED");
3119+
30973120
// close local database
30983121
db = close_db(db);
30993122

0 commit comments

Comments
 (0)