]> granicus.if.org Git - php/commitdiff
MFB: Fixed bug #42783 (pg_insert() does not accept an empty list for
authorIlia Alshanetsky <iliaa@php.net>
Wed, 3 Oct 2007 23:31:58 +0000 (23:31 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Wed, 3 Oct 2007 23:31:58 +0000 (23:31 +0000)
insertion)

NEWS
ext/pgsql/pgsql.c
ext/pgsql/tests/80_bug42783.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 0488d74c4c5594f91e08907bda4b68493cb4d84e..b33c1d1be4c5fe08690aa7982b20b6df3ac6febb 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,8 @@ PHP                                                                        NEWS
 - Fixed bug #42818 ($foo = clone(array()); leaks memory). (Dmitry)
 - Fixed bug #42785 (json_encode() formats doubles according to locale rather  
   then following standard syntax). (Ilia)
+- Fixed bug #42783 (pg_insert() does not accept an empty list for
+  insertion). (Ilia)
 - Fixed bug #42772 (Storing $this in a static var fails while handling a cast
   to string). (Dmitry)
 - Fixed bug #42767 (highlight_string() truncates trailing comment). (Ilia)
index 6765141db4f9c69ca49cd6c330b0c7b0d604f01a..b351cd15b621e9846bd8ae05825106d91abb489f 100644 (file)
@@ -5366,7 +5366,11 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var
        assert(Z_TYPE_P(var_array) == IS_ARRAY);
 
        if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0) {
-               return FAILURE;
+               smart_str_appends(&querystr, "INSERT INTO ");
+               smart_str_appends(&querystr, table);
+               smart_str_appends(&querystr, " DEFAULT VALUES");
+
+               goto no_values;
        }
 
        /* convert input array if needed */
@@ -5424,6 +5428,9 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var
        /* Remove the trailing "," */
        querystr.len--;
        smart_str_appends(&querystr, ");");
+
+no_values:
+
        smart_str_0(&querystr);
 
        if ((opt & (PGSQL_DML_EXEC|PGSQL_DML_ASYNC)) &&
@@ -5435,7 +5442,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var
        }
        
 cleanup:
-       if (!(opt & PGSQL_DML_NO_CONV)) {
+       if (!(opt & PGSQL_DML_NO_CONV) && converted) {
                zval_dtor(converted);                   
                FREE_ZVAL(converted);
        }
diff --git a/ext/pgsql/tests/80_bug42783.phpt b/ext/pgsql/tests/80_bug42783.phpt
new file mode 100644 (file)
index 0000000..575e527
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Bug #42783 (pg_insert() does not support an empty value array)
+--SKIPIF--
+<?php 
+require_once('skipif.inc');
+?>
+--FILE--
+<?php
+
+require_once('config.inc');
+       
+$dbh = @pg_connect($conn_str);
+if (!$dbh) {
+       die ("Could not connect to the server");
+}
+
+pg_query("CREATE TABLE php_test (id SERIAL PRIMARY KEY, time TIMESTAMP NOT NULL DEFAULT now())");
+
+pg_insert($dbh, 'php_test', array());
+
+var_dump(pg_fetch_assoc(pg_query("SELECT * FROM php_test")));
+
+pg_query($dbh, "DROP TABLE php_test");
+pg_close($dbh);
+?>
+===DONE===
+--EXPECTF--
+array(2) {
+  ["id"]=>
+  string(%d) "%d"
+  ["time"]=>
+  string(%d) "%s"
+}
+===DONE===