aboutsummaryrefslogtreecommitdiff
path: root/cassandra_support.h
diff options
context:
space:
mode:
Diffstat (limited to 'cassandra_support.h')
-rw-r--r--cassandra_support.h48
1 files changed, 41 insertions, 7 deletions
diff --git a/cassandra_support.h b/cassandra_support.h
index 167f69b..b0726c8 100644
--- a/cassandra_support.h
+++ b/cassandra_support.h
@@ -3,17 +3,21 @@
#include <stddef.h>
#include <string>
+#include <algorithm>
#include <cassandra.h>
#include <stdexcept>
+#include <functional>
+#include <vector>
+#include <iostream>
namespace trygvis {
namespace cassandra_support {
using namespace std;
-class cassandra_error : runtime_error {
+class cassandra_error : public runtime_error {
public:
- cassandra_error(const string &context, CassError error) : runtime_error("Cassandra error: context=" + context + ", error=" + to_string((int)error)) {
+ cassandra_error(const string &context, CassError error) : runtime_error("Cassandra error: context=" + context + ", error=" + cass_error_desc(error)) {
}
};
@@ -73,21 +77,39 @@ public:
cass_collection_free(collection);
}
- void append_tuple(cassandra_tuple &&tuple) {
+ void append(const cassandra_tuple &&tuple) {
cass_collection_append_tuple(collection, tuple.tuple);
}
+ void append(const string &&value) {
+ cass_collection_append_string(collection, value.c_str());
+ }
+
+ void append(const string &value) {
+ cass_collection_append_string(collection, value.c_str());
+ }
+
+ operator CassCollection *() const {
+ return collection;
+ };
+
+private:
CassCollection *collection;
};
-CassError wait_for_future(CassFuture *future) {
+void handle_future(CassFuture *future, std::function<void(CassFuture *)> success,
+ std::function<void(CassFuture *, CassError)> error) {
cass_future_wait(future);
CassError rc = cass_future_error_code(future);
- cass_future_free(future);
+ if(rc == CASS_OK) {
+ success(future);
+ } else {
+ error(future, rc);
+ }
- return rc;
+ cass_future_free(future);
};
class cassandra_statement {
@@ -121,7 +143,19 @@ public:
}
void bind(size_t i, const cassandra_collection &value) {
- auto err = cass_statement_bind_collection(statement, i, value.collection);
+ auto err = cass_statement_bind_collection(statement, i, value);
+ assert_ok("cass_statement_bind_collection", err);
+ }
+
+ void bind(size_t i, const std::vector<string> &&values) {
+ cassandra_collection c(CassCollectionType::CASS_COLLECTION_TYPE_LIST, values.size());
+
+ for (const auto &value : values) {
+ c.append(value);
+ }
+
+ std::cout << "values.size=" << values.size() << std::endl;
+ auto err = cass_statement_bind_collection(statement, i, c);
assert_ok("cass_statement_bind_collection", err);
}