Shows N1QL query API.
Shows N1QL query API. Also because queries executed in a loop, the sample might be used as simple benchmark (more sofisticated shipped with cbc tools, as cbc-n1qlback)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <libcouchbase/ixmgmt.h>
static void check(
lcb_STATUS err,
const char *msg)
{
if (err != LCB_SUCCESS) {
exit(EXIT_FAILURE);
}
}
{
switch (err) {
case LCB_SUCCESS:
return 32;
case LCB_ERR_DOCUMENT_EXISTS:
return 33;
default:
return 31;
}
}
static void ln2space(const void *buf, size_t nbuf)
{
size_t ii;
char *str = (char *)buf;
for (ii = 0; ii < nbuf; ii++) {
if (str[ii] == '\n') {
str[ii] = ' ';
}
}
}
{
const char *row;
size_t nrow;
lcb_respquery_row(resp, &row, &nrow);
ln2space(row, nrow);
fprintf(stderr,
"[\x1b[%dmQUERY\x1b[0m] %s, (size=%d) %.*s\n", err2color(rc),
lcb_strerror_short(rc), (
int)nrow,
(int)nrow, row);
if (lcb_respquery_is_final(resp)) {
fprintf(stderr, "\n");
}
}
{
const char *row;
size_t nrow;
lcb_respquery_row(inner, &row, &nrow);
ln2space(row, nrow);
fprintf(stderr,
"[\x1b[%dmINDEX\x1b[0m] %s, (%d) %.*s\n", err2color(resp->
rc),
lcb_strerror_short(resp->rc),
(int)nrow, (int)nrow, row);
}
{
const char *key;
size_t nkey;
lcb_respstore_key(resp, &key, &nkey);
(int)nkey, key);
}
{
const char *key;
size_t nkey;
rc = lcb_respget_status(resp);
lcb_respget_key(resp, &key, &nkey);
(int)nkey, key);
}
static int running = 1;
static void sigint_handler(int unused)
{
running = 0;
}
int main(int argc, char *argv[])
{
char *bucket = NULL;
const char *key = "user:king_arthur";
const char *val = "{"
" \"email\": \"kingarthur@couchbase.com\","
" \"interests\": [\"Holy Grail\", \"African Swallows\"]"
"}";
if (argc < 2) {
fprintf(stderr, "Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
{
lcb_CREATEOPTS *options = NULL;
lcb_createopts_connstr(options, argv[1], strlen(argv[1]));
if (argc > 3) {
lcb_createopts_credentials(options, argv[3], strlen(argv[3]), argv[2], strlen(argv[2]));
}
check(
lcb_create(&instance, options),
"create couchbase handle");
lcb_createopts_destroy(options);
}
{
lcb_CMDSTORE *cmd;
lcb_cmdstore_key(cmd, key, strlen(key));
lcb_cmdstore_value(cmd, val, strlen(val));
check(lcb_store(instance, NULL, cmd), "schedule STORE operation");
lcb_cmdstore_destroy(cmd);
}
{
lcb_CMDGET *cmd;
lcb_cmdget_create(&cmd);
lcb_cmdget_key(cmd, key, strlen(key));
check(lcb_get(instance, NULL, cmd), "schedule GET operation");
lcb_cmdget_destroy(cmd);
}
{
char query[1024] = {0};
snprintf(query, sizeof(query), "CREATE PRIMARY INDEX ON `%s` USING gsi", bucket);
lcb_cmdquery_callback(cmd, row_callback);
check(
lcb_query(instance, NULL, cmd),
"schedule QUERY INDEX create operation");
}
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_handler = sigint_handler;
action.sa_flags = 0;
sigaction(SIGINT, &action, NULL);
while (running) {
char query[1024] = {0};
const char *params = "[\"African Swallows\"]";
snprintf(query, sizeof(query), "SELECT * FROM `%s` WHERE $1 in interests LIMIT 1", bucket);
"set QUERY 'pretty' option");
lcb_cmdquery_callback(cmd, row_callback);
check(
lcb_query(instance, NULL, cmd),
"schedule QUERY operation");
}
return 0;
}
Main header file for Couchbase.
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
#define LCB_CNTL_BUCKETNAME
Get the name of the bucket This returns the name of the bucket this instance is connected to,...
Definition cntl.h:175
#define LCB_CNTL_GET
Retrieve a setting.
Definition cntl.h:123
lcb_STATUS lcb_cntl(lcb_INSTANCE *instance, int mode, int cmd, void *arg)
This function exposes an ioctl/fcntl-like interface to read and write various configuration propertie...
LCB_INTERNAL_API const char * lcb_strerror_short(lcb_STATUS error)
Get a shorter textual description of an error message.
lcb_STATUS
Error codes returned by the library.
Definition error.h:213
struct lcb_RESPGET_ lcb_RESPGET
Command for retrieving a single item.
Definition couchbase.h:687
lcb_STATUS lcb_get_bootstrap_status(lcb_INSTANCE *instance)
Gets the initial bootstrap status.
lcb_STATUS lcb_create(lcb_INSTANCE **instance, const lcb_CREATEOPTS *options)
Create an instance of lcb.
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition couchbase.h:35
lcb_STATUS lcb_connect(lcb_INSTANCE *instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
@ LCB_TYPE_BUCKET
Handle for data access (default)
Definition couchbase.h:256
lcb_RESPCALLBACK lcb_install_callback(lcb_INSTANCE *instance, int cbtype, lcb_RESPCALLBACK cb)
void(* lcb_RESPCALLBACK)(lcb_INSTANCE *instance, int cbtype, const lcb_RESPBASE *resp)
Callback invoked for responses.
Definition couchbase.h:554
const char * lcb_strcbtype(int cbtype)
Returns the type of the callback as a string.
@ LCB_CALLBACK_GET
lcb_get()
Definition couchbase.h:471
@ LCB_CALLBACK_STORE
lcb_store()
Definition couchbase.h:472
lcb_STATUS lcb_query(lcb_INSTANCE *instance, void *cookie, const lcb_CMDQUERY *cmd)
Execute a N1QL query.
lcb_STATUS lcb_cmdquery_statement(lcb_CMDQUERY *cmd, const char *statement, size_t statement_len)
Sets the actual statement to be executed.
lcb_STATUS lcb_cmdquery_option(lcb_CMDQUERY *cmd, const char *name, size_t name_len, const char *value, size_t value_len)
Set a query option.
struct lcb_RESPQUERY_ lcb_RESPQUERY
Opaque query response structure.
Definition couchbase.h:2845
lcb_STATUS lcb_cmdquery_create(lcb_CMDQUERY **cmd)
Create a new lcb_CMDQUERY object.
struct lcb_CMDQUERY_ lcb_CMDQUERY
Opaque query command structure.
Definition couchbase.h:2850
lcb_STATUS lcb_cmdquery_destroy(lcb_CMDQUERY *cmd)
Free the command structure.
lcb_STATUS lcb_cmdquery_positional_params(lcb_CMDQUERY *cmd, const char *value, size_t value_len)
Sets the positional arguments for the query.
struct lcb_RESPSTORE_ lcb_RESPSTORE
Schedule a single storage request.
Definition couchbase.h:978
@ LCB_STORE_UPSERT
The default storage mode.
Definition couchbase.h:894
lcb_STATUS lcb_wait(lcb_INSTANCE *instance, lcb_WAITFLAGS flags)
Wait for completion of scheduled operations.
@ LCB_WAIT_DEFAULT
Behave like the old lcb_wait()
Definition couchbase.h:1861
const lcb_RESPQUERY * inner
Inner N1QL response.
Definition ixmgmt.h:171