From 070020bb731f8a1f83e08a109836afadc7612588 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Fri, 21 Nov 2014 18:25:20 +0100 Subject: [PATCH 1/8] get complete row - start --- cassandra.cpp | 623 +++++++++++++++++++++++++++++--------------------- 1 file changed, 363 insertions(+), 260 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index acbda0c..928398c 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -317,6 +317,9 @@ ZEND_BEGIN_ARG_INFO_EX(cql_result_get, 0, 0, 1) ZEND_ARG_INFO(0, typeColumn) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(cql_result_get_row, 0, 0, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(cql_result_get_column_count, 0, 0, 0) ZEND_END_ARG_INFO() @@ -329,6 +332,7 @@ ZEND_END_ARG_INFO() PHP_METHOD(CqlResult, __construct); PHP_METHOD(CqlResult, exists); PHP_METHOD(CqlResult, get); +PHP_METHOD(CqlResult, getRow); PHP_METHOD(CqlResult, getColumnCount); PHP_METHOD(CqlResult, getRowCount); PHP_METHOD(CqlResult, next); @@ -337,6 +341,7 @@ const zend_function_entry php_cql_result_class_methods[] = { PHP_ME(CqlResult, __construct, cql_result_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) PHP_ME(CqlResult, exists, cql_result_exists, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, get, cql_result_get, ZEND_ACC_PUBLIC) + PHP_ME(CqlResult, getRow, cql_result_get_row, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, getColumnCount, cql_result_get_column_count, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, getRowCount, cql_result_get_row_count, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, next, cql_result_next, ZEND_ACC_PUBLIC) @@ -1002,10 +1007,22 @@ PHP_METHOD(CqlResult, get) int column_len; long column_type = cql::CQL_COLUMN_TYPE_UNKNOWN; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"s|l", &column, &column_len, &column_type) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"|sl", &column, &column_len, &column_type) == FAILURE) { return; } - + +// if (ZEND_NUM_ARGS() <= 0) +// { +// php_printf("
No arguments - get all columns
"); +//// get_column(); +// } +// else +// { +// php_printf("
Column: %s
", column); +//// get_column(column); +// } +// +// RETURN_NULL(); cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); cql::cql_bigint_t tmp_value_big_int = 0; @@ -1017,6 +1034,8 @@ PHP_METHOD(CqlResult, get) zval * tmp_zval; char * tmp_key; char * tmp_value_char; + int columns_count = 1; + const char * column_name = column; time_t ts = 0; long orig_column_type = 0; @@ -1028,387 +1047,471 @@ PHP_METHOD(CqlResult, get) std::vector< cql::cql_byte_t > data; std::stringstream ss; - cql::cql_column_type_enum type; - obj->cql_result->column_type(column, type); - orig_column_type = static_cast(type); - - if (column_type == cql::CQL_COLUMN_TYPE_UNKNOWN) { - column_type = orig_column_type; + if (ZEND_NUM_ARGS() <= 0) { + columns_count = obj->cql_result->column_count(); } - switch (column_type) { + php_array_init(return_value); - case cql::CQL_COLUMN_TYPE_BIGINT: + for (size_t i = 0; i < columns_count; ++i) { + std::string _keyspace, _table, _column; - obj->cql_result->get_bigint(column, tmp_value_big_int); + if (ZEND_NUM_ARGS() <= 0) { + obj->cql_result->column_name(i, _keyspace, _table, _column); + column_name = _column.c_str(); +// column = _column.c_str(); + php_printf("Keyspace: %s | Table: %s | Column: %s
", _keyspace.c_str(), _table.c_str(), column_name); + } - if (column_type != orig_column_type && tmp_value_big_int == 0) { - RETURN_NULL(); - return; - } + php_printf("
Column: %s
", column_name); - RETURN_LONG(tmp_value_big_int); + cql::cql_column_type_enum type; + obj->cql_result->column_type(column, type); + orig_column_type = static_cast(type); - break; + if (column_type == cql::CQL_COLUMN_TYPE_UNKNOWN) { + column_type = orig_column_type; + } - case cql::CQL_COLUMN_TYPE_BOOLEAN: + switch (column_type) { - obj->cql_result->get_bool(column, tmp_value_bool); + case cql::CQL_COLUMN_TYPE_BIGINT: - if (column_type != orig_column_type && !tmp_value_bool) { - RETURN_NULL(); - return; - } + obj->cql_result->get_bigint(column, tmp_value_big_int); - RETURN_BOOL(tmp_value_bool); + if (column_type != orig_column_type && tmp_value_big_int == 0) { + add_assoc_null(return_value, column_name); + return; + } - break; + add_assoc_long(return_value, column_name, tmp_value_big_int); - case cql::CQL_COLUMN_TYPE_DOUBLE: + break; - obj->cql_result->get_double(column, tmp_value_double); + case cql::CQL_COLUMN_TYPE_BOOLEAN: - if (column_type != orig_column_type && tmp_value_double == 0) { - RETURN_NULL(); - return; - } + obj->cql_result->get_bool(column, tmp_value_bool); - RETURN_DOUBLE(tmp_value_double); + if (column_type != orig_column_type && !tmp_value_bool) { + RETURN_NULL(); + return; + } - break; + RETURN_BOOL(tmp_value_bool); - case cql::CQL_COLUMN_TYPE_FLOAT: + break; - obj->cql_result->get_float(column, tmp_value_float); + case cql::CQL_COLUMN_TYPE_DOUBLE: - if (column_type != orig_column_type && tmp_value_float == 0) { - RETURN_NULL(); - return; - } + obj->cql_result->get_double(column, tmp_value_double); - RETURN_DOUBLE(tmp_value_float); + if (column_type != orig_column_type && tmp_value_double == 0) { + RETURN_NULL(); + return; + } - break; + RETURN_DOUBLE(tmp_value_double); - case cql::CQL_COLUMN_TYPE_INT: + break; - obj->cql_result->get_int(column, tmp_value_int); + case cql::CQL_COLUMN_TYPE_FLOAT: - if (column_type != orig_column_type && tmp_value_int == 0) { - RETURN_NULL(); - return; - } + obj->cql_result->get_float(column, tmp_value_float); - RETURN_LONG(tmp_value_int); + if (column_type != orig_column_type && tmp_value_float == 0) { + RETURN_NULL(); + return; + } - break; + RETURN_DOUBLE(tmp_value_float); - case cql::CQL_COLUMN_TYPE_MAP: + break; - if (!obj->cql_result->get_map(column, tmp_collection_map) || tmp_collection_map == NULL) { - RETURN_NULL(); - return; - } + case cql::CQL_COLUMN_TYPE_INT: - php_array_init(return_value); - ALLOC_INIT_ZVAL(tmp_zval); + obj->cql_result->get_int(column, tmp_value_int); - for (size_t i = 0; i < tmp_collection_map->size(); ++i) { + if (column_type != orig_column_type && tmp_value_int == 0) { + RETURN_NULL(); + return; + } + php_printf("
INT: %d
", tmp_value_int); + RETURN_LONG(tmp_value_int); - // detect key type - switch (tmp_collection_map->key_type()) { + break; - case cql::CQL_COLUMN_TYPE_BIGINT: - tmp_collection_map->get_key_bigint(i, tmp_value_big_int); - ZVAL_LONG(tmp_zval, tmp_value_big_int); - break; + case cql::CQL_COLUMN_TYPE_MAP: - case cql::CQL_COLUMN_TYPE_BOOLEAN: - tmp_collection_map->get_key_bool(i, tmp_value_bool); - ZVAL_BOOL(tmp_zval, tmp_value_bool); - break; + if (!obj->cql_result->get_map(column, tmp_collection_map) || tmp_collection_map == NULL) { + RETURN_NULL(); + return; + } - case cql::CQL_COLUMN_TYPE_DOUBLE: - tmp_collection_map->get_key_double(i, tmp_value_double); - ZVAL_DOUBLE(tmp_zval, tmp_value_double); - break; + php_array_init(return_value); + ALLOC_INIT_ZVAL(tmp_zval); - case cql::CQL_COLUMN_TYPE_FLOAT: - tmp_collection_map->get_key_float(i, tmp_value_float); - ZVAL_DOUBLE(tmp_zval, tmp_value_float); - break; + for (size_t i = 0; i < tmp_collection_map->size(); ++i) { - case cql::CQL_COLUMN_TYPE_INT: - tmp_collection_map->get_key_int(i, tmp_value_int); - ZVAL_LONG(tmp_zval, tmp_value_int); - break; + // detect key type + switch (tmp_collection_map->key_type()) { - case cql::CQL_COLUMN_TYPE_TIMESTAMP: - case cql::CQL_COLUMN_TYPE_TIMEUUID: + case cql::CQL_COLUMN_TYPE_BIGINT: + tmp_collection_map->get_key_bigint(i, tmp_value_big_int); + ZVAL_LONG(tmp_zval, tmp_value_big_int); + break; - // Driver doesn't have method for getting timestamp [workaround] - tmp_collection_map->get_key_bigint(i, tmp_value_big_int); + case cql::CQL_COLUMN_TYPE_BOOLEAN: + tmp_collection_map->get_key_bool(i, tmp_value_bool); + ZVAL_BOOL(tmp_zval, tmp_value_bool); + break; - if (tmp_value_big_int > 1000) { - ts = (time_t) (tmp_value_big_int / 1000); - } - else { - ts = (time_t) 0; - } + case cql::CQL_COLUMN_TYPE_DOUBLE: + tmp_collection_map->get_key_double(i, tmp_value_double); + ZVAL_DOUBLE(tmp_zval, tmp_value_double); + break; - tmp_key = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); - ZVAL_STRING(tmp_zval, tmp_key, 1); + case cql::CQL_COLUMN_TYPE_FLOAT: + tmp_collection_map->get_key_float(i, tmp_value_float); + ZVAL_DOUBLE(tmp_zval, tmp_value_float); + break; - break; + case cql::CQL_COLUMN_TYPE_INT: + tmp_collection_map->get_key_int(i, tmp_value_int); + ZVAL_LONG(tmp_zval, tmp_value_int); + break; - case cql::CQL_COLUMN_TYPE_TEXT: - case cql::CQL_COLUMN_TYPE_VARCHAR: - tmp_collection_map->get_key_string(i, tmp_value_string); - ZVAL_STRING(tmp_zval, tmp_value_string.c_str(), 1); - break; + case cql::CQL_COLUMN_TYPE_TIMESTAMP: + case cql::CQL_COLUMN_TYPE_TIMEUUID: - } + // Driver doesn't have method for getting timestamp [workaround] + tmp_collection_map->get_key_bigint(i, tmp_value_big_int); - if (tmp_zval->type != IS_STRING) { - convert_to_string_ex(&tmp_zval); - } + if (tmp_value_big_int > 1000) { + ts = (time_t) (tmp_value_big_int / 1000); + } + else { + ts = (time_t) 0; + } - tmp_key = Z_STRVAL_P(tmp_zval); + tmp_key = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); + ZVAL_STRING(tmp_zval, tmp_key, 1); - // detect value type and add new element to array - switch (tmp_collection_map->value_type()) { + break; - case cql::CQL_COLUMN_TYPE_BIGINT: - tmp_collection_map->get_value_bigint(i, tmp_value_big_int); - add_assoc_long(return_value, tmp_key, tmp_value_big_int); - break; + case cql::CQL_COLUMN_TYPE_TEXT: + case cql::CQL_COLUMN_TYPE_VARCHAR: + tmp_collection_map->get_key_string(i, tmp_value_string); + ZVAL_STRING(tmp_zval, tmp_value_string.c_str(), 1); + break; - case cql::CQL_COLUMN_TYPE_BOOLEAN: - tmp_collection_map->get_value_bool(i, tmp_value_bool); - add_assoc_bool(return_value, tmp_key, tmp_value_bool); - break; + } - case cql::CQL_COLUMN_TYPE_DOUBLE: - tmp_collection_map->get_value_double(i, tmp_value_double); - add_assoc_double(return_value, tmp_key, tmp_value_double); - break; + if (tmp_zval->type != IS_STRING) { + convert_to_string_ex(&tmp_zval); + } - case cql::CQL_COLUMN_TYPE_FLOAT: - tmp_collection_map->get_value_float(i, tmp_value_float); - add_assoc_double(return_value, tmp_key, tmp_value_float); - break; + tmp_key = Z_STRVAL_P(tmp_zval); - case cql::CQL_COLUMN_TYPE_INT: - tmp_collection_map->get_value_int(i, tmp_value_int); - add_assoc_long(return_value, tmp_key, tmp_value_int); - break; + // detect value type and add new element to array + switch (tmp_collection_map->value_type()) { - case cql::CQL_COLUMN_TYPE_TIMESTAMP: - case cql::CQL_COLUMN_TYPE_TIMEUUID: + case cql::CQL_COLUMN_TYPE_BIGINT: + tmp_collection_map->get_value_bigint(i, tmp_value_big_int); + add_assoc_long(return_value, tmp_key, tmp_value_big_int); + break; - // Driver doesn't have method for getting timestamp [workaround] - tmp_collection_map->get_value_bigint(i, tmp_value_big_int); + case cql::CQL_COLUMN_TYPE_BOOLEAN: + tmp_collection_map->get_value_bool(i, tmp_value_bool); + add_assoc_bool(return_value, tmp_key, tmp_value_bool); + break; - if (tmp_value_big_int > 1000) { - ts = (time_t) (tmp_value_big_int / 1000); - } - else { - ts = (time_t) 0; - } + case cql::CQL_COLUMN_TYPE_DOUBLE: + tmp_collection_map->get_value_double(i, tmp_value_double); + add_assoc_double(return_value, tmp_key, tmp_value_double); + break; - tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); - add_assoc_string(return_value, tmp_key, tmp_value_char, 1); + case cql::CQL_COLUMN_TYPE_FLOAT: + tmp_collection_map->get_value_float(i, tmp_value_float); + add_assoc_double(return_value, tmp_key, tmp_value_float); + break; - break; + case cql::CQL_COLUMN_TYPE_INT: + tmp_collection_map->get_value_int(i, tmp_value_int); + add_assoc_long(return_value, tmp_key, tmp_value_int); + break; - case cql::CQL_COLUMN_TYPE_TEXT: - case cql::CQL_COLUMN_TYPE_VARCHAR: - tmp_collection_map->get_value_string(i, tmp_value_string); - add_assoc_string(return_value, tmp_key, (char *) tmp_value_string.c_str(), 1); - break; + case cql::CQL_COLUMN_TYPE_TIMESTAMP: + case cql::CQL_COLUMN_TYPE_TIMEUUID: - } + // Driver doesn't have method for getting timestamp [workaround] + tmp_collection_map->get_value_bigint(i, tmp_value_big_int); - } + if (tmp_value_big_int > 1000) { + ts = (time_t) (tmp_value_big_int / 1000); + } + else { + ts = (time_t) 0; + } - return; + tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); + add_assoc_string(return_value, tmp_key, tmp_value_char, 1); - break; + break; - case cql::CQL_COLUMN_TYPE_LIST: + case cql::CQL_COLUMN_TYPE_TEXT: + case cql::CQL_COLUMN_TYPE_VARCHAR: + tmp_collection_map->get_value_string(i, tmp_value_string); + add_assoc_string(return_value, tmp_key, (char *) tmp_value_string.c_str(), 1); + break; + + } + + } - if (!obj->cql_result->get_list(column, tmp_collection_list) || tmp_collection_list == NULL) { - RETURN_NULL(); return; - } - php_array_init(return_value); + break; - for (size_t i = 0; i < tmp_collection_list->size(); ++i) { + case cql::CQL_COLUMN_TYPE_LIST: - switch (tmp_collection_list->element_type()) { + if (!obj->cql_result->get_list(column, tmp_collection_list) || tmp_collection_list == NULL) { + RETURN_NULL(); + return; + } - case cql::CQL_COLUMN_TYPE_BIGINT: - tmp_collection_list->get_bigint(i, tmp_value_big_int); - add_next_index_long(return_value, tmp_value_big_int); - break; + php_array_init(return_value); - case cql::CQL_COLUMN_TYPE_BOOLEAN: - tmp_collection_list->get_bool(i, tmp_value_bool); - add_next_index_bool(return_value, tmp_value_bool); - break; + for (size_t i = 0; i < tmp_collection_list->size(); ++i) { - case cql::CQL_COLUMN_TYPE_DOUBLE: - tmp_collection_list->get_double(i, tmp_value_double); - add_next_index_double(return_value, tmp_value_double); - break; + switch (tmp_collection_list->element_type()) { - case cql::CQL_COLUMN_TYPE_FLOAT: - tmp_collection_list->get_float(i, tmp_value_float); - add_next_index_double(return_value, tmp_value_float); - break; + case cql::CQL_COLUMN_TYPE_BIGINT: + tmp_collection_list->get_bigint(i, tmp_value_big_int); + add_next_index_long(return_value, tmp_value_big_int); + break; - case cql::CQL_COLUMN_TYPE_INT: - tmp_collection_list->get_int(i, tmp_value_int); - add_next_index_long(return_value, tmp_value_int); - break; + case cql::CQL_COLUMN_TYPE_BOOLEAN: + tmp_collection_list->get_bool(i, tmp_value_bool); + add_next_index_bool(return_value, tmp_value_bool); + break; - case cql::CQL_COLUMN_TYPE_VARCHAR: - case cql::CQL_COLUMN_TYPE_TEXT: - tmp_collection_list->get_string(i, tmp_value_string); - add_next_index_string(return_value, tmp_value_string.c_str(), 1); - break; + case cql::CQL_COLUMN_TYPE_DOUBLE: + tmp_collection_list->get_double(i, tmp_value_double); + add_next_index_double(return_value, tmp_value_double); + break; - } + case cql::CQL_COLUMN_TYPE_FLOAT: + tmp_collection_list->get_float(i, tmp_value_float); + add_next_index_double(return_value, tmp_value_float); + break; - } + case cql::CQL_COLUMN_TYPE_INT: + tmp_collection_list->get_int(i, tmp_value_int); + add_next_index_long(return_value, tmp_value_int); + break; - return; + case cql::CQL_COLUMN_TYPE_VARCHAR: + case cql::CQL_COLUMN_TYPE_TEXT: + tmp_collection_list->get_string(i, tmp_value_string); + add_next_index_string(return_value, tmp_value_string.c_str(), 1); + break; - break; + } - case cql::CQL_COLUMN_TYPE_SET: + } - if (!obj->cql_result->get_set(column, tmp_collection_set) || tmp_collection_set == NULL) { - RETURN_NULL(); return; - } - php_array_init(return_value); + break; + + case cql::CQL_COLUMN_TYPE_SET: + + if (!obj->cql_result->get_set(column, tmp_collection_set) || tmp_collection_set == NULL) { + RETURN_NULL(); + return; + } + + php_array_init(return_value); - for (size_t i = 0; i < tmp_collection_set->size(); ++i) { + for (size_t i = 0; i < tmp_collection_set->size(); ++i) { - switch (tmp_collection_set->element_type()) { + switch (tmp_collection_set->element_type()) { - case cql::CQL_COLUMN_TYPE_BIGINT: - tmp_collection_set->get_bigint(i, tmp_value_big_int); - add_next_index_long(return_value, tmp_value_big_int); - break; + case cql::CQL_COLUMN_TYPE_BIGINT: + tmp_collection_set->get_bigint(i, tmp_value_big_int); + add_next_index_long(return_value, tmp_value_big_int); + break; - case cql::CQL_COLUMN_TYPE_BOOLEAN: - tmp_collection_set->get_bool(i, tmp_value_bool); - add_next_index_bool(return_value, tmp_value_bool); - break; + case cql::CQL_COLUMN_TYPE_BOOLEAN: + tmp_collection_set->get_bool(i, tmp_value_bool); + add_next_index_bool(return_value, tmp_value_bool); + break; - case cql::CQL_COLUMN_TYPE_DOUBLE: - tmp_collection_set->get_double(i, tmp_value_double); - add_next_index_double(return_value, tmp_value_double); - break; + case cql::CQL_COLUMN_TYPE_DOUBLE: + tmp_collection_set->get_double(i, tmp_value_double); + add_next_index_double(return_value, tmp_value_double); + break; - case cql::CQL_COLUMN_TYPE_FLOAT: - tmp_collection_set->get_float(i, tmp_value_float); - add_next_index_double(return_value, tmp_value_float); - break; + case cql::CQL_COLUMN_TYPE_FLOAT: + tmp_collection_set->get_float(i, tmp_value_float); + add_next_index_double(return_value, tmp_value_float); + break; - case cql::CQL_COLUMN_TYPE_INT: - tmp_collection_set->get_int(i, tmp_value_int); - add_next_index_long(return_value, tmp_value_int); - break; + case cql::CQL_COLUMN_TYPE_INT: + tmp_collection_set->get_int(i, tmp_value_int); + add_next_index_long(return_value, tmp_value_int); + break; - case cql::CQL_COLUMN_TYPE_VARCHAR: - case cql::CQL_COLUMN_TYPE_TEXT: - tmp_collection_set->get_string(i, tmp_value_string); - add_next_index_string(return_value, tmp_value_string.c_str(), 1); - break; + case cql::CQL_COLUMN_TYPE_VARCHAR: + case cql::CQL_COLUMN_TYPE_TEXT: + tmp_collection_set->get_string(i, tmp_value_string); + add_next_index_string(return_value, tmp_value_string.c_str(), 1); + break; + + } } - } + return; - return; + break; - break; + case cql::CQL_COLUMN_TYPE_TIMESTAMP: + case cql::CQL_COLUMN_TYPE_TIMEUUID: - case cql::CQL_COLUMN_TYPE_TIMESTAMP: - case cql::CQL_COLUMN_TYPE_TIMEUUID: + // Driver doesn't have method for getting timestamp [workaround] + obj->cql_result->get_bigint(column, tmp_value_big_int); - // Driver doesn't have method for getting timestamp [workaround] - obj->cql_result->get_bigint(column, tmp_value_big_int); + if (column_type != orig_column_type && tmp_value_big_int == 0) { + RETURN_NULL(); + return; + } - if (column_type != orig_column_type && tmp_value_big_int == 0) { - RETURN_NULL(); - return; - } + if (tmp_value_big_int > 1000) { + ts = (time_t) (tmp_value_big_int / 1000); + } + else { + ts = (time_t) 0; + } - if (tmp_value_big_int > 1000) { - ts = (time_t) (tmp_value_big_int / 1000); - } - else { - ts = (time_t) 0; - } + tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); + RETURN_STRING(tmp_value_char, 1); - tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); - RETURN_STRING(tmp_value_char, 1); + break; - break; + case cql::CQL_COLUMN_TYPE_TEXT: + case cql::CQL_COLUMN_TYPE_VARCHAR: - case cql::CQL_COLUMN_TYPE_TEXT: - case cql::CQL_COLUMN_TYPE_VARCHAR: + obj->cql_result->get_string(column, tmp_value_string); - obj->cql_result->get_string(column, tmp_value_string); + if (tmp_value_string.length() < 1) { + RETURN_NULL(); + return; + } - if (tmp_value_string.length() < 1) { - RETURN_NULL(); - return; - } + RETURN_STRING(tmp_value_string.c_str(), 1); - RETURN_STRING(tmp_value_string.c_str(), 1); + break; - break; + case cql::CQL_COLUMN_TYPE_UUID: - case cql::CQL_COLUMN_TYPE_UUID: + obj->cql_result->get_data(column, data); - obj->cql_result->get_data(column, data); + for (size_t i = 0; i < data.size(); ++i) { - for (size_t i = 0; i < data.size(); ++i) { + unsigned char * ch = reinterpret_cast(&data[i]); - unsigned char * ch = reinterpret_cast(&data[i]); + int c = (int) *ch; + ss << std::setfill('0') << std::setw(2) << std::hex << c; - int c = (int) *ch; - ss << std::setfill('0') << std::setw(2) << std::hex << c; + if (i == 3 || i == 5 || i == 7 || i == 9) { + ss << "-"; + } - if (i == 3 || i == 5 || i == 7 || i == 9) { - ss << "-"; } - } - - tmp_value_string = ss.str(); + tmp_value_string = ss.str(); - if (tmp_value_string.length() < 1) { - RETURN_NULL(); - return; - } + if (tmp_value_string.length() < 1) { + RETURN_NULL(); + return; + } - RETURN_STRING(tmp_value_string.c_str(), 1); + RETURN_STRING(tmp_value_string.c_str(), 1); - break; + break; + } } RETURN_NULL(); } +PHP_METHOD(CqlResult, getRow) +{ + char *mystr; + zval *mysubarray; + php_array_init(return_value); + + add_index_long(return_value, 42, 123); + + add_next_index_string(return_value, "I should now be found at index 43", 1); + + add_next_index_stringl(return_value, "I'm at 44!", 10, 1); + add_assoc_long(return_value, "index_45", 456); + + mystr = estrdup("Forty Five"); + add_next_index_string(return_value, mystr, 0); + + add_assoc_double(return_value, "pi", 3.1415926535); + + ALLOC_INIT_ZVAL(mysubarray); + php_array_init(mysubarray); + add_next_index_string(mysubarray, "hello", 1); + add_assoc_zval(return_value, "subarray", mysubarray); + + zval **d = NULL; + int index = 42; + + zend_hash_find(Z_ARRVAL_P(return_value), "index_45", 9, (void **)&d); +// +// /* Make sure the zval is a string */ +// convert_to_string(*d); + + RETURN_ZVAL(*d, true, true); +// php_printf("
return value of index %u = %s
", index, Z_STRVAL_PP(d)); + +// RETURN_NULL(); + +// cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); +// cql_future_result_object *future_obj = (cql_future_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); +// +//// while (obj->cql_result->next()) +//// { +// int count; +// count = obj->cql_result->column_count(); +// php_printf("%d
", count); +// +// for (size_t i = 0; i < obj->cql_result->column_count(); ++i) +// { +// cql::cql_byte_t data; +// int exists; +// std::string keyspace, table, column; +// +// exists = obj->cql_result->exists("test");//get(); +// obj->cql_result->column_name(i, keyspace, table, column); +//// php_printf("%u
", exists); +// php_printf("Keyspace: %s | Table: %s | Column: %s
", keyspace.c_str(), table.c_str(), column.c_str()); +//// php_printf("%s", obj->cql_result); +//// php_echo('xxxx'); +// //php_print(&data[0]) +// //php_print(data.size()); +// } +//// } +// +// RETURN_NULL(); +} + PHP_METHOD(CqlResult, getColumnCount) { cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); From 77dc5269264961597e6e80fd9e572b982229a17d Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Mon, 24 Nov 2014 02:15:23 +0100 Subject: [PATCH 2/8] get complete row if empty arguments --- cassandra.cpp | 167 ++++++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 87 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index 928398c..d7eb243 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -1006,23 +1006,12 @@ PHP_METHOD(CqlResult, get) char * column; int column_len; long column_type = cql::CQL_COLUMN_TYPE_UNKNOWN; + long tmp_column_type = column_type; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"|sl", &column, &column_len, &column_type) == FAILURE) { return; } - -// if (ZEND_NUM_ARGS() <= 0) -// { -// php_printf("
No arguments - get all columns
"); -//// get_column(); -// } -// else -// { -// php_printf("
Column: %s
", column); -//// get_column(column); -// } -// -// RETURN_NULL(); + cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); cql::cql_bigint_t tmp_value_big_int = 0; @@ -1035,7 +1024,7 @@ PHP_METHOD(CqlResult, get) char * tmp_key; char * tmp_value_char; int columns_count = 1; - const char * column_name = column; + zval * subarray; time_t ts = 0; long orig_column_type = 0; @@ -1058,72 +1047,65 @@ PHP_METHOD(CqlResult, get) if (ZEND_NUM_ARGS() <= 0) { obj->cql_result->column_name(i, _keyspace, _table, _column); - column_name = _column.c_str(); -// column = _column.c_str(); - php_printf("Keyspace: %s | Table: %s | Column: %s
", _keyspace.c_str(), _table.c_str(), column_name); + column = const_cast(_column.c_str()); } - php_printf("
Column: %s
", column_name); - cql::cql_column_type_enum type; obj->cql_result->column_type(column, type); orig_column_type = static_cast(type); if (column_type == cql::CQL_COLUMN_TYPE_UNKNOWN) { - column_type = orig_column_type; + tmp_column_type = orig_column_type; } - switch (column_type) { + switch (tmp_column_type) { case cql::CQL_COLUMN_TYPE_BIGINT: obj->cql_result->get_bigint(column, tmp_value_big_int); - if (column_type != orig_column_type && tmp_value_big_int == 0) { - add_assoc_null(return_value, column_name); - return; + if (tmp_column_type != orig_column_type && tmp_value_big_int == 0) { + add_assoc_null(return_value, column); + continue; } - add_assoc_long(return_value, column_name, tmp_value_big_int); + add_assoc_long(return_value, column, tmp_value_big_int); break; case cql::CQL_COLUMN_TYPE_BOOLEAN: - obj->cql_result->get_bool(column, tmp_value_bool); - if (column_type != orig_column_type && !tmp_value_bool) { - RETURN_NULL(); - return; + if (tmp_column_type != orig_column_type && !tmp_value_bool) { + add_assoc_null(return_value, column); + continue; } - RETURN_BOOL(tmp_value_bool); + add_assoc_bool(return_value, column, tmp_value_bool); break; case cql::CQL_COLUMN_TYPE_DOUBLE: - obj->cql_result->get_double(column, tmp_value_double); - if (column_type != orig_column_type && tmp_value_double == 0) { - RETURN_NULL(); - return; + if (tmp_column_type != orig_column_type && tmp_value_double == 0) { + add_assoc_null(return_value, column); + continue; } - RETURN_DOUBLE(tmp_value_double); + add_assoc_double(return_value, column, tmp_value_double); break; case cql::CQL_COLUMN_TYPE_FLOAT: - obj->cql_result->get_float(column, tmp_value_float); - if (column_type != orig_column_type && tmp_value_float == 0) { - RETURN_NULL(); - return; + if (tmp_column_type != orig_column_type && tmp_value_float == 0) { + add_assoc_null(return_value, column); + continue; } - RETURN_DOUBLE(tmp_value_float); + add_assoc_double(return_value, column, tmp_value_float); break; @@ -1131,23 +1113,24 @@ PHP_METHOD(CqlResult, get) obj->cql_result->get_int(column, tmp_value_int); - if (column_type != orig_column_type && tmp_value_int == 0) { - RETURN_NULL(); - return; + if (tmp_column_type != orig_column_type && tmp_value_int == 0) { + add_assoc_null(return_value, column); + continue; } - php_printf("
INT: %d
", tmp_value_int); - RETURN_LONG(tmp_value_int); + + add_assoc_long(return_value, column, tmp_value_int); break; case cql::CQL_COLUMN_TYPE_MAP: if (!obj->cql_result->get_map(column, tmp_collection_map) || tmp_collection_map == NULL) { - RETURN_NULL(); - return; + add_assoc_null(return_value, column); + continue; } - php_array_init(return_value); + subarray = NULL; + php_array_init(subarray); ALLOC_INIT_ZVAL(tmp_zval); for (size_t i = 0; i < tmp_collection_map->size(); ++i) { @@ -1217,27 +1200,27 @@ PHP_METHOD(CqlResult, get) case cql::CQL_COLUMN_TYPE_BIGINT: tmp_collection_map->get_value_bigint(i, tmp_value_big_int); - add_assoc_long(return_value, tmp_key, tmp_value_big_int); + add_assoc_long(subarray, tmp_key, tmp_value_big_int); break; case cql::CQL_COLUMN_TYPE_BOOLEAN: tmp_collection_map->get_value_bool(i, tmp_value_bool); - add_assoc_bool(return_value, tmp_key, tmp_value_bool); + add_assoc_bool(subarray, tmp_key, tmp_value_bool); break; case cql::CQL_COLUMN_TYPE_DOUBLE: tmp_collection_map->get_value_double(i, tmp_value_double); - add_assoc_double(return_value, tmp_key, tmp_value_double); + add_assoc_double(subarray, tmp_key, tmp_value_double); break; case cql::CQL_COLUMN_TYPE_FLOAT: tmp_collection_map->get_value_float(i, tmp_value_float); - add_assoc_double(return_value, tmp_key, tmp_value_float); + add_assoc_double(subarray, tmp_key, tmp_value_float); break; case cql::CQL_COLUMN_TYPE_INT: tmp_collection_map->get_value_int(i, tmp_value_int); - add_assoc_long(return_value, tmp_key, tmp_value_int); + add_assoc_long(subarray, tmp_key, tmp_value_int); break; case cql::CQL_COLUMN_TYPE_TIMESTAMP: @@ -1254,32 +1237,33 @@ PHP_METHOD(CqlResult, get) } tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); - add_assoc_string(return_value, tmp_key, tmp_value_char, 1); + add_assoc_string(subarray, tmp_key, tmp_value_char, 1); break; case cql::CQL_COLUMN_TYPE_TEXT: case cql::CQL_COLUMN_TYPE_VARCHAR: tmp_collection_map->get_value_string(i, tmp_value_string); - add_assoc_string(return_value, tmp_key, (char *) tmp_value_string.c_str(), 1); + add_assoc_string(subarray, tmp_key, (char *) tmp_value_string.c_str(), 1); break; } } - return; + add_assoc_zval(return_value, column, subarray); break; case cql::CQL_COLUMN_TYPE_LIST: if (!obj->cql_result->get_list(column, tmp_collection_list) || tmp_collection_list == NULL) { - RETURN_NULL(); - return; + add_assoc_null(return_value, column); + continue; } - php_array_init(return_value); + subarray = NULL; + php_array_init(subarray); for (size_t i = 0; i < tmp_collection_list->size(); ++i) { @@ -1287,51 +1271,52 @@ PHP_METHOD(CqlResult, get) case cql::CQL_COLUMN_TYPE_BIGINT: tmp_collection_list->get_bigint(i, tmp_value_big_int); - add_next_index_long(return_value, tmp_value_big_int); + add_next_index_long(subarray, tmp_value_big_int); break; case cql::CQL_COLUMN_TYPE_BOOLEAN: tmp_collection_list->get_bool(i, tmp_value_bool); - add_next_index_bool(return_value, tmp_value_bool); + add_next_index_bool(subarray, tmp_value_bool); break; case cql::CQL_COLUMN_TYPE_DOUBLE: tmp_collection_list->get_double(i, tmp_value_double); - add_next_index_double(return_value, tmp_value_double); + add_next_index_double(subarray, tmp_value_double); break; case cql::CQL_COLUMN_TYPE_FLOAT: tmp_collection_list->get_float(i, tmp_value_float); - add_next_index_double(return_value, tmp_value_float); + add_next_index_double(subarray, tmp_value_float); break; case cql::CQL_COLUMN_TYPE_INT: tmp_collection_list->get_int(i, tmp_value_int); - add_next_index_long(return_value, tmp_value_int); + add_next_index_long(subarray, tmp_value_int); break; case cql::CQL_COLUMN_TYPE_VARCHAR: case cql::CQL_COLUMN_TYPE_TEXT: tmp_collection_list->get_string(i, tmp_value_string); - add_next_index_string(return_value, tmp_value_string.c_str(), 1); + add_next_index_string(subarray, tmp_value_string.c_str(), 1); break; } } - return; + add_assoc_zval(return_value, column, subarray); break; case cql::CQL_COLUMN_TYPE_SET: if (!obj->cql_result->get_set(column, tmp_collection_set) || tmp_collection_set == NULL) { - RETURN_NULL(); - return; + add_assoc_null(return_value, column); + continue; } - php_array_init(return_value); + subarray = NULL; + php_array_init(subarray); for (size_t i = 0; i < tmp_collection_set->size(); ++i) { @@ -1339,40 +1324,40 @@ PHP_METHOD(CqlResult, get) case cql::CQL_COLUMN_TYPE_BIGINT: tmp_collection_set->get_bigint(i, tmp_value_big_int); - add_next_index_long(return_value, tmp_value_big_int); + add_next_index_long(subarray, tmp_value_big_int); break; case cql::CQL_COLUMN_TYPE_BOOLEAN: tmp_collection_set->get_bool(i, tmp_value_bool); - add_next_index_bool(return_value, tmp_value_bool); + add_next_index_bool(subarray, tmp_value_bool); break; case cql::CQL_COLUMN_TYPE_DOUBLE: tmp_collection_set->get_double(i, tmp_value_double); - add_next_index_double(return_value, tmp_value_double); + add_next_index_double(subarray, tmp_value_double); break; case cql::CQL_COLUMN_TYPE_FLOAT: tmp_collection_set->get_float(i, tmp_value_float); - add_next_index_double(return_value, tmp_value_float); + add_next_index_double(subarray, tmp_value_float); break; case cql::CQL_COLUMN_TYPE_INT: tmp_collection_set->get_int(i, tmp_value_int); - add_next_index_long(return_value, tmp_value_int); + add_next_index_long(subarray, tmp_value_int); break; case cql::CQL_COLUMN_TYPE_VARCHAR: case cql::CQL_COLUMN_TYPE_TEXT: tmp_collection_set->get_string(i, tmp_value_string); - add_next_index_string(return_value, tmp_value_string.c_str(), 1); + add_next_index_string(subarray, tmp_value_string.c_str(), 1); break; } } - return; + add_assoc_zval(return_value, column, subarray); break; @@ -1382,9 +1367,9 @@ PHP_METHOD(CqlResult, get) // Driver doesn't have method for getting timestamp [workaround] obj->cql_result->get_bigint(column, tmp_value_big_int); - if (column_type != orig_column_type && tmp_value_big_int == 0) { - RETURN_NULL(); - return; + if (tmp_column_type != orig_column_type && tmp_value_big_int == 0) { + add_assoc_null(return_value, column); + continue; } if (tmp_value_big_int > 1000) { @@ -1395,7 +1380,7 @@ PHP_METHOD(CqlResult, get) } tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); - RETURN_STRING(tmp_value_char, 1); + add_assoc_string(return_value, column, tmp_value_char, 1); break; @@ -1405,11 +1390,11 @@ PHP_METHOD(CqlResult, get) obj->cql_result->get_string(column, tmp_value_string); if (tmp_value_string.length() < 1) { - RETURN_NULL(); - return; + add_assoc_null(return_value, column); + continue; } - RETURN_STRING(tmp_value_string.c_str(), 1); + add_assoc_string(return_value, column, const_cast(tmp_value_string.c_str()), 1); break; @@ -1433,18 +1418,26 @@ PHP_METHOD(CqlResult, get) tmp_value_string = ss.str(); if (tmp_value_string.length() < 1) { - RETURN_NULL(); - return; + add_assoc_null(return_value, column); + continue; } - RETURN_STRING(tmp_value_string.c_str(), 1); + add_assoc_string(return_value, column, const_cast(tmp_value_string.c_str()), 1); break; } } - RETURN_NULL(); + if (ZEND_NUM_ARGS() > 0) { + zval **d = NULL; + const char * cname = (const char *)column; + zend_hash_find(Z_ARRVAL_P(return_value), cname, (column_len + 1), (void **)&d); + if (d) { + RETURN_ZVAL(*d, true, true); + } + RETURN_NULL(); + } } PHP_METHOD(CqlResult, getRow) From e5e3c665859e58b6a9f5cc782eebba3c2c0981b1 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Tue, 25 Nov 2014 15:31:00 +0100 Subject: [PATCH 3/8] merge --- cassandra.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index d7eb243..a184e68 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -26,6 +26,8 @@ extern "C" { #endif +#define CQL_USE_BOOST_MULTIPRECISION true; + #include "php.h" #include "php_ini.h" #include "ext/date/php_date.h" @@ -52,6 +54,8 @@ extern "C" { #include #include #include +#include +#include /* If you declare any globals in php_cassandra.h uncomment this: @@ -1025,6 +1029,11 @@ PHP_METHOD(CqlResult, get) char * tmp_value_char; int columns_count = 1; zval * subarray; + boost::asio::ip::address tmp_value_inet; + cql::cql_decimal_t tmp_value_decimal; + cql::cql_varint_t tmp_value_varint; + double _tmp_value_decimal; + boost::multiprecision::cpp_int _tmp_value_varint; time_t ts = 0; long orig_column_type = 0; @@ -1384,6 +1393,8 @@ PHP_METHOD(CqlResult, get) break; + case cql::CQL_COLUMN_TYPE_ASCII: + case cql::CQL_COLUMN_TYPE_BLOB: case cql::CQL_COLUMN_TYPE_TEXT: case cql::CQL_COLUMN_TYPE_VARCHAR: @@ -1398,9 +1409,50 @@ PHP_METHOD(CqlResult, get) break; - case cql::CQL_COLUMN_TYPE_UUID: + case cql::CQL_COLUMN_TYPE_INET: + + obj->cql_result->get_inet(column, tmp_value_inet); + + if (tmp_value_inet.is_unspecified()) { + add_assoc_null(return_value, column); + continue; + } + + add_assoc_string(return_value, column, const_cast(tmp_value_inet.to_string().c_str()), 1); + + break; + + case cql::CQL_COLUMN_TYPE_DECIMAL: + + obj->cql_result->get_decimal(column, tmp_value_decimal); + + if (!tmp_value_decimal.is_convertible_to_double()) { + add_assoc_null(return_value, column); + continue; + } - obj->cql_result->get_data(column, data); + tmp_value_decimal.convert_to_double(_tmp_value_decimal); + + add_assoc_double(return_value, column, _tmp_value_decimal); + + break; + + case cql::CQL_COLUMN_TYPE_VARINT: + + obj->cql_result->get_varint(column, tmp_value_varint); + + tmp_value_varint.convert_to_boost_multiprecision(_tmp_value_varint); + + if (_tmp_value_varint.is_zero() && !tmp_value_varint.is_convertible_to_int64()) { + add_assoc_null(return_value, column); + continue; + } + + add_assoc_string(return_value, column, const_cast(_tmp_value_varint.str().c_str()), 1); + + break; + + case cql::CQL_COLUMN_TYPE_UUID: for (size_t i = 0; i < data.size(); ++i) { From 50d8590af1c3d2992a359b36628ca37549ff8ef5 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Tue, 25 Nov 2014 15:53:23 +0100 Subject: [PATCH 4/8] remove test code --- cassandra.cpp | 67 --------------------------------------------------- 1 file changed, 67 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index a184e68..56d1004 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -336,7 +336,6 @@ ZEND_END_ARG_INFO() PHP_METHOD(CqlResult, __construct); PHP_METHOD(CqlResult, exists); PHP_METHOD(CqlResult, get); -PHP_METHOD(CqlResult, getRow); PHP_METHOD(CqlResult, getColumnCount); PHP_METHOD(CqlResult, getRowCount); PHP_METHOD(CqlResult, next); @@ -345,7 +344,6 @@ const zend_function_entry php_cql_result_class_methods[] = { PHP_ME(CqlResult, __construct, cql_result_construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR) PHP_ME(CqlResult, exists, cql_result_exists, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, get, cql_result_get, ZEND_ACC_PUBLIC) - PHP_ME(CqlResult, getRow, cql_result_get_row, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, getColumnCount, cql_result_get_column_count, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, getRowCount, cql_result_get_row_count, ZEND_ACC_PUBLIC) PHP_ME(CqlResult, next, cql_result_next, ZEND_ACC_PUBLIC) @@ -1492,71 +1490,6 @@ PHP_METHOD(CqlResult, get) } } -PHP_METHOD(CqlResult, getRow) -{ - char *mystr; - zval *mysubarray; - php_array_init(return_value); - - add_index_long(return_value, 42, 123); - - add_next_index_string(return_value, "I should now be found at index 43", 1); - - add_next_index_stringl(return_value, "I'm at 44!", 10, 1); - add_assoc_long(return_value, "index_45", 456); - - mystr = estrdup("Forty Five"); - add_next_index_string(return_value, mystr, 0); - - add_assoc_double(return_value, "pi", 3.1415926535); - - ALLOC_INIT_ZVAL(mysubarray); - php_array_init(mysubarray); - add_next_index_string(mysubarray, "hello", 1); - add_assoc_zval(return_value, "subarray", mysubarray); - - zval **d = NULL; - int index = 42; - - zend_hash_find(Z_ARRVAL_P(return_value), "index_45", 9, (void **)&d); -// -// /* Make sure the zval is a string */ -// convert_to_string(*d); - - RETURN_ZVAL(*d, true, true); -// php_printf("
return value of index %u = %s
", index, Z_STRVAL_PP(d)); - -// RETURN_NULL(); - -// cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); -// cql_future_result_object *future_obj = (cql_future_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); -// -//// while (obj->cql_result->next()) -//// { -// int count; -// count = obj->cql_result->column_count(); -// php_printf("%d
", count); -// -// for (size_t i = 0; i < obj->cql_result->column_count(); ++i) -// { -// cql::cql_byte_t data; -// int exists; -// std::string keyspace, table, column; -// -// exists = obj->cql_result->exists("test");//get(); -// obj->cql_result->column_name(i, keyspace, table, column); -//// php_printf("%u
", exists); -// php_printf("Keyspace: %s | Table: %s | Column: %s
", keyspace.c_str(), table.c_str(), column.c_str()); -//// php_printf("%s", obj->cql_result); -//// php_echo('xxxx'); -// //php_print(&data[0]) -// //php_print(data.size()); -// } -//// } -// -// RETURN_NULL(); -} - PHP_METHOD(CqlResult, getColumnCount) { cql_result_object *obj = (cql_result_object *) zend_object_store_get_object(getThis() TSRMLS_CC); From 11171ea3ee77298d0e79c5f824a81211d14a0648 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Wed, 26 Nov 2014 17:52:36 +0100 Subject: [PATCH 5/8] Update README.md --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0255d9d..2c0767a 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,57 @@ A PHP driver for Apache Cassandra. This driver works exclusively with the Cassan ## Building +#### CentOS 6 - Requirements + +Git version >= 1.7.10 + +Check your git version: +``` +git --version +``` + +if git version < 1.7.10 install newer version for example from rpmforge (http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-f0c3ecee3dbb407e4eed79a56ec0ae92d1398e01) +``` +wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm +sudo rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt +sudo rpm -K rpmforge-release-0.5.3-1.el6.rf.*.rpm +rpm -i rpmforge-release-0.5.3-1.el6.rf.*.rpm +sudo yum erase git +### Enable RPM extras repo => set enabled=1 +sudo nano /etc/yum.repos.d/rpmforge.repo +sudo yum install git +## Disable RPM extras repo => set enabled=0 +sudo nano /etc/yum.repos.d/rpmforge.repo + +## check new git version +git --version +``` + +Install required packages +``` +yum install cmake gcc-c++ openssl-devel libssh2-devel +``` + +Install boost >= 1.55 +``` +sudo wget http://repo.enetres.net/enetres.repo -O /etc/yum.repos.d/enetres.repo +sudo yum install boost-devel +``` + +Install PHP >= 5.3.10 +``` +wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm +wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm +wget http://rpms.famillecollet.com/RPM-GPG-KEY-remi +sudo rpm --import RPM-GPG-KEY-remi +sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm +## Enable Remi repo => set enabled=1 +sudo nano /etc/yum.repos.d/remi.repo +sudo yum install php-devel +## Disable Remi repo => set enabled=0 +sudo nano /etc/yum.repos.d/remi.repo +``` + The library use official c++ driver by DataStax https://github.com/datastax/cpp-driver Before build php-cassandra you should download and install DataStax C++ Driver for Apache Cassandra. @@ -15,7 +66,7 @@ cmake . && make && make install ``` ``` -git clone https://github.com/aparkhomenko/php-cassandra.git +git clone https://github.com/michaltrmac/php-cassandra.git --branch get_row --single-branch php-cassandra cd php-cassandra phpize && ./configure && make ``` @@ -74,6 +125,8 @@ if (null === $future->getError()) { while ($result->next()) { echo "strategy_options: " . $result->get("strategy_options") . "\n"; + // or you can use $result->get() to get complete row as associative array + // var_dump($result->get()); } } From 77580cd1a5a8ab32b006fe001cc5f95a6af34a0b Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Thu, 22 Jan 2015 13:06:15 +0100 Subject: [PATCH 6/8] close connection at request shutdown --- cassandra.cpp | 81 +++------- php_cassandra.h => php_cassandra.hpp | 217 +++++++++++++++++---------- 2 files changed, 158 insertions(+), 140 deletions(-) rename php_cassandra.h => php_cassandra.hpp (63%) diff --git a/cassandra.cpp b/cassandra.cpp index 56d1004..7057eab 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -30,9 +30,9 @@ extern "C" { #include "php.h" #include "php_ini.h" +#include "zend_exceptions.h" #include "ext/date/php_date.h" #include "ext/standard/info.h" -#include "php_cassandra.h" #define php_array_init(arg) _array_init((arg), 0 ZEND_FILE_LINE_CC) #ifdef array_init @@ -43,24 +43,7 @@ extern "C" { } #endif -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - - -/* If you declare any globals in php_cassandra.h uncomment this: -ZEND_DECLARE_MODULE_GLOBALS(cassandra) -*/ +#include "php_cassandra.hpp" /* True global resources - no need for thread safety here */ static int le_cassandra; @@ -83,40 +66,8 @@ zend_object_handlers cql_query_handlers; zend_object_handlers cql_session_handlers; zend_object_handlers cql_result_handlers; -struct cql_builder_object { - zend_object std; - boost::shared_ptr cql_builder; -}; - -struct cql_cluster_object { - zend_object std; - boost::shared_ptr cql_cluster; -}; - -struct cql_error_object { - zend_object std; - cql::cql_error_t cql_error; -}; - -struct cql_future_result_object { - zend_object std; - boost::shared_future cql_future_result; -}; - -struct cql_query_object { - zend_object std; - boost::shared_ptr cql_query; -}; - -struct cql_session_object { - zend_object std; - boost::shared_ptr cql_session; -}; - -struct cql_result_object { - zend_object std; - boost::shared_ptr cql_result; -}; +/* If you declare any globals in php_cassandra.h uncomment this: */ +ZEND_DECLARE_MODULE_GLOBALS(cassandra) zend_object_value php_cql_builder_object_new(zend_class_entry *type TSRMLS_DC); zend_object_value php_cql_cluster_object_new(zend_class_entry *type TSRMLS_DC); @@ -321,9 +272,6 @@ ZEND_BEGIN_ARG_INFO_EX(cql_result_get, 0, 0, 1) ZEND_ARG_INFO(0, typeColumn) ZEND_END_ARG_INFO() -ZEND_BEGIN_ARG_INFO_EX(cql_result_get_row, 0, 0, 0) -ZEND_END_ARG_INFO() - ZEND_BEGIN_ARG_INFO_EX(cql_result_get_column_count, 0, 0, 0) ZEND_END_ARG_INFO() @@ -601,7 +549,15 @@ PHP_METHOD(CqlCluster, __construct) } cql_cluster_object *obj = (cql_cluster_object *) zend_object_store_get_object(getThis() TSRMLS_CC); - obj->cql_cluster = boost::shared_ptr(obj_builder->cql_builder->build()); + try + { + obj->cql_cluster = boost::shared_ptr(obj_builder->cql_builder->build()); + } + catch (cql::cql_no_host_available_exception e) + { + zend_throw_exception(zend_exception_get_default(TSRMLS_C), "CqlCluster: No Host Available", 0 TSRMLS_CC); + RETURN_FALSE; + } } /* }}} */ @@ -610,6 +566,7 @@ PHP_METHOD(CqlCluster, __construct) PHP_METHOD(CqlCluster, connect) { cql_cluster_object *obj = (cql_cluster_object *) zend_object_store_get_object(getThis() TSRMLS_CC); + CASSANDRA_G(g_cluster_object) = obj; if (obj == NULL || obj->cql_cluster == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find obj->cql_cluster in CqlCluster.connect"); @@ -905,6 +862,7 @@ PHP_METHOD(CqlSession, __construct) try { cql_session_object *obj = (cql_session_object *) zend_object_store_get_object(getThis() TSRMLS_CC); + CASSANDRA_G(g_session_object) = obj; obj->cql_session = obj_cql_cluster->cql_cluster->connect(); } @@ -1698,6 +1656,15 @@ PHP_RINIT_FUNCTION(cassandra) */ PHP_RSHUTDOWN_FUNCTION(cassandra) { + try { + if (CASSANDRA_G(g_cluster_object)) + CASSANDRA_G(g_cluster_object)->cql_cluster->shutdown(-1); + + if (CASSANDRA_G(g_session_object)) + CASSANDRA_G(g_session_object)->cql_session->close(); + + } + catch (std::exception & e) {} return SUCCESS; } /* }}} */ diff --git a/php_cassandra.h b/php_cassandra.hpp similarity index 63% rename from php_cassandra.h rename to php_cassandra.hpp index 77fd0c6..8ebcc39 100644 --- a/php_cassandra.h +++ b/php_cassandra.hpp @@ -1,83 +1,134 @@ -/* - +----------------------------------------------------------------------+ - | PHP Version 5 | - +----------------------------------------------------------------------+ - | Copyright (c) 1997-2012 The PHP Group | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ - | Author: | - +----------------------------------------------------------------------+ -*/ - -/* $Id$ */ - -#ifndef PHP_CASSANDRA_H -#define PHP_CASSANDRA_H - -extern zend_module_entry cassandra_module_entry; -#define phpext_cassandra_ptr &cassandra_module_entry - -#ifdef PHP_WIN32 -# define PHP_CASSANDRA_API __declspec(dllexport) -#elif defined(__GNUC__) && __GNUC__ >= 4 -# define PHP_CASSANDRA_API __attribute__ ((visibility("default"))) -#else -# define PHP_CASSANDRA_API -#endif - -#ifdef ZTS -#include "TSRM.h" -#endif - -PHP_MINIT_FUNCTION(cassandra); -PHP_MSHUTDOWN_FUNCTION(cassandra); -PHP_RINIT_FUNCTION(cassandra); -PHP_RSHUTDOWN_FUNCTION(cassandra); -PHP_MINFO_FUNCTION(cassandra); - -PHP_FUNCTION(confirm_cassandra_compiled); /* For testing, remove later. */ - -/* - Declare any global variables you may need between the BEGIN - and END macros here: - -ZEND_BEGIN_MODULE_GLOBALS(cassandra) - long global_value; - char *global_string; -ZEND_END_MODULE_GLOBALS(cassandra) -*/ - -/* In every utility function you add that needs to use variables - in php_cassandra_globals, call TSRMLS_FETCH(); after declaring other - variables used by that function, or better yet, pass in TSRMLS_CC - after the last function argument and declare your utility function - with TSRMLS_DC after the last declared argument. Always refer to - the globals in your function as CASSANDRA_G(variable). You are - encouraged to rename these macros something shorter, see - examples in any other php module directory. -*/ - -#ifdef ZTS -#define CASSANDRA_G(v) TSRMG(cassandra_globals_id, zend_cassandra_globals *, v) -#else -#define CASSANDRA_G(v) (cassandra_globals.v) -#endif - -#endif /* PHP_CASSANDRA_H */ - - -/* - * Local variables: - * tab-width: 4 - * c-basic-offset: 4 - * End: - * vim600: noet sw=4 ts=4 fdm=marker - * vim<600: noet sw=4 ts=4 - */ +/* + +----------------------------------------------------------------------+ + | PHP Version 5 | + +----------------------------------------------------------------------+ + | Copyright (c) 1997-2012 The PHP Group | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.01 of the PHP license, | + | that is bundled with this package in the file LICENSE, and is | + | available through the world-wide-web at the following url: | + | http://www.php.net/license/3_01.txt | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Author: | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_CASSANDRA_H +#define PHP_CASSANDRA_H + +extern zend_module_entry cassandra_module_entry; +#define phpext_cassandra_ptr &cassandra_module_entry + +#ifdef PHP_WIN32 +# define PHP_CASSANDRA_API __declspec(dllexport) +#elif defined(__GNUC__) && __GNUC__ >= 4 +# define PHP_CASSANDRA_API __attribute__ ((visibility("default"))) +#else +# define PHP_CASSANDRA_API +#endif + +#ifdef ZTS +#include "TSRM.h" +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +PHP_MINIT_FUNCTION(cassandra); +PHP_MSHUTDOWN_FUNCTION(cassandra); +PHP_RINIT_FUNCTION(cassandra); +PHP_RSHUTDOWN_FUNCTION(cassandra); +PHP_MINFO_FUNCTION(cassandra); + +PHP_FUNCTION(confirm_cassandra_compiled); /* For testing, remove later. */ + +struct cql_builder_object { + zend_object std; + boost::shared_ptr cql_builder; +}; + +struct cql_cluster_object { + zend_object std; + boost::shared_ptr cql_cluster; +}; + +struct cql_error_object { + zend_object std; + cql::cql_error_t cql_error; +}; + +struct cql_future_result_object { + zend_object std; + boost::shared_future cql_future_result; +}; + +struct cql_query_object { + zend_object std; + boost::shared_ptr cql_query; +}; + +struct cql_session_object { + zend_object std; + boost::shared_ptr cql_session; +}; + +struct cql_result_object { + zend_object std; + boost::shared_ptr cql_result; +}; + +/* + Declare any global variables you may need between the BEGIN + and END macros here: + + */ +ZEND_BEGIN_MODULE_GLOBALS(cassandra) + cql_cluster_object *g_cluster_object; + cql_session_object *g_session_object; +ZEND_END_MODULE_GLOBALS(cassandra) + +/* In every utility function you add that needs to use variables + in php_cassandra_globals, call TSRMLS_FETCH(); after declaring other + variables used by that function, or better yet, pass in TSRMLS_CC + after the last function argument and declare your utility function + with TSRMLS_DC after the last declared argument. Always refer to + the globals in your function as CASSANDRA_G(variable). You are + encouraged to rename these macros something shorter, see + examples in any other php module directory. +*/ + +#ifdef ZTS +#define CASSANDRA_G(v) TSRMG(cassandra_globals_id, zend_cassandra_globals *, v) +#else +#define CASSANDRA_G(v) (cassandra_globals.v) +#endif + +#endif /* PHP_CASSANDRA_H */ + + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ From c37e255cc8ccede5ed17d38344545164a4667fc6 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Thu, 22 Jan 2015 16:33:05 +0100 Subject: [PATCH 7/8] return date as int --- cassandra.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index 7057eab..98776ad 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -1133,7 +1133,7 @@ PHP_METHOD(CqlResult, get) // Driver doesn't have method for getting timestamp [workaround] tmp_collection_map->get_key_bigint(i, tmp_value_big_int); - +/* if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1143,6 +1143,8 @@ PHP_METHOD(CqlResult, get) tmp_key = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); ZVAL_STRING(tmp_zval, tmp_key, 1); +*/ + ZVAL_LONG(tmp_zval, tmp_value_big_int); break; @@ -1193,7 +1195,7 @@ PHP_METHOD(CqlResult, get) // Driver doesn't have method for getting timestamp [workaround] tmp_collection_map->get_value_bigint(i, tmp_value_big_int); - +/* if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1203,6 +1205,8 @@ PHP_METHOD(CqlResult, get) tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); add_assoc_string(subarray, tmp_key, tmp_value_char, 1); +*/ + add_assoc_long(subarray, tmp_key, tmp_value_big_int); break; @@ -1336,7 +1340,7 @@ PHP_METHOD(CqlResult, get) add_assoc_null(return_value, column); continue; } - +/* if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1346,6 +1350,8 @@ PHP_METHOD(CqlResult, get) tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); add_assoc_string(return_value, column, tmp_value_char, 1); +*/ + add_assoc_long(return_value, column, tmp_value_big_int); break; From 4e6978f8ce4c92872a0218014477403c6d55d628 Mon Sep 17 00:00:00 2001 From: Michal Trmac Date: Thu, 19 Feb 2015 11:45:07 +0100 Subject: [PATCH 8/8] fix bigint --- cassandra.cpp | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/cassandra.cpp b/cassandra.cpp index 98776ad..7e77d96 100644 --- a/cassandra.cpp +++ b/cassandra.cpp @@ -1009,6 +1009,11 @@ PHP_METHOD(CqlResult, get) for (size_t i = 0; i < columns_count; ++i) { std::string _keyspace, _table, _column; + tmp_value_big_int = NULL; + tmp_value_bool = false; + tmp_value_double = NULL; + tmp_value_float = NULL; + tmp_value_int = NULL; if (ZEND_NUM_ARGS() <= 0) { obj->cql_result->column_name(i, _keyspace, _table, _column); @@ -1129,11 +1134,16 @@ PHP_METHOD(CqlResult, get) break; case cql::CQL_COLUMN_TYPE_TIMESTAMP: + // Driver doesn't have method for getting timestamp [workaround] + tmp_collection_map->get_key_bigint(i, tmp_value_big_int); + ZVAL_LONG(tmp_zval, tmp_value_big_int); + break; + case cql::CQL_COLUMN_TYPE_TIMEUUID: // Driver doesn't have method for getting timestamp [workaround] tmp_collection_map->get_key_bigint(i, tmp_value_big_int); -/* + if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1143,8 +1153,6 @@ PHP_METHOD(CqlResult, get) tmp_key = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); ZVAL_STRING(tmp_zval, tmp_key, 1); -*/ - ZVAL_LONG(tmp_zval, tmp_value_big_int); break; @@ -1191,11 +1199,16 @@ PHP_METHOD(CqlResult, get) break; case cql::CQL_COLUMN_TYPE_TIMESTAMP: + // Driver doesn't have method for getting timestamp [workaround] + tmp_collection_map->get_value_bigint(i, tmp_value_big_int); + add_assoc_long(subarray, tmp_key, tmp_value_big_int); + break; + case cql::CQL_COLUMN_TYPE_TIMEUUID: // Driver doesn't have method for getting timestamp [workaround] tmp_collection_map->get_value_bigint(i, tmp_value_big_int); -/* + if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1205,8 +1218,6 @@ PHP_METHOD(CqlResult, get) tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); add_assoc_string(subarray, tmp_key, tmp_value_char, 1); -*/ - add_assoc_long(subarray, tmp_key, tmp_value_big_int); break; @@ -1331,6 +1342,18 @@ PHP_METHOD(CqlResult, get) break; case cql::CQL_COLUMN_TYPE_TIMESTAMP: + + // Driver doesn't have method for getting timestamp [workaround] + obj->cql_result->get_bigint(column, tmp_value_big_int); + if (tmp_column_type != orig_column_type && tmp_value_big_int == 0) { + add_assoc_null(return_value, column); + continue; + } + + add_assoc_long(return_value, column, tmp_value_big_int); + + break; + case cql::CQL_COLUMN_TYPE_TIMEUUID: // Driver doesn't have method for getting timestamp [workaround] @@ -1340,7 +1363,7 @@ PHP_METHOD(CqlResult, get) add_assoc_null(return_value, column); continue; } -/* + if (tmp_value_big_int > 1000) { ts = (time_t) (tmp_value_big_int / 1000); } @@ -1350,8 +1373,6 @@ PHP_METHOD(CqlResult, get) tmp_value_char = php_format_date("Y-m-d H:i:s", 11, ts, 1 TSRMLS_CC); add_assoc_string(return_value, column, tmp_value_char, 1); -*/ - add_assoc_long(return_value, column, tmp_value_big_int); break;