]> granicus.if.org Git - php/commitdiff
Imprement FR #25854 Return value for pg_insert should be resource instead of bool
authorYasuo Ohgaki <yohgaki@php.net>
Sun, 16 Feb 2014 21:36:54 +0000 (06:36 +0900)
committerYasuo Ohgaki <yohgaki@php.net>
Sun, 16 Feb 2014 21:36:54 +0000 (06:36 +0900)
ext/pgsql/pgsql.c
ext/pgsql/php_pgsql.h
ext/pgsql/tests/12pg_insert_9.phpt
ext/pgsql/tests/13pg_select_9.phpt

index f8e15a370e2e4b1d3b93087d5bd0611ba1a4373b..822dae8de7a7a5b878290015a7bbe37750bcf3d0 100644 (file)
@@ -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;
index 9aa3883a5a852ca2d63364a90475fa4f02ff330b..b6caf9d2012939a6ebf539b2e1d82379ca3f4259 100644 (file)
@@ -24,6 +24,8 @@
 
 #if HAVE_PGSQL
 
+#define PHP_PGSQL_API_VERSION 20140217
+
 extern zend_module_entry pgsql_module_entry;
 #define pgsql_module_ptr &pgsql_module_entry
 
@@ -201,6 +203,7 @@ PHP_FUNCTION(pg_select);
 #define PGSQL_DML_STRING            (1<<11)    /* Return query string */
 #define PGSQL_DML_ESCAPE            (1<<12)    /* No convert, but escape only */
 
+
 /* exported functions */
 PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended TSRMLS_DC);
 PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, ulong opt TSRMLS_DC);
index bedf3e29ad7454b8b8736a31ac64b32892ca49ac..b84e25b8d397d11860569720d8a7959a96e4e170 100644 (file)
@@ -19,10 +19,12 @@ $fields = array('num'=>'1234', 'str'=>'AAA', 'bin'=>'BBB');
 pg_insert($db, $table_name, $fields) or print "Error in test 1\n";
 echo pg_insert($db, $table_name, $fields, PGSQL_DML_STRING)."\n";
 echo pg_insert($db, $table_name, $fields, PGSQL_DML_STRING|PGSQL_DML_ESCAPE)."\n";
+var_dump( pg_insert($db, $table_name, $fields, PGSQL_DML_EXEC) ); // Return resource
 
 echo "Ok\n";
 ?>
---EXPECT--
+--EXPECTF--
 INSERT INTO "php_pgsql_test" ("num","str","bin") VALUES (1234,E'AAA',E'\\x424242');
 INSERT INTO "php_pgsql_test" ("num","str","bin") VALUES ('1234','AAA','BBB');
+resource(%d) of type (pgsql result)
 Ok
\ No newline at end of file
index 73582b650b002bf756f8c534e906b79db6f66144..3b3ccb7462c99655e8c515447e4349a4485651c9 100644 (file)
@@ -25,7 +25,7 @@ echo "Ok\n";
 
 ?>
 --EXPECT--
-array(1) {
+array(2) {
   [0]=>
   array(3) {
     ["num"]=>
@@ -35,6 +35,15 @@ array(1) {
     ["bin"]=>
     string(8) "\x424242"
   }
+  [1]=>
+  array(3) {
+    ["num"]=>
+    string(4) "1234"
+    ["str"]=>
+    string(3) "AAA"
+    ["bin"]=>
+    string(8) "\x424242"
+  }
 }
 SELECT * FROM "php_pgsql_test" WHERE "num"=1234;
 SELECT * FROM "php_pgsql_test" WHERE "num"='1234';