Csilk 0.2.1
A lightweight, high-performance C HTTP web framework
Loading...
Searching...
No Matches
sqlite.c File Reference

SQLite3 database driver for csilk. More...

#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include "csilk/csilk.h"
#include "csilk/drivers/db.h"
Include dependency graph for sqlite.c:

Data Structures

struct  sqlite_conn_t
 Per-connection data for the SQLite driver. More...
 

Functions

static int sqlite_connect (csilk_db_pool_t *pool, const char *dsn)
 Open a connection to a SQLite database file.
 
static int sqlite_disconnect (csilk_db_pool_t *pool)
 Close a SQLite connection and free the pool's connection data.
 
static void sqlite_free_result (csilk_db_result_t *result)
 Free all memory associated with a query result set.
 
static int sqlite_query (csilk_db_pool_t *pool, const char *sql, csilk_db_result_t *result)
 Execute a SQL query and return the full result set.
 
static int sqlite_exec (csilk_db_pool_t *pool, const char *sql)
 Execute a SQL statement that returns no result rows.
 
static int sqlite_transaction_begin (csilk_db_pool_t *pool)
 Begin a SQLite transaction (delegates to sqlite_exec).
 
static int sqlite_transaction_commit (csilk_db_pool_t *pool)
 Commit the current SQLite transaction.
 
static int sqlite_transaction_rollback (csilk_db_pool_t *pool)
 Rollback the current SQLite transaction.
 
void csilk_db_sqlite_init (void)
 Initialize and register the SQLite database driver with csilk.
 

Variables

csilk_db_driver_t csilk_db_sqlite_driver
 Pre-built driver vtable for the SQLite3 database backend.
 

Detailed Description

SQLite3 database driver for csilk.

Implements the csilk_db_driver_t vtable using the native sqlite3 C API. Key design points:

  • Uses sqlite3_open_v2 with SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE (the database file is created if it does not exist).
  • SELECT queries use sqlite3_prepare_v2 + sqlite3_step for row-by-row iteration; the full result set is buffered before returning.
  • Non-query statements (INSERT, UPDATE, DELETE, DDL) use the simpler sqlite3_exec callback-free interface.
  • The DSN is simply a filesystem path to the .db file.

Data Structure Documentation

◆ sqlite_conn_t

struct sqlite_conn_t

Per-connection data for the SQLite driver.

Data Fields
sqlite3 * db

Function Documentation

◆ csilk_db_sqlite_init()

void csilk_db_sqlite_init ( void  )

Initialize and register the SQLite database driver with csilk.

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.

Note
This function is idempotent-safe but typically called once.

◆ sqlite_connect()

static int sqlite_connect ( csilk_db_pool_t pool,
const char *  dsn 
)
static

Open a connection to a SQLite database file.

Uses sqlite3_open_v2 with read/write+create flags. Stores the connection handle in the pool. On failure, frees the allocated connection and returns an error.

Parameters
poolThe database pool to initialize.
dsnFilesystem path to the SQLite database file.
Returns
0 on success, -1 if parameters are invalid or open fails.

◆ sqlite_disconnect()

static int sqlite_disconnect ( csilk_db_pool_t pool)
static

Close a SQLite connection and free the pool's connection data.

Calls sqlite3_close() on the underlying handle, frees the connection struct, and sets pool->connection to NULL.

Parameters
poolThe database pool to shut down.
Returns
0 on success, -1 if pool or its connection is NULL.

◆ sqlite_exec()

static int sqlite_exec ( csilk_db_pool_t pool,
const char *  sql 
)
static

Execute a SQL statement that returns no result rows.

Uses sqlite3_exec() for DDL/INSERT/UPDATE/DELETE statements.

Parameters
poolThe database pool (must be connected).
sqlSQL statement string.
Returns
0 on success, -1 on error.

Execute a SQL statement that returns no result rows.

Uses sqlite3_exec with no callback for DDL/INSERT/UPDATE/DELETE. The callback-free invocation discards any output rows silently. On error, the error message from sqlite3_errmsg is freed after logging.

