Skip to content

Commit 95bd78d

Browse files
committed
PCBC-722: fix boolean search query encoding
Change-Id: I4667fcc6235ff737ab474d554abc6a64794a07ef Reviewed-on: http://review.couchbase.org/c/php-couchbase/+/138407 Tested-by: Build Bot <[email protected]> Reviewed-by: Sergey Avseyev <[email protected]>
1 parent 848be35 commit 95bd78d

File tree

3 files changed

+39
-95
lines changed

3 files changed

+39
-95
lines changed

api/couchbase.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,26 +2335,26 @@ public function boost($boost): BooleanSearchQuery
23352335
}
23362336

23372337
/**
2338-
* @param SearchQuery ...$queries
2338+
* @param ConjunctionSearchQuery $query
23392339
* @return BooleanSearchQuery
23402340
*/
2341-
public function must(SearchQuery ...$queries): BooleanSearchQuery
2341+
public function must(ConjunctionSearchQuery $query): BooleanSearchQuery
23422342
{
23432343
}
23442344

23452345
/**
2346-
* @param SearchQuery ...$queries
2346+
* @param DisjunctionSearchQuery $query
23472347
* @return BooleanSearchQuery
23482348
*/
2349-
public function mustNot(SearchQuery ...$queries): BooleanSearchQuery
2349+
public function mustNot(DisjunctionSearchQuery $query): BooleanSearchQuery
23502350
{
23512351
}
23522352

23532353
/**
2354-
* @param SearchQuery ...$queries
2354+
* @param DisjunctionSearchQuery $query
23552355
* @return BooleanSearchQuery
23562356
*/
2357-
public function should(SearchQuery ...$queries): BooleanSearchQuery
2357+
public function should(DisjunctionSearchQuery $query): BooleanSearchQuery
23582358
{
23592359
}
23602360
}

src/couchbase/search/boolean_query.c

Lines changed: 27 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#define LOGARGS(lvl) LCB_LOG_##lvl, NULL, "pcbc/boolean_query", __FILE__, __LINE__
2323

2424
zend_class_entry *pcbc_boolean_search_query_ce;
25+
extern zend_class_entry *pcbc_conjunction_search_query_ce;
26+
extern zend_class_entry *pcbc_disjunction_search_query_ce;
2527

2628
PHP_METHOD(BooleanSearchQuery, boost)
2729
{
@@ -40,111 +42,42 @@ PHP_METHOD(BooleanSearchQuery, boost)
4042

4143
PHP_METHOD(BooleanSearchQuery, must)
4244
{
43-
zval *args = NULL;
44-
int num_args = 0;
45-
int rv;
45+
zval *conjunct = NULL;
4646

47-
rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &num_args);
48-
if (rv == FAILURE) {
47+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS() TSRMLS_CC, "O", &conjunct, pcbc_conjunction_search_query_ce) ==
48+
FAILURE) {
4949
return;
5050
}
5151

52-
if (num_args && args) {
53-
zval *container, rv1;
54-
container = zend_read_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("must"), 0, &rv1);
55-
if (Z_TYPE_P(container) == IS_NULL) {
56-
array_init(&rv1);
57-
container = &rv1;
58-
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("must"), container TSRMLS_CC);
59-
Z_DELREF_P(container);
60-
}
61-
int i;
62-
for (i = 0; i < num_args; ++i) {
63-
zval *query;
64-
query = &args[i];
65-
if (Z_TYPE_P(query) != IS_OBJECT ||
66-
!instanceof_function(Z_OBJCE_P(query), pcbc_search_query_ce TSRMLS_CC)) {
67-
pcbc_log(LOGARGS(WARN), "query has to implement SearchQuery interface (skipping argument #%d)", i);
68-
continue;
69-
}
70-
add_next_index_zval(container, query);
71-
PCBC_ADDREF_P(query);
72-
}
73-
}
52+
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("must"), conjunct TSRMLS_CC);
7453

7554
RETURN_ZVAL(getThis(), 1, 0);
7655
}
7756

7857
PHP_METHOD(BooleanSearchQuery, mustNot)
7958
{
80-
zval *args = NULL;
81-
int num_args = 0;
82-
int rv;
59+
zval *disjunct = NULL;
8360

84-
rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &num_args);
85-
if (rv == FAILURE) {
61+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS() TSRMLS_CC, "O", &disjunct, pcbc_disjunction_search_query_ce) ==
62+
FAILURE) {
8663
return;
8764
}
8865

89-
if (num_args && args) {
90-
zval *container, rv1;
91-
container = zend_read_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("must_not"), 0, &rv1);
92-
if (Z_TYPE_P(container) == IS_NULL) {
93-
array_init(&rv1);
94-
container = &rv1;
95-
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("must_not"), container TSRMLS_CC);
96-
Z_DELREF_P(container);
97-
}
98-
int i;
99-
for (i = 0; i < num_args; ++i) {
100-
zval *query;
101-
query = &args[i];
102-
if (Z_TYPE_P(query) != IS_OBJECT ||
103-
!instanceof_function(Z_OBJCE_P(query), pcbc_search_query_ce TSRMLS_CC)) {
104-
pcbc_log(LOGARGS(WARN), "query has to implement SearchQuery interface (skipping argument #%d)", i);
105-
continue;
106-
}
107-
add_next_index_zval(container, query);
108-
PCBC_ADDREF_P(query);
109-
}
110-
}
66+
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("mustNot"), disjunct TSRMLS_CC);
11167

