Ejemplo n.º 1
0
inline std::string generate_unique_str(CassUuidGen* uuid_gen) {
  CassUuid uuid;
  cass_uuid_gen_time(uuid_gen, &uuid);
  char buffer[CASS_UUID_STRING_LENGTH];
  cass_uuid_string(uuid, buffer);
  return boost::replace_all_copy(std::string(buffer), "-", "");
}
Ejemplo n.º 2
0
void on_select(CassFuture* future, void* data) {
  CassError code = cass_future_error_code(future);
  if (code != CASS_OK) {
    print_error(future);
  } else {
    const CassResult* result = cass_future_get_result(future);
    CassIterator* iterator = cass_iterator_from_result(result);
    while (cass_iterator_next(iterator)) {
      CassUuid key;
      char key_str[CASS_UUID_STRING_LENGTH];
      cass_uint64_t value = 0;
      const CassRow* row = cass_iterator_get_row(iterator);

      cass_value_get_uuid(cass_row_get_column(row, 0), &key);

      cass_uuid_string(key, key_str);
      cass_value_get_int64(cass_row_get_column(row, 1), (cass_int64_t*)&value);

      printf("%s, %llu\n", key_str, (unsigned long long)value);
    }
    cass_iterator_free(iterator);
    cass_result_free(result);
  }

  signal_exit((CassSession*)data);
}
Ejemplo n.º 3
0
/* {{{ Cassandra\Timeuuid::value() */
PHP_METHOD(Timeuuid, uuid)
{
  cassandra_uuid* uuid   = (cassandra_uuid*) zend_object_store_get_object(getThis() TSRMLS_CC);
  char*           string = emalloc((CASS_UUID_STRING_LENGTH) * sizeof(char));

  cass_uuid_string(uuid->uuid, string);

  RETURN_STRING(string, 0);
}
Ejemplo n.º 4
0
 static std::string to_string(CassUuid value) {
   char c_string[CASS_UUID_STRING_LENGTH];
   cass_uuid_string(value, c_string);
   return std::string(c_string);
 }
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
CassError select_from_udt(CassSession* session) {
  CassError rc = CASS_OK;
  CassStatement* statement = NULL;
  CassFuture* future = NULL;

  const char* query = "SELECT * FROM examples.udt";

  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);
  } else {
    const CassResult* result = NULL;
    CassIterator* rows = NULL;

    result = cass_future_get_result(future);
    rows = cass_iterator_from_result(result);

    while(cass_iterator_next(rows)) {
      CassUuid id;
      char id_str[CASS_UUID_STRING_LENGTH];
      const CassRow* row = cass_iterator_get_row(rows);
      const CassValue* id_value = cass_row_get_column_by_name(row, "id");
      const CassValue* address_value = cass_row_get_column_by_name(row, "address");
      CassIterator* fields = cass_iterator_fields_from_user_type(address_value);

      cass_value_get_uuid(id_value, &id);
      cass_uuid_string(id, id_str);

      printf("id %s ", id_str);

      while(fields != NULL && cass_iterator_next(fields)) {
        const char* field_name;
        size_t field_name_length;
        const CassValue* field_value = NULL;
        cass_iterator_get_user_type_field_name(fields, &field_name, &field_name_length);
        field_value = cass_iterator_get_user_type_field_value(fields);
        printf("%.*s ", (int)field_name_length, field_name);

        if (!cass_value_is_null(field_value)) {
          if (cass_value_type(field_value) == CASS_VALUE_TYPE_VARCHAR) {
            const char* text;
            size_t text_length;
            cass_value_get_string(field_value, &text, &text_length);
            printf("\"%.*s\" ", (int)text_length, text);
          } else if (cass_value_type(field_value) == CASS_VALUE_TYPE_INT) {
            cass_int32_t i;
            cass_value_get_int32(field_value, &i);
            printf("%d ", i);
          } else if (cass_value_type(field_value) == CASS_VALUE_TYPE_SET) {
            CassIterator* phone_numbers = cass_iterator_from_collection(field_value);
            while (cass_iterator_next(phone_numbers)) {
              const CassValue* phone_value = cass_iterator_get_value(phone_numbers);
              CassIterator* phone_fields = cass_iterator_fields_from_user_type(phone_value);
              assert(cass_value_type(phone_value) == CASS_VALUE_TYPE_UDT);
              while (cass_iterator_next(phone_fields)) {
                const CassValue* phone_number_value = cass_iterator_get_user_type_field_value(phone_fields);
                cass_int32_t i;
                cass_value_get_int32(phone_number_value, &i);
                printf("%d ", i);
              }
            }
          } else {
            printf("<invalid> ");
          }
        } else {
          printf("<null> ");
        }
      }

      printf("\n");
    }

    cass_result_free(result);
    cass_iterator_free(rows);
  }

  cass_future_free(future);
  cass_statement_free(statement);

  return rc;
}
Ejemplo n.º 7
0
std::string string_from_uuid(CassUuid uuid) {
  char buffer[CASS_UUID_STRING_LENGTH];
  cass_uuid_string(uuid, buffer);
  return std::string(buffer);
}