diff --git a/src/tarantool.c b/src/tarantool.c index c3ab7a4..44881fa 100644 --- a/src/tarantool.c +++ b/src/tarantool.c @@ -631,21 +631,30 @@ int convert_iterator(zval *iter, int all) { } int get_spaceno_by_name(tarantool_connection *obj, zval *name) { - if (Z_TYPE_P(name) == IS_LONG) - return Z_LVAL_P(name); - if (Z_TYPE_P(name) != IS_STRING) { + if (Z_TYPE_P(name) != IS_STRING && Z_TYPE_P(name) != IS_LONG) { tarantool_throw_exception("Space ID must be String or Long"); return FAILURE; } - int32_t space_no = tarantool_schema_get_sid_by_string(obj->schema, - Z_STRVAL_P(name), Z_STRLEN_P(name)); - if (space_no != FAILURE) - return space_no; - + int32_t space_no = -1; tarantool_tp_update(obj->tps); - tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096); - tp_key(obj->tps, 1); - tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name)); + if (Z_TYPE_P(name) == IS_LONG) { + space_no = tarantool_schema_get_sid_by_number(obj->schema, + Z_LVAL_P(name)); + if (space_no != FAILURE) + return space_no; + tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NO, 0, 4096); + tp_key(obj->tps, 1); + tp_encode_uint(obj->tps, Z_LVAL_P(name)); + } else { + space_no = tarantool_schema_get_sid_by_string(obj->schema, + Z_STRVAL_P(name), + Z_STRLEN_P(name)); + if (space_no != FAILURE) + return space_no; + tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096); + tp_key(obj->tps, 1); + tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name)); + } tp_reqid(obj->tps, TARANTOOL_G(sync_counter)++); obj->value->len = tp_used(obj->tps); @@ -660,10 +669,11 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) { size_t body_size = php_mp_unpack_package_size(pack_len); smart_string_ensure(obj->value, body_size); if (tarantool_stream_read(obj, obj->value->c, - body_size) == FAILURE) + body_size) == FAILURE) return FAILURE; - struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response)); + struct tnt_response resp; + memset(&resp, 0, sizeof(struct tnt_response)); if (php_tp_response(&resp, obj->value->c, body_size) == -1) { tarantool_throw_parsingexception("query"); return FAILURE; @@ -679,10 +689,22 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) { tarantool_throw_parsingexception("schema (space)"); return FAILURE; } - space_no = tarantool_schema_get_sid_by_string(obj->schema, - Z_STRVAL_P(name), Z_STRLEN_P(name)); - if (space_no == FAILURE) - THROW_EXC("No space '%s' defined", Z_STRVAL_P(name)); + + if (Z_TYPE_P(name) == IS_LONG) { + space_no = tarantool_schema_get_sid_by_number(obj->schema, + Z_LVAL_P(name)); + if (space_no == FAILURE) { + THROW_EXC("No space '%s' defined", Z_STRVAL_P(name)); + } + } else { + space_no = tarantool_schema_get_sid_by_string(obj->schema, + Z_STRVAL_P(name), + Z_STRLEN_P(name)); + if (space_no == FAILURE) { + THROW_EXC("No space '%ul' defined", Z_LVAL_P(name)); + } + } + return space_no; } @@ -696,6 +718,7 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no, THROW_EXC("Index ID must be String or Long"); return FAILURE; } + int32_t index_no = tarantool_schema_get_iid_by_string(obj->schema, space_no, Z_STRVAL_P(name), Z_STRLEN_P(name)); if (index_no != FAILURE) @@ -722,7 +745,8 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no, if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE) return FAILURE; - struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response)); + struct tnt_response resp; + memset(&resp, 0, sizeof(struct tnt_response)); if (php_tp_response(&resp, obj->value->c, body_size) == -1) { tarantool_throw_parsingexception("query"); return FAILURE; @@ -777,7 +801,8 @@ int get_fieldno_by_name(tarantool_connection *obj, uint32_t space_no, if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE) return FAILURE; - struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response)); + struct tnt_response resp; + memset(&resp, 0, sizeof(struct tnt_response)); if (php_tp_response(&resp, obj->value->c, body_size) == -1) { tarantool_throw_parsingexception("query"); return FAILURE; diff --git a/src/tarantool_proto.h b/src/tarantool_proto.h index 658d0fb..49f0432 100644 --- a/src/tarantool_proto.h +++ b/src/tarantool_proto.h @@ -12,6 +12,8 @@ #define SPACE_SPACE 281 #define SPACE_INDEX 289 +#define INDEX_SPACE_NO 0 +#define INDEX_INDEX_NO 0 #define INDEX_SPACE_NAME 2 #define INDEX_INDEX_NAME 2 diff --git a/src/tarantool_schema.c b/src/tarantool_schema.c index 01d7e6f..d6e6e16 100644 --- a/src/tarantool_schema.c +++ b/src/tarantool_schema.c @@ -398,6 +398,21 @@ schema_add_space( return -1; } +int +tarantool_schema_has_space_no( + struct tarantool_schema *schema_obj, + uint32_t space_no) { + /* Prepare key for space search */ + struct schema_key space_key; + space_key.number = space_no; + space_key.id = (void *)&(space_key.number); + space_key.id_len = sizeof(uint32_t); + + struct mh_schema_space_t *schema = schema_obj->space_hash; + mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL); + return (space_slot != mh_end(schema)); +} + int tarantool_schema_add_spaces( struct tarantool_schema *schema_obj, @@ -557,6 +572,25 @@ tarantool_schema_get_sid_by_string( return space->space_number; } +/* ALIAS for verify existens */ +int32_t +tarantool_schema_get_sid_by_number( + struct tarantool_schema *schema_obj, + uint32_t sid +) { + struct mh_schema_space_t *schema = schema_obj->space_hash; + struct schema_key space_key = { + (void *)&sid, + sizeof(uint32_t), 0 + }; + mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL); + if (space_slot == mh_end(schema)) + return -1; + const struct schema_space_value *space = *mh_schema_space_node(schema, + space_slot); + return space->space_number; +} + int32_t tarantool_schema_get_iid_by_string( struct tarantool_schema *schema_obj, uint32_t sid, diff --git a/src/tarantool_schema.h b/src/tarantool_schema.h index 537738e..5316684 100644 --- a/src/tarantool_schema.h +++ b/src/tarantool_schema.h @@ -47,6 +47,9 @@ struct tarantool_schema { struct mh_schema_space_t *space_hash; }; +int +tarantool_schema_has_space_no(struct tarantool_schema *, uint32_t); + int tarantool_schema_add_spaces(struct tarantool_schema *, const char *, uint32_t); int @@ -56,6 +59,8 @@ int32_t tarantool_schema_get_sid_by_string(struct tarantool_schema *, const char *, uint32_t); int32_t +tarantool_schema_get_sid_by_number(struct tarantool_schema *, uint32_t); +int32_t tarantool_schema_get_iid_by_string(struct tarantool_schema *, uint32_t, const char *, uint32_t); int32_t diff --git a/test/DMLTest.php b/test/DMLTest.php index 5dfb5e9..a51d47d 100644 --- a/test/DMLTest.php +++ b/test/DMLTest.php @@ -468,4 +468,12 @@ public function test_18_02_delete_loop() { gc_collect_cycles(); gc_collect_cycles(); } + + /** + * @doesNotPerformAssertions + */ + public function test_19_schema_obtain_failed() { + $empty_tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test'); + $empty_tarantool->select(289, [], 'name'); + } }