1 /*
2   This is free and unencumbered software released into the public domain.
3 
4   Anyone is free to copy, modify, publish, use, compile, sell, or
5   distribute this software, either in source code form or as a compiled
6   binary, for any purpose, commercial or non-commercial, and by any
7   means.
8 
9   In jurisdictions that recognize copyright laws, the author or authors
10   of this software dedicate any and all copyright interest in the
11   software to the public domain. We make this dedication for the benefit
12   of the public at large and to the detriment of our heirs and
13   successors. We intend this dedication to be an overt act of
14   relinquishment in perpetuity of all present and future rights to this
15   software under copyright law.
16 
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23   OTHER DEALINGS IN THE SOFTWARE.
24 
25   For more information, please refer to <http://unlicense.org/>
26 */
27 
28 #include <cassandra.h>
29 #include <stdio.h>
30 
main(int argc,char * argv[])31 int main(int argc, char* argv[]) {
32   /* Setup and connect to cluster */
33   CassFuture* connect_future = NULL;
34   CassCluster* cluster = cass_cluster_new();
35   CassSession* session = cass_session_new();
36   char* hosts = "127.0.0.1";
37   if (argc > 1) {
38     hosts = argv[1];
39   }
40 
41   /* Add contact points */
42   cass_cluster_set_contact_points(cluster, hosts);
43 
44   /* Provide the cluster object as configuration to connect the session */
45   connect_future = cass_session_connect(session, cluster);
46 
47   if (cass_future_error_code(connect_future) == CASS_OK) {
48     /* Build statement and execute query */
49     const char* query = "SELECT release_version FROM system.local";
50     CassStatement* statement = cass_statement_new(query, 0);
51 
52     CassFuture* result_future = cass_session_execute(session, statement);
53 
54     if (cass_future_error_code(result_future) == CASS_OK) {
55       /* Retrieve result set and get the first row */
56       const CassResult* result = cass_future_get_result(result_future);
57       const CassRow* row = cass_result_first_row(result);
58 
59       if (row) {
60         const CassValue* value = cass_row_get_column_by_name(row, "release_version");
61 
62         const char* release_version;
63         size_t release_version_length;
64         cass_value_get_string(value, &release_version, &release_version_length);
65         printf("release_version: '%.*s'\n", (int)release_version_length, release_version);
66       }
67 
68       cass_result_free(result);
69     } else {
70       /* Handle error */
71       const char* message;
72       size_t message_length;
73       cass_future_error_message(result_future, &message, &message_length);
74       fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message);
75     }
76 
77     cass_statement_free(statement);
78     cass_future_free(result_future);
79   } else {
80     /* Handle error */
81     const char* message;
82     size_t message_length;
83     cass_future_error_message(connect_future, &message, &message_length);
84     fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message);
85   }
86 
87   cass_future_free(connect_future);
88   cass_cluster_free(cluster);
89   cass_session_free(session);
90 
91   return 0;
92 }
93