|
Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
|


Go to the source code of this file.
Data Structures | |
| struct | csilk_db_stats_t |
| Database statistics. More... | |
| struct | csilk_db_pool_t |
| A (currently single-connection) database pool. More... | |
| struct | csilk_db_row_t |
| A single row of a query result. More... | |
| struct | csilk_db_result_t |
| A complete query result set. More... | |
| struct | csilk_db_driver_t |
| Virtual function table implemented by each database driver. More... | |
Functions | |
| void | csilk_db_get_stats (csilk_db_stats_t *stats) |
| Get current database statistics. | |
| csilk_db_pool_t * | csilk_db_pool_new (const char *driver_name, const char *dsn) |
| Create a new database pool and connect using the named driver. | |
| void | csilk_db_pool_free (csilk_db_pool_t *pool) |
| Destroy a database pool and disconnect. | |
| cJSON * | csilk_db_query_json (csilk_db_pool_t *pool, const char *sql) |
| Execute a SELECT query and return the rows as a JSON array. | |
| int | csilk_db_exec (csilk_db_pool_t *pool, const char *sql) |
| Execute a statement that produces no result rows. | |
| cJSON * | csilk_db_query_param_json (csilk_db_pool_t *pool, const char *sql, const char **params) |
| Execute a parameterised SELECT query with ? placeholders. | |
| int | csilk_db_register_driver (const char *name, csilk_db_driver_t *driver) |
| Register a database driver implementation. | |
| csilk_db_driver_t * | csilk_db_get_driver (const char *name) |
| Look up a registered driver by name. | |
| void | csilk_db_sqlite_init (void) |
| Internal: Register the built-in SQLite3 driver. | |
| void | csilk_db_init (void) |
| Internal: Register the built-in MySQL driver. | |
| struct csilk_db_stats_t |
| struct csilk_db_pool_t |
A (currently single-connection) database pool.
Wraps a driver + connection with a mutex for thread-safe access. All queries and statements go through this pool. The pool is created via csilk_db_pool_new (which opens the connection) and destroyed via csilk_db_pool_free (which closes it).
The pool's mutex serialises all driver operations across libuv worker threads (uv_queue_work). Functions like csilk_db_query_json acquire the mutex automatically. Do NOT call driver functions directly without holding the mutex.
| struct csilk_db_row_t |
A single row of a query result.
Values are strings (human-readable representations of column data). NULL database values are represented as NULL pointers in the values array.
| Data Fields | ||
|---|---|---|
| int | count |
Number of columns (length of |
| char ** | values |
Array of NUL-terminated string values, one per column. NULL if the SQL value was NULL. |
| struct csilk_db_result_t |
A complete query result set.
Contains all rows, column names, and dimension information. Allocated by the driver's query function and freed by free_result.

| Data Fields | ||
|---|---|---|
| int | column_count |
Number of columns (length of |
| char ** | column_names |
Array of column name strings (length |
| int | row_count |
Number of rows in |
| csilk_db_row_t ** | rows |
Array of row pointers (length |
| int csilk_db_exec | ( | csilk_db_pool_t * | pool, |
| const char * | sql | ||
| ) |
Execute a statement that produces no result rows.
Acquires the pool mutex and calls the driver's exec function.
| pool | Connection pool. |
| sql | SQL statement (INSERT, UPDATE, DELETE, DDL). |
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 | ) |
Internal: Register the built-in MySQL driver.
Called automatically by csilk_db_init. Requires libmysqlclient. Not intended for direct use.
Internal: Register the built-in PostgreSQL driver.
Called automatically by csilk_db_init. Requires libpq. Not intended for direct use.
Internal: Register the built-in MongoDB driver.
Called automatically by csilk_db_init. Requires libmongoc. Not intended for direct use.
Internal: Register the built-in Redis driver.
Called automatically by csilk_db_init. Requires hiredis. Not intended for direct use.
Initialise the database subsystem.
Registers all built-in drivers (SQLite3, MySQL, PostgreSQL, MongoDB, Redis). Must be called once before any csilk_db_pool_new call. Safe to call multiple times.
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 | ) |
Destroy a database pool and disconnect.
Calls the driver's disconnect, frees internal resources, and deallocates the pool struct.
| pool | Pool to destroy. Must not be NULL. |
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 pool and connect using the named driver.
Looks up the driver by driver_name (must have been registered via csilk_db_register_driver or the built-in init), allocates a pool, and calls the driver's connect function.
| driver_name | Driver identifier (e.g., "sqlite"). Must not be NULL. |
| dsn | Data source name (driver-specific format). |
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 SELECT query and return the rows as a JSON array.
Internally acquires the pool mutex, calls the driver's query function, converts each row to a JSON object (keyed by column name), frees the native result, and returns the cJSON array.
| pool | Connection pool. |
| sql | SQL SELECT statement. |
Execute a SELECT query and return the rows as a JSON array.
Execute a SELECT query and return the result as a JSON array.
| cJSON * csilk_db_query_param_json | ( | csilk_db_pool_t * | pool, |
| const char * | sql, | ||
| const char ** | params | ||
| ) |
Execute a parameterised SELECT query with ? placeholders.
Each ? in sql is substituted with the corresponding value from params (escaping is handled by the driver). Results are returned as a JSON array.
| pool | Connection pool. |
| sql | SQL with ? placeholders. |
| params | NULL-terminated array of string values. The number of values must match the number of ? placeholders. |
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 | ||
| ) |
Register a database driver implementation.
Makes the driver available for use with csilk_db_pool_new. The name must be unique (earlier registrations are not overwritten).
| name | Driver identifier string (e.g., "postgres"). Must remain valid for the program lifetime. |
| driver | Pointer to a csilk_db_driver_t with all function pointers populated. The struct must remain valid for the program lifetime. |
name is already registered.Register a database driver implementation.
| void csilk_db_sqlite_init | ( | void | ) |
Internal: Register the built-in SQLite3 driver.
Called automatically by csilk_db_init. Not intended for direct use.
Internal: Register the built-in SQLite3 driver.
Call this at startup (e.g., in the app init callback) to make the "sqlite" driver available for csilk_db_create() calls.