Created
July 9, 2019 20:07
-
-
Save beccam/ee4572647672d5140b33eb17dffc4e43 to your computer and use it in GitHub Desktop.
A basic C++ demo CRUD application using the DataStax C/C++ Driver for Apache Cassandra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "cassandra.h" | |
struct Users_ { | |
const char* lastname; | |
cass_int32_t age; | |
const char* city; | |
const char* email; | |
const char* firstname; | |
}; | |
typedef struct Users_ Users; | |
void print_error(CassFuture* future) { | |
const char* message; | |
size_t message_length; | |
cass_future_error_message(future, &message, &message_length); | |
fprintf(stderr, "Error: %.*s\n", (int)message_length, message); | |
} | |
CassCluster* create_cluster(const char* hosts) { | |
CassCluster* cluster = cass_cluster_new(); | |
cass_cluster_set_contact_points(cluster, hosts); | |
return cluster; | |
} | |
CassError connect_session(CassSession* session, const CassCluster* cluster) { | |
CassError rc = CASS_OK; | |
CassFuture* future = cass_session_connect(session, cluster); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
return rc; | |
} | |
CassError insert_user(CassSession* session, const Users* users) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "INSERT INTO demo.users (lastname, age, city, email, firstname) VALUES (?, ?, ?, ?, ?)"; | |
statement = cass_statement_new(query, 5); | |
cass_statement_bind_string(statement, 0, users->lastname); | |
cass_statement_bind_int32(statement, 1, users->age); | |
cass_statement_bind_string(statement, 2, users->city); | |
cass_statement_bind_string(statement, 3, users->email); | |
cass_statement_bind_string(statement, 4, users->firstname); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
CassError select_user(CassSession* session, const Users* users) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "SELECT * FROM demo.users WHERE lastname=?"; | |
statement = cass_statement_new(query, 1); | |
cass_statement_bind_string(statement, 0, users->lastname); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} else { | |
const CassResult* result = cass_future_get_result(future); | |
const CassRow* row = cass_result_first_row(result); | |
if (row) { | |
const CassValue* firstvalue = cass_row_get_column_by_name(row, "firstname"); | |
const char* firstname; | |
size_t firstname_length; | |
cass_value_get_string(firstvalue, &firstname, &firstname_length); | |
const CassValue* secondvalue = cass_row_get_column_by_name(row, "age"); | |
cass_int32_t age; | |
cass_value_get_int32(secondvalue, &age); | |
printf("firstname: '%.*s' age: %d\n", (int)firstname_length, firstname, age); | |
} | |
cass_statement_free(statement); | |
cass_future_free(future); | |
} | |
return rc; | |
} | |
CassError update_user(CassSession* session, const Users* users) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "UPDATE demo.users SET age =? WHERE lastname =? "; | |
statement = cass_statement_new(query, 2); | |
cass_statement_bind_int32(statement, 0, users->age); | |
cass_statement_bind_string(statement, 1, users->lastname); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
CassError delete_user(CassSession* session, const Users* users) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "DELETE FROM demo.users WHERE lastname=?"; | |
statement = cass_statement_new(query, 1); | |
cass_statement_bind_string(statement, 0, users->lastname); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
int main(int argc, char* argv[]) { | |
CassCluster* cluster = NULL; | |
CassSession* session = cass_session_new(); | |
Users input = { "Jones", 39, "Austin", "[email protected]", "Bob" }; | |
Users select = { "Jones", NULL, NULL, NULL, NULL}; | |
Users update = { "Jones", 40, NULL, NULL, NULL}; | |
Users deletion = { "Jones", NULL, NULL, NULL, NULL}; | |
char* hosts = "127.0.0.1"; | |
if (argc > 1) { | |
hosts = argv[1]; | |
} | |
cluster = create_cluster(hosts); | |
if (connect_session(session, cluster) != CASS_OK) { | |
cass_cluster_free(cluster); | |
cass_session_free(session); | |
return -1; | |
} | |
insert_user(session, &input); | |
select_user(session, &select); | |
update_user(session, &update); | |
delete_user(session, &deletion); | |
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