ODBC: implementation of lastInsertId()

Implemented lastInsertId() for some ODBC compatible databases.

Change-Id: I0b75a8e68369af39e258e4761b384767ab8a371e
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
Israel Lins 2013-02-15 19:47:43 -03:00 committed by The Qt Project
parent afaa6e42f0
commit 70bb34ccd5
2 changed files with 38 additions and 1 deletions

View File

@ -1677,6 +1677,38 @@ QSqlRecord QODBCResult::record() const
return d->rInf; return d->rInf;
} }
QVariant QODBCResult::lastInsertId() const
{
QString sql;
switch (d->driverPrivate->dbmsType) {
case QODBCDriverPrivate::MSSqlServer:
case QODBCDriverPrivate::Sybase:
sql = QLatin1String("SELECT @@IDENTITY;");
break;
case QODBCDriverPrivate::MySqlServer:
sql = QLatin1String("SELECT LAST_INSERT_ID();");
break;
case QODBCDriverPrivate::PostgreSQL:
sql = QLatin1String("SELECT lastval();");
break;
default:
break;
}
if (!sql.isEmpty()) {
QSqlQuery qry(driver()->createResult());
if (qry.exec(sql) && qry.next())
return qry.value(0);
qSqlWarning(QLatin1String("QODBCResult::lastInsertId: Unable to get lastInsertId"), d);
} else {
qSqlWarning(QLatin1String("QODBCResult::lastInsertId: not implemented for this DBMS"), d);
}
return QVariant();
}
QVariant QODBCResult::handle() const QVariant QODBCResult::handle() const
{ {
return QVariant(qRegisterMetaType<SQLHANDLE>("SQLHANDLE"), &d->hStmt); return QVariant(qRegisterMetaType<SQLHANDLE>("SQLHANDLE"), &d->hStmt);
@ -1797,12 +1829,16 @@ bool QODBCDriver::hasFeature(DriverFeature f) const
return true; return true;
case QuerySize: case QuerySize:
case NamedPlaceholders: case NamedPlaceholders:
case LastInsertId:
case BatchOperations: case BatchOperations:
case SimpleLocking: case SimpleLocking:
case EventNotifications: case EventNotifications:
case CancelQuery: case CancelQuery:
return false; return false;
case LastInsertId:
return (d->dbmsType == QODBCDriverPrivate::MSSqlServer)
|| (d->dbmsType == QODBCDriverPrivate::Sybase)
|| (d->dbmsType == QODBCDriverPrivate::MySqlServer)
|| (d->dbmsType == QODBCDriverPrivate::PostgreSQL);
case MultipleResultSets: case MultipleResultSets:
return d->hasMultiResultSets; return d->hasMultiResultSets;
case BLOB: { case BLOB: {

View File

@ -97,6 +97,7 @@ public:
bool prepare(const QString& query); bool prepare(const QString& query);
bool exec(); bool exec();
QVariant lastInsertId() const;
QVariant handle() const; QVariant handle() const;
virtual void setForwardOnly(bool forward); virtual void setForwardOnly(bool forward);