Skip to content

Commit d0ceecc

Browse files
committed
Use zend_string* for PGSQL_API functions
1 parent cde6bee commit d0ceecc

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

UPGRADING.INTERNALS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ PHP 8.1 INTERNALS UPGRADE NOTES
8888
- The php_pgsql_meta_data(), php_pgsql_convert(), php_pgsql_insert(),
8989
php_pgsql_update(), php_pgsql_delete(), and php_pgsql_select() have
9090
had their return type formalized to zend_result.
91+
- The php_pgsql_meta_data(), php_pgsql_convert(), php_pgsql_insert(),
92+
php_pgsql_update(), php_pgsql_delete(), and php_pgsql_select() now
93+
accept a zend_string* instead of a char*.

ext/pgsql/pgsql.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,7 +4225,7 @@ PHP_FUNCTION(pg_flush)
42254225
* table_name must not be empty
42264226
* TODO: Add meta_data cache for better performance
42274227
*/
4228-
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, bool extended)
4228+
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended)
42294229
{
42304230
PGresult *pg_result;
42314231
char *src, *tmp_name, *tmp_name2 = NULL;
@@ -4235,9 +4235,9 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table
42354235
int i, num_rows;
42364236
zval elem;
42374237

4238-
ZEND_ASSERT(*table_name);
4238+
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
42394239

4240-
src = estrdup(table_name);
4240+
src = estrdup(ZSTR_VAL(table_name));
42414241
tmp_name = php_strtok_r(src, ".", &tmp_name2);
42424242
if (!tmp_name) {
42434243
// TODO ValueError (empty table name)?
@@ -4291,7 +4291,7 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table
42914291

42924292
pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
42934293
if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
4294-
php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", table_name);
4294+
php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", ZSTR_VAL(table_name));
42954295
smart_str_free(&querystr);
42964296
PQclear(pg_result);
42974297
return FAILURE;
@@ -4358,7 +4358,7 @@ PHP_FUNCTION(pg_meta_data)
43584358
}
43594359

43604360
array_init(return_value);
4361-
if (php_pgsql_meta_data(pgsql, ZSTR_VAL(table_name), return_value, extended) == FAILURE) {
4361+
if (php_pgsql_meta_data(pgsql, table_name, return_value, extended) == FAILURE) {
43624362
zend_array_destroy(Z_ARR_P(return_value)); /* destroy array */
43634363
RETURN_FALSE;
43644364
}
@@ -4550,7 +4550,7 @@ static int php_pgsql_add_quotes(zval *src, bool should_free)
45504550
/* {{{ php_pgsql_convert
45514551
* check and convert array values (fieldname=>value pair) for sql
45524552
*/
4553-
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt)
4553+
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt)
45544554
{
45554555
zend_string *field = NULL;
45564556
zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
@@ -4563,10 +4563,10 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const char *table_n
45634563
ZEND_ASSERT(!(opt & ~PGSQL_CONV_OPTS));
45644564
ZEND_ASSERT(table_name);
45654565
/* Table name cannot be empty for php_pgsql_meta_data() */
4566-
ZEND_ASSERT(*table_name);
4566+
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
45674567

45684568
array_init(&meta);
4569-
/* table_name is escaped by php_pgsql_meta_data */
4569+
/* table_name is escaped by php_pgsql_meta_data */
45704570
if (php_pgsql_meta_data(pg_link, table_name, &meta, 0) == FAILURE) {
45714571
zval_ptr_dtor(&meta);
45724572
return FAILURE;
@@ -5236,7 +5236,7 @@ PHP_FUNCTION(pg_convert)
52365236
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
52375237
}
52385238
array_init(return_value);
5239-
if (php_pgsql_convert(pg_link, ZSTR_VAL(table_name), values, return_value, option) == FAILURE) {
5239+
if (php_pgsql_convert(pg_link, table_name, values, return_value, option) == FAILURE) {
52405240
zend_array_destroy(Z_ARR_P(return_value));
52415241
RETURN_FALSE;
52425242
}
@@ -5267,23 +5267,22 @@ static int do_exec(smart_str *querystr, ExecStatusType expect, PGconn *pg_link,
52675267
}
52685268
/* }}} */
52695269

5270-
static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table) /* {{{ */
5270+
static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const zend_string *table) /* {{{ */
52715271
{
5272-
size_t table_len = strlen(table);
5273-
52745272
/* schema.table should be "schema"."table" */
5275-
const char *dot = memchr(table, '.', table_len);
5276-
size_t len = dot ? dot - table : table_len;
5277-
if (_php_pgsql_identifier_is_escaped(table, len)) {
5278-
smart_str_appendl(querystr, table, len);
5273+
const char *dot = memchr(ZSTR_VAL(table), '.', ZSTR_LEN(table));
5274+
size_t len = dot ? dot - ZSTR_VAL(table) : ZSTR_LEN(table);
5275+
5276+
if (_php_pgsql_identifier_is_escaped(ZSTR_VAL(table), len)) {
5277+
smart_str_appendl(querystr, ZSTR_VAL(table), len);
52795278
} else {
5280-
char *escaped = PQescapeIdentifier(pg_link, table, len);
5279+
char *escaped = PQescapeIdentifier(pg_link, ZSTR_VAL(table), len);
52815280
smart_str_appends(querystr, escaped);
52825281
PQfreemem(escaped);
52835282
}
52845283
if (dot) {
52855284
const char *after_dot = dot + 1;
5286-
len = table_len - len - 1;
5285+
len = ZSTR_LEN(table) - len - 1;
52875286
/* "schema"."table" format */
52885287
if (_php_pgsql_identifier_is_escaped(after_dot, len)) {
52895288
smart_str_appendc(querystr, '.');
@@ -5299,7 +5298,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
52995298
/* }}} */
53005299

53015300
/* {{{ php_pgsql_insert */
5302-
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, zend_ulong opt, zend_string **sql)
5301+
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *var_array, zend_ulong opt, zend_string **sql)
53035302
{
53045303
zval *val, converted;
53055304
char buf[256];
@@ -5450,7 +5449,7 @@ PHP_FUNCTION(pg_insert)
54505449
if (option & PGSQL_DML_EXEC) {
54515450
/* return resource when executed */
54525451
option = option & ~PGSQL_DML_EXEC;
5453-
if (php_pgsql_insert(pg_link, ZSTR_VAL(table), values, option|PGSQL_DML_STRING, &sql) == FAILURE) {
5452+
if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql) == FAILURE) {
54545453
RETURN_FALSE;
54555454
}
54565455
pg_result = PQexec(pg_link, ZSTR_VAL(sql));
@@ -5490,7 +5489,7 @@ PHP_FUNCTION(pg_insert)
54905489
}
54915490
break;
54925491
}
5493-
} else if (php_pgsql_insert(pg_link, ZSTR_VAL(table), values, option, &sql) == FAILURE) {
5492+
} else if (php_pgsql_insert(pg_link, table, values, option, &sql) == FAILURE) {
54945493
RETURN_FALSE;
54955494
}
54965495
if (return_sql) {
@@ -5564,7 +5563,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
55645563
/* }}} */
55655564

55665565
/* {{{ php_pgsql_update */
5567-
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const char *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql)
5566+
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql)
55685567
{
55695568
zval var_converted, ids_converted;
55705569
smart_str querystr = {0};
@@ -5662,7 +5661,7 @@ PHP_FUNCTION(pg_update)
56625661
if (php_pgsql_flush_query(pg_link)) {
56635662
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
56645663
}
5665-
if (php_pgsql_update(pg_link, ZSTR_VAL(table), values, ids, option, &sql) == FAILURE) {
5664+
if (php_pgsql_update(pg_link, table, values, ids, option, &sql) == FAILURE) {
56665665
RETURN_FALSE;
56675666
}
56685667
if (option & PGSQL_DML_STRING) {
@@ -5673,7 +5672,7 @@ PHP_FUNCTION(pg_update)
56735672
/* }}} */
56745673

56755674
/* {{{ php_pgsql_delete */
5676-
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, zend_ulong opt, zend_string **sql)
5675+
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids_array, zend_ulong opt, zend_string **sql)
56775676
{
56785677
zval ids_converted;
56795678
smart_str querystr = {0};
@@ -5757,7 +5756,7 @@ PHP_FUNCTION(pg_delete)
57575756
if (php_pgsql_flush_query(pg_link)) {
57585757
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
57595758
}
5760-
if (php_pgsql_delete(pg_link, ZSTR_VAL(table), ids, option, &sql) == FAILURE) {
5759+
if (php_pgsql_delete(pg_link, table, ids, option, &sql) == FAILURE) {
57615760
RETURN_FALSE;
57625761
}
57635762
if (option & PGSQL_DML_STRING) {
@@ -5808,7 +5807,7 @@ PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array,
58085807
/* }}} */
58095808

58105809
/* {{{ php_pgsql_select */
5811-
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const char *table, zval *ids_array, zval *ret_array, zend_ulong opt, long result_type, zend_string **sql)
5810+
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids_array, zval *ret_array, zend_ulong opt, long result_type, zend_string **sql)
58125811
{
58135812
zval ids_converted;
58145813
smart_str querystr = {0};
@@ -5904,7 +5903,7 @@ PHP_FUNCTION(pg_select)
59045903
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
59055904
}
59065905
array_init(return_value);
5907-
if (php_pgsql_select(pg_link, ZSTR_VAL(table), ids, return_value, option, result_type, &sql) == FAILURE) {
5906+
if (php_pgsql_select(pg_link, table, ids, return_value, option, result_type, &sql) == FAILURE) {
59085907
zval_ptr_dtor(return_value);
59095908
RETURN_FALSE;
59105909
}

ext/pgsql/php_pgsql.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ PHP_FUNCTION(pg_select);
176176
#define PGSQL_DML_ESCAPE (1<<12) /* No convert, but escape only */
177177

178178
/* exported functions */
179-
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, bool extended);
180-
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt);
181-
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const char *table, zval *values, zend_ulong opt, zend_string **sql);
182-
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const char *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
183-
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids, zend_ulong opt, zend_string **sql);
184-
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const char *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
179+
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended);
180+
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt);
181+
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *values, zend_ulong opt, zend_string **sql);
182+
PHP_PGSQL_API zend_result php_pgsql_update(PGconn *pg_link, const zend_string *table, zval *values, zval *ids, zend_ulong opt , zend_string **sql);
183+
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids, zend_ulong opt, zend_string **sql);
184+
PHP_PGSQL_API zend_result php_pgsql_select(PGconn *pg_link, const zend_string *table, zval *ids, zval *ret_array, zend_ulong opt, long fetch_option, zend_string **sql );
185185
PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array, long fetch_option);
186186

187187
/* internal functions */

0 commit comments

Comments
 (0)