cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
orm.c File Reference

Tiny ORM client over a framed Unix socket stream. More...

#include <cwist/core/orm/orm.h>
#include <cwist/core/mem/alloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <stdint.h>
#include <arpa/inet.h>
Include dependency graph for orm.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

cwist_orm_t * cwist_orm_open_socket (int socket_fd)
 Wrap a connected socket descriptor in an ORM session.
 
void cwist_orm_close_socket (cwist_orm_t *orm)
 Close an ORM session and release its socket.
 
void cwist_orm_immediate_commit (bool enable)
 Set the global immediate-commit default for new ORM sessions.
 
void cwist_orm_use_dialect (cwist_orm_t *orm, cwist_orm_dialect_t dialect)
 Select the SQL dialect used for query generation.
 
cwist_error_t cwist_orm_commit (cwist_orm_t *orm)
 Commit the current transaction.
 
cwist_error_t cwist_orm_rollback (cwist_orm_t *orm)
 Roll back the current transaction.
 
cwist_error_t cwist_orm_exec (cwist_orm_t *orm, const char *sql)
 Execute a single SQL statement, discarding any result rows.
 
cwist_error_t cwist_orm_query (cwist_orm_t *orm, const char *sql, cJSON **result)
 Execute a SELECT-style query and return the result rows.
 
cwist_error_t cwist_orm_insert (cwist_orm_t *orm, const char *table, const cJSON *data)
 Insert a JSON object into a table.
 
cwist_error_t cwist_orm_update (cwist_orm_t *orm, const char *table, const cJSON *data, const char *where_clause)
 Update rows of a table with values from a JSON object.
 
cwist_error_t cwist_orm_delete (cwist_orm_t *orm, const char *table, const char *where_clause)
 Delete rows from a table.
 
cwist_error_t cwist_orm_select (cwist_orm_t *orm, const char *table, const char *columns, const char *where_clause, cJSON **result)
 Select rows from a table.
 
cwist_error_t cwist_orm_insert_returning_json (cwist_orm_t *orm, const char *table, const cJSON *data, const char *returning_col, cJSON **out)
 Insert a row and return a JSON value from the new row.
 
cwist_error_t cwist_orm_insert_returning_int (cwist_orm_t *orm, const char *table, const cJSON *data, const char *returning_col, int *out)
 Insert a row and return a column value as an int.
 
cwist_error_t cwist_orm_insert_returning_long (cwist_orm_t *orm, const char *table, const cJSON *data, const char *returning_col, long *out)
 Insert a row and return a column value as a long.
 
cwist_error_t cwist_orm_insert_returning_llong (cwist_orm_t *orm, const char *table, const cJSON *data, const char *returning_col, long long *out)
 Insert a row and return a column value as a long long.
 
cwist_error_t cwist_orm_select_one_json (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, cJSON **out)
 Select a single column value from the first matching row as JSON.
 
cwist_error_t cwist_orm_select_one_int (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, int *out)
 Select a single column value from the first matching row as an int.
 
cwist_error_t cwist_orm_select_one_long (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, long *out)
 Select a single column value from the first matching row as a long.
 
cwist_error_t cwist_orm_select_one_llong (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, long long *out)
 Select a single column value from the first matching row as a long long.
 
cwist_error_t cwist_orm_select_one_double (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, double *out)
 Select a single column value from the first matching row as a double.
 
cwist_error_t cwist_orm_select_one_string (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, char **out)
 Select a single column value from the first matching row as a string.
 
cwist_error_t cwist_orm_select_one_i64 (cwist_orm_t *orm, const char *table, const char *column, const char *where_clause, int64_t *out)
 Select a single integer cell exactly as an int64_t.
 
cwist_error_t cwist_orm_insert_returning_i64 (cwist_orm_t *orm, const char *table, const cJSON *data, const char *returning_col, int64_t *out)
 Insert a row and return an integer column exactly as an int64_t.
 
cwist_error_t cwist_orm_query_typed (cwist_orm_t *orm, const char *sql, cwist_orm_result_t **result)
 Execute a SELECT-style query and return column-typed rows.
 