11268
RETURN_ZVAL(getThis(), 1, 0);
11369
}
11470

11571
PHP_METHOD(BooleanSearchQuery, should)
11672
{
117-
zval *args = NULL;
118-
int num_args = 0;
119-
int rv;
73+
zval *disjunct = NULL;
12074

121-
rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &num_args);
122-
if (rv == FAILURE) {
75+
if (zend_parse_parameters_throw(ZEND_NUM_ARGS() TSRMLS_CC, "O", &disjunct, pcbc_disjunction_search_query_ce) ==
76+
FAILURE) {
12377
return;
12478
}
12579

126-
if (num_args && args) {
127-
zval *container, rv1;
128-
container = zend_read_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("should"), 0, &rv1);
129-
if (Z_TYPE_P(container) == IS_NULL) {
130-
array_init(&rv1);
131-
container = &rv1;
132-
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("should"), container TSRMLS_CC);
133-
Z_DELREF_P(container);
134-
}
135-
int i;
136-
for (i = 0; i < num_args; ++i) {
137-
zval *query;
138-
query = &args[i];
139-
if (Z_TYPE_P(query) != IS_OBJECT ||
140-
!instanceof_function(Z_OBJCE_P(query), pcbc_search_query_ce TSRMLS_CC)) {
141-
pcbc_log(LOGARGS(WARN), "query has to implement SearchQuery interface (skipping argument #%d)", i);
142-
continue;
143-
}
144-
add_next_index_zval(container, query);
145-
PCBC_ADDREF_P(query);
146-
}
147-
}
80+
zend_update_property(pcbc_boolean_search_query_ce, getThis(), ZEND_STRL("should"), disjunct TSRMLS_CC);
14881

14982
RETURN_ZVAL(getThis(), 1, 0);
15083
}
@@ -193,17 +126,25 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_BooleanSearchQuery_boost, 0, 1, Couchb
193126
ZEND_ARG_TYPE_INFO(0, boost, IS_DOUBLE, 0)
194127
ZEND_END_ARG_INFO()
195128

196-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_BooleanSearchQuery_queries, 0, 1, Couchbase\\BooleanSearchQuery, 0)
197-
PCBC_ARG_VARIADIC_INFO(0, queries)
129+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_BooleanSearchQuery_must, 0, 1, Couchbase\\BooleanSearchQuery, 0)
130+
ZEND_ARG_OBJ_INFO(0, query, Couchbase\\ConjunctionSearchQuery, 0)
131+
ZEND_END_ARG_INFO()
132+
133+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_BooleanSearchQuery_mustNot, 0, 1, Couchbase\\BooleanSearchQuery, 0)
134+
ZEND_ARG_OBJ_INFO(0, query, Couchbase\\DisjunctionSearchQuery, 0)
135+
ZEND_END_ARG_INFO()
136+
137+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(ai_BooleanSearchQuery_should, 0, 1, Couchbase\\BooleanSearchQuery, 0)
138+
ZEND_ARG_OBJ_INFO(0, query, Couchbase\\DisjunctionSearchQuery, 0)
198139
ZEND_END_ARG_INFO()
199140

200141
// clang-format off
201142
zend_function_entry boolean_search_query_methods[] = {
202143
PHP_ME(BooleanSearchQuery, jsonSerialize, ai_BooleanSearchQuery_none, ZEND_ACC_PUBLIC)
203144
PHP_ME(BooleanSearchQuery, boost, ai_BooleanSearchQuery_boost, ZEND_ACC_PUBLIC)
204-
PHP_ME(BooleanSearchQuery, must, ai_BooleanSearchQuery_queries, ZEND_ACC_PUBLIC)
205-
PHP_ME(BooleanSearchQuery, mustNot, ai_BooleanSearchQuery_queries, ZEND_ACC_PUBLIC)
206-
PHP_ME(BooleanSearchQuery, should, ai_BooleanSearchQuery_queries, ZEND_ACC_PUBLIC)
145+
PHP_ME(BooleanSearchQuery, must, ai_BooleanSearchQuery_must, ZEND_ACC_PUBLIC)
146+
PHP_ME(BooleanSearchQuery, mustNot, ai_BooleanSearchQuery_mustNot, ZEND_ACC_PUBLIC)
147+
PHP_ME(BooleanSearchQuery, should, ai_BooleanSearchQuery_should, ZEND_ACC_PUBLIC)
207148
PHP_FE_END
208149
};
209150
// clang-format on

tests/SearchQueryTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ function testSearchQuery() {
2727
->either(new \Couchbase\NumericRangeSearchQuery(3, 42.5))
2828
->either((new \Couchbase\WildcardSearchQuery('user*'))->field('type'))
2929
)
30-
->mustNot((new \Couchbase\PhraseSearchQuery('foo', 'bar', 'baz'))->field('description'))
31-
->mustNot((new \Couchbase\RegexpSearchQuery('user.*'))->field('_class_name'))
32-
;
30+
->mustNot(
31+
new \Couchbase\DisjunctionSearchQuery([
32+
(new \Couchbase\PhraseSearchQuery('foo', 'bar', 'baz'))->field('description'),
33+
(new \Couchbase\RegexpSearchQuery('user.*'))->field('_class_name')
34+
])
35+
);
3336
$result = json_encode($query);
3437
$this->assertNotNull($result);
3538
$this->assertEquals(JSON_ERROR_NONE, json_last_error());

0 commit comments

Comments
 (0)