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 <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "cassandra.h"
34
35 struct Basic_ {
36 cass_bool_t bln;
37 cass_float_t flt;
38 cass_double_t dbl;
39 cass_int32_t i32;
40 cass_int64_t i64;
41 };
42
43 typedef struct Basic_ Basic;
44
print_error(CassFuture * future)45 void print_error(CassFuture* future) {
46 const char* message;
47 size_t message_length;
48 cass_future_error_message(future, &message, &message_length);
49 fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
50 }
51
create_cluster(const char * hosts)52 CassCluster* create_cluster(const char* hosts) {
53 CassCluster* cluster = cass_cluster_new();
54 cass_cluster_set_contact_points(cluster, hosts);
55 return cluster;
56 }
57
connect_session(CassSession * session,const CassCluster * cluster)58 CassError connect_session(CassSession* session, const CassCluster* cluster) {
59 CassError rc = CASS_OK;
60 CassFuture* future = cass_session_connect(session, cluster);
61
62 cass_future_wait(future);
63 rc = cass_future_error_code(future);
64 if (rc != CASS_OK) {
65 print_error(future);
66 }
67 cass_future_free(future);
68
69 return rc;
70 }
71
execute_query(CassSession * session,const char * query)72 CassError execute_query(CassSession* session, const char* query) {
73 CassError rc = CASS_OK;
74 CassStatement* statement = cass_statement_new(query, 0);
75 CassFuture* future = cass_session_execute(session, statement);
76
77 cass_future_wait(future);
78
79 rc = cass_future_error_code(future);
80 if (rc != CASS_OK) {
81 print_error(future);
82 }
83
84 cass_future_free(future);
85 cass_statement_free(statement);
86
87 return rc;
88 }
89
insert_into_basic(CassSession * session,const char * key,const Basic * basic)90 CassError insert_into_basic(CassSession* session, const char* key, const Basic* basic) {
91 CassError rc = CASS_OK;
92 CassStatement* statement = NULL;
93 CassFuture* future = NULL;
94
95 const char* query = "INSERT INTO examples.basic (key, bln, flt, dbl, i32, i64) VALUES (:k, :b, "
96 ":f, :d, :i32, :i64);";
97
98 statement = cass_statement_new(query, 6);
99
100 cass_statement_bind_string_by_name(statement, "k", key);
101 cass_statement_bind_bool_by_name(statement, "b", basic->bln);
102 cass_statement_bind_float_by_name(statement, "f", basic->flt);
103 cass_statement_bind_double_by_name(statement, "d", basic->dbl);
104 cass_statement_bind_int32_by_name(statement, "i32", basic->i32);
105 cass_statement_bind_int64_by_name(statement, "i64", basic->i64);
106
107 future = cass_session_execute(session, statement);
108
109 cass_future_wait(future);
110
111 rc = cass_future_error_code(future);
112 if (rc != CASS_OK) {
113 print_error(future);
114 }
115
116 cass_future_free(future);
117 cass_statement_free(statement);
118
119 return rc;
120 }
121
select_from_basic(CassSession * session,const char * key,Basic * basic)122 CassError select_from_basic(CassSession* session, const char* key, Basic* basic) {
123 CassError rc = CASS_OK;
124 CassStatement* statement = NULL;
125 CassFuture* future = NULL;
126
127 const char* query = "SELECT * FROM examples.basic WHERE key = ?";
128
129 statement = cass_statement_new(query, 1);
130
131 cass_statement_bind_string_by_name(statement, "key", key);
132
133 future = cass_session_execute(session, statement);
134 cass_future_wait(future);
135
136 rc = cass_future_error_code(future);
137 if (rc != CASS_OK) {
138 print_error(future);
139 } else {
140 const CassResult* result = cass_future_get_result(future);
141 CassIterator* iterator = cass_iterator_from_result(result);
142
143 if (cass_iterator_next(iterator)) {
144 const CassRow* row = cass_iterator_get_row(iterator);
145
146 cass_value_get_bool(cass_row_get_column_by_name(row, "BLN"), &basic->bln);
147 cass_value_get_double(cass_row_get_column_by_name(row, "dbl"), &basic->dbl);
148 cass_value_get_float(cass_row_get_column_by_name(row, "flt"), &basic->flt);
149 cass_value_get_int32(cass_row_get_column_by_name(row, "\"i32\""), &basic->i32);
150 cass_value_get_int64(cass_row_get_column_by_name(row, "i64"), &basic->i64);
151 }
152
153 cass_result_free(result);
154 cass_iterator_free(iterator);
155 }
156
157 cass_future_free(future);
158 cass_statement_free(statement);
159
160 return rc;
161 }
162
main(int argc,char * argv[])163 int main(int argc, char* argv[]) {
164 CassCluster* cluster = NULL;
165 CassSession* session = cass_session_new();
166 Basic input = { cass_true, 0.001f, 0.0002, 1, 2 };
167 Basic output;
168 char* hosts = "127.0.0.1";
169 if (argc > 1) {
170 hosts = argv[1];
171 }
172 cluster = create_cluster(hosts);
173
174 if (connect_session(session, cluster) != CASS_OK) {
175 cass_cluster_free(cluster);
176 cass_session_free(session);
177 return -1;
178 }
179
180 execute_query(session, "CREATE KEYSPACE examples WITH replication = { \
181 'class': 'SimpleStrategy', 'replication_factor': '3' };");
182
183 execute_query(session, "CREATE TABLE examples.basic (key text, \
184 bln boolean, \
185 flt float, dbl double,\
186 i32 int, i64 bigint, \
187 PRIMARY KEY (key));");
188
189 insert_into_basic(session, "named_parameters", &input);
190 select_from_basic(session, "named_parameters", &output);
191
192 assert(input.bln == output.bln);
193 assert(input.flt == output.flt);
194 assert(input.dbl == output.dbl);
195 assert(input.i32 == output.i32);
196 assert(input.i64 == output.i64);
197
198 cass_cluster_free(cluster);
199 cass_session_free(session);
200
201 return 0;
202 }
203