From fa9d76f819f0731642cb5ff106c0257b6ef8d742 Mon Sep 17 00:00:00 2001 From: Piotr Jastrzebski Date: Fri, 29 Nov 2024 10:15:18 +0100 Subject: [PATCH 1/2] C bindings: Make example show embedded replicas Signed-off-by: Piotr Jastrzebski --- bindings/c/example.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/bindings/c/example.c b/bindings/c/example.c index 9e241fd56d..154e4dcad8 100644 --- a/bindings/c/example.c +++ b/bindings/c/example.c @@ -1,6 +1,7 @@ #include "libsql.h" #include #include +#include int main(int argc, char *argv[]) { @@ -8,14 +9,38 @@ int main(int argc, char *argv[]) libsql_rows_t rows; libsql_row_t row; libsql_database_t db; + libsql_config config; const char *err = NULL; int retval = 0; + char db_path[1024]; - retval = libsql_open_ext(":memory:", &db, &err); - if (retval != 0) { - fprintf(stderr, "%s\n", err); - goto quit; - } + if (argc > 1) { + char* url = argv[1]; + char auth_token[1024]; + auth_token[0] = '\0'; + if (argc > 2) { + strncpy(auth_token, argv[2], strlen(argv[2])); + } + strncpy(db_path, "test.db", strlen("test.db")); + config.db_path = db_path; + config.primary_url = url; + config.auth_token = auth_token; + config.read_your_writes = 0; + config.encryption_key = NULL; + config.sync_interval = 0; + config.with_webpki = 0; + retval = libsql_open_sync_with_config(config, &db, &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + } else { + retval = libsql_open_ext(":memory:", &db, &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + } retval = libsql_connect(db, &conn, &err); if (retval != 0) { From c00652f2646c41753d0addf3477fd2707be25a5b Mon Sep 17 00:00:00 2001 From: Piotr Jastrzebski Date: Fri, 29 Nov 2024 10:51:13 +0100 Subject: [PATCH 2/2] C bindings include sync into example Signed-off-by: Piotr Jastrzebski --- bindings/c/example.c | 55 ++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/bindings/c/example.c b/bindings/c/example.c index 154e4dcad8..7243ea60f0 100644 --- a/bindings/c/example.c +++ b/bindings/c/example.c @@ -13,6 +13,7 @@ int main(int argc, char *argv[]) const char *err = NULL; int retval = 0; char db_path[1024]; + char sync = 0; if (argc > 1) { char* url = argv[1]; @@ -34,6 +35,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "%s\n", err); goto quit; } + sync = 1; } else { retval = libsql_open_ext(":memory:", &db, &err); if (retval != 0) { @@ -48,30 +50,43 @@ int main(int argc, char *argv[]) goto quit; } - retval = libsql_query(conn, "SELECT 1", &rows, &err); + retval = libsql_execute(conn, "CREATE TABLE IF NOT EXISTS guest_book_entries (text TEXT)", &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + + retval = libsql_execute(conn, "INSERT INTO guest_book_entries VALUES('hi there')", &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + + retval = libsql_execute(conn, "INSERT INTO guest_book_entries VALUES('some more hi there')", &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + + retval = libsql_query(conn, "SELECT text FROM guest_book_entries", &rows, &err); if (retval != 0) { fprintf(stderr, "%s\n", err); goto quit; } - int num_cols = libsql_column_count(rows); - while ((retval = libsql_next_row(rows, &row, &err)) == 0) { if (!err && !row) { break; } - for (int col = 0; col < num_cols; col++) { - if (col > 0) { - printf(", "); - } - long long value; - retval = libsql_get_int(row, col, &value, &err); - if (retval != 0) { - fprintf(stderr, "%s\n", err); - } else { - printf("%lld\n", value); - } - } + const char * value = NULL; + retval = libsql_get_string(row, 0, &value, &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + } else { + printf("%s\n", value); + libsql_free_string(value); + value = NULL; + } err = NULL; } @@ -80,6 +95,16 @@ int main(int argc, char *argv[]) goto quit; } + if (sync) { + printf("Syncing database to remote...\n"); + retval = libsql_sync(db, &err); + if (retval != 0) { + fprintf(stderr, "%s\n", err); + goto quit; + } + printf("Done!\n"); + } + quit: libsql_free_rows(rows); libsql_disconnect(conn);