void cwist_orm_result_free (cwist_orm_result_t *result)
 Release a result set from cwist_orm_query_typed().
 
size_t cwist_orm_result_column_index (const cwist_orm_result_t *result, const char *column)
 Find a column index by name in a typed result set.
 
const cwist_orm_value_t * cwist_orm_result_cell (const cwist_orm_result_t *result, size_t row, size_t column)
 Read one cell of a typed result set.
 

Detailed Description

Tiny ORM client over a framed Unix socket stream.

This implementation never includes sqlite3.h. All communication with the SQL backend is serialised through a socket produced by cwist_db_transfer_sqlite_to_socket().

Definition in file orm.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 11 of file orm.c.

Function Documentation

◆ cwist_orm_close_socket()

void cwist_orm_close_socket ( cwist_orm_t *  orm)

Close an ORM session and release its socket.

Destroy an ORM session and close its underlying socket.

Issues a best-effort ROLLBACK to discard any dangling transaction, then closes the socket descriptor and frees the handle. Safe to call with NULL.

Parameters
ormSession to close (may be NULL).

Definition at line 163 of file orm.c.

References cap.

◆ cwist_orm_commit()

cwist_error_t cwist_orm_commit ( cwist_orm_t *  orm)

Commit the current transaction.

Parameters
ormActive session.
Returns
CWIST_SUCCESS on success, otherwise an error code.

Definition at line 211 of file orm.c.

References cap, and cwist_orm_exec().

◆ cwist_orm_delete()

cwist_error_t cwist_orm_delete ( cwist_orm_t *  orm,
const char *  table,
const char *  where_clause 
)

Delete rows from a table.

If where_clause is non-empty it is appended as a WHERE clause; a NULL or empty clause deletes all rows.

Parameters
ormActive session.
tableTable name.
where_clauseOptional raw SQL WHERE condition (not escaped).
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_NOMEM on allocation failure, otherwise the error from cwist_orm_exec().

Definition at line 727 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, cwist_orm_exec(), and make_error().

◆ cwist_orm_exec()

cwist_error_t cwist_orm_exec ( cwist_orm_t *  orm,
const char *  sql 
)

Execute a single SQL statement, discarding any result rows.

Execute a non-query SQL statement.

If immediate-commit mode is enabled, an INSERT/UPDATE/DELETE statement is followed by an automatic COMMIT.

Parameters
ormActive session.
sqlNull-terminated SQL statement.
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_IO on transport failure, or CWIST_ERROR_PROTOCOL when the backend reports an error.

Definition at line 241 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_IO, CWIST_ERROR_PROTOCOL, CWIST_SUCCESS, and make_error().

Referenced by cwist_orm_commit(), cwist_orm_delete(), cwist_orm_insert(), cwist_orm_rollback(), and cwist_orm_update().

◆ cwist_orm_immediate_commit()

void cwist_orm_immediate_commit ( bool  enable)

Set the global immediate-commit default for new ORM sessions.

Toggle automatic commit mode.

Sessions opened afterwards inherit this value; existing sessions are not affected. When enabled, INSERT/UPDATE/DELETE statements executed through cwist_orm_exec() are followed by an automatic COMMIT.

Parameters
enabletrue to enable immediate commit, false to disable.

Definition at line 188 of file orm.c.

References cap.

◆ cwist_orm_insert()

cwist_error_t cwist_orm_insert ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data 
)

Insert a JSON object into a table.

Column names and values are taken from the object's keys/values and rendered according to the session dialect.

Parameters
ormActive session.
tableTable name.
dataJSON object with column names as keys.
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments or a non-object data, CWIST_ERROR_NOMEM on allocation failure, otherwise the error from cwist_orm_exec().

Definition at line 586 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, cwist_orm_exec(), and make_error().

◆ cwist_orm_insert_returning_i64()

cwist_error_t cwist_orm_insert_returning_i64 ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  returning_col,
int64_t *  out 
)

Insert a row and return an integer column exactly as an int64_t.

Same statement as cwist_orm_insert_returning_llong(), with the returned value read as in cwist_orm_select_one_i64().

