Skip to content

Commit 4cb7778

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

File tree

5 files changed

+93
-19
lines changed

5 files changed

+93
-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: 34 additions & 0 deletions
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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,12 @@ 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+
$empty_tarantool = new Tarantool('localhost', getenv('PRIMARY_PORT'), 'test');
477+
$empty_tarantool->select(289, [], 'name');
478+
}
471479
}

0 commit comments

Comments
 (0)