|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|
Unified database interface implementation. More...
#include "csilk/drivers/db.h"#include <stdatomic.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <uv.h>#include "cJSON.h"
Functions | |
| void | csilk_db_get_stats (csilk_db_stats_t *stats) |
| Get current database statistics. | |
| static cJSON * | csilk_db_query_json_locked (csilk_db_pool_t *pool, const char *sql) |
| Internal: execute a query and return the result as a cJSON array. | |
| csilk_db_pool_t * | csilk_db_pool_new (const char *driver_name, const char *dsn) |
| Create a new database connection pool with the named driver. | |
| void | csilk_db_pool_free (csilk_db_pool_t *pool) |
| Free a database pool and its underlying connection. | |
| cJSON * | csilk_db_query_json (csilk_db_pool_t *pool, const char *sql) |
| Execute a SQL query and return the result as a cJSON array. | |
| int | csilk_db_exec (csilk_db_pool_t *pool, const char *sql) |
| Execute a SQL statement (INSERT, UPDATE, DELETE, DDL) that does not return rows. | |
| cJSON * | csilk_db_query_param_json (csilk_db_pool_t *pool, const char *sql, const char **params) |
| Execute a parameterized SQL query and return JSON result. | |
| static void | ensure_registry_init (void) |
| void | csilk_db_init (void) |
| Initialise the database subsystem. | |
| int | csilk_db_register_driver (const char *name, csilk_db_driver_t *driver) |
| Statically-sized registry of registered database drivers (max 16). | |
| csilk_db_driver_t * | csilk_db_get_driver (const char *name) |
| Look up a registered driver by name. | |
Variables | |
| static atomic_uint_fast64_t | db_queries_total = 0 |
| static atomic_uint_fast64_t | db_execs_total = 0 |
| static atomic_uint_fast64_t | db_errors_total = 0 |
| static atomic_uint_fast64_t | db_duration_us_total = 0 |
| static csilk_db_driver_t * | drivers [16] |
| Statically-sized registry of registered database drivers (max 16). | |
| static int | driver_count = 0 |
| static uv_mutex_t | registry_mutex |
| static int | registry_initialized = 0 |
Unified database interface implementation.
The DB layer uses a pluggable driver registry pattern:
csilk_db_driver_t (abstract interface) ├── connect(pool, dsn) → establishes connection ├── query(pool, sql, result) → returns tabular results ├── exec(pool, sql) → executes non-query statements ├── free_result(result) → releases query results └── disconnect(pool) → tears down connection
Drivers self-register at startup (e.g., csilk_db_sqlite_init() calls csilk_db_register_driver()). The registry is a simple fixed-size array protected by a mutex.
csilk_db_pool_new() → driver->connect() → [query/exec] → pool_free() → driver->disconnect()
The pool itself is a thin wrapper: it holds a driver pointer, a mutex (for serializing access to the single connection), and driver-specific state in an opaque handle field.
csilk_db_query_json() and friends convert the driver's tabular result (csilk_db_result_t) into a cJSON array of objects — one object per row, with column names as keys. This is the format expected by the HTTP layer for JSON API responses.
| int csilk_db_exec | ( | csilk_db_pool_t * | pool, |
| const char * | sql | ||
| ) |
Execute a SQL statement (INSERT, UPDATE, DELETE, DDL) that does not return rows.
Execute a statement that produces no result rows.
Execute a statement that returns no result rows.
| csilk_db_driver_t * csilk_db_get_driver | ( | const char * | name | ) |
Look up a registered driver by name.
| name | Driver identifier string. |
| void csilk_db_get_stats | ( | csilk_db_stats_t * | stats | ) |
Get current database statistics.
| stats | [out] Pointer to stats struct to populate. |
| void csilk_db_init | ( | void | ) |
Initialise the database subsystem.
Internal: Register the built-in MySQL driver.
Registers all built-in drivers (SQLite3, MySQL, PostgreSQL, etc.). Must be called once before any csilk_db_pool_new call. Safe to call multiple times.
| void csilk_db_pool_free | ( | csilk_db_pool_t * | pool | ) |
Free a database pool and its underlying connection.
Destroy a database pool and disconnect.
Free a database pool and disconnect.
The driver's free_result is not called here — any outstanding results must have been freed by the caller.
| csilk_db_pool_t * csilk_db_pool_new | ( | const char * | driver_name, |
| const char * | dsn | ||
| ) |
Create a new database connection pool with the named driver.
Create a new database pool and connect using the named driver.
Create a new database connection pool.
The pool holds exactly ONE connection (mutex-serialized access). This is intentional — the caller is expected to manage a pool of pools if concurrency is needed.
| driver_name | Registered driver name (e.g., "sqlite3"). |
| dsn | Data source name (e.g., "/tmp/test.db" or "host=..."). |
| cJSON * csilk_db_query_json | ( | csilk_db_pool_t * | pool, |
| const char * | sql | ||
| ) |
Execute a SQL query and return the result as a cJSON array.
Execute a SELECT query and return the rows as a JSON array.
Execute a SELECT query and return the result as a JSON array.
|
static |
Internal: execute a query and return the result as a cJSON array.
All values are represented as cJSON strings — no type inference is attempted. The caller can parse numerics/bools with cJSON_GetNumberValue etc. if needed.
| pool | Database pool with an active connection. |
| sql | SQL query string. |
| cJSON * csilk_db_query_param_json | ( | csilk_db_pool_t * | pool, |
| const char * | sql, | ||
| const char ** | params | ||
| ) |
Execute a parameterized SQL query and return JSON result.
Execute a parameterised SELECT query with ? placeholders.
This is naive string substitution — NOT prepared-statement binding. Parameter values are NOT escaped for SQL special characters. A parameter containing "' OR '1'='1" will inject into the SQL verbatim.
| pool | Database pool. |
| sql | SQL pattern with ? placeholders. |
| params | NULL-terminated array of string values. |
| int csilk_db_register_driver | ( | const char * | name, |
| csilk_db_driver_t * | driver | ||
| ) |
Statically-sized registry of registered database drivers (max 16).
Register a database driver implementation.
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Statically-sized registry of registered database drivers (max 16).
|
static |
|
static |