Returns
As cwist_orm_select_one_i64(); CWIST_ERROR_NOMEM when the SQL cannot be built.

Definition at line 1404 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, and make_error().

◆ cwist_orm_insert_returning_int()

cwist_error_t cwist_orm_insert_returning_int ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  returning_col,
int *  out 
)

Insert a row and return a column value as an int.

Parameters
ormActive session.
tableTable name.
datacJSON object with the column values to insert.
returning_colColumn (or alias) of the value to return.
out[out] Receives the value as an int.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_insert_returning_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 932 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_insert_returning_json().

◆ cwist_orm_insert_returning_json()

cwist_error_t cwist_orm_insert_returning_json ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  returning_col,
cJSON **  out 
)

Insert a row and return a JSON value from the new row.

Appends a dialect-specific RETURNING clause (or an equivalent last-insert-id follow-up query) to the INSERT built from data, then extracts the returning_col value from the first result row. For MySQL/SQLite the returned value is the last insert id, not an arbitrary column.

Parameters
ormActive session.
tableTable name.
datacJSON object with the column values to insert.
returning_colColumn (or alias) of the value to return.
out[out] Receives a duplicated cJSON value; caller must release with cJSON_Delete().
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_NOMEM on allocation failure, CWIST_ERROR_PROTOCOL when the value is missing from the result.

Definition at line 872 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, CWIST_ERROR_PROTOCOL, cwist_orm_query(), and make_error().

Referenced by cwist_orm_insert_returning_int(), cwist_orm_insert_returning_llong(), and cwist_orm_insert_returning_long().

◆ cwist_orm_insert_returning_llong()

cwist_error_t cwist_orm_insert_returning_llong ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  returning_col,
long long *  out 
)

Insert a row and return a column value as a long long.

Parameters
ormActive session.
tableTable name.
datacJSON object with the column values to insert.
returning_colColumn (or alias) of the value to return.
out[out] Receives the value as a long long.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_insert_returning_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 985 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_insert_returning_json().

◆ cwist_orm_insert_returning_long()

cwist_error_t cwist_orm_insert_returning_long ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  returning_col,
long *  out 
)

Insert a row and return a column value as a long.

Parameters
ormActive session.
tableTable name.
datacJSON object with the column values to insert.
returning_colColumn (or alias) of the value to return.
out[out] Receives the value as a long.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_insert_returning_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 958 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_insert_returning_json().

◆ cwist_orm_open_socket()

cwist_orm_t * cwist_orm_open_socket ( int  socket_fd)

Wrap a connected socket descriptor in an ORM session.

Create an ORM session from an existing SQL socket.

Takes ownership of socket_fd: the descriptor is closed by cwist_orm_close_socket(). The session inherits the current global immediate-commit default and defaults to the SQLite dialect.

Parameters
socket_fdConnected socket descriptor (must be >= 0).
Returns
New ORM handle, or NULL on invalid descriptor or allocation failure.

Definition at line 145 of file orm.c.

References cap, and CWIST_ORM_SQLITE.

◆ cwist_orm_query()

cwist_error_t cwist_orm_query ( cwist_orm_t *  orm,
const char *  sql,
cJSON **  result 
)

Execute a SELECT-style query and return the result rows.

Execute a SELECT query and return the result set.

On success *result receives a cJSON array of row objects (empty array if no rows) that the caller must release with cJSON_Delete().

Parameters
ormActive session.
sqlNull-terminated SQL query.
result[out] Receives the rows array.
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_IO on transport failure, or CWIST_ERROR_PROTOCOL on backend error or malformed response.

Definition at line 291 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_IO, CWIST_ERROR_PROTOCOL, CWIST_SUCCESS, and make_error().

Referenced by cwist_orm_insert_returning_json(), cwist_orm_select(), and cwist_orm_select_one_json().

◆ cwist_orm_query_typed()

cwist_error_t cwist_orm_query_typed ( cwist_orm_t *  orm,
const char *  sql,
cwist_orm_result_t **  result 
)

Execute a SELECT-style query and return column-typed rows.