◆ sqlite_free_result()

static void sqlite_free_result ( csilk_db_result_t result)
static

Free all memory associated with a query result set.

Iterates rows, column names, and the top-level arrays, freeing each allocation. Resets row_count and column_count to 0 after cleanup.

Note
Currently frees row structs directly without freeing the row->values[] array or its individual string elements.
Parameters
resultThe result set to free (may be NULL).

◆ sqlite_query()

static int sqlite_query ( csilk_db_pool_t pool,
const char *  sql,
csilk_db_result_t result 
)
static

Execute a SQL query and return the full result set.

Prepares the statement, retrieves column names, then steps through all rows, allocating each row's values array. On any allocation failure, partial results are freed and -1 is returned.

Parameters
poolThe database pool (must be connected).
sqlSQL query string.
result[out] Populated result set (must be freed with sqlite_free_result).
Returns
0 on success, -1 on error.

Execute a SELECT query and buffer the full result set.

Flow:

  1. Prepare the statement via sqlite3_prepare_v2.
  2. Extract column metadata from the prepared statement.
  3. Step through rows with sqlite3_step, allocating a csilk_db_row_t per row. Each cell is extracted via sqlite3_column_text and duplicated via strdup (NULL cells become empty strings).
  4. Finalize the statement. On any allocation failure, partial results are freed and -1 is returned.

◆ sqlite_transaction_begin()

static int sqlite_transaction_begin ( csilk_db_pool_t pool)
static

Begin a SQLite transaction (delegates to sqlite_exec).

Parameters
poolThe database pool.
Returns
0 on success, -1 on error.

◆ sqlite_transaction_commit()

static int sqlite_transaction_commit ( csilk_db_pool_t pool)
static

Commit the current SQLite transaction.

Parameters
poolThe database pool.
Returns
0 on success, -1 on error.

◆ sqlite_transaction_rollback()

static int sqlite_transaction_rollback ( csilk_db_pool_t pool)
static

Rollback the current SQLite transaction.

Parameters
poolThe database pool.
Returns
0 on success, -1 on error.

Variable Documentation

◆ csilk_db_sqlite_driver

csilk_db_driver_t csilk_db_sqlite_driver
Initial value:
= {
.name = "sqlite",
.connect = sqlite_connect,
.disconnect = sqlite_disconnect,
.query = sqlite_query,
.exec = sqlite_exec,
.transaction_begin = sqlite_transaction_begin,
.transaction_commit = sqlite_transaction_commit,
.transaction_rollback = sqlite_transaction_rollback,
.free_result = sqlite_free_result,
}
static int sqlite_query(csilk_db_pool_t *pool, const char *sql, csilk_db_result_t *result)
Execute a SQL query and return the full result set.
Definition sqlite.c:148
static int sqlite_transaction_rollback(csilk_db_pool_t *pool)
Rollback the current SQLite transaction.
Definition sqlite.c:273
static int sqlite_disconnect(csilk_db_pool_t *pool)
Close a SQLite connection and free the pool's connection data.
Definition sqlite.c:73
static int sqlite_exec(csilk_db_pool_t *pool, const char *sql)
Execute a SQL statement that returns no result rows.
Definition sqlite.c:229
static void sqlite_free_result(csilk_db_result_t *result)
Free all memory associated with a query result set.
Definition sqlite.c:98
static int sqlite_transaction_begin(csilk_db_pool_t *pool)
Begin a SQLite transaction (delegates to sqlite_exec).
Definition sqlite.c:253
static int sqlite_connect(csilk_db_pool_t *pool, const char *dsn)
Open a connection to a SQLite database file.
Definition sqlite.c:40
static int sqlite_transaction_commit(csilk_db_pool_t *pool)
Commit the current SQLite transaction.
Definition sqlite.c:263

Pre-built driver vtable for the SQLite3 database backend.

Registered automatically when csilk_db_sqlite_init() is called. Users can also reference this struct directly to register manually.