A more complex schema based on a call center example, showing how to map some SQL constructs onto the WiredTiger API.
#include <test_util.h>
static const char *home;
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(int argc, char *argv[])
{
int count, exact;
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}};
home = example_setup(argc, argv);
error_check(conn->
open_session(conn, NULL, NULL, &session));
error_check(session->
create(session,
"table:customers",
"key_format=r,"
"value_format=SSS,"
"columns=(id,name,address,phone),"
"colgroups=(main,address)"));
error_check(session->
create(session,
"colgroup:customers:main",
"columns=(name,phone)"));
error_check(session->
create(session,
"colgroup:customers:address",
"columns=(address)"));
error_check(session->
create(session,
"index:customers:phone",
"columns=(phone)"));
error_check(session->
open_cursor(session,
"table:customers", NULL,
"append", &cursor));
for (custp = cust_sample; custp->name != NULL; custp++) {
cursor->
set_value(cursor, custp->name, custp->address, custp->phone);
error_check(cursor->
insert(cursor));
}
error_check(cursor->
close(cursor));
error_check(session->
create(session,
"table:calls",
"key_format=r,"
"value_format=qrrSS,"
"columns=(id,call_date,cust_id,emp_id,call_type,notes)"));
error_check(session->
create(session,
"index:calls:cust_date",
"columns=(cust_id,call_date)"));
error_check(session->
open_cursor(session,
"table:calls", NULL,
"append", &cursor));
for (callp = call_sample; callp->call_type != NULL; callp++) {
cursor, callp->call_date, callp->cust_id, callp->emp_id, callp->call_type, callp->notes);
error_check(cursor->
insert(cursor));
}
error_check(cursor->
close(cursor));
error_check(
session->
open_cursor(session,
"index:customers:phone(id,name)", NULL, NULL, &cursor));
cursor->
set_key(cursor,
"123-456-7890");
error_check(cursor->
search(cursor));
error_check(cursor->
get_value(cursor, &cust.id, &cust.name));
printf("Read customer record for %s (ID %" PRIu64 ")\n", cust.name, cust.id);
error_check(cursor->
close(cursor));
session, "index:calls:cust_date(cust_id,call_type,notes)", NULL, NULL, &cursor));
cust.id = 1;
cursor->
set_key(cursor, cust.id + 1, 0);
if (exact >= 0)
error_check(cursor->
prev(cursor));
for (count = 0; count < 3; ++count) {
error_check(cursor->
get_value(cursor, &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);
error_check(cursor->
prev(cursor));
}
error_check(conn->
close(conn, NULL));
return (EXIT_SUCCESS);
}