Reads the worker's "typed" payload, which types every cell from its SQLite storage class: INTEGER as exact int64 digits, REAL as a double, TEXT as a string, BLOB as decoded bytes with its length, and NULL as a distinct type. On success *result receives a heap-allocated result set the caller releases with cwist_orm_result_free().

Parameters
ormActive session.
sqlNull-terminated SQL query.
result[out] Receives the result set, or NULL on failure.
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_IO on transport failure, or CWIST_ERROR_PROTOCOL on a backend error or a malformed typed payload.

Definition at line 1564 of file orm.c.

References cap, cwist_orm_result::cells, cwist_orm_result::column_count, cwist_orm_result::columns, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_IO, CWIST_ERROR_NOMEM, CWIST_ERROR_PROTOCOL, cwist_orm_result_free(), CWIST_SUCCESS, make_error(), and cwist_orm_result::row_count.

◆ cwist_orm_result_cell()

const cwist_orm_value_t * cwist_orm_result_cell ( const cwist_orm_result_t *  result,
size_t  row,
size_t  column 
)

Read one cell of a typed result set.

Parameters
resultResult set.
rowRow index.
columnColumn index.
Returns
Pointer to the cell, or NULL when the coordinates are out of range or result is NULL. The pointer is owned by the result set and stays valid until cwist_orm_result_free().

Definition at line 1688 of file orm.c.

References cap, cwist_orm_result::cells, cwist_orm_result::column_count, and cwist_orm_result::row_count.

◆ cwist_orm_result_column_index()

size_t cwist_orm_result_column_index ( const cwist_orm_result_t *  result,
const char *  column 
)

Find a column index by name in a typed result set.

Parameters
resultResult set.
columnColumn name.
Returns
The column index, or result->column_count when the name is not present (or either argument is NULL).

Definition at line 1680 of file orm.c.

References cap, cwist_orm_result::column_count, and cwist_orm_result::columns.

◆ cwist_orm_result_free()

void cwist_orm_result_free ( cwist_orm_result_t *  result)

Release a result set from cwist_orm_query_typed().

Frees the column names, every owned cell payload, and the structure itself. Safe to call with NULL.

Parameters
resultResult set to free.

Definition at line 1663 of file orm.c.

References cwist_orm_value::blob, cap, cwist_orm_result::cells, cwist_orm_result::column_count, cwist_orm_result::columns, cwist_orm_result::row_count, and cwist_orm_value::text.

Referenced by cwist_orm_query_typed().

◆ cwist_orm_rollback()

cwist_error_t cwist_orm_rollback ( cwist_orm_t *  orm)

Roll back the current transaction.

Parameters
ormActive session.
Returns
CWIST_SUCCESS on success, otherwise an error code.

Definition at line 221 of file orm.c.

References cap, and cwist_orm_exec().

◆ cwist_orm_select()

cwist_error_t cwist_orm_select ( cwist_orm_t *  orm,
const char *  table,
const char *  columns,
const char *  where_clause,
cJSON **  result 
)

Select rows from a table.

Convenience SELECT helper.

Parameters
ormActive session.
tableTable name.
columnsRaw comma-separated column list (not escaped).
where_clauseOptional raw SQL WHERE condition (not escaped).
result[out] Receives a cJSON array of row objects; caller must release with cJSON_Delete().
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_NOMEM on allocation failure, otherwise the error from cwist_orm_query().

Definition at line 773 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, cwist_orm_query(), and make_error().

◆ cwist_orm_select_one_double()

cwist_error_t cwist_orm_select_one_double ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
double *  out 
)

Select a single column value from the first matching row as a double.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives the value as a double.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_select_one_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 1190 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().

◆ cwist_orm_select_one_i64()

cwist_error_t cwist_orm_select_one_i64 ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
int64_t *  out 
)

Select a single integer cell exactly as an int64_t.

