Added support for libSQL as an compatible alternative for SQLite. (0.33.0)
This commit is contained in:
parent
9416ec3457
commit
56712206d1
17 changed files with 418 additions and 5 deletions
59
examples/libsql_example.fun
Normal file
59
examples/libsql_example.fun
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2025-11-26
|
||||
*/
|
||||
|
||||
// Demonstrates the optional libSQL extension
|
||||
// Build with: cmake -S . -B build -DFUN_WITH_LIBSQL=ON && cmake --build build
|
||||
|
||||
// Prepare sample DB from SQL if needed (requires sqlite3 CLI installed)
|
||||
// Create it once with:
|
||||
// sqlite3 ./database.sqlite < ./examples/data/database.sql
|
||||
|
||||
number h = libsql_open("./database.sqlite")
|
||||
if h == 0
|
||||
print("Failed to open libSQL database")
|
||||
else
|
||||
libsql_exec(h, "CREATE TABLE IF NOT EXISTS todos(id INTEGER PRIMARY KEY, title TEXT, done INT)")
|
||||
libsql_exec(h, "DELETE FROM todos")
|
||||
libsql_exec(h, "INSERT INTO todos(title, done) VALUES('Buy milk', 0)")
|
||||
libsql_exec(h, "INSERT INTO todos(title, done) VALUES('Write code', 1)")
|
||||
|
||||
rows = libsql_query(h, "SELECT id, title, done FROM todos ORDER BY id")
|
||||
for row in rows
|
||||
print(to_string(row["id"]) + ": " + to_string(row["title"]) + " (done="+to_string(row["done"]) + ")")
|
||||
|
||||
rows = libsql_query(h, "SELECT id, title, done, created_at FROM tasks ORDER BY id;")
|
||||
print("Tasks (" + to_string(len(rows)) + "):")
|
||||
for row in rows
|
||||
string status = "✘"
|
||||
if row["done"] == 1
|
||||
status = "✔"
|
||||
print("- [" + status + "] (#" + to_string(row["id"]) + ") " + to_string(row["title"]) + " — " + to_string(row["created_at"]))
|
||||
|
||||
number rc = libsql_exec(h, "INSERT INTO tasks (title, done) VALUES ('Try Fun + SQLite', 0);")
|
||||
print("Insert rc=" + to_string(rc))
|
||||
|
||||
rows2 = libsql_query(h, "SELECT count(*) AS cnt FROM tasks;")
|
||||
print("Total tasks now: " + to_string(rows2[0]["cnt"]))
|
||||
|
||||
libsql_close(h)
|
||||
|
||||
/* Example output:
|
||||
1: Buy milk (done=0)
|
||||
2: Write code (done=1)
|
||||
Tasks (3):
|
||||
- [✔] (#1) Write Fun + SQLite example — 2025-11-26 23:20:41
|
||||
- [✘] (#2) Ship optional feature flag — 2025-11-26 23:20:41
|
||||
- [✘] (#3) Celebrate with coffee — 2025-11-26 23:20:41
|
||||
Insert rc=0
|
||||
Total tasks now: 4
|
||||
*/
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env fun
|
||||
|
||||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
|
|
@ -11,9 +13,9 @@
|
|||
|
||||
// Prepare sample DB from SQL if needed (requires sqlite3 CLI installed)
|
||||
// Create it once with:
|
||||
// sqlite3 todo.sqlite < ./examples/data/todo.sql
|
||||
// sqlite3 ./database.sqlite < ./examples/data/database.sql
|
||||
|
||||
string db_path = "./todo.sqlite"
|
||||
string db_path = "./database.sqlite"
|
||||
|
||||
number h = sqlite_open(db_path)
|
||||
if h == 0
|
||||
|
|
@ -35,3 +37,12 @@ rows2 = sqlite_query(h, "SELECT count(*) AS cnt FROM tasks;")
|
|||
print("Total tasks now: " + to_string(rows2[0]["cnt"]))
|
||||
|
||||
sqlite_close(h)
|
||||
|
||||
/* Example output:
|
||||
Tasks (3):
|
||||
- [✔] (#1) Write Fun + SQLite example — 2025-11-26 23:22:04
|
||||
- [✘] (#2) Ship optional feature flag — 2025-11-26 23:22:04
|
||||
- [✘] (#3) Celebrate with coffee — 2025-11-26 23:22:04
|
||||
Insert rc=0
|
||||
Total tasks now: 4
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue