Skip to content

PGSQL: Allow unconditional selection in pg_select. #5332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -5777,29 +5777,34 @@ PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *t

ZEND_ASSERT(pg_link != NULL);
ZEND_ASSERT(table != NULL);
ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
if (ids_array) {
ZEND_ASSERT(Z_TYPE_P(ids_array) == IS_ARRAY);
}
ZEND_ASSERT(Z_TYPE_P(ret_array) == IS_ARRAY);
ZEND_ASSERT(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));

if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
return FAILURE;
}
zend_bool is_valid_ids_array = ids_array && zend_hash_num_elements(Z_ARRVAL_P(ids_array)) != 0;

ZVAL_UNDEF(&ids_converted);
if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
array_init(&ids_converted);
if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
goto cleanup;
if (is_valid_ids_array) {
ZVAL_UNDEF(&ids_converted);
if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
array_init(&ids_converted);
if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) == FAILURE) {
goto cleanup;
}
ids_array = &ids_converted;
}
ids_array = &ids_converted;
}

smart_str_appends(&querystr, "SELECT * FROM ");
build_tablename(&querystr, pg_link, table);
smart_str_appends(&querystr, " WHERE ");

if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt))
goto cleanup;
if (is_valid_ids_array) {
smart_str_appends(&querystr, " WHERE ");
if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt)) {
goto cleanup;
}
}

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

cleanup:
zval_ptr_dtor(&ids_converted);
if (is_valid_ids_array) {
zval_ptr_dtor(&ids_converted);
}
if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
*sql = querystr.s;
}
Expand All @@ -5828,7 +5835,8 @@ PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *t
/* {{{ Select records that has ids (id=>value) */
PHP_FUNCTION(pg_select)
{
zval *pgsql_link, *ids;
zval *pgsql_link;
zval *ids = NULL;
pgsql_link_handle *link;
zend_string *table;
zend_ulong option = PGSQL_DML_EXEC;
Expand All @@ -5837,7 +5845,7 @@ PHP_FUNCTION(pg_select)
zend_string *sql = NULL;

/* TODO Document result_type param on php.net (apparently it was added in PHP 7.1) */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OPa|ll",
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|all",
&pgsql_link, pgsql_link_ce, &table, &ids, &option, &result_type
) == FAILURE) {
RETURN_THROWS();
Expand Down
2 changes: 1 addition & 1 deletion ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ function pg_delete(PgSql\Connection $connection, string $table_name, array $cond
* @return array<int, array>|string|false
* @refcount 1
*/
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions, int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}
function pg_select(PgSql\Connection $connection, string $table_name, array $conditions = [], int $flags = PGSQL_DML_EXEC, int $mode = PGSQL_ASSOC): array|string|false {}

#ifdef LIBPQ_HAS_PIPELINING
function pg_enter_pipeline_mode(PgSql\Connection $connection): bool {}
Expand Down
6 changes: 3 additions & 3 deletions ext/pgsql/pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions ext/pgsql/tests/pg_select_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ var_dump(pg_select($conn, 'phptests.bar', array('id4' => 4)));
/* Use a different result type */
var_dump(pg_select($conn, 'phptests.bar', array('id4' => 4), 0, PGSQL_NUM));

/* Empty array */
var_dump(pg_select($conn, 'phptests.bar', array()));

/* No array */
var_dump(pg_select($conn, 'phptests.bar'));

pg_query($conn, 'DROP TABLE phptests.foo');
pg_query($conn, 'DROP TABLE phptests.bar');
pg_query($conn, 'DROP SCHEMA phptests');
Expand Down Expand Up @@ -74,3 +80,35 @@ array(1) {
string(1) "5"
}
}
array(2) {
[0]=>
array(2) {
["id4"]=>
string(1) "4"
["id3"]=>
string(1) "5"
}
[1]=>
array(2) {
["id4"]=>
string(1) "6"
["id3"]=>
string(1) "7"
}
}
array(2) {
[0]=>
array(2) {
["id4"]=>
string(1) "4"
["id3"]=>
string(1) "5"
}
[1]=>
array(2) {
["id4"]=>
string(1) "6"
["id3"]=>
string(1) "7"
}
}