1# C API 2 3EJDB can be embedded into any `C/C++` application. 4`C API` documented in the following headers: 5 6* [ejdb.h](https://github.com/Softmotions/ejdb/blob/master/src/ejdb2.h) Main API functions 7* [jbl.h](https://github.com/Softmotions/ejdb/blob/master/src/jbl/jbl.h) JSON documents management API 8* [jql.h](https://github.com/Softmotions/ejdb/blob/master/src/jql/jql.h) Query building API 9 10Example application: 11```c 12#include <ejdb2/ejdb2.h> 13 14#define CHECK(rc_) \ 15 if (rc_) { \ 16 iwlog_ecode_error3(rc_); \ 17 return 1; \ 18 } 19 20static iwrc documents_visitor(EJDB_EXEC *ctx, const EJDB_DOC doc, int64_t *step) { 21 // Print document to stderr 22 return jbl_as_json(doc->raw, jbl_fstream_json_printer, stderr, JBL_PRINT_PRETTY); 23} 24 25int main() { 26 27 EJDB_OPTS opts = { 28 .kv = { 29 .path = "example.db", 30 .oflags = IWKV_TRUNC 31 } 32 }; 33 EJDB db; // EJDB2 storage handle 34 int64_t id; // Document id placeholder 35 JQL q = 0; // Query instance 36 JBL jbl = 0; // Json document 37 38 iwrc rc = ejdb_init(); 39 CHECK(rc); 40 41 rc = ejdb_open(&opts, &db); 42 CHECK(rc); 43 44 // First record 45 rc = jbl_from_json(&jbl, "{\"name\":\"Bianca\", \"age\":4}"); 46 RCGO(rc, finish); 47 rc = ejdb_put_new(db, "parrots", jbl, &id); 48 RCGO(rc, finish); 49 jbl_destroy(&jbl); 50 51 // Second record 52 rc = jbl_from_json(&jbl, "{\"name\":\"Darko\", \"age\":8}"); 53 RCGO(rc, finish); 54 rc = ejdb_put_new(db, "parrots", jbl, &id); 55 RCGO(rc, finish); 56 jbl_destroy(&jbl); 57 58 // Now execute a query 59 rc = jql_create(&q, "parrots", "/[age > :age]"); 60 RCGO(rc, finish); 61 62 EJDB_EXEC ux = { 63 .db = db, 64 .q = q, 65 .visitor = documents_visitor 66 }; 67 68 // Set query placeholder value. 69 // Actual query will be /[age > 3] 70 rc = jql_set_i64(q, "age", 0, 3); 71 RCGO(rc, finish); 72 73 // Now execute the query 74 rc = ejdb_exec(&ux); 75 76finish: 77 jql_destroy(&q); 78 jbl_destroy(&jbl); 79 ejdb_close(&db); 80 CHECK(rc); 81 return 0; 82} 83``` 84 85Compile and run: 86``` 87gcc -std=gnu11 -Wall -pedantic -c -o example1.o example1.c 88gcc -o example1 example1.o -lejdb2 89 90./example1 91{ 92 "name": "Darko", 93 "age": 8 94}{ 95 "name": "Bianca", 96 "age": 4 97} 98``` 99