|
| 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 | + |
| 31 | +int main(int argc, char* argv[]) { |
| 32 | + /* Setup and connect to cluster */ |
| 33 | + CassFuture* connect_future = NULL; |
| 34 | + CassCluster* cluster; |
| 35 | + CassSession* session; |
| 36 | + |
| 37 | + const char* secure_connect_bundle; |
| 38 | + const char* username; |
| 39 | + const char* password; |
| 40 | + |
| 41 | + if (argc < 4) { |
| 42 | + fprintf(stderr, "Usage: %s <secure connect bundle zip> <username> <password>\n", argv[0]); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + secure_connect_bundle = argv[1]; |
| 47 | + username = argv[2]; |
| 48 | + password = argv[3]; |
| 49 | + |
| 50 | + cluster = cass_cluster_new(); |
| 51 | + session = cass_session_new(); |
| 52 | + |
| 53 | + /* Setup driver to connect to the cloud using the secure connection bundle */ |
| 54 | + if (cass_cluster_set_cloud_secure_connection_bundle(cluster, secure_connect_bundle) != CASS_OK) { |
| 55 | + fprintf(stderr, "Unable to configure cloud using the secure connection bundle: %s\n", |
| 56 | + secure_connect_bundle); |
| 57 | + } |
| 58 | + |
| 59 | + cass_cluster_set_credentials(cluster, username, password); |
| 60 | + |
| 61 | + /* Provide the cluster object as configuration to connect the session */ |
| 62 | + connect_future = cass_session_connect(session, cluster); |
| 63 | + |
| 64 | + if (cass_future_error_code(connect_future) == CASS_OK) { |
| 65 | + /* Build statement and execute query */ |
| 66 | + const char* query = "SELECT release_version FROM system.local"; |
| 67 | + CassStatement* statement = cass_statement_new(query, 0); |
| 68 | + |
| 69 | + CassFuture* result_future = cass_session_execute(session, statement); |
| 70 | + |
| 71 | + if (cass_future_error_code(result_future) == CASS_OK) { |
| 72 | + /* Retrieve result set and get the first row */ |
| 73 | + const CassResult* result = cass_future_get_result(result_future); |
| 74 | + const CassRow* row = cass_result_first_row(result); |
| 75 | + |
| 76 | + if (row) { |
| 77 | + const CassValue* value = cass_row_get_column_by_name(row, "release_version"); |
| 78 | + |
| 79 | + const char* release_version; |
| 80 | + size_t release_version_length; |
| 81 | + cass_value_get_string(value, &release_version, &release_version_length); |
| 82 | + printf("release_version: '%.*s'\n", (int)release_version_length, release_version); |
| 83 | + } |
| 84 | + |
| 85 | + cass_result_free(result); |
| 86 | + } else { |
| 87 | + /* Handle error */ |
| 88 | + const char* message; |
| 89 | + size_t message_length; |
| 90 | + cass_future_error_message(result_future, &message, &message_length); |
| 91 | + fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message); |
| 92 | + } |
| 93 | + |
| 94 | + cass_statement_free(statement); |
| 95 | + cass_future_free(result_future); |
| 96 | + } else { |
| 97 | + /* Handle error */ |
| 98 | + const char* message; |
| 99 | + size_t message_length; |
| 100 | + cass_future_error_message(connect_future, &message, &message_length); |
| 101 | + fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message); |
| 102 | + } |
| 103 | + |
| 104 | + cass_future_free(connect_future); |
| 105 | + cass_cluster_free(cluster); |
| 106 | + cass_session_free(session); |
| 107 | + |
| 108 | + return 0; |
| 109 | +} |
0 commit comments