Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
sqlite.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file sqlite.c
12 * @brief SQLite handle registry and helper utilities for the Fun VM extension.
13 *
14 * This module provides a very small registry for SQLite database handles when
15 * the project is compiled with FUN_WITH_SQLITE. It assigns small integer
16 * identifiers to opened sqlite3 connections and allows retrieval or removal of
17 * those entries. The registry itself does not open or close SQLite databases —
18 * it only stores pointers provided by the caller.
19 *
20 * Thread-safety: This registry is not thread-safe. Callers must ensure
21 * external synchronization if used from multiple threads.
22 */
23#ifdef FUN_WITH_SQLITE
24#include <sqlite3.h>
25
26/**
27 * @brief Node in a singly-linked list of registered SQLite handles.
28 */
29typedef struct SqlHandle {
30 int id;
31 sqlite3 *db;
32 struct SqlHandle *next;
34
35/** Global head of the SQLite handle list. */
36static SqlHandle *g_sql_handles = NULL;
37/** Next positive identifier to assign to a newly added handle. */
38static int g_sql_next_id = 1;
39
40/**
41 * @brief Add a sqlite3 handle to the registry.
42 *
43 * The function allocates a new list node, assigns a fresh positive id, and
44 * prepends it to the internal registry list.
45 *
46 * @param db Valid pointer to an opened sqlite3 connection.
47 * @return Pointer to the newly created SqlHandle entry on success; NULL on
48 * allocation failure. The returned pointer remains owned by the
49 * registry; do not free it directly.
50 * @note This function does not take ownership of the sqlite3 connection in the
51 * sense of closing it; removal from the registry will not call
52 * sqlite3_close().
53 */
54static SqlHandle *sql_reg_add(sqlite3 *db) {
55 SqlHandle *h = (SqlHandle *)calloc(1, sizeof(SqlHandle));
56 if (!h) return NULL;
57 h->id = g_sql_next_id++;
58 h->db = db;
59 h->next = g_sql_handles;
61 return h;
62}
63
64/**
65 * @brief Look up a registered SQLite handle by id.
66 *
67 * @param id Positive identifier previously returned by sql_reg_add().
68 * @return Pointer to the SqlHandle entry if found; NULL otherwise.
69 */
70static SqlHandle *sql_reg_get(int id) {
71 for (SqlHandle *p = g_sql_handles; p; p = p->next)
72 if (p->id == id) return p;
73 return NULL;
74}
75
76/**
77 * @brief Remove a SQLite handle entry from the registry.
78 *
79 * Deletes the list node associated with the given id.
80 *
81 * @param id Positive identifier of the entry to remove.
82 * @note This function does not close the underlying sqlite3 connection; the
83 * caller is responsible for calling sqlite3_close() if appropriate.
84 */
85static void sql_reg_del(int id) {
87 while (*pp) {
88 if ((*pp)->id == id) {
89 SqlHandle *d = *pp;
90 *pp = d->next;
91 free(d);
92 return;
93 }
94 pp = &(*pp)->next;
95 }
96}
97#endif
CURL * h
Definition download.c:59
double db
Definition fmax.c:27
dictionary * d
Definition get_bool.c:43
free(vals)
const char * p
Definition read_file.c:37
static void sql_reg_del(int id)
Remove a SQLite handle entry from the registry.
Definition sqlite.c:85
static SqlHandle * sql_reg_get(int id)
Look up a registered SQLite handle by id.
Definition sqlite.c:70
static SqlHandle * g_sql_handles
Definition sqlite.c:36
static int g_sql_next_id
Definition sqlite.c:38
static SqlHandle * sql_reg_add(sqlite3 *db)
Add a sqlite3 handle to the registry.
Definition sqlite.c:54
Node in a singly-linked list of registered SQLite handles.
Definition sqlite.c:29
int id
Definition sqlite.c:30
struct SqlHandle * next
Definition sqlite.c:32
sqlite3 * db
Definition sqlite.c:31