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 #include <time.h>
33
34 #include "cassandra.h"
35
print_error(CassFuture * future)36 void print_error(CassFuture* future) {
37 const char* message;
38 size_t message_length;
39 cass_future_error_message(future, &message, &message_length);
40 fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
41 }
42
create_cluster(const char * hosts)43 CassCluster* create_cluster(const char* hosts) {
44 CassCluster* cluster = cass_cluster_new();
45 cass_cluster_set_contact_points(cluster, hosts);
46 return cluster;
47 }
48
connect_session(CassSession * session,const CassCluster * cluster)49 CassError connect_session(CassSession* session, const CassCluster* cluster) {
50 CassError rc = CASS_OK;
51 CassFuture* future = cass_session_connect(session, cluster);
52
53 cass_future_wait(future);
54 rc = cass_future_error_code(future);
55 if (rc != CASS_OK) {
56 print_error(future);
57 }
58 cass_future_free(future);
59
60 return rc;
61 }
62
execute_query(CassSession * session,const char * query)63 CassError execute_query(CassSession* session, const char* query) {
64 CassError rc = CASS_OK;
65 CassFuture* future = NULL;
66 CassStatement* statement = cass_statement_new(query, 0);
67
68 future = cass_session_execute(session, statement);
69 cass_future_wait(future);
70
71 rc = cass_future_error_code(future);
72 if (rc != CASS_OK) {
73 print_error(future);
74 }
75
76 cass_future_free(future);
77 cass_statement_free(statement);
78
79 return rc;
80 }
81
insert_into(CassSession * session,const char * key)82 CassError insert_into(CassSession* session, const char* key) {
83 CassError rc = CASS_OK;
84 CassStatement* statement = NULL;
85 CassFuture* future = NULL;
86 cass_uint32_t d;
87 cass_int64_t t;
88 const char* query = "INSERT INTO examples.date_time (key, d, t) VALUES (?, ?, ?);";
89 time_t now = time(NULL);
90
91 d = cass_date_from_epoch(now);
92 t = cass_time_from_epoch(now);
93
94 statement = cass_statement_new(query, 3);
95
96 cass_statement_bind_string(statement, 0, key);
97 cass_statement_bind_uint32(statement, 1, d);
98 cass_statement_bind_int64(statement, 2, t);
99
100 future = cass_session_execute(session, statement);
101 cass_future_wait(future);
102
103 rc = cass_future_error_code(future);
104 if (rc != CASS_OK) {
105 print_error(future);
106 }
107
108 cass_future_free(future);
109 cass_statement_free(statement);
110
111 return rc;
112 }
113
select_from(CassSession * session,const char * key)114 CassError select_from(CassSession* session, const char* key) {
115 CassError rc = CASS_OK;
116 CassStatement* statement = NULL;
117 CassFuture* future = NULL;
118 const char* query = "SELECT * FROM examples.date_time WHERE key = ?";
119
120 statement = cass_statement_new(query, 1);
121
122 cass_statement_bind_string(statement, 0, key);
123
124 future = cass_session_execute(session, statement);
125 cass_future_wait(future);
126
127 rc = cass_future_error_code(future);
128 if (rc != CASS_OK) {
129 print_error(future);
130 } else {
131 const CassResult* result = cass_future_get_result(future);
132 CassIterator* iterator = cass_iterator_from_result(result);
133
134 if (cass_iterator_next(iterator)) {
135 cass_uint32_t d;
136 cass_int64_t t;
137 time_t time;
138 const CassRow* row = cass_iterator_get_row(iterator);
139 cass_value_get_uint32(cass_row_get_column(row, 1), &d);
140 cass_value_get_int64(cass_row_get_column(row, 2), &t);
141
142 time = (time_t)cass_date_time_to_epoch(d, t);
143
144 printf("Date and time: %s", asctime(localtime(&time)));
145 }
146
147 cass_result_free(result);
148 cass_iterator_free(iterator);
149 }
150
151 cass_future_free(future);
152 cass_statement_free(statement);
153
154 return rc;
155 }
156
main(int argc,char * argv[])157 int main(int argc, char* argv[]) {
158 CassCluster* cluster = NULL;
159 CassSession* session = cass_session_new();
160 char* hosts = "127.0.0.1";
161 if (argc > 1) {
162 hosts = argv[1];
163 }
164 cluster = create_cluster(hosts);
165
166 if (connect_session(session, cluster) != CASS_OK) {
167 cass_cluster_free(cluster);
168 cass_session_free(session);
169 return -1;
170 }
171
172 execute_query(session, "CREATE KEYSPACE examples WITH replication = { \
173 'class': 'SimpleStrategy', 'replication_factor': '3' };");
174
175 execute_query(session, "CREATE TABLE examples.date_time (key text PRIMARY KEY, d date, t time)");
176
177 insert_into(session, "test");
178 select_from(session, "test");
179
180 cass_cluster_free(cluster);
181 cass_session_free(session);
182
183 return 0;
184 }
185