mirror of
https://github.com/php/php-src.git
synced 2025-08-16 22:18:50 +02:00
Added support for fetching current value of a sequence when the
optional sequence name has been passed to PDO::lastInsertId()
This commit is contained in:
parent
6b4910b028
commit
1a10666b08
1 changed files with 32 additions and 3 deletions
|
@ -212,9 +212,38 @@ static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* TODO: if name != NULL, pull out last value for that sequence/column */
|
||||
if (name == NULL) {
|
||||
*len = spprintf(&id, 0, "%ld", (long) H->pgoid);
|
||||
} else {
|
||||
PGresult *res;
|
||||
char *name_escaped, *q;
|
||||
size_t l = strlen(name);
|
||||
ExecStatusType status;
|
||||
|
||||
*len = spprintf(&id, 0, "%ld", (long) H->pgoid);
|
||||
name_escaped = safe_emalloc(l, 2, 1);
|
||||
PQescapeString(name_escaped, name, l);
|
||||
spprintf(&q, 0, "SELECT CURRVAL('%s')", name_escaped);
|
||||
res = PQexec(H->server, q);
|
||||
efree(name_escaped);
|
||||
efree(q);
|
||||
status = PQresultStatus(res);
|
||||
|
||||
if (res && (status == PGRES_TUPLES_OK)) {
|
||||
id = estrdup((char *)PQgetvalue(res, 0, 0));
|
||||
*len = PQgetlength(res, 0, 0);
|
||||
} else {
|
||||
#if HAVE_PQRESULTERRORFIELD
|
||||
char * sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
|
||||
pdo_pgsql_error(dbh, status, (const char *)sqlstate);
|
||||
#else
|
||||
pdo_pgsql_error(dbh, status, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (res) {
|
||||
PQclear(res);
|
||||
}
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue