Skip to content

Instantly share code, notes, and snippets.

@abimaelmartell
Last active January 1, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abimaelmartell/8140541 to your computer and use it in GitHub Desktop.
Save abimaelmartell/8140541 to your computer and use it in GitHub Desktop.
ejdb example
all:
gcc main.c -o main -ltcejdb -std=c99 -Wall -fPIC -pedantic -O2
#include <tcejdb/ejdb.h>
#include <stdio.h>
int main(void){
// lets open our database
EJDB *database = ejdbnew();
if(!ejdbopen(database, "addressbook", JBOWRITER | JBOCREAT | JBOTRUNC)){
// cant open database
return 1;
}
EJCOLL *contacts_collection = ejdbcreatecoll(database, "contacts", NULL);
for(int i = 0; i < 100000; i++){
bson bsrec;
bson_oid_t oid;
bson_init(&bsrec);
bson_append_string(&bsrec, "name", "John Travolta");
bson_append_string(&bsrec, "phone", "333-222-333");
bson_append_int(&bsrec, "age", 58);
bson_finish(&bsrec);
ejdbsavebson(contacts_collection, &bsrec, &oid);
bson_destroy(&bsrec);
}
// query all records
bson bson_query;
bson_init_as_query(&bson_query);
bson_finish(&bson_query);
EJQ *query = ejdbcreatequery(database, &bson_query, NULL, 0, NULL);
uint32_t records_count;
TCLIST *res = ejdbqryexecute(contacts_collection, query, &records_count, 0, NULL);
printf("\n\nRecords Count -> %d\n\n", records_count);
/*for (int i = 0; i < TCLISTNUM(res); ++i) {
void *bsdata = TCLISTVALPTR(res, i);
bson_print_raw(bsdata, 0);
}*/
tclistdel(res);
ejdbquerydel(query);
bson_destroy(&bson_query);
ejdbclose(database);
ejdbdel(database);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment