Shows N1QL for Analytics queries.
#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);
}
{
char buf[1024] = {0};
fail(buf);
}
}
{
switch (err) {
return 49;
default:
return 31;
}
}
{
int *idx = (int *)resp->cookie;
}
printf("\n");
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);
}
}
printf("\x1b[1mMETA:\x1b[0m ");
} else {
printf("\x1b[1mR%d:\x1b[0m ", (*idx)++);
}
printf(
"%.*s\n", (
int)resp->
nrow, (
char *)resp->
row);
printf("\n");
}
}
int main(int argc, char *argv[])
{
char *bucket = NULL;
size_t ii;
if (argc < 2) {
printf("Usage: %s couchbase://host/beer-sample [ password [ username ] ]\n", argv[0]);
exit(EXIT_FAILURE);
}
{
if (argc > 2) {
}
if (argc > 3) {
}
check(
lcb_create(&instance, &create_options),
"create couchbase handle");
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);
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);
}
return 0;
}