Skip to content

Commit 0b2db7f

Browse files
committed
Selecting by space_no/index_name shouldn't throw error now
closes tarantoolgh-42
1 parent d62eb1a commit 0b2db7f

File tree

5 files changed

+93
-19
lines changed

5 files changed

+93
-19
lines changed

src/tarantool.c

+44-19
Original file line numberDiff line numberDiff line change
@@ -630,21 +630,30 @@ int convert_iterator(zval *iter, int all) {
630630
}
631631

632632
int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
633-
if (Z_TYPE_P(name) == IS_LONG)
634-
return Z_LVAL_P(name);
635-
if (Z_TYPE_P(name) != IS_STRING) {
633+
if (Z_TYPE_P(name) != IS_STRING && Z_TYPE_P(name) != IS_LONG) {
636634
tarantool_throw_exception("Space ID must be String or Long");
637635
return FAILURE;
638636
}
639-
int32_t space_no = tarantool_schema_get_sid_by_string(obj->schema,
640-
Z_STRVAL_P(name), Z_STRLEN_P(name));
641-
if (space_no != FAILURE)
642-
return space_no;
643-
637+
int32_t space_no = -1;
644638
tarantool_tp_update(obj->tps);
645-
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096);
646-
tp_key(obj->tps, 1);
647-
tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name));
639+
if (Z_TYPE_P(name) == IS_LONG) {
640+
space_no = tarantool_schema_get_sid_by_number(obj->schema,
641+
Z_LVAL_P(name));
642+
if (space_no != FAILURE)
643+
return space_no;
644+
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NO, 0, 4096);
645+
tp_key(obj->tps, 1);
646+
tp_encode_uint(obj->tps, Z_LVAL_P(name));
647+
} else {
648+
space_no = tarantool_schema_get_sid_by_string(obj->schema,
649+
Z_STRVAL_P(name),
650+
Z_STRLEN_P(name));
651+
if (space_no != FAILURE)
652+
return space_no;
653+
tp_select(obj->tps, SPACE_SPACE, INDEX_SPACE_NAME, 0, 4096);
654+
tp_key(obj->tps, 1);
655+
tp_encode_str(obj->tps, Z_STRVAL_P(name), Z_STRLEN_P(name));
656+
}
648657
tp_reqid(obj->tps, TARANTOOL_G(sync_counter)++);
649658

650659
obj->value->len = tp_used(obj->tps);
@@ -659,10 +668,11 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
659668
size_t body_size = php_mp_unpack_package_size(pack_len);
660669
smart_string_ensure(obj->value, body_size);
661670
if (tarantool_stream_read(obj, obj->value->c,
662-
body_size) == FAILURE)
671+
body_size) == FAILURE)
663672
return FAILURE;
664673

665-
struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
674+
struct tnt_response resp;
675+
memset(&resp, 0, sizeof(struct tnt_response));
666676
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
667677
tarantool_throw_parsingexception("query");
668678
return FAILURE;
@@ -678,10 +688,22 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
678688
tarantool_throw_parsingexception("schema (space)");
679689
return FAILURE;
680690
}
681-
space_no = tarantool_schema_get_sid_by_string(obj->schema,
682-
Z_STRVAL_P(name), Z_STRLEN_P(name));
683-
if (space_no == FAILURE)
684-
THROW_EXC("No space '%s' defined", Z_STRVAL_P(name));
691+
692+
if (Z_TYPE_P(name) == IS_LONG) {
693+
space_no = tarantool_schema_get_sid_by_number(obj->schema,
694+
Z_LVAL_P(name));
695+
if (space_no == FAILURE) {
696+
THROW_EXC("No space '%s' defined", Z_STRVAL_P(name));
697+
}
698+
} else {
699+
space_no = tarantool_schema_get_sid_by_string(obj->schema,
700+
Z_STRVAL_P(name),
701+
Z_STRLEN_P(name));
702+
if (space_no == FAILURE) {
703+
THROW_EXC("No space '%ul' defined", Z_LVAL_P(name));
704+
}
705+
}
706+
685707
return space_no;
686708
}
687709

