Skip to content

Commit

Permalink
Merge pull request #1855 from tursodatabase/bindings_example
Browse files Browse the repository at this point in the history
Make C bindings example do sync
  • Loading branch information
haaawk authored Nov 29, 2024
2 parents ff06574 + c00652f commit 15849e4
Showing 1 changed file with 70 additions and 20 deletions.
90 changes: 70 additions & 20 deletions bindings/c/example.c
Original file line number Diff line number Diff line change
@@ -1,52 +1,92 @@
#include "libsql.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
libsql_connection_t conn;
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];
char sync = 0;

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;
}
sync = 1;
} 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) {
fprintf(stderr, "%s\n", err);
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;
}

Expand All @@ -55,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);
Expand Down

0 comments on commit 15849e4

Please sign in to comment.