1
0
Fork 0
forked from fun/fun

Added support for libSQL as an compatible alternative for SQLite. (0.33.0)

This commit is contained in:
Johannes Findeisen 2025-11-27 00:42:47 +01:00
commit 56712206d1
17 changed files with 418 additions and 5 deletions

32
src/vm/libsql/close.c Normal file
View file

@ -0,0 +1,32 @@
/*
* 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
*/
/**
* OP_LIBSQL_CLOSE: (handle:int) -> Nil
*/
case OP_LIBSQL_CLOSE: {
#ifdef FUN_WITH_LIBSQL
Value vh = pop_value(vm);
int hid = (int)vh.i;
free_value(vh);
LibSqlHandle *h = libsql_reg_get(hid);
if (h && h->db) {
sqlite3_close(h->db);
h->db = NULL;
libsql_reg_del(hid);
}
push_value(vm, make_nil());
#else
Value v = pop_value(vm); free_value(v);
push_value(vm, make_nil());
#endif
break;
}

55
src/vm/libsql/common.c Normal file
View file

@ -0,0 +1,55 @@
/*
* 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
*/
#ifdef FUN_WITH_LIBSQL
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sqlite3.h> /* libsql provides a sqlite3-compatible C API */
typedef struct LibSqlHandle {
int id;
sqlite3 *db;
struct LibSqlHandle *next;
} LibSqlHandle;
static LibSqlHandle *g_libsql_handles = NULL;
static int g_libsql_next_id = 1;
static LibSqlHandle *libsql_reg_add(sqlite3 *db) {
LibSqlHandle *h = (LibSqlHandle*)malloc(sizeof(LibSqlHandle));
if (!h) return NULL;
h->id = g_libsql_next_id++;
h->db = db;
h->next = g_libsql_handles;
g_libsql_handles = h;
return h;
}
static LibSqlHandle *libsql_reg_get(int id) {
LibSqlHandle *p = g_libsql_handles;
while (p) { if (p->id == id) return p; p = p->next; }
return NULL;
}
static void libsql_reg_del(int id) {
LibSqlHandle **pp = &g_libsql_handles;
while (*pp) {
if ((*pp)->id == id) {
LibSqlHandle *dead = *pp;
*pp = (*pp)->next;
free(dead);
return;
}
pp = &((*pp)->next);
}
}
#endif /* FUN_WITH_LIBSQL */

36
src/vm/libsql/exec.c Normal file
View file

@ -0,0 +1,36 @@
/*
* 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
*/
/**
* OP_LIBSQL_EXEC: (handle:int, sql:string) -> int rc (0=OK)
*/
case OP_LIBSQL_EXEC: {
#ifdef FUN_WITH_LIBSQL
Value vsql = pop_value(vm);
Value vh = pop_value(vm);
int hid = (int)vh.i;
char *sql = value_to_string_alloc(&vsql);
free_value(vh);
free_value(vsql);
LibSqlHandle *h = libsql_reg_get(hid);
if (!h || !h->db || !sql) { if (sql) free(sql); push_value(vm, make_int(SQLITE_MISUSE)); break; }
char *errmsg = NULL;
int rc = sqlite3_exec(h->db, sql, NULL, NULL, &errmsg);
if (errmsg) sqlite3_free(errmsg);
free(sql);
push_value(vm, make_int(rc));
#else
Value v1 = pop_value(vm); free_value(v1);
Value v2 = pop_value(vm); free_value(v2);
push_value(vm, make_int(-1));
#endif
break;
}

37
src/vm/libsql/open.c Normal file
View file

@ -0,0 +1,37 @@
/*
* 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
*/
/**
* OP_LIBSQL_OPEN: (url_or_path:string) -> handle:int (>0) or 0 on error
*/
case OP_LIBSQL_OPEN: {
#ifdef FUN_WITH_LIBSQL
Value vpath = pop_value(vm);
char *path = value_to_string_alloc(&vpath);
free_value(vpath);
if (!path) { push_value(vm, make_int(0)); break; }
sqlite3 *db = NULL;
int rc = sqlite3_open(path, &db);
free(path);
if (rc != SQLITE_OK || !db) {
if (db) sqlite3_close(db);
push_value(vm, make_int(0));
break;
}
LibSqlHandle *h = libsql_reg_add(db);
if (!h) { sqlite3_close(db); push_value(vm, make_int(0)); break; }
push_value(vm, make_int(h->id));
#else
Value v = pop_value(vm); free_value(v);
push_value(vm, make_int(0));
#endif
break;
}

60
src/vm/libsql/query.c Normal file
View file

@ -0,0 +1,60 @@
/*
* 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
*/
/**
* OP_LIBSQL_QUERY: (handle:int, sql:string) -> array<map<string,any>>
*/
case OP_LIBSQL_QUERY: {
#ifdef FUN_WITH_LIBSQL
Value vsql = pop_value(vm);
Value vh = pop_value(vm);
int hid = (int)vh.i;
char *sql = value_to_string_alloc(&vsql);
free_value(vh);
free_value(vsql);
LibSqlHandle *h = libsql_reg_get(hid);
if (!h || !h->db || !sql) { if (sql) free(sql); push_value(vm, make_array_from_values(NULL, 0)); break; }
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(h->db, sql, -1, &stmt, NULL) != SQLITE_OK) {
free(sql);
push_value(vm, make_array_from_values(NULL, 0));
break;
}
free(sql);
Value rows = make_array_from_values(NULL, 0);
int ncols = sqlite3_column_count(stmt);
while (sqlite3_step(stmt) == SQLITE_ROW) {
Value row = make_map_empty();
for (int i = 0; i < ncols; i++) {
const char *name = sqlite3_column_name(stmt, i);
int type = sqlite3_column_type(stmt, i);
Value kv;
switch (type) {
case SQLITE_INTEGER: kv = make_int((int64_t)sqlite3_column_int64(stmt, i)); break;
case SQLITE_FLOAT: kv = make_float(sqlite3_column_double(stmt, i)); break;
case SQLITE_TEXT: kv = make_string((const char*)sqlite3_column_text(stmt, i)); break;
case SQLITE_NULL: kv = make_nil(); break;
default: kv = make_nil(); break; /* ignore blobs for now */
}
(void)map_set(&row, name ? name : "", kv);
}
(void)array_push(&rows, row);
/* Do NOT free 'row' here; owned by rows array. */
}
sqlite3_finalize(stmt);
push_value(vm, rows);
#else
Value v1 = pop_value(vm); free_value(v1);
Value v2 = pop_value(vm); free_value(v2);
push_value(vm, make_array_from_values(NULL, 0));
#endif
break;
}