Skip to content

Commit f3ba38f

Browse files
committed
Selecting by space_no/index_name shouldn't throw error now
1 parent 21f638b commit f3ba38f

File tree

5 files changed

+75
-19
lines changed

5 files changed

+75
-19
lines changed

src/tarantool.c

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -631,21 +631,30 @@ int convert_iterator(zval *iter, int all) {
631631
}
632632

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

651660
obj->value->len = tp_used(obj->tps);
@@ -660,10 +669,11 @@ int get_spaceno_by_name(tarantool_connection *obj, zval *name) {
660669
size_t body_size = php_mp_unpack_package_size(pack_len);
661670
smart_string_ensure(obj->value, body_size);
662671
if (tarantool_stream_read(obj, obj->value->c,
663-
body_size) == FAILURE)
672+
body_size) == FAILURE)
664673
return FAILURE;
665674

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

@@ -696,6 +718,7 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
696718
THROW_EXC("Index ID must be String or Long");
697719
return FAILURE;
698720
}
721+
699722
int32_t index_no = tarantool_schema_get_iid_by_string(obj->schema,
700723
space_no, Z_STRVAL_P(name), Z_STRLEN_P(name));
701724
if (index_no != FAILURE)
@@ -722,7 +745,8 @@ int get_indexno_by_name(tarantool_connection *obj, int space_no,
722745
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
723746
return FAILURE;
724747

725-
struct tnt_response resp; memset(&resp, 0, sizeof(struct tnt_response));
748+
struct tnt_response resp;
749+
memset(&resp, 0, sizeof(struct tnt_response));
726750
if (php_tp_response(&resp, obj->value->c, body_size) == -1) {
727751
tarantool_throw_parsingexception("query");
728752
return FAILURE;
@@ -777,7 +801,8 @@ int get_fieldno_by_name(tarantool_connection *obj, uint32_t space_no,
777801
if (tarantool_stream_read(obj, obj->value->c, body_size) == FAILURE)
778802
return FAILURE;
779803

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

src/tarantool_proto.h

Lines changed: 2 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,24 @@ tarantool_schema_get_sid_by_string(
557557
return space->space_number;
558558
}
559559

560+
int32_t
561+
tarantool_schema_get_sid_by_number(
562+
struct tarantool_schema *schema_obj,
563+
uint32_t sid
564+
) {
565+
struct mh_schema_space_t *schema = schema_obj->space_hash;
566+
struct schema_key space_key = {
567+
(void *)&sid,
568+
sizeof(uint32_t), 0
569+
};
570+
mh_int_t space_slot = mh_schema_space_find(schema, &space_key, NULL);
571+
if (space_slot == mh_end(schema))
572+
return -1;
573+
const struct schema_space_value *space = *mh_schema_space_node(schema,
574+
space_slot);
575+
return space->space_number;
576+
}
577+
560578
int32_t
561579
tarantool_schema_get_iid_by_string(
562580
struct tarantool_schema *schema_obj, uint32_t sid,

src/tarantool_schema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ int32_t
5656
tarantool_schema_get_sid_by_string(struct tarantool_schema *, const char *,
5757
uint32_t);
5858
int32_t
59+
tarantool_schema_get_sid_by_number(struct tarantool_schema *, uint32_t);
60+
int32_t
5961
tarantool_schema_get_iid_by_string(struct tarantool_schema *, uint32_t,
6062
const char *, uint32_t);
6163
int32_t

test/DMLTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,13 @@ public function test_18_02_delete_loop() {
468468
gc_collect_cycles();
469469
gc_collect_cycles();
470470
}
471+
472+
/**
473+
* @doesNotPerformAssertions
474+
*/
475+
public function test_19_schema_obtain_failed() {
476+
$port = TestHelpers::getTarantoolPort();
477+
$empty_tarantool = new Tarantool('localhost', $port, 'test');
478+
$empty_tarantool->select(289, [], 'name');
479+
}
471480
}

0 commit comments

Comments
 (0)