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:
Edin Kadribasic 2005-07-01 21:54:50 +00:00
parent 6b4910b028
commit 1a10666b08

View file

@ -212,9 +212,38 @@ static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, unsigned
return NULL; 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); *len = spprintf(&id, 0, "%ld", (long) H->pgoid);
} else {
PGresult *res;
char *name_escaped, *q;
size_t l = strlen(name);
ExecStatusType status;
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; return id;
} }