summaryrefslogtreecommitdiff
path: root/ext/pgsql/pgsql.c
diff options
context:
space:
mode:
authorYasuo Ohgaki <yohgaki@php.net>2014-02-17 06:36:54 +0900
committerYasuo Ohgaki <yohgaki@php.net>2014-02-17 06:36:54 +0900
commitd8aa13029675c6651243f144c328411c9f9c38d5 (patch)
tree218d08dc1146d918f2aae4d65e2057c959b280cb /ext/pgsql/pgsql.c
parent5fd6365c7751dc3dcc37765bf39c559ae8454ca9 (diff)
downloadphp-git-d8aa13029675c6651243f144c328411c9f9c38d5.tar.gz
Imprement FR #25854 Return value for pg_insert should be resource instead of bool
Diffstat (limited to 'ext/pgsql/pgsql.c')
-rw-r--r--ext/pgsql/pgsql.c54
1 files changed, 51 insertions, 3 deletions
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index f8e15a370e..822dae8de7 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -6382,8 +6382,11 @@ PHP_FUNCTION(pg_insert)
zval *pgsql_link, *values;
char *table, *sql = NULL;
int table_len;
- ulong option = PGSQL_DML_EXEC;
+ ulong option = PGSQL_DML_EXEC, return_sql;
PGconn *pg_link;
+ PGresult *pg_result;
+ ExecStatusType status;
+ pgsql_result_handle *pgsql_handle;
int id = -1, argc = ZEND_NUM_ARGS();
if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
@@ -6400,10 +6403,55 @@ PHP_FUNCTION(pg_insert)
if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
}
- if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) {
+ return_sql = option & PGSQL_DML_STRING;
+ if (option & PGSQL_DML_EXEC) {
+ /* return resource when executed */
+ option = option & ~PGSQL_DML_EXEC;
+ if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql TSRMLS_CC) == FAILURE) {
+ RETURN_FALSE;
+ }
+ pg_result = PQexec(pg_link, sql);
+ if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) {
+ PQclear(pg_result);
+ PQreset(pg_link);
+ pg_result = PQexec(pg_link, sql);
+ }
+ efree(sql);
+
+ if (pg_result) {
+ status = PQresultStatus(pg_result);
+ } else {
+ status = (ExecStatusType) PQstatus(pg_link);
+ }
+
+ switch (status) {
+ case PGRES_EMPTY_QUERY:
+ case PGRES_BAD_RESPONSE:
+ case PGRES_NONFATAL_ERROR:
+ case PGRES_FATAL_ERROR:
+ PHP_PQ_ERROR("Query failed: %s", pg_link);
+ PQclear(pg_result);
+ RETURN_FALSE;
+ break;
+ case PGRES_COMMAND_OK: /* successful command that did not return rows */
+ default:
+ if (pg_result) {
+ pgsql_handle = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
+ pgsql_handle->conn = pg_link;
+ pgsql_handle->result = pg_result;
+ pgsql_handle->row = 0;
+ ZEND_REGISTER_RESOURCE(return_value, pgsql_handle, le_result);
+ return;
+ } else {
+ PQclear(pg_result);
+ RETURN_FALSE;
+ }
+ break;
+ }
+ } else if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
- if (option & PGSQL_DML_STRING) {
+ if (return_sql) {
RETURN_STRING(sql, 0);
}
RETURN_TRUE;