com php-src: Fixed bug #72759 Regression in pgo_pgsql: ex t/pdo_pgsql/pgsql_driver.c

From: Date: Sun, 14 Aug 2016 17:33:24 +0000
Subject: com php-src: Fixed bug #72759 Regression in pgo_pgsql: ex t/pdo_pgsql/pgsql_driver.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-93777@lists.php.net to get a copy of this message
Commit: 977cbc2fff1a3ec9d29a2c0904fae01bfd64c6c2 Author: Anatol Belski <ab@php.net> Sun, 14 Aug 2016 19:33:24 +0200 Parents: 9f1d962ed6057a3996f1b5aa82467a3172e41e8f Branches: PHP-5.6 PHP-7.0 PHP-7.1 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=977cbc2fff1a3ec9d29a2c0904fae01bfd64c6c2 Log: Fixed bug #72759 Regression in pgo_pgsql This is caused by the fix for #72633. Namely, lastval() throws an error, if no nextval() was called earlier in the same session. This is by all means correct so far, however inside a transaction it leads to an abort. This is the opposite to MySQL's last_insert_id() which doesn't produce any error no matter something were autoincremented or not. To avoid existing scripts breakage in the stable branches, the previous patch is extended to revert the transaction to the state before the lastval() call in case of error. It is done only for 5.6 and 7.0 to retain BC. For 7.1+, the clean behavior should persist. This is already the current behavior, when the sequence name is explicitly passed. So there's no reason to obfuscate the errors where this breakage is valid. Bugs: https://bugs.php.net/72759 https://bugs.php.net/72633 Changed paths: M ext/pdo_pgsql/pgsql_driver.c Diff: diff --git a/ext/pdo_pgsql/pgsql_driver.c b/ext/pdo_pgsql/pgsql_driver.c index 5b78bcc..b27753b 100644 --- a/ext/pdo_pgsql/pgsql_driver.c +++ b/ext/pdo_pgsql/pgsql_driver.c @@ -360,8 +360,15 @@ static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned char *id = NULL; PGresult *res; ExecStatusType status; + zend_bool savepoint = 0; if (name == NULL) { + savepoint = pgsql_handle_in_transaction(dbh); + + if (savepoint) { + /* The savepoint is overwritten every time. */ + (void)PQexec(H->server, "SAVEPOINT _php_lastid_savepoint"); + } res = PQexec(H->server, "SELECT LASTVAL()"); } else { const char *q[1]; @@ -375,10 +382,17 @@ static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned id = estrdup((char *)PQgetvalue(res, 0, 0)); *len = PQgetlength(res, 0, 0); } else { + if (savepoint) { + (void)PQexec(H->server, "ROLLBACK TO SAVEPOINT _php_lastid_savepoint"); + } pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res)); *len = spprintf(&id, 0, "%ld", (long) H->pgoid); } + if (savepoint) { + (void)PQexec(H->server, "RELEASE SAVEPOINT _php_lastid_savepoint"); + } + if (res) { PQclear(res); }

« previous php.cvs (#93777) next »