diff options
author | Giuseppe D'Angelo <[email protected]> | 2021-03-16 10:53:24 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <[email protected]> | 2021-03-21 10:16:56 +0100 |
commit | 14f9f00fdb2dc428610c08e3d9d03e38e9602166 (patch) | |
tree | 6cbd9a748d3907d66d1033a5a5050af7f5e80fd3 /src/sql/models/qsqlquerymodel.cpp | |
parent | 37e0953613ef9a3db137bc8d3076441d9ae317d9 (diff) |
QSqlQuery: make it a move only type
QSqlQuery is a broken value class. Copying one object would mean
copying database state (the result set, the cursor position, etc.)
which isn't generally available for all database drivers.
For that reason, the current implementation does not honor value
semantics -- modifying a QSqlQuery object has visible side effects
on its existing copies (!).
The correct solution is to accept that QSqlQuery is a move only
type, not a value type. Add move semantics to it, and deprecate
its copies.
(We can't just *remove* copies in Qt 6 due to SC/BC constraints).
[ChangeLog][QtSql][QSqlQuery] QSqlQuery copy operations have
been deprecated. QSqlQuery copy semantics cannot be implemented
correctly, as it's not generally possible to copy a result set
of a query when copying the corresponding QSqlQuery object. This
resulted in modifications on a QSqlQuery having visible (and
unintended) side effects on its copies. Instead, treat QSqlQuery
as a move-only type.
Fixes: QTBUG-91766
Change-Id: Iabd3aa605332a5c15c524303418bf17a21ed520b
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Andy Shaw <[email protected]>
Reviewed-by: Volker Hilsheimer <[email protected]>
Diffstat (limited to 'src/sql/models/qsqlquerymodel.cpp')
-rw-r--r-- | src/sql/models/qsqlquerymodel.cpp | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp index 6d4e2c09c10..ceada8cae23 100644 --- a/src/sql/models/qsqlquerymodel.cpp +++ b/src/sql/models/qsqlquerymodel.cpp @@ -418,6 +418,20 @@ void QSqlQueryModel::queryChange() // do nothing } + +/*! + \obsolete + \overload + \since 4.5 + + Use the \c{setQuery(QSqlQuery &&query)} overload instead. +*/ +void QSqlQueryModel::setQuery(const QSqlQuery &query) +{ + QT_IGNORE_DEPRECATIONS(QSqlQuery copy = query;) + setQuery(std::move(copy)); +} + /*! Resets the model and sets the data provider to be the given \a query. Note that the query must be active and must not be @@ -428,9 +442,11 @@ void QSqlQueryModel::queryChange() \note Calling setQuery() will remove any inserted columns. + \since 6.2 + \sa query(), QSqlQuery::isActive(), QSqlQuery::setForwardOnly(), lastError() */ -void QSqlQueryModel::setQuery(const QSqlQuery &query) +void QSqlQueryModel::setQuery(QSqlQuery &&query) { Q_D(QSqlQueryModel); beginResetModel(); @@ -443,11 +459,11 @@ void QSqlQueryModel::setQuery(const QSqlQuery &query) d->bottom = QModelIndex(); d->error = QSqlError(); - d->query = query; + d->query = std::move(query); d->rec = newRec; d->atEnd = true; - if (query.isForwardOnly()) { + if (d->query.isForwardOnly()) { d->error = QSqlError(QLatin1String("Forward-only queries " "cannot be used in a data model"), QString(), QSqlError::ConnectionError); @@ -455,13 +471,13 @@ void QSqlQueryModel::setQuery(const QSqlQuery &query) return; } - if (!query.isActive()) { - d->error = query.lastError(); + if (!d->query.isActive()) { + d->error = d->query.lastError(); endResetModel(); return; } - if (query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) { + if (d->query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) { d->bottom = createIndex(d->query.size() - 1, d->rec.count() - 1); } else { d->bottom = createIndex(-1, d->rec.count() - 1); @@ -545,11 +561,14 @@ bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, \sa setQuery() */ +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED QSqlQuery QSqlQueryModel::query() const { Q_D(const QSqlQueryModel); return d->query; } +QT_WARNING_POP /*! Returns information about the last error that occurred on the |