Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
sqlite.c File Reference

SQLite handle registry and helper utilities for the Fun VM extension. More...

#include <sqlite3.h>
Include dependency graph for sqlite.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SqlHandle
 Node in a singly-linked list of registered SQLite handles. More...

Typedefs

typedef struct SqlHandle SqlHandle
 Node in a singly-linked list of registered SQLite handles.

Functions

static SqlHandlesql_reg_add (sqlite3 *db)
 Add a sqlite3 handle to the registry.
static SqlHandlesql_reg_get (int id)
 Look up a registered SQLite handle by id.
static void sql_reg_del (int id)
 Remove a SQLite handle entry from the registry.

Variables

static SqlHandleg_sql_handles = NULL
 Global head of the SQLite handle list.
static int g_sql_next_id = 1
 Next positive identifier to assign to a newly added handle.

Detailed Description

SQLite handle registry and helper utilities for the Fun VM extension.

This translation unit implements a tiny in-process registry for SQLite connection handles that can be used by the VM opcodes living under src/vm/sqlite/*.c. The registry abstracts over raw sqlite3* pointers and assigns small positive integer identifiers to each connection. VM opcodes can then pass these identifiers around instead of raw pointers.

Build-time feature flag

The code is compiled only when the CMake option FUN_WITH_SQLITE is enabled (i.e., the preprocessor symbol FUN_WITH_SQLITE is defined). When disabled, this file contributes no symbols and the corresponding opcodes should be compiled out or provide appropriate fallbacks.

Ownership and lifetime

  • The registry does NOT open or close databases. It merely stores pointers that were created elsewhere (e.g., via sqlite3_open()).
  • Adding an entry does not transfer ownership of the sqlite3 connection. Callers remain responsible for invoking sqlite3_close() at the appropriate time.
  • Removing an entry from the registry does NOT close the connection; it only forgets the mapping between id and pointer.
  • Integer identifiers are monotonically increasing per process. Once a handle id is deleted, it will not be reused within the same process lifetime.

Error handling

Functions in this module perform only basic validation and memory allocation. Allocation failures return NULL (for lookups/additions) or are silently ignored (for deletions of non-existent ids). No SQLite API calls are made here, so no sqlite error codes are produced by this module itself.

Thread-safety

The registry is implemented as a simple singly-linked list with no synchronization. It is NOT thread-safe. If the VM uses SQLite from multiple threads, the caller must provide external synchronization around all calls to these helpers.

Example

// Open a database elsewhere:
sqlite3 *db = NULL;
if (sqlite3_open(":memory:", &db) == SQLITE_OK) {
// Register and get an id:
int id = h ? h->id : -1;
// Later look it up:
SqlHandle *same = sql_reg_get(id);
if (same) {
// use same->db with SQLite APIs
}
// When finished, drop the registry entry and close manually:
sqlite3_close(db);
}
static void sql_reg_del(int id)
Remove a SQLite handle entry from the registry.
Definition sqlite.c:160
static SqlHandle * sql_reg_get(int id)
Look up a registered SQLite handle by id.
Definition sqlite.c:143
static SqlHandle * sql_reg_add(sqlite3 *db)
Add a sqlite3 handle to the registry.
Definition sqlite.c:122
Node in a singly-linked list of registered SQLite handles.
Definition sqlite.c:85
int id
Definition sqlite.c:86

Definition in file sqlite.c.

Typedef Documentation

◆ SqlHandle

typedef struct SqlHandle SqlHandle

Node in a singly-linked list of registered SQLite handles.

Each node associates a monotonically increasing positive integer identifier with a raw sqlite3* pointer. The list head is stored in a file-static global (g_sql_handles).

Function Documentation

◆ sql_reg_add()

SqlHandle * sql_reg_add ( sqlite3 * db)
static

Add a sqlite3 handle to the registry.

Allocates a new list node, assigns a fresh positive id, and prepends it to the internal registry list. Ownership of the sqlite3 connection remains with the caller; this registry does not close the handle during deletion.

Parameters
dbValid pointer to an opened sqlite3 connection.
Returns
Pointer to the newly created SqlHandle entry on success; NULL on allocation failure. The returned pointer remains owned by the registry; do not free it directly.
Note
This function does not take ownership of the sqlite3 connection in the sense of closing it; removal from the registry will not call sqlite3_close().

Definition at line 122 of file sqlite.c.

◆ sql_reg_del()

void sql_reg_del ( int id)
static

Remove a SQLite handle entry from the registry.

Deletes the list node associated with the given id.

Parameters
idPositive identifier of the entry to remove.
Note
This function does not close the underlying sqlite3 connection; the caller is responsible for calling sqlite3_close() if appropriate.
If the id does not exist, the function is a no-op.

Definition at line 160 of file sqlite.c.

◆ sql_reg_get()

SqlHandle * sql_reg_get ( int id)
static

Look up a registered SQLite handle by id.

Performs a linear search over the internal list to find a matching id.

Parameters
idPositive identifier previously returned by sql_reg_add().
Returns
Pointer to the SqlHandle entry if found; NULL otherwise.
Note
The returned pointer is owned by the registry and must not be freed by the caller.

Definition at line 143 of file sqlite.c.

Variable Documentation

◆ g_sql_handles

SqlHandle* g_sql_handles = NULL
static

Global head of the SQLite handle list.

NULL denotes an empty list. The list is modified only by the helpers in this file and is never exposed directly to callers.

Definition at line 97 of file sqlite.c.

◆ g_sql_next_id

int g_sql_next_id = 1
static

Next positive identifier to assign to a newly added handle.

Starts at 1 and increases monotonically. Identifiers are not reused across deletions within a process lifetime.

Definition at line 104 of file sqlite.c.