A more complex schema based on a call center example, showing how to map some SQL constructs onto the WiredTiger API.
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <wiredtiger.h>
const char *home = NULL;
typedef struct {
uint64_t id;
const char *name;
const char *address;
const char *phone;
} CUSTOMER;
typedef struct {
uint64_t id;
uint64_t call_date;
uint64_t cust_id;
uint64_t emp_id;
const char *call_type;
const char *notes;
} CALL;
int main(void)
{
int count, exact, ret;
CUSTOMER cust, *custp, cust_sample[] = {
{ 0, "Professor Oak", "LeafGreen Avenue", "123-456-7890" },
{ 0, "Lorelei", "Sevii Islands", "098-765-4321" },
{ 0, NULL, NULL, NULL }
};
CALL call, *callp, call_sample[] = {
{ 0, 32, 1, 2, "billing", "unavailable" },
{ 0, 33, 1, 2, "billing", "available" },
{ 0, 34, 1, 2, "reminder", "unavailable" },
{ 0, 35, 1, 2, "reminder", "available" },
{ 0, 0, 0, 0, NULL, NULL }
};
if (ret != 0) {
fprintf(stderr, "Error connecting to %s: %s\n",
return (1);
}
ret = session->
create(session,
"table:customers",
"key_format=r,"
"value_format=SSS,"
"columns=(id,name,address,phone),"
"colgroups=(main,address)");
ret = session->
create(session,
"colgroup:customers:main", "columns=(name,phone)");
ret = session->
create(session,
"colgroup:customers:address", "columns=(address)");
ret = session->
create(session,
"index:customers:phone", "columns=(phone)");
session, "table:customers", NULL, "append", &cursor);
for (custp = cust_sample; custp->name != NULL; custp++) {
custp->name, custp->address, custp->phone);
}
ret = cursor->
close(cursor);
ret = session->
create(session,
"table:calls",
"key_format=r,"
"value_format=qrrSS,"
"columns=(id,call_date,cust_id,emp_id,call_type,notes)");
ret = session->
create(session,
"index:calls:cust_date",
"columns=(cust_id,call_date)");
session, "table:calls", NULL, "append", &cursor);
for (callp = call_sample; callp->call_type != NULL; callp++) {
cursor->
set_value(cursor, callp->call_date, callp->cust_id,
callp->emp_id, callp->call_type, callp->notes);
}
ret = cursor->
close(cursor);
"index:customers:phone(id,name)", NULL, NULL, &cursor);
cursor->
set_key(cursor,
"123-456-7890");
if (ret == 0) {
ret = cursor->
get_value(cursor, &cust.id, &cust.name);
printf("Read customer record for %s (ID %" PRIu64 ")\n",
cust.name, cust.id);
}
ret = cursor->
close(cursor);
"index:calls:cust_date(cust_id,call_type,notes)",
NULL, NULL, &cursor);
cust.id = 1;
cursor->
set_key(cursor, cust.id + 1, 0);
if (ret == 0 && exact >= 0)
ret = cursor->
prev(cursor);
for (count = 0; ret == 0 && count < 3; ++count) {
&call.cust_id, &call.call_type, &call.notes);
if (call.cust_id != cust.id)
break;
printf("Call record: customer %" PRIu64 " (%s: %s)\n",
call.cust_id, call.call_type, call.notes);
ret = cursor->
prev(cursor);
}
ret = conn->
close(conn, NULL);
return (ret);
}