Skip to content

Commit 79cb3c8

Browse files
committed
Use zend_string* for PGSQL_API functions
1 parent b374092 commit 79cb3c8

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

ext/pgsql/pgsql.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,7 +4236,7 @@ PHP_FUNCTION(pg_flush)
42364236
* table_name must not be empty
42374237
* TODO: Add meta_data cache for better performance
42384238
*/
4239-
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, bool extended)
4239+
PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const zend_string *table_name, zval *meta, bool extended)
42404240
{
42414241
PGresult *pg_result;
42424242
char *src, *tmp_name, *tmp_name2 = NULL;
@@ -4246,9 +4246,9 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table
42464246
int i, num_rows;
42474247
zval elem;
42484248

4249-
ZEND_ASSERT(*table_name);
4249+
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
42504250

4251-
src = estrdup(table_name);
4251+
src = estrdup(ZSTR_VAL(table_name));
42524252
tmp_name = php_strtok_r(src, ".", &tmp_name2);
42534253
if (!tmp_name) {
42544254
// TODO ValueError (empty table name)?
@@ -4302,7 +4302,7 @@ PHP_PGSQL_API zend_result php_pgsql_meta_data(PGconn *pg_link, const char *table
43024302

43034303
pg_result = PQexec(pg_link, ZSTR_VAL(querystr.s));
43044304
if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
4305-
php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", table_name);
4305+
php_error_docref(NULL, E_WARNING, "Table '%s' doesn't exists", ZSTR_VAL(table_name));
43064306
smart_str_free(&querystr);
43074307
PQclear(pg_result);
43084308
return FAILURE;
@@ -4369,7 +4369,7 @@ PHP_FUNCTION(pg_meta_data)
43694369
}
43704370

43714371
array_init(return_value);
4372-
if (php_pgsql_meta_data(pgsql, ZSTR_VAL(table_name), return_value, extended) == FAILURE) {
4372+
if (php_pgsql_meta_data(pgsql, table_name, return_value, extended) == FAILURE) {
43734373
zend_array_destroy(Z_ARR_P(return_value)); /* destroy array */
43744374
RETURN_FALSE;
43754375
}
@@ -4560,7 +4560,7 @@ static int php_pgsql_add_quotes(zval *src, bool should_free)
45604560
/* {{{ php_pgsql_convert
45614561
* check and convert array values (fieldname=>value pair) for sql
45624562
*/
4563-
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt)
4563+
PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const zend_string *table_name, const zval *values, zval *result, zend_ulong opt)
45644564
{
45654565
zend_string *field = NULL;
45664566
zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val;
@@ -4573,10 +4573,10 @@ PHP_PGSQL_API zend_result php_pgsql_convert(PGconn *pg_link, const char *table_n
45734573
ZEND_ASSERT(!(opt & ~PGSQL_CONV_OPTS));
45744574
ZEND_ASSERT(table_name);
45754575
/* Table name cannot be empty for php_pgsql_meta_data() */
4576-
ZEND_ASSERT(*table_name);
4576+
ZEND_ASSERT(ZSTR_LEN(table_name) != 0);
45774577

45784578
array_init(&meta);
4579-
/* table_name is escaped by php_pgsql_meta_data */
4579+
/* table_name is escaped by php_pgsql_meta_data */
45804580
if (php_pgsql_meta_data(pg_link, table_name, &meta, 0) == FAILURE) {
45814581
zval_ptr_dtor(&meta);
45824582
return FAILURE;
@@ -5246,7 +5246,7 @@ PHP_FUNCTION(pg_convert)
52465246
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
52475247
}
52485248
array_init(return_value);
5249-
if (php_pgsql_convert(pg_link, ZSTR_VAL(table_name), values, return_value, option) == FAILURE) {
5249+
if (php_pgsql_convert(pg_link, table_name, values, return_value, option) == FAILURE) {
52505250
zend_array_destroy(Z_ARR_P(return_value));
52515251
RETURN_FALSE;
52525252
}
@@ -5276,23 +5276,22 @@ static bool do_exec(smart_str *querystr, ExecStatusType expect, PGconn *pg_link,
52765276
}
52775277
/* }}} */
52785278

5279-
static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table) /* {{{ */
5279+
static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const zend_string *table) /* {{{ */
52805280
{
5281-
size_t table_len = strlen(table);
5282-
52835281
/* schema.table should be "schema"."table" */
5284-
const char *dot = memchr(table, '.', table_len);
5285-
size_t len = dot ? dot - table : table_len;
5286-
if (_php_pgsql_identifier_is_escaped(table, len)) {
5287-
smart_str_appendl(querystr, table, len);
5282+
const char *dot = memchr(ZSTR_VAL(table), '.', ZSTR_LEN(table));
5283+
size_t len = dot ? dot - ZSTR_VAL(table) : ZSTR_LEN(table);
5284+
5285+
if (_php_pgsql_identifier_is_escaped(ZSTR_VAL(table), len)) {
5286+
smart_str_appendl(querystr, ZSTR_VAL(table), len);
52885287
} else {
5289-
char *escaped = PQescapeIdentifier(pg_link, table, len);
5288+
char *escaped = PQescapeIdentifier(pg_link, ZSTR_VAL(table), len);
52905289
smart_str_appends(querystr, escaped);
52915290
PQfreemem(escaped);
52925291
}
52935292
if (dot) {
52945293
const char *after_dot = dot + 1;
5295-
len = table_len - len - 1;
5294+
len = ZSTR_LEN(table) - len - 1;
52965295
/* "schema"."table" format */
52975296
if (_php_pgsql_identifier_is_escaped(after_dot, len)) {
52985297
smart_str_appendc(querystr, '.');
@@ -5308,7 +5307,7 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c
53085307
/* }}} */
53095308

53105309
/* {{{ php_pgsql_insert */
5311-
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, zend_ulong opt, zend_string **sql)
5310+
PHP_PGSQL_API zend_result php_pgsql_insert(PGconn *pg_link, const zend_string *table, zval *var_array, zend_ulong opt, zend_string **sql)
53125311
{
53135312
zval *val, converted;
53145313
char buf[256];
@@ -5459,7 +5458,7 @@ PHP_FUNCTION(pg_insert)
54595458
if (option & PGSQL_DML_EXEC) {
54605459
/* return resource when executed */
54615460
option = option & ~PGSQL_DML_EXEC;
5462-
if (php_pgsql_insert(pg_link, ZSTR_VAL(table), values, option|PGSQL_DML_STRING, &sql) == FAILURE) {
5461+
if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql) == FAILURE) {
54635462
RETURN_FALSE;
54645463
}
54655464
pg_result = PQexec(pg_link, ZSTR_VAL(sql));
@@ -5499,7 +5498,7 @@ PHP_FUNCTION(pg_insert)
54995498
}
55005499
break;
55015500
}
5502-
} else if (php_pgsql_insert(pg_link, ZSTR_VAL(table), values, option, &sql) == FAILURE) {
5501+
} else if (php_pgsql_insert(pg_link, table, values, option, &sql) == FAILURE) {
55035502
RETURN_FALSE;
55045503
}
55055504
if (return_sql) {
@@ -5573,7 +5572,7 @@ static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr,
55735572
/* }}} */
55745573

55755574
/* {{{ php_pgsql_update */
5576-
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)
5575+
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)
55775576
{
55785577
zval var_converted, ids_converted;
55795578
smart_str querystr = {0};
@@ -5671,7 +5670,7 @@ PHP_FUNCTION(pg_update)
56715670
if (php_pgsql_flush_query(pg_link)) {
56725671
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
56735672
}
5674-
if (php_pgsql_update(pg_link, ZSTR_VAL(table), values, ids, option, &sql) == FAILURE) {
5673+
if (php_pgsql_update(pg_link, table, values, ids, option, &sql) == FAILURE) {
56755674
RETURN_FALSE;
56765675
}
56775676
if (option & PGSQL_DML_STRING) {
@@ -5682,7 +5681,7 @@ PHP_FUNCTION(pg_update)
56825681
/* }}} */
56835682

56845683
/* {{{ php_pgsql_delete */
5685-
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, zend_ulong opt, zend_string **sql)
5684+
PHP_PGSQL_API zend_result php_pgsql_delete(PGconn *pg_link, const zend_string *table, zval *ids_array, zend_ulong opt, zend_string **sql)
56865685
{
56875686
zval ids_converted;
56885687
smart_str querystr = {0};
@@ -5766,7 +5765,7 @@ PHP_FUNCTION(pg_delete)
57665765
if (php_pgsql_flush_query(pg_link)) {
57675766
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
57685767
}
5769-
if (php_pgsql_delete(pg_link, ZSTR_VAL(table), ids, option, &sql) == FAILURE) {
5768+
if (php_pgsql_delete(pg_link, table, ids, option, &sql) == FAILURE) {
57705769
RETURN_FALSE;
57715770
}
57725771
if (option & PGSQL_DML_STRING) {
@@ -5818,7 +5817,7 @@ PHP_PGSQL_API void php_pgsql_result2array(PGresult *pg_result, zval *ret_array,
58185817
/* }}} */
58195818

58205819
/* {{{ php_pgsql_select */
5821-
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)
5820+
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)
58225821
{
58235822
zval ids_converted;
58245823
smart_str querystr = {0};
@@ -5914,7 +5913,7 @@ PHP_FUNCTION(pg_select)
59145913
php_error_docref(NULL, E_NOTICE, "Detected unhandled result(s) in connection");
59155914
}
59165915
array_init(return_value);
5917-
if (php_pgsql_select(pg_link, ZSTR_VAL(table), ids, return_value, option, result_type, &sql) == FAILURE) {
5916+
if (php_pgsql_select(pg_link, table, ids, return_value, option, result_type, &sql) == FAILURE) {
59185917
zval_ptr_dtor(return_value);
59195918
RETURN_FALSE;
59205919
}

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)