Same query as cwist_orm_select_one_llong(), but the value is read from the integer digits the worker sent instead of through a cJSON number (a double), so integers above 2^53 (snowflake-style IDs) keep every digit. The cell must be a JSON integer, or a JSON string holding canonical integer text (an optional '-' and digits, no leading zeros).

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives the value.
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments; CWIST_ERROR_IO when the worker exchange fails; CWIST_ERROR_PROTOCOL when no row is returned, or the value is not an integer or does not fit in int64_t. out is unchanged on failure.

Definition at line 1387 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, and make_error().

◆ cwist_orm_select_one_int()

cwist_error_t cwist_orm_select_one_int ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
int *  out 
)

Select a single column value from the first matching row as an int.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives the value as an int.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_select_one_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 1112 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().

◆ cwist_orm_select_one_json()

cwist_error_t cwist_orm_select_one_json ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
cJSON **  out 
)

Select a single column value from the first matching row as JSON.

Runs a SELECT column FROM table [WHERE ...] LIMIT 1 query and duplicates the column value from the first result row.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives a duplicated cJSON value; caller must release with cJSON_Delete().
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments, CWIST_ERROR_NOMEM on allocation failure, CWIST_ERROR_PROTOCOL when no row or column value is returned.

Definition at line 1053 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, CWIST_ERROR_PROTOCOL, cwist_orm_query(), and make_error().

Referenced by cwist_orm_select_one_double(), cwist_orm_select_one_int(), cwist_orm_select_one_llong(), cwist_orm_select_one_long(), and cwist_orm_select_one_string().

◆ cwist_orm_select_one_llong()

cwist_error_t cwist_orm_select_one_llong ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
long long *  out 
)

Select a single column value from the first matching row as a long long.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives the value as a long long.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_select_one_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 1164 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().

◆ cwist_orm_select_one_long()

cwist_error_t cwist_orm_select_one_long ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
long *  out 
)

Select a single column value from the first matching row as a long.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives the value as a long.
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_select_one_json() or CWIST_ERROR_PROTOCOL when the returned value is not a number.

Definition at line 1138 of file orm.c.

References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().

◆ cwist_orm_select_one_string()

cwist_error_t cwist_orm_select_one_string ( cwist_orm_t *  orm,
const char *  table,
const char *  column,
const char *  where_clause,
char **  out 
)

Select a single column value from the first matching row as a string.

Parameters
ormActive session.
tableTable name.
columnColumn name.
where_clauseOptional raw SQL WHERE condition (not escaped).
out[out] Receives a heap-allocated copy of the string; caller must release with free().
Returns
CWIST_SUCCESS on success; otherwise the error from cwist_orm_select_one_json(), CWIST_ERROR_PROTOCOL when the returned value is not a string, or CWIST_ERROR_NOMEM when the string copy cannot be allocated.

Definition at line 1218 of file orm.c.

References cap, CWIST_ERROR_NOMEM, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().

◆ cwist_orm_update()

cwist_error_t cwist_orm_update ( cwist_orm_t *  orm,
const char *  table,
const cJSON *  data,
const char *  where_clause 
)

Update rows of a table with values from a JSON object.

Update rows in a table.

Builds UPDATE table SET key=value,... from the object's entries; an empty object is rejected. If where_clause is non-empty it is appended as a WHERE clause (a NULL or empty clause updates all rows).

Parameters
ormActive session.
tableTable name.
dataJSON object with column names as keys (must be non-empty).
where_clauseOptional raw SQL WHERE condition (not escaped).
Returns
CWIST_SUCCESS on success; CWIST_ERROR_INVALID_PARAM for NULL arguments or an empty object, CWIST_ERROR_NOMEM on allocation failure, otherwise the error from cwist_orm_exec().

Definition at line 629 of file orm.c.

References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, cwist_orm_exec(), and make_error().

◆ cwist_orm_use_dialect()

void cwist_orm_use_dialect ( cwist_orm_t *  orm,
cwist_orm_dialect_t  dialect 
)

Select the SQL dialect used for query generation.

Affects identifier quoting and dialect-specific statements generated by the high-level helpers. Does nothing if orm is NULL.

Parameters
ormSession to configure.
dialectDialect to use (e.g. CWIST_ORM_SQLITE).

Definition at line 201 of file orm.c.

References cap.