// // Created by Gautham Banasandra on 2019-07-01. // #include #include #include #include struct Cursor { int count{ 0 }; lcb_N1QLHANDLE handle{ nullptr }; }; void RowCallback(lcb_t instance, int type, const lcb_RESPN1QL* resp) { const std::string row(resp->row, resp->row + resp->nrow); std::cout << row << std::endl; } lcb_t GetInstance() { lcb_t instance = nullptr; lcb_create_st options = { nullptr }; options.version = 3; options.v.v3.connstr = "http://127.0.0.1:9000/destination"; options.v.v3.username = "Administrator"; options.v.v3.passwd = "asdasd"; auto result = lcb_create(&instance, &options); if (result != LCB_SUCCESS) { std::cerr << "Unable to initialize Couchbase handle : " << lcb_strerror(instance, result) << std::endl; return nullptr; } result = lcb_connect(instance); if (result != LCB_SUCCESS) { std::cerr << "Unable to schedule connection : " << lcb_strerror(instance, result) << std::endl; return nullptr; } result = lcb_wait(instance); if (result != LCB_SUCCESS) { std::cerr << "Unable to connect : " << lcb_strerror(instance, result) << std::endl; return nullptr; } result = lcb_get_bootstrap_status(instance); if (result != LCB_SUCCESS) { std::cerr << "Bootstrap status : " << lcb_strerror(instance, result) << std::endl; return nullptr; } return instance; } void Query(lcb_t instance) { lcb_CMDN1QL cmd = { 0 }; lcb_N1QLPARAMS* builder = lcb_n1p_new(); auto result = lcb_n1p_setstmtz(builder, "EXECUTE test2;"); if (result != LCB_SUCCESS) { std::cerr << "Unable to set query : " << lcb_strerror(instance, result) << std::endl; return; } result = lcb_n1p_mkcmd(builder, &cmd); if (result != LCB_SUCCESS) { std::cerr << "Unable to make query cmd : " << lcb_strerror(instance, result) << std::endl; return; } cmd.callback = RowCallback; cmd.cmdflags |= LCB_CMDN1QL_F_PREPCACHE; result = lcb_n1ql_query(instance, nullptr, &cmd); if (result != LCB_SUCCESS) { std::cerr << "Unable to schedule query : " << lcb_strerror(instance, result) << std::endl; return; } lcb_n1p_free(builder); result = lcb_wait(instance); if (result != LCB_SUCCESS) { std::cerr << "Unable to execute N1QL query : " << lcb_strerror(instance, result) << std::endl; } } int main() { const auto instance = GetInstance(); Query(instance); lcb_destroy(instance); return 0; }