Skip to content

Commit 75da0d7

Browse files
OmarEmaraDevdevnexen
authored andcommitted
PGSQL: Allow unconditional selection in pg_select
Previously, pg_select did not allow unconditional selection, where an empty ids array would result in a function failure. This patch implements two changes: - Make the ids array an optional parameter. - Allow the ids array to be empty. In both cases, unconditional selection happen, which is equivalent to pg_query('SELECT * FROM <table>;'). Two test cases were added to test the aforementioned changes. Close GH-5332
1 parent 009d48d commit 75da0d7

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ DOM:
1313
Intl:
1414
. Added IntlDateFormatter::PATTERN constant. (David Carlier)
1515

16+
PGSQL:
17+
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
18+
1619
SimpleXML:
1720
. Fixed bug GH-12192 (SimpleXML infinite loop when getName() is called
1821
within foreach). (nielsdos)

ext/pgsql/pgsql.c

+24-16
Original file line numberDiff line numberDiff line change
@@ -5777,29 +5777,34 @@ PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *t
57775777

57785778
ZEND_ASSERT(pg_link != NULL);
57795779
ZEND_ASSERT(table != NULL);
5780-
ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
5780+
if (ids_array) {
5781+
ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
5782+
}
57815783
ZEND_ASSERT(Z_TYPE_P(ret_array) == IS_ARRAY);
57825784
ZEND_ASSERT(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
57835785

5784-
if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
5785-
return FAILURE;
5786-
}
5786+
zend_bool is_valid_ids_array = ids_array && zend_hash_num_elements(Z_ARRVAL_P(ids_array)) != 0;
57875787

5788-
ZVAL_UNDEF(&ids_converted);
5789-
if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
5790-
array_init(&ids_converted);
5791-
if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
5792-
goto cleanup;
5788+
if (is_valid_ids_array) {
5789+
ZVAL_UNDEF(&ids_converted);
5790+
if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
5791+
array_init(&ids_converted);
5792+
if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
5793+
goto cleanup;
5794+
}
5795+
ids_array = &ids_converted;
57935796
}
5794-
ids_array = &ids_converted;
57955797
}
57965798

57975799
smart_str_appends(&querystr, "SELECT * FROM ");
57985800
build_tablename(&querystr, pg_link, table);
5799-
smart_str_appends(&querystr, " WHERE ");
58005801

5801-
if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt))
5802-
goto cleanup;
5802+
if (is_valid_ids_array) {
5803+
smart_str_appends(&querystr, " WHERE ");
5804+
if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt)) {
5805+
goto cleanup;
5806+
}
5807+
}
58035808

58045809
smart_str_appendc(&querystr, ';');
58055810
smart_str_0(&querystr);
@@ -5814,7 +5819,9 @@ PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *t
58145819
PQclear(pg_result);
58155820

58165821
cleanup:
5817-
zval_ptr_dtor(&ids_converted);
5822+
if (is_valid_ids_array) {
5823+
zval_ptr_dtor(&ids_converted);
5824+
}
58185825
if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
58195826
*sql = querystr.s;
58205827
}
@@ -5828,7 +5835,8 @@ PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *t
58285835
/* {{{ Select records that has ids (id=>value) */
58295836
PHP_FUNCTION(pg_select)
58305837
{
5831-
zval *pgsql_link, *ids;
5838+
zval *pgsql_link;
5839+
zval *ids = NULL;
58325840
pgsql_link_handle *link;
58335841
zend_string *table;
58345842
zend_ulong option = PGSQL_DML_EXEC;
@@ -5837,7 +5845,7 @@ PHP_FUNCTION(pg_select)
58375845
zend_string *sql = NULL;
58385846

58395847
/* TODO Document result_type param on php.net (apparently it was added in PHP 7.1) */
5840-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|ll",
5848+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|all",
58415849
&pgsql_link, pgsql_link_ce, &table, &ids, &option, &result_type
58425850
) == FAILURE) {
58435851
RETURN_THROWS();

ext/pgsql/pgsql.stub.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ function pg_delete(PgSql\Connection $connection, string $table_name, array $cond
961961
* @return array<int, array>|string|false
962962
* @refcount 1
963963
*/
964-
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions, int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}
964+
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions = [], int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}
965965

966966
#ifdef LIBPQ_HAS_PIPELINING
967967
function pg_enter_pipeline_mode(PgSql\Connection $connection): bool {}

ext/pgsql/pgsql_arginfo.h

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/pgsql/tests/pg_select_001.phpt

+38
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ var_dump(pg_select($conn, 'phptests.bar', array('id4' => 4)));
3636
/* Use a different result type */
3737
var_dump(pg_select($conn, 'phptests.bar', array('id4' => 4), 0, PGSQL_NUM));
3838

39+
/* Empty array */
40+
var_dump(pg_select($conn, 'phptests.bar', array()));
41+
42+
/* No array */
43+
var_dump(pg_select($conn, 'phptests.bar'));
44+
3945
pg_query($conn, 'DROP TABLE phptests.foo');
4046
pg_query($conn, 'DROP TABLE phptests.bar');
4147
pg_query($conn, 'DROP SCHEMA phptests');
@@ -74,3 +80,35 @@ array(1) {
7480
string(1) "5"
7581
}
7682
}
83+
array(2) {
84+
[0]=>
85+
array(2) {
86+
["id4"]=>
87+
string(1) "4"
88+
["id3"]=>
89+
string(1) "5"
90+
}
91+
[1]=>
92+
array(2) {
93+
["id4"]=>
94+
string(1) "6"
95+
["id3"]=>
96+
string(1) "7"
97+
}
98+
}
99+
array(2) {
100+
[0]=>
101+
array(2) {
102+
["id4"]=>
103+
string(1) "4"
104+
["id3"]=>
105+
string(1) "5"
106+
}
107+
[1]=>
108+
array(2) {
109+
["id4"]=>
110+
string(1) "6"
111+
["id3"]=>
112+
string(1) "7"
113+
}
114+
}

0 commit comments

Comments
 (0)