Couchbase C Client  2.10.7
Asynchronous C Client for Couchbase
example/analytics/analytics.c

Shows N1QL for Analytics queries.

/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2018 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libcouchbase/analytics.h>
#include "queries.h"
#include "cJSON.h"
static void fail(const char *msg)
{
printf("[\x1b[31mERROR\x1b[0m] %s\n", msg);
exit(EXIT_FAILURE);
}
static void check(lcb_error_t err, const char *msg)
{
if (err != LCB_SUCCESS) {
char buf[1024] = {0};
snprintf(buf, sizeof(buf), "%s: %s\n", msg, lcb_strerror_short(err));
fail(buf);
}
}
static int err2color(lcb_error_t err)
{
switch (err) {
return 49;
default:
return 31;
}
}
static void row_callback(lcb_t instance, int type, const lcb_RESPANALYTICS *resp)
{
int *idx = (int *)resp->cookie;
if (resp->rc != LCB_SUCCESS) {
printf("\x1b[31m%s\x1b[0m", lcb_strerror_short(resp->rc));
if (resp->htresp) {
printf(", HTTP status: %d", resp->htresp->htstatus);
}
printf("\n");
if (resp->nrow) {
cJSON *json;
char *data = calloc(resp->nrow + 1, sizeof(char));
memcpy(data, resp->row, resp->nrow);
json = cJSON_Parse(data);
if (json && json->type == cJSON_Object) {
cJSON *errors = cJSON_GetObjectItem(json, "errors");
if (errors && errors->type == cJSON_Array) {
int ii, nerrors = cJSON_GetArraySize(errors);
for (ii = 0; ii < nerrors; ii++) {
cJSON *err = cJSON_GetArrayItem(errors, ii);
if (err && err->type == cJSON_Object) {
cJSON *code, *msg;
code = cJSON_GetObjectItem(err, "code");
msg = cJSON_GetObjectItem(err, "msg");
if (code && code->type == cJSON_Number && msg && msg->type == cJSON_String) {
printf(
"\x1b[1mcode\x1b[0m: \x1b[31m%d\x1b[0m, \x1b[1mmessage\x1b[0m: \x1b[31m%s\x1b[0m\n",
code->valueint, msg->valuestring);
}
}
}
}
}
free(data);
}
}
if (resp->rflags & LCB_RESP_F_FINAL) {
printf("\x1b[1mMETA:\x1b[0m ");
} else {
printf("\x1b[1mR%d:\x1b[0m ", (*idx)++);
}
printf("%.*s\n", (int)resp->nrow, (char *)resp->row);
if (resp->rflags & LCB_RESP_F_FINAL) {
printf("\n");
}
}
int main(int argc, char *argv[])
{
lcb_t instance;
char *bucket = NULL;
size_t ii;
if (argc < 2) {
printf("Usage: %s couchbase://host/beer-sample [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
{
struct lcb_create_st create_options = {0};
create_options.version = 3;
create_options.v.v3.connstr = argv[1];
if (argc > 2) {
create_options.v.v3.passwd = argv[2];
}
if (argc > 3) {
create_options.v.v3.username = argv[3];
}
check(lcb_create(&instance, &create_options), "create couchbase handle");
check(lcb_connect(instance), "schedule connection");
lcb_wait(instance);
check(lcb_get_bootstrap_status(instance), "bootstrap from cluster");
check(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_BUCKETNAME, &bucket), "get bucket name");
if (strcmp(bucket, "beer-sample") != 0) {
fail("expected bucket to be \"beer-sample\"");
}
}
for (ii = 0; ii < num_queries; ii++) {
lcb_CMDANALYTICS *cmd;
int idx = 0;
cmd = lcb_analytics_new();
lcb_analytics_setcallback(cmd, row_callback);
lcb_analytics_setquery(cmd, queries[ii].query, queries[ii].query_len);
check(lcb_analytics_query(instance, &idx, cmd), "schedule analytics query");
printf("----> \x1b[1m%s\x1b[0m\n", queries[ii].comment);
printf("----> \x1b[36m%.*s\x1b[0m\n", (int)queries[ii].query_len, queries[ii].query);
lcb_analytics_free(cmd);
lcb_wait(instance);
}
/* Now that we're all done, close down the connection handle */
lcb_destroy(instance);
return 0;
}
lcb_wait
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
lcb_RESPANALYTICS::row
const char * row
Current result row.
Definition: analytics.h:147
LCB_CNTL_BUCKETNAME
#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:177
lcb_create_st3::passwd
const char * passwd
Password for bucket.
Definition: couchbase.h:293
lcb_analytics_query
lcb_error_t lcb_analytics_query(lcb_t instance, const void *cookie, lcb_CMDANALYTICS *cmd)
lcb_RESPANALYTICS::htresp
const lcb_RESPHTTP * htresp
Raw HTTP response, if applicable.
Definition: analytics.h:151
lcb_get_bootstrap_status
lcb_error_t lcb_get_bootstrap_status(lcb_t instance)
Gets the initial bootstrap status.
lcb_RESPANALYTICS::rflags
lcb_U16 rflags
Flags for response structure.
Definition: analytics.h:139
lcb_RESPHTTP::htstatus
short htstatus
HTTP status code.
Definition: couchbase.h:2791
lcb_create_st3::connstr
const char * connstr
Connection string.
Definition: couchbase.h:282
lcb_connect
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
LCB_CNTL_GET
#define LCB_CNTL_GET
Retrieve a setting.
Definition: cntl.h:125
lcb_create_st3::username
const char * username
Username to use for authentication.
Definition: couchbase.h:288
lcb_error_t
lcb_error_t
Error codes returned by the library.
Definition: error.h:476
lcb_create_st::lcb_CRST_u::v3
struct lcb_create_st3 v3
Use this field.
Definition: couchbase.h:338
lcb_create_st
Wrapper structure for lcb_create()
Definition: couchbase.h:328
lcb_t
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_create
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
couchbase.h
LCB_SUCCESS
@ LCB_SUCCESS
Success.
Definition: error.h:478
lcb_RESPANALYTICS::nrow
size_t nrow
Length of the row.
Definition: analytics.h:149
lcb_destroy
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.
lcb_RESPANALYTICS
Response for a Analytics query.
Definition: analytics.h:135
lcb_create_st::version
int version
Indicates which field in the lcb_CRST_u union should be used.
Definition: couchbase.h:330
LCB_RESP_F_FINAL
@ LCB_RESP_F_FINAL
No more responses are to be received for this request.
Definition: couchbase.h:658
lcb_strerror_short
const LCB_INTERNAL_API char * lcb_strerror_short(lcb_error_t error)
Get a shorter textual description of an error message.
lcb_cntl
lcb_error_t lcb_cntl(lcb_t instance, int mode, int cmd, void *arg)
This function exposes an ioctl/fcntl-like interface to read and write various configuration propertie...