CassError insert_into_collections(CassSession* session, const char* key, const char* items[]) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; CassCollection* collection = NULL; const char** item = NULL; const char* query = "INSERT INTO examples.collections (key, items) VALUES (?, ?);"; statement = cass_statement_new(query, 2); cass_statement_bind_string(statement, 0, key); collection = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2); for (item = items; *item; item++) { cass_collection_append_string(collection, *item); } cass_statement_bind_collection(statement, 1, collection); cass_collection_free(collection); 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; }
void insert_into_perf(CassSession* session, const char* query, const CassPrepared* prepared) { int i; CassFuture* futures[NUM_CONCURRENT_REQUESTS]; CassCollection* collection = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2); cass_collection_append_string(collection, "jazz"); cass_collection_append_string(collection, "2013"); for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { CassUuid id; CassStatement* statement; if (prepared != NULL) { statement = cass_prepared_bind(prepared); } else { statement = cass_statement_new(query, 5); } cass_uuid_gen_time(uuid_gen, &id); cass_statement_bind_uuid(statement, 0, id); cass_statement_bind_string(statement, 1, big_string); cass_statement_bind_string(statement, 2, big_string); cass_statement_bind_string(statement, 3, big_string); cass_statement_bind_collection(statement, 4, collection); futures[i] = cass_session_execute(session, statement); cass_statement_free(statement); } for (i = 0; i < NUM_CONCURRENT_REQUESTS; ++i) { CassFuture* future = futures[i]; CassError rc = cass_future_error_code(future); if (rc != CASS_OK) { print_error(future); } cass_future_free(future); } cass_collection_free(collection); }
void operator()(CassCollection* ptr) { if (ptr != NULL) { cass_collection_free(ptr); } }
CassError insert_into_udt(CassSession* session) { CassError rc = CASS_OK; CassStatement* statement = NULL; CassFuture* future = NULL; CassUuid id; char id_str[CASS_UUID_STRING_LENGTH]; const CassKeyspaceMeta* keyspace_meta = NULL; const CassDataType* udt_address = NULL; const CassDataType* udt_phone = NULL; const char* query = "INSERT INTO examples.udt (id, address) VALUES (?, ?)"; statement = cass_statement_new(query, 2); cass_uuid_gen_time(uuid_gen, &id); cass_uuid_string(id, id_str); keyspace_meta = cass_schema_meta_keyspace_by_name(schema_meta, "examples"); if (keyspace_meta != NULL) { udt_address = cass_keyspace_meta_user_type_by_name(keyspace_meta, "address"); udt_phone = cass_keyspace_meta_user_type_by_name(keyspace_meta, "phone_numbers"); } if (udt_address != NULL && udt_phone != NULL) { int i; CassUserType* address = cass_user_type_new_from_data_type(udt_address); CassCollection* phone = cass_collection_new(CASS_COLLECTION_TYPE_SET, 2); for (i = 0; i < 2; ++i) { CassUserType* phone_numbers = cass_user_type_new_from_data_type(udt_phone); cass_user_type_set_int32_by_name(phone_numbers, "phone1", i + 1); cass_user_type_set_int32_by_name(phone_numbers, "phone2", i + 2); cass_collection_append_user_type(phone, phone_numbers); cass_user_type_free(phone_numbers); } cass_user_type_set_string_by_name(address, "street", id_str); cass_user_type_set_string_by_name(address, "city", id_str); cass_user_type_set_int32_by_name(address, "zip", (cass_int32_t)id.time_and_version); cass_user_type_set_collection_by_name(address, "phone", phone); cass_statement_bind_uuid(statement, 0, id); cass_statement_bind_user_type(statement, 1, address); 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_user_type_free(address); cass_collection_free(phone); } cass_statement_free(statement); return rc; }