Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/duration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 2.6.4)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ".")
set(PROJECT_EXAMPLE_NAME duration)

file(GLOB EXAMPLE_SRC_FILES ${PROJECT_SOURCE_DIR}/examples/duration/*.c)
include_directories(${INCLUDES})
add_executable(${PROJECT_EXAMPLE_NAME} ${EXAMPLE_SRC_FILES})
target_link_libraries(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET} ${CASS_LIBS})
add_dependencies(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET})

set_property(
TARGET ${PROJECT_EXAMPLE_NAME}
APPEND PROPERTY COMPILE_FLAGS ${CASS_EXAMPLE_C_FLAGS})
189 changes: 189 additions & 0 deletions examples/duration/duration.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
*/

#include <stdio.h>
#include "cassandra.h"

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 execute_query(CassSession* session, const char* query) {
CassError rc = CASS_OK;
CassFuture* future = NULL;
CassStatement* statement = cass_statement_new(query, 0);

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 insert_into(CassSession* session, const char* key, cass_int64_t months, cass_int64_t days, cass_int64_t nanos) {
CassError rc = CASS_OK;
CassStatement* statement = NULL;
CassFuture* future = NULL;
const char* query = "INSERT INTO examples.duration (key, d) VALUES (?, ?);";
cass_byte_t* data;
size_t data_size;


statement = cass_statement_new(query, 2);

cass_statement_bind_string(statement, 0, key);
cass_statement_bind_duration(statement, 1, months, days, nanos);

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_from(CassSession* session, const char* key) {
CassError rc = CASS_OK;
CassStatement* statement = NULL;
CassFuture* future = NULL;
const char* query = "SELECT d FROM examples.duration WHERE key = ?";

statement = cass_statement_new(query, 1);

cass_statement_bind_string(statement, 0, key);

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);
CassIterator* iterator = cass_iterator_from_result(result);

if (cass_iterator_next(iterator)) {
cass_int64_t months, days, nanos;
const CassRow* row = cass_iterator_get_row(iterator);

cass_value_get_duration(cass_row_get_column(row, 0), &months, &days, &nanos);
printf("months: %lld days: %lld nanos: %lld\n", months, days, nanos);
}

cass_result_free(result);
cass_iterator_free(iterator);
}

cass_future_free(future);
cass_statement_free(statement);

return rc;
}

int main(int argc, char* argv[]) {
CassCluster* cluster = NULL;
CassSession* session = cass_session_new();
CassFuture* close_future = 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;
}

execute_query(session,
"CREATE KEYSPACE examples WITH replication = { \
'class': 'SimpleStrategy', 'replication_factor': '3' };");


execute_query(session,
"CREATE TABLE examples.duration (key text PRIMARY KEY, d duration)");

// Insert some rows into the table, playing with edge values; each of months, days, and nanos may be
// a 64-bit long.

insert_into(session, "base", 0, 0, 0);
insert_into(session, "simple", 1, 2, 3);
insert_into(session, "edge", (1LL << 63) - 1, 0xffffffffffffffff, 1LL << 63);
select_from(session, "base");
select_from(session, "simple");

// This case should print out
// months: 9223372036854775807 days: -1 nanos: -9223372036854775808
select_from(session, "edge");

close_future = cass_session_close(session);
cass_future_wait(close_future);
cass_future_free(close_future);

cass_cluster_free(cluster);
cass_session_free(session);

return 0;
}
Loading