From f4d7b4d10f7d3badbb0ac5ad5c0754a044f8bdd2 Mon Sep 17 00:00:00 2001 From: Israel Lins Date: Tue, 26 Feb 2013 14:23:43 -0300 Subject: [PATCH] PSQL: lastInsertId without OID on table Make lastInsertID work for tables without OIDs. The use of OID in tables is now deprecated in PostgeSQL and lastval() is now provided. http://www.postgresql.org/docs/8.1/interactive/runtime-config-compatible.html#GUC-DEFAULT-WITH-OIDS Change-Id: I01dfdd7a2aab8826487657f691fea3c9268c16b2 Reviewed-by: Israel Lins Albuquerque Reviewed-by: Mark Brand --- src/sql/drivers/psql/qsql_psql.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sql/drivers/psql/qsql_psql.cpp b/src/sql/drivers/psql/qsql_psql.cpp index 6a37e4b50d..7d844fa1bd 100644 --- a/src/sql/drivers/psql/qsql_psql.cpp +++ b/src/sql/drivers/psql/qsql_psql.cpp @@ -474,7 +474,12 @@ int QPSQLResult::numRowsAffected() QVariant QPSQLResult::lastInsertId() const { - if (isActive()) { + if (d->privDriver->pro >= QPSQLDriver::Version81) { + QSqlQuery qry(driver()->createResult()); + // Most recent sequence value obtained from nextval + if (qry.exec(QLatin1String("SELECT lastval();")) && qry.next()) + return qry.value(0); + } else if (isActive()) { Oid id = PQoidValue(d->result); if (id != InvalidOid) return QVariant(id);