Shows N1QL for Analytics queries.
Shows N1QL for Analytics queries.
 
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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_STATUS err, 
const char *msg)
 
{
    if (err != LCB_SUCCESS) {
        char buf[1024] = {0};
        fail(buf);
    }
}
 
{
    switch (err) {
        case LCB_SUCCESS:
            return 49;
        default:
            return 31;
    }
}
 
{
    int *idx;
    const char *row;
    size_t nrow;
 
    lcb_respanalytics_cookie(resp, (void **)&idx);
    lcb_respanalytics_row(resp, &row, &nrow);
    if (rc != LCB_SUCCESS) {
        lcb_respanalytics_http_response(resp, &http);
        if (http) {
            uint16_t status;
            lcb_resphttp_http_status(http, &status);
            printf(", HTTP status: %d", (int)status);
        }
        printf("\n");
        if (nrow) {
            cJSON *json;
            char *data = calloc(nrow + 1, sizeof(char));
            memcpy(data, row, 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",
                                    (int)code->valueint, msg->valuestring);
                            }
                        }
                    }
                }
            }
            free(data);
        }
    }
    if (lcb_respanalytics_is_final(resp)) {
        printf("\x1b[1mMETA:\x1b[0m ");
    } else {
        printf("\x1b[1mR%d:\x1b[0m ", (*idx)++);
    }
    printf("%.*s\n", (int)nrow, (char *)row);
    if (lcb_respanalytics_is_final(resp)) {
        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);
    }
 
    {
        lcb_CREATEOPTS *create_options = NULL;
        lcb_createopts_connstr(create_options, argv[1], strlen(argv[1]));
        if (argc > 3) {
            lcb_createopts_credentials(create_options, argv[3], strlen(argv[3]), argv[2], strlen(argv[2]));
        }
        check(
lcb_create(&instance, create_options), 
"create couchbase handle");
 
        lcb_createopts_destroy(create_options);
        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;
        lcb_cmdanalytics_create(&cmd);
        lcb_cmdanalytics_callback(cmd, row_callback);
        lcb_cmdanalytics_payload(cmd, queries[ii].query, queries[ii].query_len);
        check(
lcb_analytics(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_cmdanalytics_destroy(cmd);
    }
 
    
    return 0;
}
Main header file for Couchbase.
 
struct lcb_RESPHTTP_ lcb_RESPHTTP
Definition couchbase.h:1678
 
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
 
lcb_STATUS lcb_analytics(lcb_INSTANCE *instance, void *cookie, const lcb_CMDANALYTICS *cmd)
Execute a Analytics query.
 
struct lcb_RESPANALYTICS_ lcb_RESPANALYTICS
Response for a Analytics query.
Definition couchbase.h:2455
 
#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
 
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_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:1865