23#define PHP_PGSQL_PRIVATE 1
46#define InvalidOid ((Oid) 0)
49#define PGSQL_ASSOC 1<<0
51#define PGSQL_BOTH (PGSQL_ASSOC|PGSQL_NUM)
53#define PGSQL_NOTICE_LAST 1
54#define PGSQL_NOTICE_ALL 2
55#define PGSQL_NOTICE_CLEAR 3
57#define PGSQL_STATUS_LONG 1
58#define PGSQL_STATUS_STRING 2
60#define PGSQL_MAX_LENGTH_OF_LONG 30
61#define PGSQL_MAX_LENGTH_OF_DOUBLE 60
63char pgsql_libpq_version[16];
67#if ZEND_LONG_MAX < UINT_MAX
68#define PGSQL_RETURN_OID(oid) do { \
69 if (oid > ZEND_LONG_MAX) { \
70 RETURN_STR(zend_ulong_to_str(oid)); \
72 RETURN_LONG((zend_long)oid); \
75#define PGSQL_RETURN_OID(oid) RETURN_LONG((zend_long)oid)
78#define CHECK_DEFAULT_LINK(x) \
80 zend_throw_error(NULL, "No PostgreSQL connection opened yet"); \
85#define FETCH_DEFAULT_LINK() \
86 (PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL); \
87 php_error_docref(NULL, E_DEPRECATED, "Automatic fetching of PostgreSQL connection is deprecated")
90#define FETCH_DEFAULT_LINK_NO_WARNING() \
91 (PGG(default_link) ? pgsql_link_from_obj(PGG(default_link)) : NULL)
93#define CHECK_PGSQL_LINK(link_handle) \
94 if (link_handle->conn == NULL) { \
95 zend_throw_error(NULL, "PostgreSQL connection has already been closed"); \
99#define CHECK_PGSQL_RESULT(result_handle) \
100 if (result_handle->result == NULL) { \
101 zend_throw_error(NULL, "PostgreSQL result has already been closed"); \
105#define CHECK_PGSQL_LOB(lob) \
106 if (lob->conn == NULL) { \
107 zend_throw_error(NULL, "PostgreSQL large object has already been closed"); \
111#ifndef HAVE_PQERRORS_SQLSTATE
112#define PQERRORS_SQLSTATE 0
143#ifdef COMPILE_DL_PGSQL
153static zend_object_handlers pgsql_link_object_handlers, pgsql_result_object_handlers, pgsql_lob_object_handlers;
155static inline pgsql_link_handle *pgsql_link_from_obj(
zend_object *obj) {
156 return (pgsql_link_handle *)((
char *)(obj) -
XtOffsetOf(pgsql_link_handle, std));
159#define Z_PGSQL_LINK_P(zv) pgsql_link_from_obj(Z_OBJ_P(zv))
162 pgsql_link_handle *intern = zend_object_alloc(
sizeof(pgsql_link_handle), class_type);
171 zend_throw_error(
NULL,
"Cannot directly construct PgSql\\Connection, use pg_connect() or pg_pconnect() instead");
175static void pgsql_link_free(pgsql_link_handle *
link)
179 while ((
res = PQgetResult(
link->conn))) {
182 if (!
link->persistent) {
183 PQuntrace(
link->conn);
184 PQfinish(
link->conn);
191 zend_string_release(
link->hash);
202 pgsql_link_handle *
link = pgsql_link_from_obj(obj);
205 pgsql_link_free(
link);
211static inline pgsql_result_handle *pgsql_result_from_obj(
zend_object *obj) {
212 return (pgsql_result_handle *)((
char *)(obj) -
XtOffsetOf(pgsql_result_handle, std));
215#define Z_PGSQL_RESULT_P(zv) pgsql_result_from_obj(Z_OBJ_P(zv))
218 pgsql_result_handle *intern = zend_object_alloc(
sizeof(pgsql_result_handle), class_type);
227 zend_throw_error(
NULL,
"Cannot directly construct PgSql\\Result, use a dedicated function instead");
231static void pgsql_result_free(pgsql_result_handle *
pg_result)
239 pgsql_result_handle *
pg_result = pgsql_result_from_obj(obj);
248static inline pgLofp *pgsql_lob_from_obj(
zend_object *obj) {
249 return (pgLofp *)((
char *)(obj) -
XtOffsetOf(pgLofp, std));
252#define Z_PGSQL_LOB_P(zv) pgsql_lob_from_obj(Z_OBJ_P(zv))
255 pgLofp *intern = zend_object_alloc(
sizeof(pgLofp), class_type);
270 pgLofp *lofp = pgsql_lob_from_obj(obj);
277static zend_string *_php_pgsql_trim_message(
const char *message)
279 size_t i =
strlen(message);
281 if (i>2 && (message[i-2] ==
'\r' || message[i-2] ==
'\n') && message[i-1] ==
'.') {
284 while (i>1 && (message[i-1] ==
'\r' || message[i-1] ==
'\n')) {
287 return zend_string_init(message, i, 0);
290#define PHP_PQ_ERROR(text, pgsql) { \
291 zend_string *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql)); \
292 php_error_docref(NULL, E_WARNING, text, ZSTR_VAL(msgbuf)); \
293 zend_string_release(msgbuf); \
296static void php_pgsql_set_default_link(
zend_object *obj)
300 if (PGG(default_link) !=
NULL) {
301 zend_object_release(PGG(default_link));
304 PGG(default_link) = obj;
309 PGconn *
link = (PGconn *)rsrc->
ptr;
312 while ((
res = PQgetResult(
link))) {
316 PGG(num_persistent)--;
321static void _php_pgsql_notice_handler(
void *l,
const char *message)
323 if (PGG(ignore_notices)) {
328 pgsql_link_handle *
link = (pgsql_link_handle *) l;
330 if (!
link->notices) {
334 zend_string *trimmed_message = _php_pgsql_trim_message(message);
335 if (PGG(log_notices)) {
343static int _rollback_transactions(
zval *el)
349 if (rsrc->
type != le_plink) {
355 if (PQsetnonblocking(
link, 0)) {
360 while ((
res = PQgetResult(
link))) {
363 if (PQtransactionStatus(
link) != PQTRANS_IDLE) {
364 int orig = PGG(ignore_notices);
365 PGG(ignore_notices) = 1;
366 res = PQexec(
link,
"ROLLBACK;");
368 PGG(ignore_notices) = orig;
374static void release_string(
zval *
zv)
379static bool _php_pgsql_identifier_is_escaped(
const char *identifier,
size_t len)
386 if (identifier[0] ==
'"' && identifier[
len-1] ==
'"') {
390 for (i = 1; i <
len-1; i++) {
391 if (identifier[i] ==
'"' && (identifier[++i] !=
'"' || i ==
len-1)) {
402#ifndef HAVE_PG_CHANGE_PASSWORD
403static PGresult *PQchangePassword(PGconn *conn,
const char *user,
const char *passwd)
409 char *enc = PQencryptPasswordConn(conn, passwd, user,
NULL);
415 char *fmtenc = PQescapeLiteral(conn, enc,
strlen(enc));
422 char *fmtuser = PQescapeIdentifier(conn, user,
strlen(user));
431 spprintf(&query, 0,
"ALTER USER %s PASSWORD %s", fmtuser, fmtenc);
433 PGresult *
pg_result = PQexec(conn, query);
443#if !defined(HAVE_PG_SOCKET_POLL)
444static int PQsocketPoll(
int socket,
int read,
int write, time_t timeout)
464 if (timeout != (time_t)ts) {
468 ts = (timeout - cur) * 1000;
490#if defined(COMPILE_DL_PGSQL) && defined(ZTS)
495 memset(pgsql_globals, 0,
sizeof(zend_pgsql_globals));
498#define ADD_REGEX(reg) \
500 ZEND_ASSERT(i < PGSQL_MAX_REGEXES); \
501 pgsql_globals->regexes[i ++] = zend_string_init(reg, strlen(reg), true);\
503 ADD_REGEX(
"#^([+-]{0,1}[0-9]+)$#n");
504 ADD_REGEX(
"#^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$#n");
505 ADD_REGEX(
"#^[+-]{0,1}(inf)(inity){0,1}$#ni");
506 ADD_REGEX(
"#^[0-9]+$#n");
507 ADD_REGEX(
"#^((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])(\\/[0-9]{1,3})?$#n");
508 ADD_REGEX(
"#^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(\\/[0-9]{1,3})?$#n");
509 ADD_REGEX(
"#^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})(([ \\t]+|T)(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$#ni");
510 ADD_REGEX(
"#^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$#ni");
511 ADD_REGEX(
"#^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}){0,1}$#ni");
512 ADD_REGEX(
"#^(@?[ \\t]+)?("
515 "[0-9]+(\\.[0-9]*)?[ \\t]*"
516 "(millenniums|millennia|millennium|mil|mils|"
517 "centuries|century|cent|c|"
518 "decades|decade|dec|decs|"
523 "hours|hour|hr|hrs|h|"
524 "minutes|minute|mins|min|m|"
525 "seconds|second|secs|sec|s))+|"
528 "[0-9]+(\\.[0-9]*)?[ \\t]*"
529 "(millenniums|millennia|millennium|mil|mils|"
530 "centuries|century|cent|c|"
531 "decades|decade|dec|decs|"
538 "(([0-9]{1,2}:){0,2}[0-9]{0,2})"
540 "([ \\t]+ago)?$#ni");
541 ADD_REGEX(
"#^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$#ni");
544static void php_libpq_version(
char *
buf,
size_t len)
546 int version = PQlibVersion();
547 int major = version / 10000;
549 int minor = version % 10000;
552 int minor = version / 100 % 100;
553 int revision = version % 100;
564 pgsql_link_ce = register_class_PgSql_Connection();
570 pgsql_link_object_handlers.
free_obj = pgsql_link_free_obj;
571 pgsql_link_object_handlers.
get_constructor = pgsql_link_get_constructor;
575 pgsql_result_ce = register_class_PgSql_Result();
581 pgsql_result_object_handlers.
free_obj = pgsql_result_free_obj;
582 pgsql_result_object_handlers.
get_constructor = pgsql_result_get_constructor;
586 pgsql_lob_ce = register_class_PgSql_Lob();
592 pgsql_lob_object_handlers.
free_obj = pgsql_lob_free_obj;
598 php_libpq_version(pgsql_libpq_version,
sizeof(pgsql_libpq_version));
600 register_pgsql_symbols(module_number);
610 for (
size_t i = 0; i < PGSQL_MAX_REGEXES; i ++)
618 PGG(default_link) =
NULL;
627 if (PGG(default_link)) {
628 zend_object_release(PGG(default_link));
629 PGG(default_link) =
NULL;
659 size_t connstring_len;
660 pgsql_link_handle *
link;
672 smart_str_appends(&str,
"pgsql");
673 smart_str_appendl(&str, connstring, connstring_len);
674 smart_str_appendc(&str,
'_');
684 if ((le = zend_hash_find_ptr(&
EG(persistent_list), str.s)) ==
NULL) {
685 if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) {
686 php_error_docref(NULL, E_WARNING,
687 "Cannot create new link. Too many open links (" ZEND_LONG_FMT
")", PGG(num_links));
692 "Cannot create new link. Too many open persistent links (" ZEND_LONG_FMT ")", PGG(num_persistent));
697 pgsql = PQconnectdb(connstring);
698 if (pgsql ==
NULL || PQstatus(pgsql) == CONNECTION_BAD) {
699 PHP_PQ_ERROR(
"Unable to connect to PostgreSQL server: %s", pgsql)
711 PGG(num_persistent)++;
713 if (le->
type != le_plink) {
725 if (PGG(auto_reset_persistent) & 1) {
732 if (PQstatus(le->
ptr) == CONNECTION_BAD) {
734 le->
ptr = PQconnectdb(connstring);
739 if (le->
ptr ==
NULL || PQstatus(le->
ptr) == CONNECTION_BAD) {
745 pgsql = (PGconn *) le->
ptr;
747 if (
zend_strtod(PQparameterStatus(pgsql,
"server_version"),
NULL) >= 7.2) {
756 link->hash = zend_string_copy(str.s);
758 link->persistent = 1;
769 php_pgsql_set_default_link(
Z_OBJ_P(index_ptr));
781 pgsql = PQconnectStart(connstring);
782 if (pgsql==
NULL || PQstatus(pgsql)==CONNECTION_BAD) {
783 PHP_PQ_ERROR(
"Unable to connect to PostgreSQL server: %s", pgsql);
790 pgsql = PQconnectdb(connstring);
791 if (pgsql==
NULL || PQstatus(pgsql)==CONNECTION_BAD) {
792 PHP_PQ_ERROR(
"Unable to connect to PostgreSQL server: %s", pgsql);
803 link->hash = zend_string_copy(str.s);
805 link->persistent = 0;
819 PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler,
link);
824 smart_str_free(&str);
828 smart_str_free(&str);
844 pgsql_link_handle *pgsql_handle;
852 pgsql_handle = Z_PGSQL_LINK_P(pgsql_link);
853 CHECK_PGSQL_LINK(pgsql_handle);
854 pgsql = pgsql_handle->conn;
856 ret = PQconnectPoll(pgsql);
873 pgsql_link_handle *
link;
881 link = FETCH_DEFAULT_LINK();
882 CHECK_DEFAULT_LINK(
link);
883 zend_object_release(PGG(default_link));
884 PGG(default_link) =
NULL;
888 link = Z_PGSQL_LINK_P(pgsql_link);
889 CHECK_PGSQL_LINK(
link);
891 if (
link == FETCH_DEFAULT_LINK_NO_WARNING()) {
893 PGG(default_link) =
NULL;
895 pgsql_link_free(
link);
901#define PHP_PG_DBNAME 1
902#define PHP_PG_ERROR_MESSAGE 2
903#define PHP_PG_OPTIONS 3
907#define PHP_PG_VERSION 7
913 pgsql_link_handle *
link;
924 link = FETCH_DEFAULT_LINK();
925 CHECK_DEFAULT_LINK(
link);
927 link = Z_PGSQL_LINK_P(pgsql_link);
928 CHECK_PGSQL_LINK(
link);
937 case PHP_PG_ERROR_MESSAGE:
938 RETURN_STR(_php_pgsql_trim_message(PQerrorMessage(pgsql)));
940 result = PQoptions(pgsql);
951 case PHP_PG_VERSION: {
954 add_assoc_string(
return_value,
"client", pgsql_libpq_version);
955 add_assoc_long(
return_value,
"protocol", PQprotocolVersion(pgsql));
956 add_assoc_string(
return_value,
"server", (
char*)PQparameterStatus(pgsql,
"server_version"));
958#define PHP_PQ_COPY_PARAM(_x) tmp = (char*)PQparameterStatus(pgsql, _x); \
959 if(tmp) add_assoc_string(return_value, _x, tmp); \
960 else add_assoc_null(return_value, _x);
962 PHP_PQ_COPY_PARAM(
"server_encoding");
963 PHP_PQ_COPY_PARAM(
"client_encoding");
964 PHP_PQ_COPY_PARAM(
"is_superuser");
965 PHP_PQ_COPY_PARAM(
"session_authorization");
966 PHP_PQ_COPY_PARAM(
"DateStyle");
967 PHP_PQ_COPY_PARAM(
"IntervalStyle");
968 PHP_PQ_COPY_PARAM(
"TimeZone");
969 PHP_PQ_COPY_PARAM(
"integer_datetimes");
970 PHP_PQ_COPY_PARAM(
"standard_conforming_strings");
971 PHP_PQ_COPY_PARAM(
"application_name");
977 res = PQexec(pgsql,
"SHOW jit_provider");
978 if (PQresultStatus(
res) != PGRES_TUPLES_OK) {
984 res = PQexec(pgsql,
"SHOW jit");
985 if (PQresultStatus(
res) != PGRES_TUPLES_OK) {
1053 pgsql_link_handle *
link;
1063 link = FETCH_DEFAULT_LINK();
1064 CHECK_DEFAULT_LINK(
link);
1066 link = Z_PGSQL_LINK_P(pgsql_link);
1067 CHECK_PGSQL_LINK(
link);
1072 param = (
char*)PQparameterStatus(pgsql, param);
1086 pgsql_link_handle *
link;
1093 if (pgsql_link ==
NULL) {
1094 link = FETCH_DEFAULT_LINK();
1095 CHECK_DEFAULT_LINK(
link);
1097 link = Z_PGSQL_LINK_P(pgsql_link);
1098 CHECK_PGSQL_LINK(
link);
1104 res = PQexec(pgsql,
"SELECT 1;");
1108 if (PQstatus(pgsql) == CONNECTION_OK)
1113 if (PQstatus(pgsql) == CONNECTION_OK) {
1126 pgsql_link_handle *
link;
1128 PGresult *pgsql_result;
1136 link = FETCH_DEFAULT_LINK();
1137 CHECK_DEFAULT_LINK(
link);
1144 link = Z_PGSQL_LINK_P(pgsql_link);
1145 CHECK_PGSQL_LINK(
link);
1153 if (PQsetnonblocking(pgsql, 0)) {
1157 while ((pgsql_result = PQgetResult(pgsql))) {
1158 PQclear(pgsql_result);
1164 pgsql_result = PQexec(pgsql, query);
1165 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1166 PQclear(pgsql_result);
1168 pgsql_result = PQexec(pgsql, query);
1172 status = PQresultStatus(pgsql_result);
1174 status = (ExecStatusType) PQstatus(pgsql);
1178 case PGRES_EMPTY_QUERY:
1179 case PGRES_BAD_RESPONSE:
1180 case PGRES_NONFATAL_ERROR:
1181 case PGRES_FATAL_ERROR:
1182 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
1183 PQclear(pgsql_result);
1186 case PGRES_COMMAND_OK:
1195 PQclear(pgsql_result);
1202static void _php_pgsql_free_params(
char **params,
int num_params)
1204 if (num_params > 0) {
1206 for (i = 0; i < num_params; i++) {
1219 zval *pv_param_arr, *tmp;
1224 char **params =
NULL;
1225 pgsql_link_handle *
link;
1227 PGresult *pgsql_result;
1237 link = FETCH_DEFAULT_LINK();
1238 CHECK_DEFAULT_LINK(
link);
1246 link = Z_PGSQL_LINK_P(pgsql_link);
1247 CHECK_PGSQL_LINK(
link);
1255 if (PQsetnonblocking(pgsql, 0)) {
1259 while ((pgsql_result = PQgetResult(pgsql))) {
1260 PQclear(pgsql_result);
1267 num_params = zend_hash_num_elements(
Z_ARRVAL_P(pv_param_arr));
1268 if (num_params > 0) {
1270 params = (
char **)
safe_emalloc(
sizeof(
char *), num_params, 0);
1277 zend_string *param_str = zval_try_get_string(tmp);
1279 _php_pgsql_free_params(params, i);
1283 zend_string_release(param_str);
1289 pgsql_result = PQexecParams(pgsql, query, num_params,
1291 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1292 PQclear(pgsql_result);
1294 pgsql_result = PQexecParams(pgsql, query, num_params,
1299 status = PQresultStatus(pgsql_result);
1301 status = (ExecStatusType) PQstatus(pgsql);
1304 _php_pgsql_free_params(params, num_params);
1307 case PGRES_EMPTY_QUERY:
1308 case PGRES_BAD_RESPONSE:
1309 case PGRES_NONFATAL_ERROR:
1310 case PGRES_FATAL_ERROR:
1311 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
1312 PQclear(pgsql_result);
1315 case PGRES_COMMAND_OK:
1324 PQclear(pgsql_result);
1335 char *query, *stmtname;
1336 size_t query_len, stmtname_len;
1339 pgsql_link_handle *
link;
1340 PGresult *pgsql_result;
1350 link = FETCH_DEFAULT_LINK();
1351 CHECK_DEFAULT_LINK(
link);
1359 link = Z_PGSQL_LINK_P(pgsql_link);
1360 CHECK_PGSQL_LINK(
link);
1368 if (PQsetnonblocking(pgsql, 0)) {
1372 while ((pgsql_result = PQgetResult(pgsql))) {
1373 PQclear(pgsql_result);
1379 pgsql_result = PQprepare(pgsql, stmtname, query, 0,
NULL);
1380 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1381 PQclear(pgsql_result);
1383 pgsql_result = PQprepare(pgsql, stmtname, query, 0,
NULL);
1387 status = PQresultStatus(pgsql_result);
1389 status = (ExecStatusType) PQstatus(pgsql);
1393 case PGRES_EMPTY_QUERY:
1394 case PGRES_BAD_RESPONSE:
1395 case PGRES_NONFATAL_ERROR:
1396 case PGRES_FATAL_ERROR:
1397 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
1398 PQclear(pgsql_result);
1401 case PGRES_COMMAND_OK:
1410 PQclear(pgsql_result);
1421 zval *pv_param_arr, *tmp;
1423 size_t stmtname_len;
1426 char **params =
NULL;
1428 pgsql_link_handle *
link;
1429 PGresult *pgsql_result;
1439 link = FETCH_DEFAULT_LINK();
1440 CHECK_DEFAULT_LINK(
link);
1448 link = Z_PGSQL_LINK_P(pgsql_link);
1449 CHECK_PGSQL_LINK(
link);
1457 if (PQsetnonblocking(pgsql, 0)) {
1461 while ((pgsql_result = PQgetResult(pgsql))) {
1462 PQclear(pgsql_result);
1469 num_params = zend_hash_num_elements(
Z_ARRVAL_P(pv_param_arr));
1470 if (num_params > 0) {
1472 params = (
char **)
safe_emalloc(
sizeof(
char *), num_params, 0);
1480 zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
1483 zend_tmp_string_release(tmp_str);
1490 pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
1491 (
const char *
const *)params,
NULL,
NULL, 0);
1492 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
1493 PQclear(pgsql_result);
1495 pgsql_result = PQexecPrepared(pgsql, stmtname, num_params,
1496 (
const char *
const *)params,
NULL,
NULL, 0);
1500 status = PQresultStatus(pgsql_result);
1502 status = (ExecStatusType) PQstatus(pgsql);
1505 _php_pgsql_free_params(params, num_params);
1508 case PGRES_EMPTY_QUERY:
1509 case PGRES_BAD_RESPONSE:
1510 case PGRES_NONFATAL_ERROR:
1511 case PGRES_FATAL_ERROR:
1512 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
1513 PQclear(pgsql_result);
1516 case PGRES_COMMAND_OK:
1525 PQclear(pgsql_result);
1532#define PHP_PG_NUM_ROWS 1
1533#define PHP_PG_NUM_FIELDS 2
1534#define PHP_PG_CMD_TUPLES 3
1540 PGresult *pgsql_result;
1552 case PHP_PG_NUM_ROWS:
1555 case PHP_PG_NUM_FIELDS:
1558 case PHP_PG_CMD_TUPLES:
1589 pgsql_link_handle *
link;
1598 link = Z_PGSQL_LINK_P(pgsql_link);
1599 CHECK_PGSQL_LINK(
link);
1601 notices =
link->notices;
1605 zend_hash_internal_pointer_end(notices);
1606 if ((notice = zend_hash_get_current_data(notices)) ==
NULL) {
1635static inline bool is_valid_oid_string(
zend_string *oid, Oid *return_oid)
1638 *return_oid = (Oid) strtoul(
ZSTR_VAL(oid), &end_ptr, 10);
1642static zend_string *get_field_name(PGconn *pgsql, Oid oid)
1644 zend_string *
ret = zend_hash_index_find_ptr(&PGG(field_oids), oid);
1646 zend_string_addref(
ret);
1650 PGresult *
result = PQexec(pgsql,
"select oid,typname from pg_type");
1651 if (!
result || PQresultStatus(
result) != PGRES_TUPLES_OK) {
1658 int num_rows = PQntuples(
result);
1659 int oid_offset = PQfnumber(
result,
"oid");
1660 int name_offset = PQfnumber(
result,
"typname");
1661 for (
int i = 0; i < num_rows; i++) {
1662 char *tmp_oid_str = PQgetvalue(
result, i, oid_offset);
1673 Oid tmp_oid = strtoul(tmp_oid_str, &end_ptr, 10);
1676 zend_hash_index_update_ptr(&PGG(field_oids), tmp_oid,
name);
1677 if (!
ret && tmp_oid == oid) {
1692 bool return_oid = 0;
1709 if (fnum >= PQnfields(
pg_result->result)) {
1714 Oid oid = PQftable(
pg_result->result, (
int)fnum);
1715 if (InvalidOid == oid) {
1720 PGSQL_RETURN_OID(oid);
1723 zend_string *field_table = zend_hash_index_find_ptr(&PGG(table_oids), oid);
1730 smart_str_appends(&querystr,
"select relname from pg_class where oid=");
1731 smart_str_append_unsigned(&querystr, oid);
1732 smart_str_0(&querystr);
1735 smart_str_free(&querystr);
1736 if (!tmp_res || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
1743 char *table_name = PQgetvalue(tmp_res, 0, 0);
1749 field_table = zend_string_init(table_name,
strlen(table_name), 0);
1750 zend_hash_index_update_ptr(&PGG(table_oids), oid, field_table);
1757#define PHP_PG_FIELD_NAME 1
1758#define PHP_PG_FIELD_SIZE 2
1759#define PHP_PG_FIELD_TYPE 3
1760#define PHP_PG_FIELD_TYPE_OID 4
1767 PGresult *pgsql_result;
1786 if (field >= PQnfields(pgsql_result)) {
1791 switch (entry_type) {
1792 case PHP_PG_FIELD_NAME:
1795 case PHP_PG_FIELD_SIZE:
1798 case PHP_PG_FIELD_TYPE:
1801 case PHP_PG_FIELD_TYPE_OID:
1803 oid = PQftype(pgsql_result, (
int)field);
1804 PGSQL_RETURN_OID(oid);
1845 PGresult *pgsql_result;
1865 if (field_offset < 0) {
1872 if (field_offset < 0) {
1876 if (field_offset >= PQnfields(
result)) {
1881 return field_offset;
1890 bool row_is_null =
false;
1891 PGresult *pgsql_result;
1898 "use the 3-parameter signature with a null $row parameter instead");
1932 if (pgsql_row >= PQntuples(pgsql_result)) {
1941 if (row >= PQntuples(pgsql_result)) {
1946 pgsql_row = (int)row;
1949 field_offset = field_arg_to_offset(pgsql_result, field_name, field_offset,
ZEND_NUM_ARGS());
1950 if (field_offset < 0) {
1954 if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
1957 RETVAL_STRINGL(PQgetvalue(pgsql_result, pgsql_row, field_offset),
1958 PQgetlength(pgsql_result, pgsql_row, field_offset));
1967 PGresult *pgsql_result;
1969 int i, num_fields, pgsql_row;
1971 bool row_is_null =
true;
1998 if (!row_is_null && row < 0) {
2013 if (row >= PQntuples(pgsql_result)) {
2018 pgsql_row = (int)row;
2023 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2030 for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
2031 if (PQgetisnull(pgsql_result, pgsql_row, i)) {
2036 field_name = PQfname(pgsql_result, i);
2040 char *element = PQgetvalue(pgsql_result, pgsql_row, i);
2042 const size_t element_len =
strlen(element);
2049 field_name = PQfname(pgsql_result, i);
2050 add_assoc_stringl(
return_value, field_name, element, element_len);
2071 }
else if (ctor_params && zend_hash_num_elements(ctor_params) > 0) {
2073 "must be empty when the specified class (%s) does not have a constructor",
2120 PGresult *pgsql_result;
2139 php_pgsql_result2array(pgsql_result,
return_value, result_type);
2147 PGresult *pgsql_result;
2169 num_fields = PQnfields(pgsql_result);
2177 if ((
pg_numrows = PQntuples(pgsql_result)) <= 0) {
2181 for (pg_row = 0; pg_row <
pg_numrows; pg_row++) {
2182 if (PQgetisnull(pgsql_result, pg_row, (
int)colno)) {
2216#define PHP_PG_DATA_LENGTH 1
2217#define PHP_PG_DATA_ISNULL 2
2225 bool row_is_null =
false;
2226 PGresult *pgsql_result;
2233 "use the 3-parameter signature with a null $row parameter instead",
2268 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
2276 if (row >= PQntuples(pgsql_result)) {
2281 pgsql_row = (int)row;
2284 field_offset = field_arg_to_offset(pgsql_result, field_name, field_offset,
ZEND_NUM_ARGS());
2285 if (field_offset < 0) {
2289 switch (entry_type) {
2290 case PHP_PG_DATA_LENGTH:
2291 RETVAL_LONG(PQgetlength(pgsql_result, pgsql_row, field_offset));
2293 case PHP_PG_DATA_ISNULL:
2294 RETVAL_LONG(PQgetisnull(pgsql_result, pgsql_row, field_offset));
2351 PGresult *pgsql_result;
2363 oid = PQoidValue(pgsql_result);
2364 if (oid == InvalidOid) {
2367 PGSQL_RETURN_OID(oid);
2374 char *z_filename, *
mode =
"w";
2381 pgsql_link_handle *
link;
2392 link = FETCH_DEFAULT_LINK();
2393 CHECK_DEFAULT_LINK(
link);
2395 link = Z_PGSQL_LINK_P(pgsql_link);
2396 CHECK_PGSQL_LINK(
link);
2413 if (trace_mode > 0) {
2414#ifdef PQTRACE_REGRESS_MODE
2415 if (!(trace_mode & (PQTRACE_SUPPRESS_TIMESTAMPS|PQTRACE_REGRESS_MODE))) {
2419 PQsetTraceFlags(pgsql, trace_mode);
2435 pgsql_link_handle *
link;
2442 if (pgsql_link ==
NULL) {
2443 link = FETCH_DEFAULT_LINK();
2444 CHECK_DEFAULT_LINK(
link);
2446 link = Z_PGSQL_LINK_P(pgsql_link);
2447 CHECK_PGSQL_LINK(
link);
2462 Oid pgsql_oid, wanted_oid = InvalidOid;
2463 pgsql_link_handle *
link;
2477 if (pgsql_link ==
NULL) {
2478 link = FETCH_DEFAULT_LINK();
2479 CHECK_DEFAULT_LINK(
link);
2481 link = Z_PGSQL_LINK_P(pgsql_link);
2482 CHECK_PGSQL_LINK(
link);
2494 if (!is_valid_oid_string(
Z_STR_P(oid), &wanted_oid)) {
2512 if ((pgsql_oid = lo_create(pgsql, wanted_oid)) == InvalidOid) {
2517 PGSQL_RETURN_OID(pgsql_oid);
2520 if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {
2525 PGSQL_RETURN_OID(pgsql_oid);
2537 pgsql_link_handle *
link;
2541 if (!is_valid_oid_string(oid_string, &oid)) {
2546 link = Z_PGSQL_LINK_P(pgsql_link);
2547 CHECK_PGSQL_LINK(
link);
2550 "Ol", &pgsql_link, pgsql_link_ce, &oid_long) ==
SUCCESS) {
2551 if (oid_long <= (
zend_long)InvalidOid) {
2555 oid = (Oid)oid_long;
2556 link = Z_PGSQL_LINK_P(pgsql_link);
2557 CHECK_PGSQL_LINK(
link);
2560 if (!is_valid_oid_string(oid_string, &oid)) {
2565 link = FETCH_DEFAULT_LINK();
2566 CHECK_DEFAULT_LINK(
link);
2570 if (oid_long <= (
zend_long)InvalidOid) {
2574 oid = (Oid)oid_long;
2575 link = FETCH_DEFAULT_LINK();
2576 CHECK_DEFAULT_LINK(
link);
2585 if (lo_unlink(pgsql, oid) == -1) {
2602 int pgsql_mode=0, pgsql_lofd;
2603 bool create =
false;
2605 pgsql_link_handle *
link;
2609 "OSS", &pgsql_link, pgsql_link_ce, &oid_string, &
mode) ==
SUCCESS) {
2610 if (!is_valid_oid_string(oid_string, &oid)) {
2615 link = Z_PGSQL_LINK_P(pgsql_link);
2616 CHECK_PGSQL_LINK(
link);
2619 "OlS", &pgsql_link, pgsql_link_ce, &oid_long, &
mode) ==
SUCCESS) {
2620 if (oid_long <= (
zend_long)InvalidOid) {
2624 oid = (Oid)oid_long;
2625 link = Z_PGSQL_LINK_P(pgsql_link);
2626 CHECK_PGSQL_LINK(
link);
2630 if (!is_valid_oid_string(oid_string, &oid)) {
2635 link = FETCH_DEFAULT_LINK();
2636 CHECK_DEFAULT_LINK(
link);
2640 if (oid_long <= (
zend_long)InvalidOid) {
2644 oid = (Oid)oid_long;
2645 link = FETCH_DEFAULT_LINK();
2646 CHECK_DEFAULT_LINK(
link);
2660 pgsql_mode |= INV_READ;
2662 pgsql_mode |= INV_WRITE;
2665 pgsql_mode |= INV_READ;
2666 pgsql_mode |= INV_WRITE;
2668 pgsql_mode |= INV_READ;
2669 pgsql_mode |= INV_WRITE;
2676 if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
2678 if ((oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
2682 if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
2683 if (lo_unlink(pgsql, oid) == -1) {
2700 pgsql_lofp->conn = pgsql;
2701 pgsql_lofp->lofd = pgsql_lofd;
2715 pgsql = Z_PGSQL_LOB_P(pgsql_lofp);
2716 CHECK_PGSQL_LOB(pgsql);
2718 if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
2729#define PGSQL_LO_READ_BUF_SIZE 8192
2735 zend_long buffer_length = PGSQL_LO_READ_BUF_SIZE;
2746 pgsql = Z_PGSQL_LOB_P(pgsql_id);
2747 CHECK_PGSQL_LOB(pgsql);
2749 if (buffer_length < 0) {
2754 buf = zend_string_alloc(buffer_length, 0);
2756 zend_string_efree(
buf);
2761 buf = zend_string_truncate(
buf, nbytes, 0);
2772 bool z_len_is_null = 1;
2784 if (!z_len_is_null) {
2799 pgsql = Z_PGSQL_LOB_P(pgsql_id);
2800 CHECK_PGSQL_LOB(pgsql);
2802 if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd,
ZSTR_VAL(str),
len)) == (
size_t)-1) {
2815 volatile int nbytes;
2816 char buf[PGSQL_LO_READ_BUF_SIZE];
2823 pgsql = Z_PGSQL_LOB_P(pgsql_id);
2824 CHECK_PGSQL_LOB(pgsql);
2827 while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd,
buf, PGSQL_LO_READ_BUF_SIZE))>0) {
2842 pgsql_link_handle *
link;
2845 "OP|z", &pgsql_link, pgsql_link_ce, &file_in, &oid) ==
SUCCESS) {
2846 link = Z_PGSQL_LINK_P(pgsql_link);
2847 CHECK_PGSQL_LINK(
link);
2850 "P|z", &file_in, &oid) ==
SUCCESS) {
2851 link = FETCH_DEFAULT_LINK();
2852 CHECK_DEFAULT_LINK(
link);
2869 if (!is_valid_oid_string(
Z_STR_P(oid), &wanted_oid)) {
2888 returned_oid = lo_import_with_oid(pgsql,
ZSTR_VAL(file_in), wanted_oid);
2890 if (returned_oid == InvalidOid) {
2894 PGSQL_RETURN_OID(returned_oid);
2897 returned_oid = lo_import(pgsql,
ZSTR_VAL(file_in));
2899 if (returned_oid == InvalidOid) {
2902 PGSQL_RETURN_OID(returned_oid);
2915 pgsql_link_handle *
link;
2919 "OlP", &pgsql_link, pgsql_link_ce, &oid_long, &file_out) ==
SUCCESS) {
2920 if (oid_long <= (
zend_long)InvalidOid) {
2924 oid = (Oid)oid_long;
2925 link = Z_PGSQL_LINK_P(pgsql_link);
2926 CHECK_PGSQL_LINK(
link);
2929 "OSP", &pgsql_link, pgsql_link_ce, &oid_string, &file_out) ==
SUCCESS) {
2930 if (!is_valid_oid_string(oid_string, &oid)) {
2935 link = Z_PGSQL_LINK_P(pgsql_link);
2936 CHECK_PGSQL_LINK(
link);
2939 "lP", &oid_long, &file_out) ==
SUCCESS) {
2940 if (oid_long <= (
zend_long)InvalidOid) {
2944 oid = (Oid)oid_long;
2945 link = FETCH_DEFAULT_LINK();
2946 CHECK_DEFAULT_LINK(
link);
2949 "SP", &oid_string, &file_out) ==
SUCCESS) {
2950 if (!is_valid_oid_string(oid_string, &oid)) {
2955 link = FETCH_DEFAULT_LINK();
2956 CHECK_DEFAULT_LINK(
link);
2969 if (lo_export(pgsql, oid,
ZSTR_VAL(file_out)) == -1) {
2995 pgsql = Z_PGSQL_LOB_P(pgsql_id);
2996 CHECK_PGSQL_LOB(pgsql);
2998 if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
2999 result = lo_lseek64((PGconn *)pgsql->conn, pgsql->lofd,
offset, (
int)whence);
3001 result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, (
int)
offset, (
int)whence);
3023 pgsql = Z_PGSQL_LOB_P(pgsql_id);
3024 CHECK_PGSQL_LOB(pgsql);
3027 if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
3028 offset = lo_tell64((PGconn *)pgsql->conn, pgsql->lofd);
3030 offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
3033 offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
3052 pgsql = Z_PGSQL_LOB_P(pgsql_id);
3053 CHECK_PGSQL_LOB(pgsql);
3056 if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
3057 result = lo_truncate64((PGconn *)pgsql->conn, pgsql->lofd, size);
3059 result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size);
3062 result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd,
size);
3078 pgsql_link_handle *
link;
3085 link = FETCH_DEFAULT_LINK();
3086 CHECK_DEFAULT_LINK(
link);
3093 link = Z_PGSQL_LINK_P(pgsql_link);
3094 CHECK_PGSQL_LINK(
link);
3102 if (verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE|PQERRORS_SQLSTATE)) {
3103 RETURN_LONG(PQsetErrorVerbosity(pgsql, verbosity));
3115 pgsql_link_handle *
link;
3122 link = Z_PGSQL_LINK_P(pgsql_link);
3123 CHECK_PGSQL_LINK(
link);
3127 if (visibility == PQSHOW_CONTEXT_NEVER || visibility & (PQSHOW_CONTEXT_ERRORS|PQSHOW_CONTEXT_ALWAYS)) {
3128 RETURN_LONG(PQsetErrorContextVisibility(pgsql, visibility));
3130 zend_argument_value_error(2,
"must be one of PGSQL_SHOW_CONTEXT_NEVER, PGSQL_SHOW_CONTEXT_ERRORS or PGSQL_SHOW_CONTEXT_ALWAYS");
3135#ifdef HAVE_PG_RESULT_MEMORY_SIZE
3156 size_t encoding_len;
3159 pgsql_link_handle *
link;
3166 link = FETCH_DEFAULT_LINK();
3167 CHECK_DEFAULT_LINK(
link);
3174 link = Z_PGSQL_LINK_P(pgsql_link);
3175 CHECK_PGSQL_LINK(
link);
3192 pgsql_link_handle *
link;
3199 if (pgsql_link ==
NULL) {
3200 link = FETCH_DEFAULT_LINK();
3201 CHECK_DEFAULT_LINK(
link);
3203 link = Z_PGSQL_LINK_P(pgsql_link);
3204 CHECK_PGSQL_LINK(
link);
3211 RETURN_STRING((
char *) pg_encoding_to_char(PQclientEncoding(pgsql)));
3221 pgsql_link_handle *
link;
3228 if (pgsql_link ==
NULL) {
3229 link = FETCH_DEFAULT_LINK();
3230 CHECK_DEFAULT_LINK(
link);
3232 link = Z_PGSQL_LINK_P(pgsql_link);
3233 CHECK_PGSQL_LINK(
link);
3238 result = PQendcopy(pgsql);
3241 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
3255 pgsql_link_handle *
link;
3263 link = FETCH_DEFAULT_LINK();
3264 CHECK_DEFAULT_LINK(
link);
3271 link = Z_PGSQL_LINK_P(pgsql_link);
3272 CHECK_PGSQL_LINK(
link);
3280 result = PQputline(pgsql, query);
3282 PHP_PQ_ERROR(
"Query failed: %s", pgsql);
3293 pgsql_link_handle *
link;
3296 char *pg_null_as =
NULL;
3297 size_t pg_null_as_len = 0;
3298 bool free_pg_null =
false;
3301 PGresult *pgsql_result;
3303 char *csv = (
char *)
NULL;
3313 link = Z_PGSQL_LINK_P(pgsql_link);
3314 CHECK_PGSQL_LINK(
link);
3317 if (!pg_delimiter) {
3319 }
else if (
ZSTR_LEN(pg_delimiter) != 1) {
3324 pg_null_as =
estrdup(
"\\\\N");
3325 free_pg_null =
true;
3328 spprintf(&query, 0,
"COPY %s TO STDOUT DELIMITER E'%c' NULL AS E'%s'",
ZSTR_VAL(table_name), *
ZSTR_VAL(pg_delimiter), pg_null_as);
3330 while ((pgsql_result = PQgetResult(pgsql))) {
3331 PQclear(pgsql_result);
3333 pgsql_result = PQexec(pgsql, query);
3340 status = PQresultStatus(pgsql_result);
3342 status = (ExecStatusType) PQstatus(pgsql);
3346 case PGRES_COPY_OUT:
3350 PQclear(pgsql_result);
3354 int ret = PQgetCopyData(pgsql, &csv, 0);
3361 PHP_PQ_ERROR(
"getline failed: %s", pgsql);
3370 while ((pgsql_result = PQgetResult(pgsql))) {
3371 PQclear(pgsql_result);
3374 PQclear(pgsql_result);
3379 PQclear(pgsql_result);
3380 PHP_PQ_ERROR(
"Copy command failed: %s", pgsql);
3391 pgsql_link_handle *
link;
3395 char *pg_null_as =
NULL;
3396 size_t pg_null_as_len;
3397 bool pg_null_as_free =
false;
3400 PGresult *pgsql_result;
3412 link = Z_PGSQL_LINK_P(pgsql_link);
3413 CHECK_PGSQL_LINK(
link);
3416 if (!pg_delimiter) {
3418 }
else if (
ZSTR_LEN(pg_delimiter) != 1) {
3423 pg_null_as =
estrdup(
"\\\\N");
3424 pg_null_as_free =
true;
3427 spprintf(&query, 0,
"COPY %s FROM STDIN DELIMITER E'%c' NULL AS E'%s'",
ZSTR_VAL(table_name), *
ZSTR_VAL(pg_delimiter), pg_null_as);
3428 while ((pgsql_result = PQgetResult(pgsql))) {
3429 PQclear(pgsql_result);
3431 pgsql_result = PQexec(pgsql, query);
3433 if (pg_null_as_free) {
3439 status = PQresultStatus(pgsql_result);
3441 status = (ExecStatusType) PQstatus(pgsql);
3447 int command_failed = 0;
3448 PQclear(pgsql_result);
3465 zend_string_release(tmp);
3466 PHP_PQ_ERROR(
"copy failed: %s", pgsql);
3470 zend_string_release(tmp);
3473 if (PQputCopyEnd(pgsql,
NULL) != 1) {
3474 PHP_PQ_ERROR(
"putcopyend failed: %s", pgsql);
3477 while ((pgsql_result = PQgetResult(pgsql))) {
3478 if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
3479 PHP_PQ_ERROR(
"Copy command failed: %s", pgsql);
3482 PQclear(pgsql_result);
3484 if (command_failed) {
3488 PQclear(pgsql_result);
3494 PQclear(pgsql_result);
3495 PHP_PQ_ERROR(
"Copy command failed: %s", pgsql);
3507 pgsql_link_handle *
link;
3516 link = FETCH_DEFAULT_LINK();
3524 link = Z_PGSQL_LINK_P(pgsql_link);
3525 CHECK_PGSQL_LINK(
link);
3529 to = zend_string_safe_alloc(
ZSTR_LEN(from), 2, 0, 0);
3538 to = zend_string_truncate(to,
ZSTR_LEN(to), 0);
3551 pgsql_link_handle *
link;
3559 link = FETCH_DEFAULT_LINK();
3567 link = Z_PGSQL_LINK_P(pgsql_link);
3568 CHECK_PGSQL_LINK(
link);
3574 to = (
char *)PQescapeByteaConn(pgsql, (
unsigned char *)
ZSTR_VAL(from),
ZSTR_LEN(from), &to_len);
3576 to = (
char *)PQescapeBytea((
unsigned char *)
ZSTR_VAL(from),
ZSTR_LEN(from), &to_len);
3595 tmp = (
char *)PQunescapeBytea((
unsigned char*)from, &to_len);
3611 pgsql_link_handle *
link;
3619 link = FETCH_DEFAULT_LINK();
3620 CHECK_DEFAULT_LINK(
link);
3629 link = Z_PGSQL_LINK_P(pgsql_link);
3630 CHECK_PGSQL_LINK(
link);
3636 if (escape_literal) {
3669 PGresult *pgsql_result;
3679 if (!pgsql_result) {
3683 err = PQresultErrorMessage(pgsql_result);
3693 PGresult *pgsql_result;
3704 if (!pgsql_result) {
3708 if (fieldcode & (PG_DIAG_SEVERITY|PG_DIAG_SQLSTATE|PG_DIAG_MESSAGE_PRIMARY|PG_DIAG_MESSAGE_DETAIL
3709 |PG_DIAG_MESSAGE_HINT|PG_DIAG_STATEMENT_POSITION
3710#ifdef PG_DIAG_INTERNAL_POSITION
3711 |PG_DIAG_INTERNAL_POSITION
3713#ifdef PG_DIAG_INTERNAL_QUERY
3714 |PG_DIAG_INTERNAL_QUERY
3716 |PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
3717 |PG_DIAG_SOURCE_FUNCTION)) {
3718 field = PQresultErrorField(pgsql_result, (
int)fieldcode);
3719 if (field ==
NULL) {
3734 pgsql_link_handle *
link;
3741 link = Z_PGSQL_LINK_P(pgsql_link);
3742 CHECK_PGSQL_LINK(
link);
3754 pgsql_link_handle *
link;
3761 link = Z_PGSQL_LINK_P(pgsql_link);
3762 CHECK_PGSQL_LINK(
link);
3774 pgsql_link_handle *
link;
3781 link = Z_PGSQL_LINK_P(pgsql_link);
3782 CHECK_PGSQL_LINK(
link);
3786 if (PQstatus(pgsql) == CONNECTION_BAD) {
3793#define PHP_PG_ASYNC_IS_BUSY 1
3794#define PHP_PG_ASYNC_REQUEST_CANCEL 2
3797static int php_pgsql_flush_query(PGconn *pgsql)
3802 if (PQsetnonblocking(pgsql, 1)) {
3806 while ((
res = PQgetResult(pgsql))) {
3810 PQsetnonblocking(pgsql, 0);
3819 pgsql_link_handle *
link;
3821 PGresult *pgsql_result;
3827 link = Z_PGSQL_LINK_P(pgsql_link);
3828 CHECK_PGSQL_LINK(
link);
3831 if (PQsetnonblocking(pgsql, 1)) {
3835 switch(entry_type) {
3836 case PHP_PG_ASYNC_IS_BUSY:
3837 PQconsumeInput(pgsql);
3840 case PHP_PG_ASYNC_REQUEST_CANCEL: {
3845 c = PQgetCancel(pgsql);
3850 while ((pgsql_result = PQgetResult(pgsql))) {
3851 PQclear(pgsql_result);
3858 if (PQsetnonblocking(pgsql, 0)) {
3879static bool _php_pgsql_link_has_results(PGconn *pgsql)
3882 while ((
result = PQgetResult(pgsql))) {
3894 pgsql_link_handle *
link;
3898 int is_non_blocking;
3906 link = Z_PGSQL_LINK_P(pgsql_link);
3907 CHECK_PGSQL_LINK(
link);
3910 is_non_blocking = PQisnonblocking(pgsql);
3912 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
3917 if (_php_pgsql_link_has_results(pgsql)) {
3919 "There are results on this connection. Call pg_get_result() until it returns FALSE");
3922 if (is_non_blocking) {
3923 if (!PQsendQuery(pgsql, query)) {
3926 ret = PQflush(pgsql);
3928 if (!PQsendQuery(pgsql, query)) {
3929 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
3932 if (!PQsendQuery(pgsql, query)) {
3938 while ((
ret = PQflush(pgsql))) {
3946 if (PQsetnonblocking(pgsql, 0)) {
3953 }
else if (
ret == -1) {
3964 zval *pgsql_link, *pv_param_arr, *tmp;
3965 pgsql_link_handle *
link;
3967 char **params =
NULL;
3971 int is_non_blocking;
3980 link = Z_PGSQL_LINK_P(pgsql_link);
3981 CHECK_PGSQL_LINK(
link);
3984 is_non_blocking = PQisnonblocking(pgsql);
3986 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
3991 if (_php_pgsql_link_has_results(pgsql)) {
3993 "There are results on this connection. Call pg_get_result() until it returns FALSE");
3996 num_params = zend_hash_num_elements(
Z_ARRVAL_P(pv_param_arr));
3997 if (num_params > 0) {
3999 params = (
char **)
safe_emalloc(
sizeof(
char *), num_params, 0);
4007 zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
4010 zend_tmp_string_release(tmp_str);
4017 if (PQsendQueryParams(pgsql, query, num_params,
NULL, (
const char *
const *)params,
NULL,
NULL, 0)) {
4018 _php_pgsql_free_params(params, num_params);
4019 }
else if (is_non_blocking) {
4020 _php_pgsql_free_params(params, num_params);
4023 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4026 if (!PQsendQueryParams(pgsql, query, num_params,
NULL, (
const char *
const *)params,
NULL,
NULL, 0)) {
4027 _php_pgsql_free_params(params, num_params);
4032 if (is_non_blocking) {
4033 ret = PQflush(pgsql);
4036 while ((
ret = PQflush(pgsql))) {
4044 if (PQsetnonblocking(pgsql, 0) != 0) {
4051 }
else if (
ret == -1) {
4063 pgsql_link_handle *
link;
4064 char *query, *stmtname;
4065 size_t stmtname_len, query_len;
4067 int is_non_blocking;
4076 link = Z_PGSQL_LINK_P(pgsql_link);
4077 CHECK_PGSQL_LINK(
link);
4080 is_non_blocking = PQisnonblocking(pgsql);
4082 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
4087 if (_php_pgsql_link_has_results(pgsql)) {
4089 "There are results on this connection. Call pg_get_result() until it returns FALSE");
4092 if (!PQsendPrepare(pgsql, stmtname, query, 0,
NULL)) {
4093 if (is_non_blocking) {
4096 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4099 if (!PQsendPrepare(pgsql, stmtname, query, 0,
NULL)) {
4105 if (is_non_blocking) {
4106 ret = PQflush(pgsql);
4109 while ((
ret = PQflush(pgsql))) {
4116 if (PQsetnonblocking(pgsql, 0) != 0) {
4123 }
else if (
ret == -1) {
4135 pgsql_link_handle *
link;
4136 zval *pv_param_arr, *tmp;
4138 char **params =
NULL;
4140 size_t stmtname_len;
4142 int is_non_blocking;
4151 link = Z_PGSQL_LINK_P(pgsql_link);
4152 CHECK_PGSQL_LINK(
link);
4155 is_non_blocking = PQisnonblocking(pgsql);
4157 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
4162 if (_php_pgsql_link_has_results(pgsql)) {
4164 "There are results on this connection. Call pg_get_result() until it returns FALSE");
4167 num_params = zend_hash_num_elements(
Z_ARRVAL_P(pv_param_arr));
4168 if (num_params > 0) {
4170 params = (
char **)
safe_emalloc(
sizeof(
char *), num_params, 0);
4179 _php_pgsql_free_params(params, i);
4183 zend_string_release(tmp_str);
4190 if (PQsendQueryPrepared(pgsql, stmtname, num_params, (
const char *
const *)params,
NULL,
NULL, 0)) {
4191 _php_pgsql_free_params(params, num_params);
4192 }
else if (is_non_blocking) {
4193 _php_pgsql_free_params(params, num_params);
4196 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
4199 if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (
const char *
const *)params,
NULL,
NULL, 0)) {
4200 _php_pgsql_free_params(params, num_params);
4205 if (is_non_blocking) {
4206 ret = PQflush(pgsql);
4209 while ((
ret = PQflush(pgsql))) {
4216 if (PQsetnonblocking(pgsql, 0) != 0) {
4223 }
else if (
ret == -1) {
4235 pgsql_link_handle *
link;
4237 PGresult *pgsql_result;
4244 link = Z_PGSQL_LINK_P(pgsql_link);
4245 CHECK_PGSQL_LINK(
link);
4248 pgsql_result = PQgetResult(pgsql);
4249 if (!pgsql_result) {
4268 PGresult *pgsql_result;
4282 status = PQresultStatus(pgsql_result);
4298 pgsql_link_handle *
link;
4301 PGnotify *pgsql_notify;
4309 link = Z_PGSQL_LINK_P(pgsql_link);
4310 CHECK_PGSQL_LINK(
link);
4318 PQconsumeInput(pgsql);
4319 pgsql_notify = PQnotifies(pgsql);
4320 if (!pgsql_notify) {
4329 if (
zend_strtod(PQparameterStatus(pgsql,
"server_version"),
NULL) >= 9.0) {
4334 add_assoc_string(
return_value,
"message", pgsql_notify->relname);
4335 add_assoc_long(
return_value,
"pid", pgsql_notify->be_pid);
4337 if (
zend_strtod(PQparameterStatus(pgsql,
"server_version"),
NULL) >= 9.0) {
4338 add_assoc_string(
return_value,
"payload", pgsql_notify->extra);
4341 PQfreemem(pgsql_notify);
4349 pgsql_link_handle *
link;
4356 link = Z_PGSQL_LINK_P(pgsql_link);
4357 CHECK_PGSQL_LINK(
link);
4376static int php_pgsql_fd_close(
php_stream *stream,
int close_handle)
4382static int php_pgsql_fd_flush(
php_stream *stream)
4388static int php_pgsql_fd_set_option(
php_stream *stream,
int option,
int value,
void *ptrparam)
4390 PGconn *pgsql = (PGconn *) stream->
abstract;
4393 return PQsetnonblocking(pgsql,
value);
4400static int php_pgsql_fd_cast(
php_stream *stream,
int cast_as,
void **
ret)
4402 PGconn *pgsql = (PGconn *) stream->
abstract;
4408 int fd_number = PQsocket(pgsql);
4409 if (fd_number == -1) {
4429 pgsql_link_handle *
link;
4437 link = Z_PGSQL_LINK_P(pgsql_link);
4438 CHECK_PGSQL_LINK(
link);
4456 pgsql_link_handle *
link;
4463 link = Z_PGSQL_LINK_P(pgsql_link);
4464 CHECK_PGSQL_LINK(
link);
4475 pgsql_link_handle *
link;
4478 int is_non_blocking;
4484 link = Z_PGSQL_LINK_P(pgsql_link);
4485 CHECK_PGSQL_LINK(
link);
4488 is_non_blocking = PQisnonblocking(pgsql);
4490 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 1) == -1) {
4495 ret = PQflush(pgsql);
4497 if (is_non_blocking == 0 && PQsetnonblocking(pgsql, 0) == -1) {
4519 size_t new_len,
len;
4532 if (!tmp_name2 || !*tmp_name2) {
4539 smart_str_appends(&querystr,
4540 "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef, a.attndims, t.typtype, "
4542 "FROM pg_class as c "
4543 " JOIN pg_attribute a ON (a.attrelid = c.oid) "
4544 " JOIN pg_type t ON (a.atttypid = t.oid) "
4545 " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
4546 " LEFT JOIN pg_description d ON (d.objoid=a.attrelid AND d.objsubid=a.attnum AND c.oid=d.objoid) "
4547 "WHERE a.attnum > 0 AND c.relname = '");
4549 smart_str_appends(&querystr,
4550 "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotnull, a.atthasdef, a.attndims, t.typtype "
4551 "FROM pg_class as c "
4552 " JOIN pg_attribute a ON (a.attrelid = c.oid) "
4553 " JOIN pg_type t ON (a.atttypid = t.oid) "
4554 " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
4555 "WHERE a.attnum > 0 AND c.relname = '");
4559 new_len = PQescapeStringConn(pg_link, escaped, tmp_name2,
len,
NULL);
4561 smart_str_appendl(&querystr, escaped, new_len);
4565 smart_str_appends(&querystr,
"' AND n.nspname = '");
4570 smart_str_appendl(&querystr, escaped, new_len);
4574 smart_str_appends(&querystr,
"' ORDER BY a.attnum;");
4575 smart_str_0(&querystr);
4579 if (PQresultStatus(
pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(
pg_result)) == 0) {
4581 smart_str_free(&querystr);
4585 smart_str_free(&querystr);
4587 for (i = 0; i < num_rows; i++) {
4614 add_assoc_zval(meta,
name, &elem);
4627 pgsql_link_handle *
link;
4639 link = Z_PGSQL_LINK_P(pgsql_link);
4640 CHECK_PGSQL_LINK(
link);
4658static php_pgsql_data_type php_pgsql_get_data_type(
const zend_string *type_name)
4663 if (zend_string_equals(type_name,
ZSTR_KNOWN(ZEND_STR_BOOL)) ||zend_string_equals(type_name,
ZSTR_KNOWN(ZEND_STR_BOOLEAN)))
4693 return PG_UNIX_TIME;
4695 return PG_UNIX_TIME_INTERVAL;
4697 return PG_UNIX_TIME_INTERVAL;
4703 return PG_TIME_WITH_TIMEZONE;
4705 return PG_TIMESTAMP;
4707 return PG_TIMESTAMP_WITH_TIMEZONE;
4757 for (i = 0; i <
ZSTR_LEN(str); i++) {
4766 if (
NULL == centry) {
4772 if (
NULL == match_data) {
4783 }
else if (
res < 0) {
4802#define PGSQL_CONV_CHECK_IGNORE() \
4803 if (!err && Z_TYPE(new_val) == IS_STRING && zend_string_equals_literal(Z_STR(new_val), "NULL")) { \
4805 if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_TYPE_P(has_default) == IS_TRUE) { \
4806 zval_ptr_dtor(&new_val); \
4810 else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_TYPE_P(not_null) == IS_TRUE) { \
4811 php_error_docref(NULL, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'", ZSTR_VAL(field)); \
4822 zval meta, *def, *
type, *not_null, *has_default, *is_enum, *
val, new_val;
4823 int err = 0, skip_field;
4824 php_pgsql_data_type data_type;
4836 if (php_pgsql_meta_data(pg_link, table_name, &meta, 0) ==
FAILURE) {
4849 zend_value_error(
"Array of values must be an associative array with string keys");
4884 data_type = PG_TEXT;
4886 data_type = php_pgsql_get_data_type(
Z_STR_P(
type));
4943 PGSQL_CONV_CHECK_IGNORE();
4985 PGSQL_CONV_CHECK_IGNORE();
5030 PGSQL_CONV_CHECK_IGNORE();
5070 ZVAL_STR(&new_val, php_pgsql_add_quotes(str));
5091 PGSQL_CONV_CHECK_IGNORE();
5098 case PG_UNIX_TIME_INTERVAL:
5133 PGSQL_CONV_CHECK_IGNORE();
5167 PGSQL_CONV_CHECK_IGNORE();
5177 case PG_TIME_WITH_TIMEZONE:
5179 case PG_TIMESTAMP_WITH_TIMEZONE:
5203 PGSQL_CONV_CHECK_IGNORE();
5233 PGSQL_CONV_CHECK_IGNORE();
5263 PGSQL_CONV_CHECK_IGNORE();
5310 PGSQL_CONV_CHECK_IGNORE();
5327 tmp_zstr = zend_string_init((
char *)tmp, to_len - 1,
false);
5330 ZVAL_STR(&new_val, php_pgsql_add_quotes(tmp_zstr));
5351 PGSQL_CONV_CHECK_IGNORE();
5380 PGSQL_CONV_CHECK_IGNORE();
5402 char *escaped = PQescapeIdentifier(pg_link,
ZSTR_VAL(field),
ZSTR_LEN(field));
5403 add_assoc_zval(
result, escaped, &new_val);
5422 zval *pgsql_link, *values;
5423 pgsql_link_handle *
link;
5441 if (option & ~PGSQL_CONV_OPTS) {
5443 "PGSQL_CONV_FORCE_NULL, and PGSQL_CONV_IGNORE_NOT_NULL");
5447 link = Z_PGSQL_LINK_P(pgsql_link);
5448 CHECK_PGSQL_LINK(
link);
5449 pg_link =
link->conn;
5451 if (php_pgsql_flush_query(pg_link)) {
5462static bool do_exec(
smart_str *querystr, ExecStatusType expect, PGconn *pg_link,
zend_ulong opt)
5465 if (PQsendQuery(pg_link,
ZSTR_VAL(querystr->
s))) {
5472 if (PQresultStatus(
pg_result) == expect) {
5485static inline void build_tablename(
smart_str *querystr, PGconn *pg_link,
const zend_string *table)
5491 if (_php_pgsql_identifier_is_escaped(
ZSTR_VAL(table),
len)) {
5492 smart_str_appendl(querystr,
ZSTR_VAL(table),
len);
5494 char *escaped = PQescapeIdentifier(pg_link,
ZSTR_VAL(table),
len);
5495 smart_str_appends(querystr, escaped);
5499 const char *after_dot = dot + 1;
5502 if (_php_pgsql_identifier_is_escaped(after_dot,
len)) {
5503 smart_str_appendc(querystr,
'.');
5504 smart_str_appendl(querystr, after_dot,
len);
5506 char *escaped = PQescapeIdentifier(pg_link, after_dot,
len);
5507 smart_str_appendc(querystr,
'.');
5508 smart_str_appends(querystr, escaped);
5530 if (zend_hash_num_elements(
Z_ARRVAL_P(var_array)) == 0) {
5531 smart_str_appends(&querystr,
"INSERT INTO ");
5532 build_tablename(&querystr, pg_link, table);
5533 smart_str_appends(&querystr,
" DEFAULT VALUES");
5541 if (php_pgsql_convert(pg_link, table, var_array, &converted, (opt & PGSQL_CONV_OPTS)) ==
FAILURE) {
5544 var_array = &converted;
5547 smart_str_appends(&querystr,
"INSERT INTO ");
5548 build_tablename(&querystr, pg_link, table);
5549 smart_str_appends(&querystr,
" (");
5553 zend_value_error(
"Array of values must be an associative array with string keys");
5558 smart_str_appends(&querystr, tmp);
5561 smart_str_append(&querystr, fld);
5563 smart_str_appendc(&querystr,
',');
5566 smart_str_appends(&querystr,
") VALUES (");
5578 smart_str_appendc(&querystr,
'\'');
5579 smart_str_appendl(&querystr, tmp, new_len);
5580 smart_str_appendc(&querystr,
'\'');
5593 smart_str_appendl(&querystr,
"NULL",
sizeof(
"NULL")-1);
5599 smart_str_appendc(&querystr,
',');
5603 smart_str_appends(&querystr,
");");
5607 smart_str_0(&querystr);
5610 do_exec(&querystr, PGRES_COMMAND_OK, pg_link, (opt & PGSQL_CONV_OPTS))) {
5623 smart_str_free(&querystr);
5632 zval *pgsql_link, *values;
5633 pgsql_link_handle *
link;
5656 "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
5660 link = Z_PGSQL_LINK_P(pgsql_link);
5661 CHECK_PGSQL_LINK(
link);
5662 pg_link =
link->conn;
5664 if (php_pgsql_flush_query(pg_link)) {
5675 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) {
5685 status = (ExecStatusType) PQstatus(pg_link);
5689 case PGRES_EMPTY_QUERY:
5690 case PGRES_BAD_RESPONSE:
5691 case PGRES_NONFATAL_ERROR:
5692 case PGRES_FATAL_ERROR:
5693 PHP_PQ_ERROR(
"Query failed: %s", pg_link);
5697 case PGRES_COMMAND_OK:
5701 pgsql_result_handle *pg_res = Z_PGSQL_RESULT_P(
return_value);
5702 pg_res->conn = pg_link;
5712 }
else if (php_pgsql_insert(pg_link, table, values, option, &sql) ==
FAILURE) {
5723static inline int build_assignment_string(PGconn *pg_link,
smart_str *querystr,
HashTable *
ht,
int where_cond,
const char *pad,
int pad_len,
zend_ulong opt)
5730 zend_value_error(
"Array of values must be an associative array with string keys");
5735 smart_str_appends(querystr, tmp);
5738 smart_str_append(querystr, fld);
5742 smart_str_appends(querystr,
" IS ");
5744 smart_str_appendc(querystr,
'=');
5752 smart_str_appendc(querystr,
'\'');
5753 smart_str_appendl(querystr, tmp, new_len);
5754 smart_str_appendc(querystr,
'\'');
5769 smart_str_appendl(querystr,
"NULL",
sizeof(
"NULL")-1);
5775 smart_str_appendl(querystr, pad, pad_len);
5788 zval var_converted, ids_converted;
5798 if (zend_hash_num_elements(
Z_ARRVAL_P(var_array)) == 0
5799 || zend_hash_num_elements(
Z_ARRVAL_P(ids_array)) == 0) {
5807 if (php_pgsql_convert(pg_link, table, var_array, &var_converted, (opt & PGSQL_CONV_OPTS)) ==
FAILURE) {
5810 var_array = &var_converted;
5812 if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) ==
FAILURE) {
5815 ids_array = &ids_converted;
5818 smart_str_appends(&querystr,
"UPDATE ");
5819 build_tablename(&querystr, pg_link, table);
5820 smart_str_appends(&querystr,
" SET ");
5822 if (build_assignment_string(pg_link, &querystr,
Z_ARRVAL_P(var_array), 0,
",", 1, opt))
5825 smart_str_appends(&querystr,
" WHERE ");
5827 if (build_assignment_string(pg_link, &querystr,
Z_ARRVAL_P(ids_array), 1,
" AND ",
sizeof(
" AND ")-1, opt))
5830 smart_str_appendc(&querystr,
';');
5831 smart_str_0(&querystr);
5833 if ((opt &
PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt)) {
5846 smart_str_free(&querystr);
5855 zval *pgsql_link, *values, *ids;
5856 pgsql_link_handle *
link;
5878 "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
5882 link = Z_PGSQL_LINK_P(pgsql_link);
5883 CHECK_PGSQL_LINK(
link);
5884 pg_link =
link->conn;
5886 if (php_pgsql_flush_query(pg_link)) {
5889 if (php_pgsql_update(pg_link, table, values, ids, option, &sql) ==
FAILURE) {
5911 if (zend_hash_num_elements(
Z_ARRVAL_P(ids_array)) == 0) {
5918 if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) ==
FAILURE) {
5921 ids_array = &ids_converted;
5924 smart_str_appends(&querystr,
"DELETE FROM ");
5925 build_tablename(&querystr, pg_link, table);
5926 smart_str_appends(&querystr,
" WHERE ");
5928 if (build_assignment_string(pg_link, &querystr,
Z_ARRVAL_P(ids_array), 1,
" AND ",
sizeof(
" AND ")-1, opt))
5931 smart_str_appendc(&querystr,
';');
5932 smart_str_0(&querystr);
5934 if ((opt &
PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt)) {
5946 smart_str_free(&querystr);
5955 zval *pgsql_link, *ids;
5956 pgsql_link_handle *
link;
5977 "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
5981 link = Z_PGSQL_LINK_P(pgsql_link);
5982 CHECK_PGSQL_LINK(
link);
5983 pg_link =
link->conn;
5985 if (php_pgsql_flush_query(pg_link)) {
5988 if (php_pgsql_delete(pg_link, table, ids, option, &sql) ==
FAILURE) {
5999PHP_PGSQL_API
void php_pgsql_result2array(PGresult *
pg_result,
zval *ret_array,
long result_type)
6010 for (pg_row = 0; pg_row <
pg_numrows; pg_row++) {
6012 for (i = 0, num_fields = PQnfields(
pg_result); i < num_fields; i++) {
6014 if (PQgetisnull(
pg_result, pg_row, i)) {
6016 add_assoc_null(&row, field_name);
6022 char *element = PQgetvalue(
pg_result, pg_row, i);
6024 const size_t element_len =
strlen(element);
6026 add_assoc_stringl(&row, field_name, element, element_len);
6034 add_index_zval(ret_array, pg_row, &row);
6055 zend_bool is_valid_ids_array = ids_array && zend_hash_num_elements(
Z_ARRVAL_P(ids_array)) != 0;
6057 if (is_valid_ids_array) {
6061 if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS)) ==
FAILURE) {
6064 ids_array = &ids_converted;
6068 smart_str_appends(&querystr,
"SELECT * FROM ");
6069 build_tablename(&querystr, pg_link, table);
6071 if (is_valid_ids_array) {
6072 smart_str_appends(&querystr,
" WHERE ");
6073 if (build_assignment_string(pg_link, &querystr,
Z_ARRVAL_P(ids_array), 1,
" AND ",
sizeof(
" AND ")-1, opt)) {
6078 smart_str_appendc(&querystr,
';');
6079 smart_str_0(&querystr);
6082 if (PQresultStatus(
pg_result) == PGRES_TUPLES_OK) {
6083 php_pgsql_result2array(
pg_result, ret_array, result_type);
6091 if (is_valid_ids_array) {
6098 smart_str_free(&querystr);
6109 pgsql_link_handle *
link;
6133 "PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC, and PGSQL_DML_STRING");
6141 link = Z_PGSQL_LINK_P(pgsql_link);
6142 CHECK_PGSQL_LINK(
link);
6143 pg_link =
link->conn;
6145 if (php_pgsql_flush_query(pg_link)) {
6149 if (php_pgsql_select(pg_link, table, ids,
return_value, option, result_type, &sql) ==
FAILURE) {
6164 pgsql_link_handle *
link;
6185 link = Z_PGSQL_LINK_P(pgsql_link);
6186 CHECK_PGSQL_LINK(
link);
6198 pgsql_link_handle *
link;
6206 link = Z_PGSQL_LINK_P(pgsql_link);
6207 CHECK_PGSQL_LINK(
link);
6217 pgsql_link_handle *
link;
6227 link = Z_PGSQL_LINK_P(pgsql_link);
6228 CHECK_PGSQL_LINK(
link);
6263#if defined(HAVE_PG_SET_CHUNKED_ROWS_SIZE)
6267 pgsql_link_handle *
link;
6280 link = Z_PGSQL_LINK_P(pgsql_link);
6281 CHECK_PGSQL_LINK(
link);
usleep(int $microseconds)
count(Countable|array $value, int $mode=COUNT_NORMAL)
link(string $target, string $link)
memset(ptr, 0, type->size)
zend_ffi_ctype_name_buf buf
PHPAPI int php_check_open_basedir(const char *path)
foreach($dp as $el) foreach( $dp as $el) if( $pass2< 2) echo ""
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
#define PCRE2_ERROR_NOMATCH
php_info_print_table_start()
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
pg_get_result(PgSql\Connection $connection)
pg_trace(string $filename, string $mode="w", ?PgSql\Connection $connection=null, int $trace_mode=0)
pg_fetch_result(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_lo_close(PgSql\Lob $lob)
pg_parameter_status($connection, string $name=UNKNOWN)
pg_lo_seek(PgSql\Lob $lob, int $offset, int $whence=SEEK_CUR)
pg_unescape_bytea(string $string)
pg_field_prtlen(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_pconnect(string $connection_string, int $flags=0)
const PGSQL_CONNECT_ASYNC
const PGSQL_CONV_FORCE_NULL
pg_result(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_select(PgSql\Connection $connection, string $table_name, array $conditions=[], int $flags=PGSQL_DML_EXEC, int $mode=PGSQL_ASSOC)
pg_lo_unlink($connection, $oid=UNKNOWN)
pg_untrace(?PgSql\Connection $connection=null)
pg_prepare($connection, string $statement_name, string $query=UNKNOWN)
pg_close(?PgSql\Connection $connection=null)
pg_field_name(PgSql\Result $result, int $field)
pg_fetch_all_columns(PgSql\Result $result, int $field=0)
pg_set_error_verbosity($connection, int $verbosity=UNKNOWN)
pg_fieldprtlen(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_field_type(PgSql\Result $result, int $field)
pg_query($connection, string $query=UNKNOWN)
pg_lo_tell(PgSql\Lob $lob)
pg_fetch_array(PgSql\Result $result, ?int $row=null, int $mode=PGSQL_BOTH)
pg_escape_literal($connection, string $string=UNKNOWN)
pg_set_client_encoding($connection, string $encoding=UNKNOWN)
pg_connection_status(PgSql\Connection $connection)
const PGSQL_STATUS_STRING
pg_convert(PgSql\Connection $connection, string $table_name, array $values, int $flags=0)
pg_socket(PgSql\Connection $connection)
pg_result_status(PgSql\Result $result, int $mode=PGSQL_STATUS_LONG)
pg_field_type_oid(PgSql\Result $result, int $field)
pg_connect(string $connection_string, int $flags=0)
pg_escape_string($connection, string $string=UNKNOWN)
const PGSQL_CONNECT_FORCE_NEW
pg_free_result(PgSql\Result $result)
pg_field_table(PgSql\Result $result, int $field, bool $oid_only=false)
pg_port(?PgSql\Connection $connection=null)
pg_put_copy_data(PgSql\Connection $connection, string $cmd)
pg_version(?PgSql\Connection $connection=null)
pg_client_encoding(?PgSql\Connection $connection=null)
pg_meta_data(PgSql\Connection $connection, string $table_name, bool $extended=false)
pg_last_error(?PgSql\Connection $connection=null)
pg_last_notice(PgSql\Connection $connection, int $mode=PGSQL_NOTICE_LAST)
pg_flush(PgSql\Connection $connection)
pg_num_rows(PgSql\Result $result)
pg_lo_open($connection, $oid=UNKNOWN, string $mode=UNKNOWN)
pg_lo_truncate(PgSql\Lob $lob, int $size)
pg_result_seek(PgSql\Result $result, int $row)
pg_ping(?PgSql\Connection $connection=null)
pg_put_line($connection, string $query=UNKNOWN)
pg_num_fields(PgSql\Result $result)
pg_escape_bytea($connection, string $string=UNKNOWN)
pg_field_num(PgSql\Result $result, string $field)
pg_lo_export($connection, $oid=UNKNOWN, $filename=UNKNOWN)
pg_result_memory_size(PgSql\Result $result)
pg_fieldisnull(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_escape_identifier($connection, string $string=UNKNOWN)
pg_fetch_all(PgSql\Result $result, int $mode=PGSQL_ASSOC)
pg_fetch_row(PgSql\Result $result, ?int $row=null, int $mode=PGSQL_NUM)
pg_connect_poll(PgSql\Connection $connection)
pg_get_pid(PgSql\Connection $connection)
pg_options(?PgSql\Connection $connection=null)
pg_send_prepare(PgSql\Connection $connection, string $statement_name, string $query)
pg_send_execute(PgSql\Connection $connection, string $statement_name, array $params)
pg_send_query(PgSql\Connection $connection, string $query)
pg_query_params($connection, $query, array $params=UNKNOWN)
pg_transaction_status(PgSql\Connection $connection)
pg_cancel_query(PgSql\Connection $connection)
pg_consume_input(PgSql\Connection $connection)
pg_tty(?PgSql\Connection $connection=null)
pg_end_copy(?PgSql\Connection $connection=null)
pg_connection_busy(PgSql\Connection $connection)
pg_lo_write(PgSql\Lob $lob, string $data, ?int $length=null)
pg_execute($connection, $statement_name, array $params=UNKNOWN)
pg_result_error(PgSql\Result $result)
pg_last_oid(PgSql\Result $result)
pg_result_error_field(PgSql\Result $result, int $field_code)
pg_field_is_null(PgSql\Result $result, $row, string|int $field=UNKNOWN)
pg_fetch_object(PgSql\Result $result, ?int $row=null, string $class="stdClass", array $constructor_args=[])
pg_copy_from(PgSql\Connection $connection, string $table_name, array $rows, string $separator="\t", string $null_as="\\\\N")
pg_fetch_assoc(PgSql\Result $result, ?int $row=null)
pg_affected_rows(PgSql\Result $result)
pg_dbname(?PgSql\Connection $connection=null)
pg_host(?PgSql\Connection $connection=null)
pg_lo_read(PgSql\Lob $lob, int $length=8192)
pg_insert(PgSql\Connection $connection, string $table_name, array $values, int $flags=PGSQL_DML_EXEC)
pg_lo_create($connection=UNKNOWN, $oid=UNKNOWN)
pg_get_notify(PgSql\Connection $connection, int $mode=PGSQL_ASSOC)
pg_delete(PgSql\Connection $connection, string $table_name, array $conditions, int $flags=PGSQL_DML_EXEC)
pg_numrows(PgSql\Result $result)
pg_change_password(PgSql\Connection $connection, string $user, #[\SensitiveParameter] string $password)
pg_send_query_params(PgSql\Connection $connection, string $query, array $params)
pg_lo_import($connection, $filename=UNKNOWN, $oid=UNKNOWN)
pg_copy_to(PgSql\Connection $connection, string $table_name, string $separator="\t", string $null_as="\\\\N")
pg_socket_poll($socket, int $read, int $write, int $timeout=-1)
pg_field_size(PgSql\Result $result, int $field)
pg_put_copy_end(PgSql\Connection $connection, ?string $error=null)
pg_jit(?PgSql\Connection $connection=null)
pg_lo_read_all(PgSql\Lob $lob)
pg_set_chunked_rows_size(PgSql\Connection $connection, int $size)
pg_set_error_context_visibility(PgSql\Connection $connection, int $visibility)
pg_connection_reset(PgSql\Connection $connection)
pg_update(PgSql\Connection $connection, string $table_name, array $values, array $conditions, int $flags=PGSQL_DML_EXEC)
#define PHP_MSHUTDOWN_FUNCTION
#define PHP_MINIT_FUNCTION
#define PHP_MINFO_FUNCTION
#define PHP_GINIT_FUNCTION
#define PHP_RINIT_FUNCTION
#define PHP_RSHUTDOWN_FUNCTION
#define PHP_MODULE_GLOBALS
#define STD_PHP_INI_ENTRY_EX
#define STD_PHP_INI_BOOLEAN
struct _php_pollfd php_pollfd
#define PHPWRITE(str, str_len)
PHPAPI pcre2_match_context * php_pcre_mctx(void)
PHPAPI pcre2_code * php_pcre_pce_re(pcre_cache_entry *pce)
PHPAPI pcre2_match_data * php_pcre_create_match_data(uint32_t capture_count, pcre2_code *re)
PHPAPI void php_pcre_pce_decref(pcre_cache_entry *pce)
PHPAPI void php_pcre_pce_incref(pcre_cache_entry *pce)
PHPAPI pcre_cache_entry * pcre_get_compiled_regex_cache(zend_string *regex)
PHPAPI void php_pcre_free_match_data(pcre2_match_data *match_data)
struct _pcre_cache_entry pcre_cache_entry
PHPAPI char * php_strtok_r(char *s, const char *delim, char **last)
xmlCharEncodingHandlerPtr encoding
#define php_stream_cast(stream, as, ret, show_err)
struct _php_stream php_stream
#define PHP_STREAM_OPTION_BLOCKING
#define PHP_STREAM_AS_FD_FOR_SELECT
#define php_stream_from_zval(xstr, pzval)
#define php_stream_auto_cleanup(stream)
#define php_stream_to_zval(stream, zval)
#define php_stream_close(stream)
#define php_stream_open_wrapper(path, mode, options, opened)
#define PHP_STREAM_AS_SOCKETD
#define php_stream_alloc(ops, thisptr, persistent_id, mode)
#define PHP_STREAM_AS_STDIO
zend_object *(* create_object)(zend_class_entry *class_type)
int default_properties_count
zend_function * constructor
const zend_object_handlers * default_object_handlers
zend_object_compare_t compare
zend_object_free_obj_t free_obj
zend_object_get_constructor_t get_constructor
zend_object_clone_obj_t clone_obj
ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format,...)
ZEND_API ZEND_COLD void zend_argument_count_error(const char *format,...)
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
ZEND_API zend_class_entry * zend_standard_class_def
ZEND_API ZEND_COLD void zend_type_error(const char *format,...)
ZEND_API ZEND_COLD void zend_value_error(const char *format,...)
ZEND_API ZEND_COLD void zend_error(int type, const char *format,...)
#define ZEND_TSRMLS_CACHE_UPDATE()
#define INTERNAL_FUNCTION_PARAMETERS
#define ZEND_TSRMLS_CACHE_DEFINE()
#define INTERNAL_FUNCTION_PARAM_PASSTHRU
ZEND_API zend_result add_next_index_null(zval *arg)
ZEND_API const char * zend_zval_value_name(const zval *arg)
ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b)
ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length)
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length)
ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str)
ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str)
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args)
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n)
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format,...)
ZEND_API void zend_merge_properties(zval *obj, HashTable *properties)
ZEND_API zend_result add_next_index_string(zval *arg, const char *str)
ZEND_API void add_index_null(zval *arg, zend_ulong index)
ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n)
ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec,...)
#define Z_PARAM_PATH_STR(dest)
#define ZEND_PARSE_PARAMS_QUIET
#define ZEND_PARSE_PARAMETERS_END()
#define Z_PARAM_RESOURCE(dest)
#define Z_PARAM_STR_OR_NULL(dest)
#define ZVAL_STRING(z, s)
#define ZEND_DECLARE_MODULE_GLOBALS(module_name)
#define ZEND_GET_MODULE(name)
#define Z_PARAM_STRING(dest, dest_len)
#define Z_PARAM_STR(dest)
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
#define Z_PARAM_CLASS(dest)
#define Z_PARAM_LONG(dest)
#define RETURN_NEW_STR(s)
#define Z_PARAM_ARRAY_HT(dest)
#define Z_PARAM_LONG_OR_NULL(dest, is_null)
#define Z_PARAM_STR_OR_LONG(dest_str, dest_long)
#define Z_PARAM_OBJECT_OF_CLASS_OR_NULL(dest, _ce)
#define Z_PARAM_BOOL(dest)
#define Z_PARAM_OBJECT_OF_CLASS(dest, _ce)
#define RETURN_EMPTY_STRING()
#define Z_PARAM_PATH(dest, dest_len)
#define Z_PARAM_ARRAY(dest)
ZEND_API void zend_call_known_function(zend_function *fn, zend_object *object, zend_class_entry *called_scope, zval *retval_ptr, uint32_t param_count, zval *params, HashTable *named_params)
#define Z_PARAM_ZVAL(dest)
#define ZVAL_STRINGL(z, s, l)
#define WRONG_PARAM_COUNT
#define RETVAL_STRINGL(s, l)
#define RETURN_STR_COPY(s)
#define estrndup(s, length)
#define FREE_HASHTABLE(ht)
#define safe_emalloc(nmemb, size, offset)
strcmp(string $string1, string $string2)
zend_string_release_ex(func->internal_function.function_name, 0)
ZEND_API const char * get_active_function_name(void)
union _zend_function zend_function
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func)
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
ZEND_API HashTable *ZEND_FASTCALL zend_array_dup(HashTable *source)
ZEND_API zval *ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
ZEND_API zval *ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
#define zend_new_array(size)
#define ZEND_HASH_APPLY_KEEP
#define ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _key, _val)
int(* apply_func_t)(zval *pDest)
#define ZEND_HASH_FOREACH_END()
#define ZEND_HASH_FOREACH_STR_KEY(ht, _key)
#define ZEND_HASH_FOREACH_VAL(ht, _val)
#define UNREGISTER_INI_ENTRIES()
#define REGISTER_INI_ENTRIES()
#define DISPLAY_INI_ENTRIES()
ZEND_API zend_resource * zend_register_persistent_resource(const char *key, size_t key_len, void *rsrc_pointer, int rsrc_type)
ZEND_API int zend_register_list_destructors_ex(rsrc_dtor_func_t ld, rsrc_dtor_func_t pld, const char *type_name, int module_number)
struct _zend_string zend_string
struct _zend_module_dep zend_module_dep
struct _zend_module_entry zend_module_entry
#define ZEND_MOD_REQUIRED(name)
#define STANDARD_MODULE_PROPERTIES_EX
#define STANDARD_MODULE_HEADER_EX
ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
ZEND_API zend_string *ZEND_FASTCALL zend_long_to_str(zend_long num)
ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op)
ZEND_API void ZEND_FASTCALL convert_to_long(zval *op)
#define convert_to_string(op)
#define XtOffsetOf(s_type, field)
#define ZEND_UNREACHABLE()
#define EMPTY_SWITCH_DEFAULT_CASE()
#define UNEXPECTED(condition)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
ZEND_API zend_string * zend_string_concat3(const char *str1, size_t str1_len, const char *str2, size_t str2_len, const char *str3, size_t str3_len)
#define zend_string_equals_literal(str, literal)
#define ZSTR_EMPTY_ALLOC()
#define zend_string_equals_literal_ci(str, c)
ZEND_API double zend_strtod(const char *s00, const char **se)
#define Z_STRVAL_P(zval_p)
#define Z_ARRVAL_P(zval_p)
struct _zend_resource zend_resource
struct _zend_array HashTable
#define Z_STRLEN_P(zval_p)
#define Z_OBJCE_P(zval_p)
#define ZVAL_DOUBLE(z, d)
ZEND_RESULT_CODE zend_result
struct _zend_object_handlers zend_object_handlers
#define ZVAL_COPY_VALUE(z, v)
ZEND_API void zval_ptr_dtor(zval *zval_ptr)