Subzero C Shared Library
|
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Go to the source code of this file.
Typedefs | |
typedef struct sbz_DbSchema | sbz_DbSchema |
typedef struct sbz_HTTPRequest | sbz_HTTPRequest |
typedef struct sbz_Statement | sbz_Statement |
typedef struct sbz_TwoStageStatement | sbz_TwoStageStatement |
Functions | |
struct sbz_HTTPRequest * | sbz_http_request_new_with_clone (const char *method, const char *uri, const char *body, const char *const *headers, int headers_count, const char *const *env, int env_count) |
struct sbz_HTTPRequest * | sbz_http_request_new (const char *method, const char *uri, const char *body, const char *const *headers, int headers_count, const char *const *env, int env_count) |
void | sbz_http_request_free (struct sbz_HTTPRequest *request) |
struct sbz_TwoStageStatement * | sbz_two_stage_statement_new (const char *schema_name, const char *path_prefix, const char *role, const struct sbz_DbSchema *db_schema, const struct sbz_HTTPRequest *request, const char *max_rows) |
const struct sbz_Statement * | sbz_two_stage_statement_mutate (const struct sbz_TwoStageStatement *two_stage_statement) |
const struct sbz_Statement * | sbz_two_stage_statement_select (const struct sbz_TwoStageStatement *two_stage_statement) |
int | sbz_two_stage_statement_set_ids (struct sbz_TwoStageStatement *two_stage_statement, const char *const *ids, int ids_count) |
void | sbz_two_stage_statement_free (struct sbz_TwoStageStatement *two_stage_statement) |
struct sbz_Statement * | sbz_statement_main_new (const char *schema_name, const char *path_prefix, const char *role, const struct sbz_DbSchema *db_schema, const struct sbz_HTTPRequest *request, const char *max_rows) |
struct sbz_Statement * | sbz_statement_env_new (const struct sbz_DbSchema *db_schema, const struct sbz_HTTPRequest *request) |
const char * | sbz_statement_sql (const struct sbz_Statement *statement) |
const char *const * | sbz_statement_params (const struct sbz_Statement *statement) |
const char *const * | sbz_statement_params_types (const struct sbz_Statement *statement) |
int | sbz_statement_params_count (const struct sbz_Statement *statement) |
void | sbz_statement_free (struct sbz_Statement *statement) |
void | sbz_db_schema_free (struct sbz_DbSchema *schema) |
struct sbz_DbSchema * | sbz_db_schema_new (const char *db_type, const char *db_schema_json, const char *license_key) |
int | sbz_db_schema_is_demo (const struct sbz_DbSchema *db_schema) |
char * | sbz_introspection_query (const char *db_type, const char *path, const char *custom_relations, const char *custom_permissions) |
void | sbz_introspection_query_free (char *introspection_query) |
int | sbz_last_error_message (char *buffer, int length) |
void | sbz_clear_last_error (void) |
int | sbz_last_error_length (void) |
int | sbz_last_error_http_status (void) |
typedef struct sbz_DbSchema sbz_DbSchema |
A structure for holding the information about database entities and permissions (tables, views, etc).
typedef struct sbz_HTTPRequest sbz_HTTPRequest |
A structure representing a HTTP request. This is used to pass the information about the request to the subzero core.
typedef struct sbz_Statement sbz_Statement |
A structure representing a SQL statement (query and parameters).
typedef struct sbz_TwoStageStatement sbz_TwoStageStatement |
A structure representing a two-stage statement (mutate and select). The mutate statement is used to perform the mutation (insert, update, delete) and the select statement is used to retrieve the result. This is used for databases that do not support returning the result of a mutation (sqlite/mysql).
Both statements should be executed in the same transaction. The mutate statement will return two columns: id and _subzero_check__constraint You need to collect the ids and then call two_stage_statement_set_ids to set the ids for the select statement before calling sbz_two_stage_statement_select
. You also need to check the _subzero_check__constraint column to be "truethy" for all rows and rollback the transaction if it is not.
void sbz_db_schema_free | ( | struct sbz_DbSchema * | schema | ) |
int sbz_db_schema_is_demo | ( | const struct sbz_DbSchema * | db_schema | ) |
struct sbz_DbSchema * sbz_db_schema_new | ( | const char * | db_type, |
const char * | db_schema_json, | ||
const char * | license_key ) |
Create a new sbz_DbSchema
from a JSON string.
This function is marked unsafe because it dereferences raw pointers however we are careful to check for null pointers before dereferencing them.
db_type
- The type of database this schema is for. Currently supported types are "postgresql", "clickhouse", "sqlite", and "mysql".db_schema_json
- The JSON string representing the schema.license_key
- The license key for the subzero core. Pass NULL if you are running in demo mode. A pointer to the newly created DbSchema
or a null pointer if an error occurred.
Constructing the JSON schema is tedious and for this reason we provide "introspection queries" for each database type that can be used to generate the schema JSON.
void sbz_http_request_free | ( | struct sbz_HTTPRequest * | request | ) |
struct sbz_HTTPRequest * sbz_http_request_new | ( | const char * | method, |
const char * | uri, | ||
const char * | body, | ||
const char *const * | headers, | ||
int | headers_count, | ||
const char *const * | env, | ||
int | env_count ) |
Create a new sbz_HTTPRequest
method
- The HTTP method (GET, POST, PUT, DELETE, etc).uri
- The full URI of the request (including the query string, ex: http://example.com/path?query=string).body
- The body of the request (pass NULL if there is no body).headers
- An array of key-value pairs representing the headers of the request, it needs to contain an even number of elements.headers_count
- The number of elements in the headers
array.env
- An array of key-value pairs representing the environment data that needs to be available to the query. It needs to contain an even number of elements.env_count
- The number of elements in the env
array.A pointer to the newly created sbz_HTTPRequest
or a null pointer if an error occurred.
struct sbz_HTTPRequest * sbz_http_request_new_with_clone | ( | const char * | method, |
const char * | uri, | ||
const char * | body, | ||
const char *const * | headers, | ||
int | headers_count, | ||
const char *const * | env, | ||
int | env_count ) |
Create a new sbz_HTTPRequest
and take ownership of the strings. This is usefull when the caller can not guarantee that the strings will be valid for the lifetime of the sbz_HTTPRequest
.
method
- The HTTP method (GET, POST, PUT, DELETE, etc).uri
- The full URI of the request (including the query string, ex: http://example.com/path?query=string).body
- The body of the request (pass NULL if there is no body).headers
- An array of key-value pairs representing the headers of the request, it needs to contain an even number of elements.headers_count
- The number of elements in the headers
array.env
- An array of key-value pairs representing the environment data that needs to be available to the query. It needs to contain an even number of elements.env_count
- The number of elements in the env
array.A pointer to the newly created sbz_HTTPRequest
or a null pointer if an error occurred.
char * sbz_introspection_query | ( | const char * | db_type, |
const char * | path, | ||
const char * | custom_relations, | ||
const char * | custom_permissions ) |
Get the introspection query for a database type.
db_type
- The type of database to get the introspection query for.path
- The path to the directory where the introspection query files are located.custom_relations
- An optional JSON string representing custom relations to include in the introspection query.custom_permissions
- An optional JSON string representing custom permissions to include in the introspection query.A pointer to the introspection query as a C string.
void sbz_introspection_query_free | ( | char * | introspection_query | ) |
int sbz_last_error_http_status | ( | void | ) |
int sbz_last_error_length | ( | void | ) |
Calculate the number of bytes in the last error's error message not including any trailing null
characters.
int sbz_last_error_message | ( | char * | buffer, |
int | length ) |
Write the most recent error message into a caller-provided buffer as a UTF-8 string, returning the number of bytes written.
This function is marked unsafe because it dereferences raw pointers however we are careful to check for null pointers before dereferencing them.
This writes a UTF-8 string into the buffer. Windows users may need to convert it to a UTF-16 "unicode" afterwards.
buffer
- A pointer to a buffer to write the error message into.length
- The length of the buffer.If there are no recent errors then this returns 0
(because we wrote 0 bytes). -1
is returned if there are any errors, for example when passed a null pointer or a buffer of insufficient size.
struct sbz_Statement * sbz_statement_env_new | ( | const struct sbz_DbSchema * | db_schema, |
const struct sbz_HTTPRequest * | request ) |
void sbz_statement_free | ( | struct sbz_Statement * | statement | ) |
struct sbz_Statement * sbz_statement_main_new | ( | const char * | schema_name, |
const char * | path_prefix, | ||
const char * | role, | ||
const struct sbz_DbSchema * | db_schema, | ||
const struct sbz_HTTPRequest * | request, | ||
const char * | max_rows ) |
Create a new sbz_Statement
for the main query.
schema_name
- The name of the database schema for the current request (ex: public).path_prefix
- The prefix of the path for the current request (ex: /api/).role
- The role of the user making the requestdb_schema
- A pointer to the sbz_DbSchema
request
- A pointer to the sbz_HTTPRequest
max_rows
- The maximum number of rows to return (pass NULL if there is no limit, otherwise pass a string representing the number of rows).A pointer to the newly created sbz_Statement
or a null pointer if an error occurred.
const char *const * sbz_statement_params | ( | const struct sbz_Statement * | statement | ) |
int sbz_statement_params_count | ( | const struct sbz_Statement * | statement | ) |
const char *const * sbz_statement_params_types | ( | const struct sbz_Statement * | statement | ) |
const char * sbz_statement_sql | ( | const struct sbz_Statement * | statement | ) |
void sbz_two_stage_statement_free | ( | struct sbz_TwoStageStatement * | two_stage_statement | ) |
const struct sbz_Statement * sbz_two_stage_statement_mutate | ( | const struct sbz_TwoStageStatement * | two_stage_statement | ) |
struct sbz_TwoStageStatement * sbz_two_stage_statement_new | ( | const char * | schema_name, |
const char * | path_prefix, | ||
const char * | role, | ||
const struct sbz_DbSchema * | db_schema, | ||
const struct sbz_HTTPRequest * | request, | ||
const char * | max_rows ) |
Create a new sbz_TwoStageStatement
schema_name
- The name of the database schema for the current request (ex: public).path_prefix
- The prefix of the path for the current request (ex: /api/).db_schema
- A pointer to the sbz_DbSchema
request
- A pointer to the sbz_HTTPRequest
max_rows
- The maximum number of rows to return (pass NULL if there is no limit, otherwise pass a string representing the number of rows).A pointer to the newly created sbz_TwoStageStatement
or a null pointer if an error occurred.
const struct sbz_Statement * sbz_two_stage_statement_select | ( | const struct sbz_TwoStageStatement * | two_stage_statement | ) |
Get the select statement from a sbz_TwoStageStatement
.
two_stage_statement
- A pointer to the sbz_TwoStageStatement
.A pointer to the sbz_Statement
representing the select statement.
The ids of the mutated rows need to be set before calling this function using sbz_two_stage_statement_set_ids
.
int sbz_two_stage_statement_set_ids | ( | struct sbz_TwoStageStatement * | two_stage_statement, |
const char *const * | ids, | ||
int | ids_count ) |
Set the ids for a sbz_TwoStageStatement
.
two_stage_statement
- A pointer to the sbz_TwoStageStatement
.ids
- An array of strings representing the ids of the mutated rows.ids_count
- The number of ids in the ids
array.0 if successful, -1 if an error occurred.