diff options
Diffstat (limited to 'src/sql/models/qsqltablemodel.cpp')
-rw-r--r-- | src/sql/models/qsqltablemodel.cpp | 149 |
1 files changed, 42 insertions, 107 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 4d20915c09..0d17194287 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSql module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qsqltablemodel.h" @@ -53,20 +17,17 @@ QT_BEGIN_NAMESPACE -typedef QSqlTableModelSql Sql; +using namespace Qt::StringLiterals; -QSqlTableModelPrivate::~QSqlTableModelPrivate() -{ - -} +using SqlTm = QSqlQueryModelSql; /*! \internal Populates our record with values. */ -QSqlRecord QSqlTableModelPrivate::record(const QVector<QVariant> &values) const +QSqlRecord QSqlTableModelPrivate::record(const QList<QVariant> &values) const { QSqlRecord r = rec; - for (int i = 0; i < r.count() && i < values.count(); ++i) + for (int i = 0; i < r.count() && i < values.size(); ++i) r.setValue(i, values.at(i)); return r; } @@ -177,11 +138,10 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, return false; } } - int i; - for (i = 0; i < rec.count(); ++i) + for (int i = 0; i < rec.count(); ++i) if (rec.isGenerated(i)) editQuery.addBindValue(rec.value(i)); - for (i = 0; i < whereValues.count(); ++i) + for (int i = 0; i < whereValues.count(); ++i) if (whereValues.isGenerated(i) && !whereValues.isNull(i)) editQuery.addBindValue(whereValues.value(i)); @@ -211,7 +171,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example: - \snippet sqldatabase/sqldatabase.cpp 24 + \snippet sqldatabase/sqldatabase_snippet.cpp 24 We set the SQL table's name and the edit strategy, then we set up the labels displayed in the view header. The edit strategy @@ -222,7 +182,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, QSqlTableModel can also be used to access a database programmatically, without binding it to a view: - \snippet sqldatabase/sqldatabase.cpp 21 + \snippet sqldatabase/sqldatabase.cpp 25 The code snippet above extracts the \c salary field from record 4 in the result set of the query \c{SELECT * from employee}. @@ -239,7 +199,7 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, want to resolve foreign keys. \sa QSqlRelationalTableModel, QSqlQuery, {Model/View Programming}, - {Table Model Example}, {Cached Table Example} + {Table Model Example}, {Cached SQL Table} */ /*! @@ -333,7 +293,7 @@ void QSqlTableModel::setTable(const QString &tableName) d->initRecordAndPrimaryIndex(); if (d->rec.count() == 0) - d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), + d->error = QSqlError("Unable to find table "_L1 + d->tableName, QString(), QSqlError::StatementError); // Remember the auto index column if there is one now. @@ -376,10 +336,9 @@ bool QSqlTableModel::select() d->clearCache(); - QSqlQuery qu(query, d->db); - setQuery(qu); + this->QSqlQueryModel::setQuery(query, d->db); - if (!qu.isActive() || lastError().isValid()) { + if (!d->query.isActive() || lastError().isValid()) { // something went wrong - revert to non-select state d->initRecordAndPrimaryIndex(); endResetModel(); @@ -414,9 +373,9 @@ bool QSqlTableModel::selectRow(int row) d->tableName, primaryValues(row), false); - static const QString wh = Sql::where() + Sql::sp(); + static const QString wh = SqlTm::where() + SqlTm::sp(); if (d->filter.startsWith(wh, Qt::CaseInsensitive)) - d->filter.remove(0, wh.length()); + d->filter.remove(0, wh.size()); QString stmt; @@ -493,9 +452,9 @@ QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, in if (orientation == Qt::Vertical && role == Qt::DisplayRole) { const QSqlTableModelPrivate::Op op = d->cache.value(section).op(); if (op == QSqlTableModelPrivate::Insert) - return QLatin1String("*"); + return "*"_L1; else if (op == QSqlTableModelPrivate::Delete) - return QLatin1String("!"); + return "!"_L1; } return QSqlQueryModel::headerData(section, orientation, role); } @@ -510,10 +469,8 @@ QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, in bool QSqlTableModel::isDirty() const { Q_D(const QSqlTableModel); - QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin(); - const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd(); - for (; i != e; ++i) { - if (!i.value().submitted()) + for (const auto &val : std::as_const(d->cache)) { + if (!val.submitted()) return true; } return false; @@ -607,7 +564,6 @@ bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, in return true; } -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) /*! \reimp */ @@ -615,19 +571,6 @@ bool QSqlTableModel::clearItemData(const QModelIndex &index) { return setData(index, QVariant(), Qt::EditRole); } -#endif - -/*! - This function simply calls QSqlQueryModel::setQuery(\a query). - You should normally not call it on a QSqlTableModel. Instead, use - setTable(), setSort(), setFilter(), etc., to set up the query. - - \sa selectStatement() -*/ -void QSqlTableModel::setQuery(const QSqlQuery &query) -{ - QSqlQueryModel::setQuery(query); -} /*! Updates the given \a row in the currently active database table @@ -659,12 +602,11 @@ bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values) whereValues, prepStatement); if (stmt.isEmpty() || where.isEmpty() || row < 0 || row >= rowCount()) { - d->error = QSqlError(QLatin1String("No Fields to update"), QString(), - QSqlError::StatementError); + d->error = QSqlError("No Fields to update"_L1, QString(), QSqlError::StatementError); return false; } - return d->exec(Sql::concat(stmt, where), prepStatement, rec, whereValues); + return d->exec(SqlTm::concat(stmt, where), prepStatement, rec, whereValues); } @@ -692,8 +634,7 @@ bool QSqlTableModel::insertRowIntoTable(const QSqlRecord &values) rec, prepStatement); if (stmt.isEmpty()) { - d->error = QSqlError(QLatin1String("No Fields to update"), QString(), - QSqlError::StatementError); + d->error = QSqlError("No Fields to update"_L1, QString(), QSqlError::StatementError); return false; } @@ -729,12 +670,11 @@ bool QSqlTableModel::deleteRowFromTable(int row) prepStatement); if (stmt.isEmpty() || where.isEmpty()) { - d->error = QSqlError(QLatin1String("Unable to delete row"), QString(), - QSqlError::StatementError); + d->error = QSqlError("Unable to delete row"_L1, QString(), QSqlError::StatementError); return false; } - return d->exec(Sql::concat(stmt, where), prepStatement, QSqlRecord() /* no new values */, whereValues); + return d->exec(SqlTm::concat(stmt, where), prepStatement, QSqlRecord() /* no new values */, whereValues); } /*! @@ -1003,10 +943,11 @@ QString QSqlTableModel::orderByClause() const //we can safely escape the field because it would have been obtained from the database //and have the correct case - QString field = d->tableName + QLatin1Char('.') + QString field = d->db.driver()->escapeIdentifier(d->tableName, QSqlDriver::TableName) + + u'.' + d->db.driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName); - field = d->sortOrder == Qt::AscendingOrder ? Sql::asc(field) : Sql::desc(field); - return Sql::orderBy(field); + field = d->sortOrder == Qt::AscendingOrder ? SqlTm::asc(field) : SqlTm::desc(field); + return SqlTm::orderBy(field); } /*! @@ -1030,12 +971,11 @@ QString QSqlTableModel::selectStatement() const { Q_D(const QSqlTableModel); if (d->tableName.isEmpty()) { - d->error = QSqlError(QLatin1String("No table name given"), QString(), - QSqlError::StatementError); + d->error = QSqlError("No table name given"_L1, QString(), QSqlError::StatementError); return QString(); } if (d->rec.isEmpty()) { - d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), + d->error = QSqlError("Unable to find table "_L1 + d->tableName, QString(), QSqlError::StatementError); return QString(); } @@ -1045,11 +985,11 @@ QString QSqlTableModel::selectStatement() const d->rec, false); if (stmt.isEmpty()) { - d->error = QSqlError(QLatin1String("Unable to select fields from table ") + d->tableName, + d->error = QSqlError("Unable to select fields from table "_L1 + d->tableName, QString(), QSqlError::StatementError); return stmt; } - return Sql::concat(Sql::concat(stmt, Sql::where(d->filter)), orderByClause()); + return SqlTm::concat(SqlTm::concat(stmt, SqlTm::where(d->filter)), orderByClause()); } /*! @@ -1105,12 +1045,8 @@ bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &par bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) { Q_D(QSqlTableModel); - if (parent.isValid() || row < 0 || count <= 0) + if (parent.isValid() || row < 0 || count <= 0 || row + count > rowCount()) return false; - else if (row + count > rowCount()) - return false; - else if (!count) - return true; if (d->strategy != OnManualSubmit) if (count > 1 || (d->cache.value(row).submitted() && isDirty())) @@ -1416,8 +1352,7 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) return false; // Check field names and remember mapping - typedef QMap<int, int> Map; - Map map; + QMap<int, int> map; for (int i = 0; i < values.count(); ++i) { int idx = d->nameToIndex(values.fieldName(i)); if (idx == -1) @@ -1430,18 +1365,16 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(row)); - Map::const_iterator i = map.constBegin(); - const Map::const_iterator e = map.constEnd(); - for ( ; i != e; ++i) { + for (const auto i : map.asKeyValueRange()) { // have to use virtual setData() here rather than mrow.setValue() EditStrategy strategy = d->strategy; d->strategy = OnManualSubmit; - QModelIndex cIndex = createIndex(row, i.value()); - setData(cIndex, values.value(i.key())); + QModelIndex cIndex = createIndex(row, i.second); + setData(cIndex, values.value(i.first)); d->strategy = strategy; // setData() sets generated to TRUE, but source record should prevail. - if (!values.isGenerated(i.key())) - mrow.recRef().setGenerated(i.value(), false); + if (!values.isGenerated(i.first)) + mrow.recRef().setGenerated(i.second, false); } if (d->strategy != OnManualSubmit) @@ -1471,3 +1404,5 @@ QSqlRecord QSqlTableModel::primaryValues(int row) const } QT_END_NAMESPACE + +#include "moc_qsqltablemodel.cpp" |