@@ -695,6 +717,7 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
695717
THROW_EXC("Index ID must be String or Long");
696718
return FAILURE;
697719
}
720+
698721
int32_t index_no = tarantool_schema_get_iid_by_string(obj->schema,
699722
space_no, Z_STRVAL_P(name), Z_STRLEN_P(name));
700723
if (index_no != FAILURE)
@@ -721,7 +744,8 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
721744
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
722745
return FAILURE;
723746

724-
struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
747+
struct tnt_response resp;
748+
memset(&resp, 0, sizeof(struct tnt_response));
725749
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
726750
tarantool_throw_parsingexception("query");
727751
return FAILURE;
@@ -776,7 +800,8 @@ int get_fieldno_by_name(tarantool_connection *obj, uint32_t space_no,
776800
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
777801
return FAILURE;
778802

779-
struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
803+
struct tnt_response resp;
804+
memset(&resp, 0, sizeof(struct tnt_response));
780805
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
781806
tarantool_throw_parsingexception("query");
782807
return FAILURE;

src/tarantool_proto.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define SPACE_SPACE 281
1313
#define SPACE_INDEX 289
1414

15+
#define INDEX_SPACE_NO 0
16+
#define INDEX_INDEX_NO 0
1517
#define INDEX_SPACE_NAME 2
1618
#define INDEX_INDEX_NAME 2
1719

src/tarantool_schema.c

+34
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ schema_add_space(
398398
return -1;
399399
}
400400

401+
int
402+
tarantool_schema_has_space_no(
403+
struct tarantool_schema *schema_obj,
404+
uint32_t space_no) {
405+
/* Prepare key for space search */
406+
struct schema_key space_key;
407+
space_key.number = space_no;
408+
space_key.id = (void *)&(space_key.number);
409+
space_key.id_len = sizeof(uint32_t);
410+
411+
struct mh_schema_space_t *schema = schema_obj->space_hash;
412+
mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL);
413+
return (space_slot != mh_end(schema));
414+
}
415+
401416
int
402417
tarantool_schema_add_spaces(
403418
struct tarantool_schema *schema_obj,
@@ -557,6 +572,25 @@ tarantool_schema_get_sid_by_string(
557572
return space->space_number;
558573
}
559574

575+
/* ALIAS for verify existens */
576+
int32_t
577+
tarantool_schema_get_sid_by_number(
578+
struct tarantool_schema *schema_obj,
579+
uint32_t sid
580+
) {
581+
struct mh_schema_space_t *schema = schema_obj->space_hash;
582+
struct schema_key space_key = {
583+
(void *)&sid,
584+
sizeof(uint32_t), 0
585+
};
586+
mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL);
587+
if (space_slot == mh_end(schema))
588+
return -1;
589+
const struct schema_space_value *space = *mh_schema_space_node(schema,
590+
space_slot);
591+
return space->space_number;
592+
}
593+
560594
int32_t
561595
tarantool_schema_get_iid_by_string(
562596
struct tarantool_schema *schema_obj, uint32_t sid,

src/tarantool_schema.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ struct tarantool_schema {
4747
struct mh_schema_space_t *space_hash;
4848
};
4949

50+
int
51+
tarantool_schema_has_space_no(struct tarantool_schema *, uint32_t);
52+
5053
int
5154
tarantool_schema_add_spaces(struct tarantool_schema *, const char *, uint32_t);
5255
int
@@ -56,6 +59,8 @@ int32_t
5659
tarantool_schema_get_sid_by_string(struct tarantool_schema *, const char *,
5760
uint32_t);
5861
int32_t
62+
tarantool_schema_get_sid_by_number(struct tarantool_schema *, uint32_t);
63+
int32_t
5964
tarantool_schema_get_iid_by_string(struct tarantool_schema *, uint32_t,
6065
const char *, uint32_t);
6166
int32_t

test/DMLTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,12 @@ public function test_18_02_delete_loop() {
474474
gc_collect_cycles();
475475
gc_collect_cycles();
476476
}
477+
478+
/**
479+
* @doesNotPerformAssertions
480+
*/
481+
public function test_19_schema_obtain_failed() {
482+
$empty_tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test');
483+
$empty_tarantool->select(289, [], 'name');
484+
}
477485
}

0 commit comments

Comments
 (0)