Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit a65bf49

Browse files
committed
More escaping methods
* Added "escapeLiteral" method * Added "escapeIdentifier" method
1 parent b752072 commit a65bf49

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

swoole_postgresql_coro.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static PHP_METHOD(swoole_postgresql_coro, __construct);
116116
static PHP_METHOD(swoole_postgresql_coro, __destruct);
117117
static PHP_METHOD(swoole_postgresql_coro, connect);
118118
static PHP_METHOD(swoole_postgresql_coro, escape);
119+
static PHP_METHOD(swoole_postgresql_coro, escapeLiteral);
120+
static PHP_METHOD(swoole_postgresql_coro, escapeIdentifier);
119121
static PHP_METHOD(swoole_postgresql_coro, query);
120122
static PHP_METHOD(swoole_postgresql_coro, prepare);
121123
static PHP_METHOD(swoole_postgresql_coro, execute);
@@ -227,6 +229,8 @@ static const zend_function_entry swoole_postgresql_coro_methods[] =
227229
PHP_ME(swoole_postgresql_coro, fieldCount, arginfo_pg_field_count, ZEND_ACC_PUBLIC)
228230
PHP_ME(swoole_postgresql_coro, metaData, arginfo_pg_meta_data, ZEND_ACC_PUBLIC)
229231
PHP_ME(swoole_postgresql_coro, escape, arginfo_pg_escape, ZEND_ACC_PUBLIC)
232+
PHP_ME(swoole_postgresql_coro, escapeLiteral, arginfo_pg_escape, ZEND_ACC_PUBLIC)
233+
PHP_ME(swoole_postgresql_coro, escapeIdentifier, arginfo_pg_escape, ZEND_ACC_PUBLIC)
230234
PHP_ME(swoole_postgresql_coro, fetchObject, arginfo_pg_fetch_object, ZEND_ACC_PUBLIC)
231235
PHP_ME(swoole_postgresql_coro, fetchAssoc, arginfo_pg_fetch_assoc, ZEND_ACC_PUBLIC)
232236
PHP_ME(swoole_postgresql_coro, fetchArray, arginfo_pg_fetch_array, ZEND_ACC_PUBLIC)
@@ -1462,6 +1466,52 @@ static PHP_METHOD(swoole_postgresql_coro, escape) {
14621466
}
14631467
}
14641468

1469+
static PHP_METHOD(swoole_postgresql_coro, escapeLiteral) {
1470+
char *str, *tmp;
1471+
size_t l_str;
1472+
PGconn *pgsql;
1473+
1474+
ZEND_PARSE_PARAMETERS_START(1, 1)
1475+
Z_PARAM_STRING(str, l_str)
1476+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1477+
1478+
pg_object *object = php_swoole_postgresql_coro_get_object(ZEND_THIS);
1479+
pgsql = object->conn;
1480+
1481+
tmp = PQescapeLiteral(pgsql, str, l_str);
1482+
if (tmp == nullptr) {
1483+
zend_update_property_string(swoole_postgresql_coro_ce, ZEND_THIS, ZEND_STRL("error"), PQerrorMessage(pgsql));
1484+
1485+
RETURN_FALSE;
1486+
}
1487+
1488+
RETVAL_STRING(tmp);
1489+
PQfreemem(tmp);
1490+
}
1491+
1492+
static PHP_METHOD(swoole_postgresql_coro, escapeIdentifier) {
1493+
char *str, *tmp;
1494+
size_t l_str;
1495+
PGconn *pgsql;
1496+
1497+
ZEND_PARSE_PARAMETERS_START(1, 1)
1498+
Z_PARAM_STRING(str, l_str)
1499+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1500+
1501+
pg_object *object = php_swoole_postgresql_coro_get_object(ZEND_THIS);
1502+
pgsql = object->conn;
1503+
1504+
tmp = PQescapeIdentifier(pgsql, str, l_str);
1505+
if (tmp == nullptr) {
1506+
zend_update_property_string(swoole_postgresql_coro_ce, ZEND_THIS, ZEND_STRL("error"), PQerrorMessage(pgsql));
1507+
1508+
RETURN_FALSE;
1509+
}
1510+
1511+
RETVAL_STRING(tmp);
1512+
PQfreemem(tmp);
1513+
}
1514+
14651515
/* {{{ PHP_MINIT_FUNCTION
14661516
*/
14671517
PHP_MINIT_FUNCTION(swoole_postgresql) {

0 commit comments

Comments
 (0)