|
cwist
Modern C web framework with libttak runtime
|
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>
Go to the source code of this file.
Macros | |
| #define | _GNU_SOURCE |
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.
| 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.
| orm | Session to close (may be NULL). |
Definition at line 163 of file orm.c.
References cap.
| cwist_error_t cwist_orm_commit | ( | cwist_orm_t * | orm | ) |
Commit the current transaction.
| orm | Active session. |
Definition at line 211 of file orm.c.
References cap, and cwist_orm_exec().
| 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.
| orm | Active session. |
| table | Table name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
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_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.
| orm | Active session. |
| sql | Null-terminated SQL statement. |
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().
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.
| enable | true to enable immediate commit, false to disable. |
Definition at line 188 of file orm.c.
References cap.
| 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.
| orm | Active session. |
| table | Table name. |
| data | JSON object with column names as keys. |
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_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().
Definition at line 1404 of file orm.c.
References cap, CWIST_ERR_INT16, CWIST_ERROR_INVALID_PARAM, CWIST_ERROR_NOMEM, and make_error().
| 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.
| orm | Active session. |
| table | Table name. |
| data | cJSON object with the column values to insert. |
| returning_col | Column (or alias) of the value to return. |
| out | [out] Receives the value as an int. |
Definition at line 932 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and 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.
| orm | Active session. |
| table | Table name. |
| data | cJSON object with the column values to insert. |
| returning_col | Column (or alias) of the value to return. |
| out | [out] Receives a duplicated cJSON value; caller must release with cJSON_Delete(). |
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_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.
| orm | Active session. |
| table | Table name. |
| data | cJSON object with the column values to insert. |
| returning_col | Column (or alias) of the value to return. |
| out | [out] Receives the value as a long long. |
Definition at line 985 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_insert_returning_json().
| 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.
| orm | Active session. |
| table | Table name. |
| data | cJSON object with the column values to insert. |
| returning_col | Column (or alias) of the value to return. |
| out | [out] Receives the value as a long. |
Definition at line 958 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_insert_returning_json().
| 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.
| socket_fd | Connected socket descriptor (must be >= 0). |
Definition at line 145 of file orm.c.
References cap, and CWIST_ORM_SQLITE.
| 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().
| orm | Active session. |
| sql | Null-terminated SQL query. |
| result | [out] Receives the rows array. |
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_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().
| orm | Active session. |
| sql | Null-terminated SQL query. |
| result | [out] Receives the result set, or NULL on failure. |
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.
| 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.
| result | Result set. |
| row | Row index. |
| column | Column index. |
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.
| 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.
| result | Result set. |
| column | Column name. |
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.
| 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.
| result | Result 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_error_t cwist_orm_rollback | ( | cwist_orm_t * | orm | ) |
Roll back the current transaction.
| orm | Active session. |
Definition at line 221 of file orm.c.
References cap, and cwist_orm_exec().
| 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.
| orm | Active session. |
| table | Table name. |
| columns | Raw comma-separated column list (not escaped). |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| result | [out] Receives a cJSON array of row objects; caller must release with cJSON_Delete(). |
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_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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives the value as a double. |
Definition at line 1190 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().
| 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).
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives the value. |
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_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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives the value as an int. |
Definition at line 1112 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and 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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives a duplicated cJSON value; caller must release with cJSON_Delete(). |
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_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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives the value as a long long. |
Definition at line 1164 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().
| 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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives the value as a long. |
Definition at line 1138 of file orm.c.
References cap, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().
| 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.
| orm | Active session. |
| table | Table name. |
| column | Column name. |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
| out | [out] Receives a heap-allocated copy of the string; caller must release with free(). |
Definition at line 1218 of file orm.c.
References cap, CWIST_ERROR_NOMEM, CWIST_ERROR_PROTOCOL, and cwist_orm_select_one_json().
| 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).
| orm | Active session. |
| table | Table name. |
| data | JSON object with column names as keys (must be non-empty). |
| where_clause | Optional raw SQL WHERE condition (not escaped). |
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().
| 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.
| orm | Session to configure. |
| dialect | Dialect to use (e.g. CWIST_ORM_SQLITE). |
Definition at line 201 of file orm.c.
References cap.