Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created October 6, 2017 18:19
Show Gist options
  • Save mpenick/85313d649bfc84d470030ca6f462c0ce to your computer and use it in GitHub Desktop.
Save mpenick/85313d649bfc84d470030ca6f462c0ce to your computer and use it in GitHub Desktop.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <stdio.h>
#include <cassandra.h>
void print_query_result(CassFuture* future) {
if (cass_future_error_code(future) == CASS_OK) {
/* Retrieve result set and get the first row */
const CassResult* result = cass_future_get_result(future);
const CassRow* row = cass_result_first_row(result);
if (row) {
const CassValue* value = cass_row_get_column_by_name(row, "release_version");
const char* release_version;
size_t release_version_length;
cass_value_get_string(value, &release_version, &release_version_length);
printf("release_version: '%.*s'\n", (int)release_version_length,
release_version);
}
cass_result_free(result);
} else {
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(future, &message, &message_length);
fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length,
message);
}
}
CassFuture* run_query(CassSession* session, CassFutureCallback callback) {
/* Build statement and execute query */
const char* query = "SELECT release_version FROM system.local";
CassStatement* statement = cass_statement_new(query, 0);
CassFuture* future = cass_session_execute(session, statement);
cass_future_set_callback(future, callback, session);
cass_statement_free(statement);
return future;
}
void on_query2(CassFuture* future, void* data) {
printf("Query 2: ");
print_query_result(future);
}
void on_query1(CassFuture* future, void* data) {
CassSession* session = (CassSession*)data;
printf("Query 1: ");
print_query_result(future);
CassFuture* result_future = run_query(session, on_query2);
cass_future_free(result_future);
}
int main(int argc, char* argv[]) {
/* Setup and connect to cluster */
CassFuture* connect_future = NULL;
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
char* hosts = "127.0.0.1";
if (argc > 1) {
hosts = argv[1];
}
/* Add contact points */
cass_cluster_set_contact_points(cluster, hosts);
/* Provide the cluster object as configuration to connect the session */
connect_future = cass_session_connect(session, cluster);
if (cass_future_error_code(connect_future) == CASS_OK) {
CassFuture* result_future = run_query(session, on_query1);
cass_future_wait(result_future);
cass_future_free(result_future);
} else {
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(connect_future, &message, &message_length);
fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length,
message);
}
cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment