Skip to content

Commit 346df2d

Browse files
committed
ext/pgsql: Refactor parameter parsing for a few functions
1 parent 0c577c2 commit 346df2d

File tree

1 file changed

+89
-69
lines changed

1 file changed

+89
-69
lines changed

ext/pgsql/pgsql.c

Lines changed: 89 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,39 +1984,12 @@ PHP_FUNCTION(pg_fetch_result)
19841984
/* }}} */
19851985

19861986
/* {{{ void php_pgsql_fetch_hash */
1987-
static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object)
1987+
static void php_pgsql_fetch_hash(zval *return_value, const zval *result, zend_long row, bool row_is_null, zend_long result_type)
19881988
{
1989-
zval *result;
19901989
PGresult *pgsql_result;
19911990
pgsql_result_handle *pg_result;
19921991
int i, num_fields, pgsql_row;
1993-
zend_long row;
1994-
bool row_is_null = true;
19951992
char *field_name;
1996-
HashTable *ctor_params = NULL;
1997-
zend_class_entry *ce = NULL;
1998-
1999-
if (into_object) {
2000-
ZEND_PARSE_PARAMETERS_START(1, 4)
2001-
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2002-
Z_PARAM_OPTIONAL
2003-
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2004-
Z_PARAM_CLASS(ce)
2005-
Z_PARAM_ARRAY_HT(ctor_params)
2006-
ZEND_PARSE_PARAMETERS_END();
2007-
2008-
if (!ce) {
2009-
ce = zend_standard_class_def;
2010-
}
2011-
result_type = PGSQL_ASSOC;
2012-
} else {
2013-
ZEND_PARSE_PARAMETERS_START(1, 3)
2014-
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2015-
Z_PARAM_OPTIONAL
2016-
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2017-
Z_PARAM_LONG(result_type)
2018-
ZEND_PARSE_PARAMETERS_END();
2019-
}
20201993

20211994
if (!row_is_null && row < 0) {
20221995
zend_argument_value_error(2, "must be greater than or equal to 0");
@@ -2060,7 +2033,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
20602033
add_assoc_null(return_value, field_name);
20612034
}
20622035
} else {
2063-
char *element = PQgetvalue(pgsql_result, pgsql_row, i);
2036+
const char *element = PQgetvalue(pgsql_result, pgsql_row, i);
20642037
if (element) {
20652038
const size_t element_len = strlen(element);
20662039

@@ -2075,63 +2048,110 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_
20752048
}
20762049
}
20772050
}
2078-
2079-
if (into_object) {
2080-
zval dataset;
2081-
2082-
ZVAL_COPY_VALUE(&dataset, return_value);
2083-
object_init_ex(return_value, ce);
2084-
if (!ce->default_properties_count && !ce->__set) {
2085-
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
2086-
} else {
2087-
zend_merge_properties(return_value, Z_ARRVAL(dataset));
2088-
zval_ptr_dtor(&dataset);
2089-
}
2090-
2091-
if (ce->constructor) {
2092-
zend_call_known_function(ce->constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value),
2093-
/* retval */ NULL, /* argc */ 0, /* params */ NULL, ctor_params);
2094-
} else if (ctor_params && zend_hash_num_elements(ctor_params) > 0) {
2095-
zend_argument_value_error(3,
2096-
"must be empty when the specified class (%s) does not have a constructor",
2097-
ZSTR_VAL(ce->name)
2098-
);
2099-
}
2100-
}
21012051
}
21022052
/* }}} */
21032053

21042054
/* {{{ Get a row as an enumerated array */
21052055
PHP_FUNCTION(pg_fetch_row)
21062056
{
2107-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM, 0);
2057+
zval *result;
2058+
zend_long row;
2059+
bool row_is_null = true;
2060+
zend_long result_type = PGSQL_NUM;
2061+
2062+
ZEND_PARSE_PARAMETERS_START(1, 3)
2063+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2064+
Z_PARAM_OPTIONAL
2065+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2066+
Z_PARAM_LONG(result_type)
2067+
ZEND_PARSE_PARAMETERS_END();
2068+
2069+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, result_type);
21082070
}
21092071
/* }}} */
21102072

21112073
/* {{{ Fetch a row as an assoc array */
21122074
PHP_FUNCTION(pg_fetch_assoc)
21132075
{
2114-
/* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
2115-
there is 3rd parameter */
2116-
if (ZEND_NUM_ARGS() > 2)
2117-
WRONG_PARAM_COUNT;
2118-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 0);
2076+
zval *result;
2077+
zend_long row;
2078+
bool row_is_null = true;
2079+
2080+
ZEND_PARSE_PARAMETERS_START(1, 2)
2081+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2082+
Z_PARAM_OPTIONAL
2083+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2084+
ZEND_PARSE_PARAMETERS_END();
2085+
2086+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, PGSQL_ASSOC);
21192087
}
21202088
/* }}} */
21212089

21222090
/* {{{ Fetch a row as an array */
21232091
PHP_FUNCTION(pg_fetch_array)
21242092
{
2125-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH, 0);
2093+
zval *result;
2094+
zend_long row;
2095+
bool row_is_null = true;
2096+
zend_long result_type = PGSQL_BOTH;
2097+
2098+
ZEND_PARSE_PARAMETERS_START(1, 3)
2099+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2100+
Z_PARAM_OPTIONAL
2101+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2102+
Z_PARAM_LONG(result_type)
2103+
ZEND_PARSE_PARAMETERS_END();
2104+
2105+
php_pgsql_fetch_hash(return_value, result, row, row_is_null, result_type);
21262106
}
21272107
/* }}} */
21282108

21292109
/* {{{ Fetch a row as an object */
21302110
PHP_FUNCTION(pg_fetch_object)
21312111
{
2112+
zval *result;
2113+
zend_long row;
2114+
bool row_is_null = true;
2115+
zend_class_entry *ce = NULL;
2116+
HashTable *ctor_params = NULL;
2117+
2118+
ZEND_PARSE_PARAMETERS_START(1, 4)
2119+
Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
2120+
Z_PARAM_OPTIONAL
2121+
Z_PARAM_LONG_OR_NULL(row, row_is_null)
2122+
Z_PARAM_CLASS(ce)
2123+
Z_PARAM_ARRAY_HT(ctor_params)
2124+
ZEND_PARSE_PARAMETERS_END();
2125+
2126+
if (!ce) {
2127+
ce = zend_standard_class_def;
2128+
}
2129+
2130+
if (!ce->constructor && ctor_params && zend_hash_num_elements(ctor_params) > 0) {
2131+
zend_argument_value_error(3,
2132+
"must be empty when the specified class (%s) does not have a constructor",
2133+
ZSTR_VAL(ce->name)
2134+
);
2135+
RETURN_THROWS();
2136+
}
2137+
21322138
/* pg_fetch_object() allowed result_type used to be. 3rd parameter
21332139
must be allowed for compatibility */
2134-
php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 1);
2140+
zval dataset;
2141+
php_pgsql_fetch_hash(&dataset, result, row, row_is_null, PGSQL_ASSOC);
2142+
2143+
object_init_ex(return_value, ce);
2144+
if (!ce->default_properties_count && !ce->__set) {
2145+
Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
2146+
} else {
2147+
zend_merge_properties(return_value, Z_ARRVAL(dataset));
2148+
zval_ptr_dtor(&dataset);
2149+
}
2150+
2151+
if (ce->constructor) {
2152+
zend_call_known_function(ce->constructor, Z_OBJ_P(return_value), Z_OBJCE_P(return_value),
2153+
/* retval */ NULL, /* argc */ 0, /* params */ NULL, ctor_params);
2154+
}
21352155
}
21362156
/* }}} */
21372157

@@ -2865,19 +2885,19 @@ PHP_FUNCTION(pg_lo_import)
28652885
Oid returned_oid;
28662886
pgsql_link_handle *link;
28672887

2868-
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
2869-
"OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) == SUCCESS) {
2870-
link = Z_PGSQL_LINK_P(pgsql_link);
2871-
CHECK_PGSQL_LINK(link);
2872-
}
2873-
else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(),
2874-
"P|z", &file_in, &oid) == SUCCESS) {
2888+
/* Deprecated signature with implicit connection */
2889+
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "P|z", &file_in, &oid) == SUCCESS) {
28752890
link = FETCH_DEFAULT_LINK();
28762891
CHECK_DEFAULT_LINK(link);
2892+
goto fn_body;
28772893
}
2878-
else {
2879-
WRONG_PARAM_COUNT;
2894+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) == FAILURE) {
2895+
RETURN_THROWS();
28802896
}
2897+
link = Z_PGSQL_LINK_P(pgsql_link);
2898+
CHECK_PGSQL_LINK(link);
2899+
2900+
fn_body:
28812901

28822902
if (php_check_open_basedir(ZSTR_VAL(file_in))) {
28832903
RETURN_FALSE;

0 commit comments

Comments
 (0)