|
Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
|
A lightweight, JSON-backed ORM Query Builder with bound parameters. More...
#include <QueryBuilder.hpp>
Public Member Functions | |
| QueryBuilder (std::shared_ptr< DBClient > db, const std::string &table) | |
| QueryBuilder & | where (const std::string &field, const std::string &op, const std::string &val) |
Adds a WHERE condition. val is bound as a parameter. | |
| QueryBuilder & | where (const Expr &expr) |
| QueryBuilder & | order_by (const std::string &field, Order direction=Order::Asc) |
| QueryBuilder & | limit (uint64_t n) |
Returns at most n rows. | |
| QueryBuilder & | offset (uint64_t n) |
Skips the first n rows (use with order_by() for stable pages). | |
| QueryBuilder & | all () |
| QueryBuilder & | primary_key (const std::string &column) |
| The model's primary key column (default "id"). | |
| Statement | insert_statement (const ModelType &model, bool returning=false) const |
The INSERT that insert_async() runs (create_async() adds RETURNING *). An unset primary key is left out. | |
| Statement | select_statement () const |
The SELECT that get_async() runs (placeholders as ?). | |
| Statement | count_statement () const |
| The SELECT COUNT(*) that count_async() runs. | |
| Statement | update_statement (const nlohmann::json &changes) const |
| The UPDATE that update_async() runs. | |
| Statement | delete_statement () const |
| QueryGetAwaiter< DBClient, ModelType > | get_async () |
| Executes a SELECT query asynchronously and maps results to a vector of ModelType. | |
| QueryCountAwaiter< DBClient > | count_async () |
| Counts the matching rows. | |
| QueryInsertAwaiter< DBClient, ModelType > | update_async (const nlohmann::json &changes) |
| Updates the matching rows; resumes with the number affected. | |
| QueryInsertAwaiter< DBClient, ModelType > | update_async (const ModelType &model) |
| QueryInsertAwaiter< DBClient, ModelType > | remove_async () |
| Deletes the matching rows; resumes with the number affected. | |
| QueryInsertAwaiter< DBClient, ModelType > | insert_async (const ModelType &model) |
| Executes an INSERT query for a model, binding every value. | |
| QueryCreateAwaiter< DBClient, ModelType > | create_async (const ModelType &model) |
Inserts model and resumes with the row as stored, including the generated primary key and column defaults. | |
Private Member Functions | |
| void | append_where (Statement &st) const |
| void | require_condition (const char *what) const |
Static Private Member Functions | |
| static Statement | finish (Statement st) |
Private Attributes | |
| std::shared_ptr< DBClient > | db_ |
| std::string | table_ |
| std::vector< Expr > | wheres_ |
| std::vector< std::string > | orders_ |
| std::optional< uint64_t > | limit_ |
| std::optional< uint64_t > | offset_ |
| bool | all_rows_ = false |
| std::string | primary_key_ = "id" |
A lightweight, JSON-backed ORM Query Builder with bound parameters.
Values are always bound, never spliced. Table and column names must be plain identifiers; SQL reserved words among them (user, order, ...) are quoted for the client's dialect.
|
inline |
| std::invalid_argument | if table is not a plain identifier. |
|
inline |
Allows update_async()/remove_async() without a WHERE condition, i.e. on every row. Without it they throw, so a forgotten where() cannot rewrite or empty a whole table.
|
inlineprivate |
|
inline |
Counts the matching rows.
|
inline |
The SELECT COUNT(*) that count_async() runs.
|
inline |
Inserts model and resumes with the row as stored, including the generated primary key and column defaults.
Uses INSERT ... RETURNING *: PostgreSQL, and MariaDB 10.5 or later (MySQL Server has no RETURNING and reports an error).
| DatabaseError | (on co_await) if the INSERT fails. |
|
inline |
The DELETE that remove_async() runs.
| std::logic_error | without a WHERE condition unless all() was called. |
|
inlinestaticprivate |
|
inline |
Executes a SELECT query asynchronously and maps results to a vector of ModelType.
|
inline |
Executes an INSERT query for a model, binding every value.
| std::invalid_argument | if a field name is not a plain identifier. |
Inserts model; resumes with the number of rows inserted (1). An unset primary key (see primary_key()) is left for the database.
| DatabaseError | (on co_await) if the INSERT fails. |
|
inline |
The INSERT that insert_async() runs (create_async() adds RETURNING *). An unset primary key is left out.
| std::invalid_argument | if the model does not serialise to a JSON object with at least one column to insert. |
|
inline |
Returns at most n rows.
|
inline |
Skips the first n rows (use with order_by() for stable pages).
|
inline |
Sorts the results; call again for further sort keys.
| std::invalid_argument | if field is not a plain identifier. |
|
inline |
The model's primary key column (default "id").
insert_async() and create_async() leave the key out when it is unset (null, 0 or an empty string) so the database generates it, and update_async(model) never rewrites it.
| std::invalid_argument | if column is not a plain identifier. |
|
inline |
Deletes the matching rows; resumes with the number affected.
|
inlineprivate |
|
inline |
The SELECT that get_async() runs (placeholders as ?).
|
inline |
Updates the matching rows to the fields of model, except its primary key (a default-valued id would otherwise overwrite the key).
|
inline |
Updates the matching rows; resumes with the number affected.
|
inline |
The UPDATE that update_async() runs.
| changes | A JSON object of column -> new value (null sets NULL). |
| std::invalid_argument | if changes is not a non-empty object or names an invalid column; std::logic_error without a WHERE condition unless all() was called. |
|
inline |
|
inline |
Adds a WHERE condition. val is bound as a parameter.
| std::invalid_argument | for an invalid field name or an operator other than =, !=, <>, <, <=, >, >=, [NOT] LIKE, [NOT] ILIKE. |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |