-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Makefile.common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Encryption | ||
|
||
This example demonstrates how to create and use an encrypted SQLite database with libSQL. | ||
|
||
## Building | ||
|
||
```bash | ||
make | ||
``` | ||
|
||
## Running | ||
|
||
```bash | ||
./example | ||
``` | ||
|
||
This example will: | ||
|
||
1. Create an encrypted SQLite database file named `encrypted.db`. | ||
2. Create a table called `secrets`. | ||
3. Insert some sample data into the `secrets` table. | ||
4. Query and display all secrets in the table. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "../../libsql.h" | ||
#include <stdio.h> | ||
|
||
int main() { | ||
libsql_setup((libsql_config_t){0}); | ||
|
||
const char *encryption_key = "my_secret_key"; | ||
|
||
libsql_database_t db = libsql_database_init((libsql_database_desc_t | ||
){.path = "encrypted.db", | ||
.encryption_key = encryption_key, | ||
.cypher = LIBSQL_CYPHER_AES256}); | ||
|
||
if (db.err) { | ||
fprintf( | ||
stderr, | ||
"Error initializing encrypted database: %s\n", | ||
libsql_error_message(db.err) | ||
); | ||
return 1; | ||
} | ||
|
||
libsql_connection_t conn = libsql_database_connect(db); | ||
if (conn.err) { | ||
fprintf( | ||
stderr, | ||
"Error connecting to encrypted database: %s\n", | ||
libsql_error_message(conn.err) | ||
); | ||
return 1; | ||
} | ||
|
||
const char *setup_sql = "CREATE TABLE IF NOT EXISTS secrets (id INTEGER " | ||
"PRIMARY KEY AUTOINCREMENT, content TEXT);" | ||
"INSERT INTO secrets (content) VALUES ('Top Secret " | ||
"Info 1'), ('Classified Data 2');"; | ||
|
||
libsql_batch_t batch = libsql_connection_batch(conn, setup_sql); | ||
if (batch.err) { | ||
fprintf( | ||
stderr, | ||
"Error executing setup batch: %s\n", | ||
libsql_error_message(batch.err) | ||
); | ||
return 1; | ||
} | ||
|
||
printf("Table created and data inserted into encrypted database.\n"); | ||
|
||
libsql_statement_t query_stmt = | ||
libsql_connection_prepare(conn, "SELECT * FROM secrets"); | ||
if (query_stmt.err) { | ||
fprintf( | ||
stderr, | ||
"Error preparing query: %s\n", | ||
libsql_error_message(query_stmt.err) | ||
); | ||
return 1; | ||
} | ||
|
||
libsql_rows_t rows = libsql_statement_query(query_stmt); | ||
if (rows.err) { | ||
fprintf( | ||
stderr, | ||
"Error executing query: %s\n", | ||
libsql_error_message(rows.err) | ||
); | ||
return 1; | ||
} | ||
|
||
printf("Secrets in the encrypted database:\n"); | ||
libsql_row_t row; | ||
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) { | ||
libsql_result_value_t id = libsql_row_value(row, 0); | ||
libsql_result_value_t content = libsql_row_value(row, 1); | ||
|
||
if (id.err || content.err) { | ||
fprintf(stderr, "Error retrieving row values\n"); | ||
continue; | ||
} | ||
|
||
printf( | ||
"%lld: %s\n", | ||
(long long)id.ok.value.integer, | ||
(char *)content.ok.value.text.ptr | ||
); | ||
|
||
libsql_row_deinit(row); | ||
} | ||
|
||
libsql_rows_deinit(rows); | ||
libsql_statement_deinit(query_stmt); | ||
libsql_connection_deinit(conn); | ||
libsql_database_deinit(db); | ||
|
||
printf("Database closed. The file 'encrypted.db' is now encrypted.\n"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Makefile.common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# In-Memory | ||
|
||
This example demonstrates how to use libsql with an in-memory SQLite database. | ||
|
||
## Building | ||
|
||
```bash | ||
make | ||
``` | ||
|
||
## Running | ||
|
||
```bash | ||
./example | ||
``` | ||
|
||
This example will: | ||
|
||
1. Create an in-memory SQLite database. | ||
2. Create a table called `users`. | ||
3. Insert some sample data into the `users` table. | ||
4. Query and display all users in the table. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "../../libsql.h" | ||
#include <stdio.h> | ||
|
||
int main() { | ||
libsql_setup((libsql_config_t){0}); | ||
|
||
libsql_database_t db = | ||
libsql_database_init((libsql_database_desc_t){.path = ":memory:"}); | ||
if (db.err) { | ||
fprintf( | ||
stderr, | ||
"Error initializing in-memory database: %s\n", | ||
libsql_error_message(db.err) | ||
); | ||
return 1; | ||
} | ||
|
||
libsql_connection_t conn = libsql_database_connect(db); | ||
if (conn.err) { | ||
fprintf( | ||
stderr, | ||
"Error connecting to in-memory database: %s\n", | ||
libsql_error_message(conn.err) | ||
); | ||
return 1; | ||
} | ||
|
||
const char *setup_sql = | ||
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);" | ||
"INSERT INTO users (name) VALUES ('Alice'), ('Bob'), ('Charlie');"; | ||
|
||
libsql_batch_t batch = libsql_connection_batch(conn, setup_sql); | ||
if (batch.err) { | ||
fprintf( | ||
stderr, | ||
"Error executing setup batch: %s\n", | ||
libsql_error_message(batch.err) | ||
); | ||
return 1; | ||
} | ||
|
||
printf("Table created and data inserted.\n"); | ||
|
||
libsql_statement_t query_stmt = | ||
libsql_connection_prepare(conn, "SELECT * FROM users"); | ||
if (query_stmt.err) { | ||
fprintf( | ||
stderr, | ||
"Error preparing query: %s\n", | ||
libsql_error_message(query_stmt.err) | ||
); | ||
return 1; | ||
} | ||
|
||
libsql_rows_t rows = libsql_statement_query(query_stmt); | ||
if (rows.err) { | ||
fprintf( | ||
stderr, | ||
"Error executing query: %s\n", | ||
libsql_error_message(rows.err) | ||
); | ||
return 1; | ||
} | ||
|
||
printf("Users in the in-memory database:\n"); | ||
libsql_row_t row; | ||
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) { | ||
libsql_result_value_t id = libsql_row_value(row, 0); | ||
libsql_result_value_t name = libsql_row_value(row, 1); | ||
|
||
if (id.err || name.err) { | ||
fprintf(stderr, "Error retrieving row values\n"); | ||
continue; | ||
} | ||
|
||
printf( | ||
"%lld: %s\n", | ||
(long long)id.ok.value.integer, | ||
(char *)name.ok.value.text.ptr | ||
); | ||
|
||
libsql_row_deinit(row); | ||
} | ||
|
||
libsql_rows_deinit(rows); | ||
libsql_statement_deinit(query_stmt); | ||
libsql_connection_deinit(conn); | ||
libsql_database_deinit(db); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters