Skip to content

Commit 12628e9

Browse files
phackwerweltling
authored andcommitted
Implemented FR #72633 Postgres PDO lastInsertId() should work without specifying a sequence
1 parent c523227 commit 12628e9

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,31 +361,30 @@ static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *
361361
{
362362
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
363363
char *id = NULL;
364+
PGresult *res;
365+
ExecStatusType status;
366+
const char *q[1];
367+
q[0] = name;
364368

365-
if (name == NULL) {
366-
if (H->pgoid == InvalidOid) {
367-
return NULL;
368-
}
369-
*len = spprintf(&id, 0, ZEND_LONG_FMT, (zend_long) H->pgoid);
369+
if (PHP_PDO_PGSQL_LASTVAL_PG_VERSION <= PQserverVersion(H->server) && name == NULL) {
370+
res = PQexec(H->server, "SELECT LASTVAL()");
370371
} else {
371-
PGresult *res;
372-
ExecStatusType status;
373-
const char *q[1];
374-
q[0] = name;
375372
res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
376-
status = PQresultStatus(res);
373+
}
374+
status = PQresultStatus(res);
377375

378-
if (res && (status == PGRES_TUPLES_OK)) {
379-
id = estrdup((char *)PQgetvalue(res, 0, 0));
380-
*len = PQgetlength(res, 0, 0);
381-
} else {
382-
pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
383-
}
376+
if (res && (status == PGRES_TUPLES_OK)) {
377+
id = estrdup((char *)PQgetvalue(res, 0, 0));
378+
*len = PQgetlength(res, 0, 0);
379+
} else {
380+
pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
381+
*len = spprintf(&id, 0, ZEND_LONG_FMT, (zend_long) H->pgoid);
382+
}
384383

385-
if (res) {
386-
PQclear(res);
387-
}
384+
if (res) {
385+
PQclear(res);
388386
}
387+
389388
return id;
390389
}
391390

ext/pdo_pgsql/php_pdo_pgsql_int.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#define PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE "08006"
3131

32+
#define PHP_PDO_PGSQL_LASTVAL_PG_VERSION 80100
33+
3234
typedef struct {
3335
const char *file;
3436
int line;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
currval() vs lastval() - PDO PgSQL Bug #1134 [BUG] New record, PostgreSQL and the Primary key https://github.com/phalcon/cphalcon/issues/1134
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
6+
require dirname(__FILE__) . '/config.inc';
7+
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
8+
PDOTest::skip();
9+
?>
10+
--FILE--
11+
<?php
12+
13+
require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
14+
15+
$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
16+
17+
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
18+
19+
@$db->query('CREATE TABLE test_last_id (id SERIAL NOT NULL, field1 VARCHAR(10))');
20+
21+
$stmt = $db->prepare("INSERT INTO test_last_id (field1) VALUES ('test')");
22+
23+
$stmt->execute();
24+
25+
/**
26+
* No sequence name informed
27+
*/
28+
var_dump($db->lastInsertId());
29+
/**
30+
* Sequence name informed
31+
*/
32+
var_dump($db->lastInsertId('test_last_id_id_seq'));
33+
?>
34+
--EXPECTREGEX--
35+
string\([0-9]*\)\ \"[0-9]*\"
36+
string\([0-9]*\)\ \"[0-9]*\"

ext/pdo_pgsql/tests/common.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Postgres
55
if (!extension_loaded('pdo_pgsql')) print 'skip'; ?>
66
--REDIRECTTEST--
77
# magic auto-configuration
8-
# Also update config.inc if you make changes here...
8+
# Also update config.inc if you make changes here...
99

1010
$config = array(
1111
'TESTS' => __DIR__ . '/ext/pdo/tests'
@@ -20,5 +20,5 @@ if (false !== getenv('PDO_PGSQL_TEST_DSN')) {
2020
} else {
2121
$config['ENV']['PDOTEST_DSN'] = 'pgsql:host=localhost port=5432 dbname=test user= password=';
2222
}
23-
23+
2424
return $config;

0 commit comments

Comments
 (0)