Subzero C Shared Library
Loading...
Searching...
No Matches
SubZero C Shared Library Documentation

Introduction

This library provides the low level C API for the SubZero. It's intended to be used by other languages to interface with the SubZero library.

The purpose of this library is to take an HTTP request and return a SQL statement that can be used to query a database and return the fully formed response body.

The HTTP request needs to conform to the SubZero HTTP request format (PostgREST compatible).

Usage

Here's a brief example on how to use the SubZero library:

// include the header file
#include "subzero.h"
// ... other includes
const char* db_type = "sqlite";
// 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 automatically
const char* db_schema_json = ""
"{"
" \"schemas\": ["
" {"
" \"name\": \"public\","
" \"objects\": ["
" {"
" \"kind\": \"table\","
" \"name\": \"clients\","
" \"columns\": ["
" {"
" \"name\": \"id\","
" \"data_type\": \"INTEGER\","
" \"primary_key\": true"
" },"
" {"
" \"name\": \"name\","
" \"data_type\": \"TEXT\","
" \"primary_key\": false"
" }"
" ],"
" \"foreign_keys\": []"
" }"
" ]"
" }"
" ]"
"}"
;
// main function
int main() {
sbz_DbSchema* db_schema = sbz_db_schema_new(db_type, db_schema_json, NULL);
if (db_schema == NULL) {
const int err_len = sbz_last_error_length();
char* err = (char*)malloc(err_len);
sbz_last_error_message(err, err_len);
printf("Error: %s\n", err);
free(err);
return -1;
}
const char* headers[] = {"Content-Type", "application/json", "Accept", "application/json"};
const char* env[] = {"user_id", "1"};
"GET",
"http://localhost/rest/projects?select=id,name",
NULL,
headers, 4,
env, 2
);
sbz_Statement* stmt = sbz_statement_new(
"public",
"/rest/",
"user",
db_schema,
req,
NULL
);
if (stmt == NULL) {
const int err_len = sbz_last_error_length();
char* err = (char*)malloc(err_len);
sbz_last_error_message(err, err_len);
printf("Error: %s\n", err);
free(err);
return -1;
}
const char* sql = sbz_statement_sql(stmt);
const char *const * params = sbz_statement_params(stmt);
const char *const * params_types = sbz_statement_params_types(stmt);
int params_count = sbz_statement_params_count(stmt);
printf("SQL: %s\n", sql);
printf("params: %s\n", params[0]);
printf("params_count: %d\n", params_count);
printf("params_types: %s\n", params_types[0]);
sbz_db_schema_free(db_schema);
return 0;
}
struct sbz_DbSchema * sbz_db_schema_new(const char *db_type, const char *db_schema_json, const char *license_key)
void sbz_statement_free(struct sbz_Statement *statement)
struct sbz_Statement sbz_Statement
Definition subzero.h:135
int sbz_last_error_length(void)
const char * sbz_statement_sql(const struct sbz_Statement *statement)
struct sbz_DbSchema sbz_DbSchema
Definition subzero.h:124
const char *const * sbz_statement_params(const struct sbz_Statement *statement)
struct sbz_HTTPRequest sbz_HTTPRequest
Definition subzero.h:130
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_db_schema_free(struct sbz_DbSchema *schema)
const char *const * sbz_statement_params_types(const struct sbz_Statement *statement)
int sbz_statement_params_count(const struct sbz_Statement *statement)
int sbz_last_error_message(char *buffer, int length)
void sbz_http_request_free(struct sbz_HTTPRequest *request)

For more information, navigate to the Files section.