diff options
Diffstat (limited to 'src/sql/models')
-rw-r--r-- | src/sql/models/models.pri | 12 | ||||
-rw-r--r-- | src/sql/models/qsqlquerymodel.cpp | 95 | ||||
-rw-r--r-- | src/sql/models/qsqlquerymodel.h | 47 | ||||
-rw-r--r-- | src/sql/models/qsqlquerymodel_p.h | 74 | ||||
-rw-r--r-- | src/sql/models/qsqlrelationaldelegate.cpp | 40 | ||||
-rw-r--r-- | src/sql/models/qsqlrelationaldelegate.h | 97 | ||||
-rw-r--r-- | src/sql/models/qsqlrelationaltablemodel.cpp | 132 | ||||
-rw-r--r-- | src/sql/models/qsqlrelationaltablemodel.h | 48 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel.cpp | 149 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel.h | 45 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel_p.h | 48 |
11 files changed, 200 insertions, 587 deletions
diff --git a/src/sql/models/models.pri b/src/sql/models/models.pri deleted file mode 100644 index 972cf2f3f2..0000000000 --- a/src/sql/models/models.pri +++ /dev/null @@ -1,12 +0,0 @@ -HEADERS += models/qsqlquerymodel.h \ - models/qsqlquerymodel_p.h \ - models/qsqltablemodel.h \ - models/qsqltablemodel_p.h \ - models/qsqlrelationaldelegate.h \ - models/qsqlrelationaltablemodel.h - -SOURCES += models/qsqlquerymodel.cpp \ - models/qsqltablemodel.cpp \ - models/qsqlrelationaldelegate.cpp \ - models/qsqlrelationaltablemodel.cpp - diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp index c0b1061c6b..1aae088c64 100644 --- a/src/sql/models/qsqlquerymodel.cpp +++ b/src/sql/models/qsqlquerymodel.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) 2016 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 "qsqlquerymodel.h" #include "qsqlquerymodel_p.h" @@ -46,6 +10,8 @@ QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + #define QSQL_PREFETCH 255 void QSqlQueryModelPrivate::prefetch(int limit) @@ -113,7 +79,7 @@ int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView. For example: - \snippet sqldatabase/sqldatabase.cpp 16 + \snippet sqldatabase/sqldatabase_snippet.cpp 16 We set the model's query, then we set up the labels displayed in the view header. @@ -124,8 +90,8 @@ int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const \snippet sqldatabase/sqldatabase.cpp 21 The code snippet above extracts the \c salary field from record 4 in - the result set of the query \c{SELECT * from employee}. Assuming - that \c salary is column 2, we can rewrite the last line as follows: + the result set of the \c SELECT query. Since \c salary is the 2nd + column (or column index 1), we can rewrite the last line as follows: \snippet sqldatabase/sqldatabase.cpp 22 @@ -173,8 +139,6 @@ QSqlQueryModel::~QSqlQueryModel() } /*! - \since 4.1 - Fetches more rows from a database. This only affects databases that don't report back the size of a query (see QSqlDriver::hasFeature()). @@ -196,8 +160,6 @@ void QSqlQueryModel::fetchMore(const QModelIndex &parent) } /*! - \since 4.1 - Returns \c true if it is possible to read more rows from the database. This only affects databases that don't report back the size of a query (see QSqlDriver::hasFeature()). @@ -329,7 +291,6 @@ void QSqlQueryModel::endResetModel() } /*! \fn int QSqlQueryModel::rowCount(const QModelIndex &parent) const - \since 4.1 If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the number of rows of the current @@ -418,6 +379,18 @@ void QSqlQueryModel::queryChange() // do nothing } +#if QT_DEPRECATED_SINCE(6, 2) +/*! + \deprecated [6.2] Use the \c{setQuery(QSqlQuery &&query)} overload instead. + \overload +*/ +void QSqlQueryModel::setQuery(const QSqlQuery &query) +{ + QT_IGNORE_DEPRECATIONS(QSqlQuery copy = query;) + setQuery(std::move(copy)); +} +#endif // QT_DEPRECATED_SINCE(6, 2) + /*! 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 +401,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,25 +418,24 @@ 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()) { - d->error = QSqlError(QLatin1String("Forward-only queries " - "cannot be used in a data model"), + if (d->query.isForwardOnly()) { + d->error = QSqlError("Forward-only queries cannot be used in a data model"_L1, QString(), QSqlError::ConnectionError); endResetModel(); 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); @@ -541,11 +515,11 @@ bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, } /*! - Returns the QSqlQuery associated with this model. + Returns a reference to the const QSqlQuery object associated with this model. \sa setQuery() */ -QSqlQuery QSqlQueryModel::query() const +const QSqlQuery &QSqlQueryModel::query(QT6_IMPL_NEW_OVERLOAD) const { Q_D(const QSqlQueryModel); return d->query; @@ -644,7 +618,7 @@ bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &par d->colOffsets.append(nVal); Q_ASSERT(d->colOffsets.size() >= d->rec.count()); } - for (int i = column + 1; i < d->colOffsets.count(); ++i) + for (qsizetype i = column + 1; i < d->colOffsets.size(); ++i) ++d->colOffsets[i]; } endInsertColumns(); @@ -670,10 +644,9 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par beginRemoveColumns(parent, column, column + count - 1); - int i; - for (i = 0; i < count; ++i) + for (int i = 0; i < count; ++i) d->rec.remove(column); - for (i = column; i < d->colOffsets.count(); ++i) + for (qsizetype i = column; i < d->colOffsets.size(); ++i) d->colOffsets[i] -= count; endRemoveColumns(); @@ -702,3 +675,5 @@ QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const } QT_END_NAMESPACE + +#include "moc_qsqlquerymodel.cpp" diff --git a/src/sql/models/qsqlquerymodel.h b/src/sql/models/qsqlquerymodel.h index 427b369ae2..73308b79e8 100644 --- a/src/sql/models/qsqlquerymodel.h +++ b/src/sql/models/qsqlquerymodel.h @@ -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) 2016 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 #ifndef QSQLQUERYMODEL_H #define QSQLQUERYMODEL_H @@ -76,9 +40,16 @@ public: bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; +#if QT_DEPRECATED_SINCE(6, 2) + QT_DEPRECATED_VERSION_X_6_2("QSqlQuery is not meant to be copied. Pass it by move instead.") void setQuery(const QSqlQuery &query); +#endif + void setQuery(QSqlQuery &&query); void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase()); +#if QT_SQL_REMOVED_SINCE(6, 5) QSqlQuery query() const; +#endif + const QSqlQuery &query(QT6_DECL_NEW_OVERLOAD) const; virtual void clear(); diff --git a/src/sql/models/qsqlquerymodel_p.h b/src/sql/models/qsqlquerymodel_p.h index 64e9aeb3db..a4c408d081 100644 --- a/src/sql/models/qsqlquerymodel_p.h +++ b/src/sql/models/qsqlquerymodel_p.h @@ -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) 2016 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 #ifndef QSQLQUERYMODEL_P_H #define QSQLQUERYMODEL_P_H @@ -57,8 +21,8 @@ #include "QtSql/qsqlquery.h" #include "QtSql/qsqlrecord.h" #include "QtCore/qhash.h" +#include "QtCore/qlist.h" #include "QtCore/qvarlengtharray.h" -#include "QtCore/qvector.h" QT_REQUIRE_CONFIG(sqlmodel); @@ -80,7 +44,7 @@ public: QModelIndex bottom; QSqlRecord rec; uint atEnd : 1; - QVector<QHash<int, QVariant> > headers; + QList<QHash<int, QVariant>> headers; QVarLengthArray<int, 56> colOffsets; // used to calculate indexInQuery of columns int nestedResetLevel; }; @@ -90,22 +54,22 @@ class QSqlQueryModelSql { public: // SQL keywords - inline const static QLatin1String as() { return QLatin1String("AS"); } - inline const static QLatin1String asc() { return QLatin1String("ASC"); } - inline const static QLatin1String comma() { return QLatin1String(","); } - inline const static QLatin1String desc() { return QLatin1String("DESC"); } - inline const static QLatin1String eq() { return QLatin1String("="); } + inline const static QLatin1StringView as() { return QLatin1StringView("AS"); } + inline const static QLatin1StringView asc() { return QLatin1StringView("ASC"); } + inline const static QLatin1StringView comma() { return QLatin1StringView(","); } + inline const static QLatin1StringView desc() { return QLatin1StringView("DESC"); } + inline const static QLatin1StringView eq() { return QLatin1StringView("="); } // "and" is a C++ keyword - inline const static QLatin1String et() { return QLatin1String("AND"); } - inline const static QLatin1String from() { return QLatin1String("FROM"); } - inline const static QLatin1String leftJoin() { return QLatin1String("LEFT JOIN"); } - inline const static QLatin1String on() { return QLatin1String("ON"); } - inline const static QLatin1String orderBy() { return QLatin1String("ORDER BY"); } - inline const static QLatin1String parenClose() { return QLatin1String(")"); } - inline const static QLatin1String parenOpen() { return QLatin1String("("); } - inline const static QLatin1String select() { return QLatin1String("SELECT"); } - inline const static QLatin1String sp() { return QLatin1String(" "); } - inline const static QLatin1String where() { return QLatin1String("WHERE"); } + inline const static QLatin1StringView et() { return QLatin1StringView("AND"); } + inline const static QLatin1StringView from() { return QLatin1StringView("FROM"); } + inline const static QLatin1StringView leftJoin() { return QLatin1StringView("LEFT JOIN"); } + inline const static QLatin1StringView on() { return QLatin1StringView("ON"); } + inline const static QLatin1StringView orderBy() { return QLatin1StringView("ORDER BY"); } + inline const static QLatin1StringView parenClose() { return QLatin1StringView(")"); } + inline const static QLatin1StringView parenOpen() { return QLatin1StringView("("); } + inline const static QLatin1StringView select() { return QLatin1StringView("SELECT"); } + inline const static QLatin1StringView sp() { return QLatin1StringView(" "); } + inline const static QLatin1StringView where() { return QLatin1StringView("WHERE"); } // Build expressions based on key words inline const static QString as(const QString &a, const QString &b) { return b.isEmpty() ? a : concat(concat(a, as()), b); } diff --git a/src/sql/models/qsqlrelationaldelegate.cpp b/src/sql/models/qsqlrelationaldelegate.cpp index f01548baf9..ee9bc668b6 100644 --- a/src/sql/models/qsqlrelationaldelegate.cpp +++ b/src/sql/models/qsqlrelationaldelegate.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) 2016 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 "qglobal.h" diff --git a/src/sql/models/qsqlrelationaldelegate.h b/src/sql/models/qsqlrelationaldelegate.h index 2f16a4a54b..35c534eae4 100644 --- a/src/sql/models/qsqlrelationaldelegate.h +++ b/src/sql/models/qsqlrelationaldelegate.h @@ -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) 2016 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 #ifndef QSQLRELATIONALDELEGATE_H #define QSQLRELATIONALDELEGATE_H @@ -46,7 +10,7 @@ QT_REQUIRE_CONFIG(sqlmodel); #ifdef QT_WIDGETS_LIB -#include <QtWidgets/qitemdelegate.h> +#include <QtWidgets/qstyleditemdelegate.h> #if QT_CONFIG(listview) #include <QtWidgets/qlistview.h> #endif @@ -58,8 +22,7 @@ QT_REQUIRE_CONFIG(sqlmodel); #include <QtCore/qmetaobject.h> QT_BEGIN_NAMESPACE -// ### Qt6: QStyledItemDelegate -class QSqlRelationalDelegate: public QItemDelegate +class QSqlRelationalDelegate : public QStyledItemDelegate { static int fieldIndex(const QSqlTableModel *const model, const QSqlDriver *const driver, @@ -73,31 +36,31 @@ class QSqlRelationalDelegate: public QItemDelegate public: -explicit QSqlRelationalDelegate(QObject *aParent = nullptr) - : QItemDelegate(aParent) -{} + explicit QSqlRelationalDelegate(QObject *aParent = nullptr) + : QStyledItemDelegate(aParent) + {} -~QSqlRelationalDelegate() -{} + ~QSqlRelationalDelegate() + {} -QWidget *createEditor(QWidget *aParent, - const QStyleOptionViewItem &option, - const QModelIndex &index) const override -{ - const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model()); - QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; - if (!childModel) - return QItemDelegate::createEditor(aParent, option, index); - const QSqlDriver *const driver = childModel->database().driver(); - - QComboBox *combo = new QComboBox(aParent); - combo->setModel(childModel); - combo->setModelColumn(fieldIndex(childModel, driver, - sqlModel->relation(index.column()).displayColumn())); - combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this)); - - return combo; -} + QWidget *createEditor(QWidget *aParent, + const QStyleOptionViewItem &option, + const QModelIndex &index) const override + { + const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model()); + QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; + if (!childModel) + return QStyledItemDelegate::createEditor(aParent, option, index); + const QSqlDriver *const driver = childModel->database().driver(); + + QComboBox *combo = new QComboBox(aParent); + combo->setModel(childModel); + combo->setModelColumn(fieldIndex(childModel, driver, + sqlModel->relation(index.column()).displayColumn())); + combo->installEventFilter(const_cast<QSqlRelationalDelegate *>(this)); + + return combo; + } void setEditorData(QWidget *editor, const QModelIndex &index) const override { @@ -112,12 +75,12 @@ QWidget *createEditor(QWidget *aParent, const QByteArray n = editor->metaObject()->userProperty().name(); if (!n.isEmpty()) { if (!v.isValid()) - v = QVariant(editor->property(n.data()).userType(), nullptr); + v = QVariant(editor->property(n.data()).metaType()); editor->setProperty(n.data(), v); return; } } - QItemDelegate::setEditorData(editor, index); + QStyledItemDelegate::setEditorData(editor, index); } void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override @@ -129,7 +92,7 @@ void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr; QComboBox *combo = qobject_cast<QComboBox *>(editor); if (!sqlModel || !childModel || !combo) { - QItemDelegate::setModelData(editor, model, index); + QStyledItemDelegate::setModelData(editor, model, index); return; } const QSqlDriver *const driver = childModel->database().driver(); diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp index b0e26d7378..1218514778 100644 --- a/src/sql/models/qsqlrelationaltablemodel.cpp +++ b/src/sql/models/qsqlrelationaltablemodel.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) 2016 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 "qsqlrelationaltablemodel.h" @@ -55,13 +19,15 @@ QT_BEGIN_NAMESPACE -class QSqlRelationalTableModelSql: public QSqlTableModelSql +using namespace Qt::StringLiterals; + +class QSqlRelationalTableModelSql: public QSqlQueryModelSql { public: - inline const static QString relTablePrefix(int i) { return QString::number(i).prepend(QLatin1String("relTblAl_")); } + inline const static QString relTablePrefix(int i) { return QString::number(i).prepend("relTblAl_"_L1); } }; -typedef QSqlRelationalTableModelSql Sql; +using SqlrTm = QSqlRelationalTableModelSql; /*! \class QSqlRelation @@ -137,17 +103,17 @@ class QRelatedTableModel; struct QRelation { public: - QRelation(): model(0), m_parent(0), m_dictInitialized(false) {} + QRelation(): model(nullptr), m_parent(nullptr), m_dictInitialized(false) {} void init(QSqlRelationalTableModel *parent, const QSqlRelation &relation); void populateModel(); - bool isDictionaryInitialized(); + bool isDictionaryInitialized() const; void populateDictionary(); void clearDictionary(); void clear(); - bool isValid(); + bool isValid() const; QSqlRelation rel; QRelatedTableModel *model; @@ -161,7 +127,7 @@ struct QRelation class QRelatedTableModel : public QSqlTableModel { public: - QRelatedTableModel(QRelation *rel, QObject *parent = 0, const QSqlDatabase &db = QSqlDatabase()); + QRelatedTableModel(QRelation *rel, QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase()); bool select() override; private: bool firstSelect; @@ -174,7 +140,7 @@ private: */ void QRelation::init(QSqlRelationalTableModel *parent, const QSqlRelation &relation) { - Q_ASSERT(parent != NULL); + Q_ASSERT(parent != nullptr); m_parent = parent; rel = relation; } @@ -183,7 +149,7 @@ void QRelation::populateModel() { if (!isValid()) return; - Q_ASSERT(m_parent != NULL); + Q_ASSERT(m_parent != nullptr); if (!model) { model = new QRelatedTableModel(this, m_parent, m_parent->database()); @@ -192,7 +158,7 @@ void QRelation::populateModel() } } -bool QRelation::isDictionaryInitialized() +bool QRelation::isDictionaryInitialized() const { return m_dictInitialized; } @@ -202,7 +168,7 @@ void QRelation::populateDictionary() if (!isValid()) return; - if (model == NULL) + if (model == nullptr) populateModel(); QSqlRecord record; @@ -234,13 +200,13 @@ void QRelation::clearDictionary() void QRelation::clear() { delete model; - model = 0; + model = nullptr; clearDictionary(); } -bool QRelation::isValid() +bool QRelation::isValid() const { - return (rel.isValid() && m_parent != NULL); + return (rel.isValid() && m_parent != nullptr); } @@ -275,7 +241,7 @@ public: QString fullyQualifiedFieldName(const QString &tableName, const QString &fieldName) const; int nameToIndex(const QString &name) const override; - mutable QVector<QRelation> relations; + mutable QList<QRelation> relations; QSqlRecord baseRec; // the record without relations void clearChanges(); void clearCache() override; @@ -287,10 +253,8 @@ public: void QSqlRelationalTableModelPrivate::clearChanges() { - for (int i = 0; i < relations.count(); ++i) { - QRelation &rel = relations[i]; + for (auto &rel : relations) rel.clear(); - } } void QSqlRelationalTableModelPrivate::revertCachedRow(int row) @@ -311,8 +275,8 @@ int QSqlRelationalTableModelPrivate::nameToIndex(const QString &name) const void QSqlRelationalTableModelPrivate::clearCache() { - for (int i = 0; i < relations.count(); ++i) - relations[i].clearDictionary(); + for (auto &rel : relations) + rel.clearDictionary(); QSqlTableModelPrivate::clearCache(); } @@ -429,7 +393,7 @@ QVariant QSqlRelationalTableModel::data(const QModelIndex &index, int role) cons { Q_D(const QSqlRelationalTableModel); - if (role == Qt::DisplayRole && index.column() >= 0 && index.column() < d->relations.count() && + if (role == Qt::DisplayRole && index.column() >= 0 && index.column() < d->relations.size() && d->relations.value(index.column()).isValid()) { QRelation &relation = d->relations[index.column()]; if (!relation.isDictionaryInitialized()) @@ -472,7 +436,7 @@ bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant int role) { Q_D(QSqlRelationalTableModel); - if ( role == Qt::EditRole && index.column() > 0 && index.column() < d->relations.count() + if ( role == Qt::EditRole && index.column() > 0 && index.column() < d->relations.size() && d->relations.value(index.column()).isValid()) { QRelation &relation = d->relations[index.column()]; if (!relation.isDictionaryInitialized()) @@ -528,7 +492,7 @@ QString QSqlRelationalTableModelPrivate::fullyQualifiedFieldName(const QString & { QString ret; ret.reserve(tableName.size() + fieldName.size() + 1); - ret.append(tableName).append(QLatin1Char('.')).append(fieldName); + ret.append(tableName).append(u'.').append(fieldName); return ret; } @@ -574,12 +538,12 @@ QString QSqlRelationalTableModel::selectStatement() const QString fList; QString conditions; - QString from = Sql::from(tableName()); + QString from = SqlrTm::from(tableName()); for (int i = 0; i < d->baseRec.count(); ++i) { QSqlRelation relation = d->relations.value(i).rel; const QString tableField = d->fullyQualifiedFieldName(tableName(), d->db.driver()->escapeIdentifier(d->baseRec.fieldName(i), QSqlDriver::FieldName)); if (relation.isValid()) { - const QString relTableAlias = Sql::relTablePrefix(i); + const QString relTableAlias = SqlrTm::relTablePrefix(i); QString displayTableField = d->fullyQualifiedFieldName(relTableAlias, relation.displayColumn()); // Duplicate field names must be aliased @@ -590,38 +554,40 @@ QString QSqlRelationalTableModel::selectStatement() const QString displayColumn = relation.displayColumn(); if (d->db.driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName)) displayColumn = d->db.driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); - const QString alias = QString::fromLatin1("%1_%2_%3") + QString alias = QString::fromLatin1("%1_%2_%3") .arg(relTableName, displayColumn, QString::number(fieldNames.value(fieldList[i]))); - displayTableField = Sql::as(displayTableField, alias); + alias.truncate(d->db.driver()->maximumIdentifierLength(QSqlDriver::FieldName)); + alias = d->db.driver()->escapeIdentifier(alias, QSqlDriver::FieldName); + displayTableField = SqlrTm::as(displayTableField, alias); --fieldNames[fieldList[i]]; } - fList = Sql::comma(fList, displayTableField); + fList = SqlrTm::comma(fList, displayTableField); // Join related table - const QString tblexpr = Sql::concat(relation.tableName(), relTableAlias); + const QString tblexpr = SqlrTm::concat(relation.tableName(), relTableAlias); const QString relTableField = d->fullyQualifiedFieldName(relTableAlias, relation.indexColumn()); - const QString cond = Sql::eq(tableField, relTableField); + const QString cond = SqlrTm::eq(tableField, relTableField); if (d->joinMode == QSqlRelationalTableModel::InnerJoin) { // FIXME: InnerJoin code is known to be broken. // Use LeftJoin mode if you want correct behavior. - from = Sql::comma(from, tblexpr); - conditions = Sql::et(conditions, cond); + from = SqlrTm::comma(from, tblexpr); + conditions = SqlrTm::et(conditions, cond); } else { - from = Sql::concat(from, Sql::leftJoin(tblexpr)); - from = Sql::concat(from, Sql::on(cond)); + from = SqlrTm::concat(from, SqlrTm::leftJoin(tblexpr)); + from = SqlrTm::concat(from, SqlrTm::on(cond)); } } else { - fList = Sql::comma(fList, tableField); + fList = SqlrTm::comma(fList, tableField); } } if (fList.isEmpty()) return QString(); - const QString stmt = Sql::concat(Sql::select(fList), from); - const QString where = Sql::where(Sql::et(Sql::paren(conditions), Sql::paren(filter()))); - return Sql::concat(Sql::concat(stmt, where), orderByClause()); + const QString stmt = SqlrTm::concat(SqlrTm::select(fList), from); + const QString where = SqlrTm::where(SqlrTm::et(SqlrTm::paren(conditions), SqlrTm::paren(filter()))); + return SqlrTm::concat(SqlrTm::concat(stmt, where), orderByClause()); } /*! @@ -636,7 +602,7 @@ QString QSqlRelationalTableModel::selectStatement() const QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const { Q_D(const QSqlRelationalTableModel); - if (column < 0 || column >= d->relations.count()) + if (column < 0 || column >= d->relations.size()) return nullptr; QRelation &relation = const_cast<QSqlRelationalTableModelPrivate *>(d)->relations[column]; @@ -676,7 +642,6 @@ void QSqlRelationalTableModel::clear() \value LeftJoin - Left join mode, returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). \sa QSqlRelationalTableModel::setJoinMode() - \since 4.8 */ /*! @@ -685,7 +650,6 @@ void QSqlRelationalTableModel::clear() LeftJoin mode if you want to show them. \sa QSqlRelationalTableModel::JoinMode - \since 4.8 */ void QSqlRelationalTableModel::setJoinMode( QSqlRelationalTableModel::JoinMode joinMode ) { @@ -765,9 +729,9 @@ QString QSqlRelationalTableModel::orderByClause() const if (!rel.isValid()) return QSqlTableModel::orderByClause(); - QString f = d->fullyQualifiedFieldName(Sql::relTablePrefix(d->sortColumn), rel.displayColumn()); - f = d->sortOrder == Qt::AscendingOrder ? Sql::asc(f) : Sql::desc(f); - return Sql::orderBy(f); + QString f = d->fullyQualifiedFieldName(SqlrTm::relTablePrefix(d->sortColumn), rel.displayColumn()); + f = d->sortOrder == Qt::AscendingOrder ? SqlrTm::asc(f) : SqlrTm::desc(f); + return SqlrTm::orderBy(f); } /*! @@ -782,10 +746,12 @@ bool QSqlRelationalTableModel::removeColumns(int column, int count, const QModel for (int i = 0; i < count; ++i) { d->baseRec.remove(column); - if (d->relations.count() > column) + if (d->relations.size() > column) d->relations.remove(column); } return QSqlTableModel::removeColumns(column, count, parent); } QT_END_NAMESPACE + +#include "moc_qsqlrelationaltablemodel.cpp" diff --git a/src/sql/models/qsqlrelationaltablemodel.h b/src/sql/models/qsqlrelationaltablemodel.h index 2ebadaabd1..814bae41bb 100644 --- a/src/sql/models/qsqlrelationaltablemodel.h +++ b/src/sql/models/qsqlrelationaltablemodel.h @@ -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) 2016 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 #ifndef QSQLRELATIONALTABLEMODEL_H #define QSQLRELATIONALTABLEMODEL_H @@ -60,9 +24,9 @@ public: void swap(QSqlRelation &other) noexcept { - qSwap(tName, other.tName); - qSwap(iColumn, other.iColumn); - qSwap(dColumn, other.dColumn); + tName.swap(other.tName); + iColumn.swap(other.iColumn); + dColumn.swap(other.dColumn); } inline QString tableName() const @@ -76,7 +40,7 @@ public: private: QString tName, iColumn, dColumn; }; -Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QSqlRelation) +Q_DECLARE_SHARED(QSqlRelation) class QSqlRelationalTableModelPrivate; 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" diff --git a/src/sql/models/qsqltablemodel.h b/src/sql/models/qsqltablemodel.h index 1ac8bd9b04..8af3f84c59 100644 --- a/src/sql/models/qsqltablemodel.h +++ b/src/sql/models/qsqltablemodel.h @@ -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) 2016 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 #ifndef QSQLTABLEMODEL_H #define QSQLTABLEMODEL_H @@ -58,6 +22,7 @@ class Q_SQL_EXPORT QSqlTableModel: public QSqlQueryModel { Q_OBJECT Q_DECLARE_PRIVATE(QSqlTableModel) + Q_MOC_INCLUDE(<QtSql/qsqlrecord.h>) public: enum EditStrategy {OnFieldChange, OnRowChange, OnManualSubmit}; @@ -74,9 +39,7 @@ public: QSqlRecord record(int row) const; QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; -#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) bool clearItemData(const QModelIndex &index) override; -#endif QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; @@ -136,7 +99,9 @@ protected: virtual QString selectStatement() const; void setPrimaryKey(const QSqlIndex &key); +#if QT_SQL_REMOVED_SINCE(6, 5) void setQuery(const QSqlQuery &query); +#endif QModelIndex indexInQuery(const QModelIndex &item) const override; QSqlRecord primaryValues(int row) const; }; diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h index 9ac34e7259..864c977ebb 100644 --- a/src/sql/models/qsqltablemodel_p.h +++ b/src/sql/models/qsqltablemodel_p.h @@ -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) 2016 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 #ifndef QSQLTABLEMODEL_P_H #define QSQLTABLEMODEL_P_H @@ -71,11 +35,10 @@ public: strategy(QSqlTableModel::OnRowChange), busyInsertingRows(false) {} - ~QSqlTableModelPrivate(); void clear(); virtual void clearCache(); - QSqlRecord record(const QVector<QVariant> &values) const; + QSqlRecord record(const QList<QVariant> &values) const; bool exec(const QString &stmt, bool prepStatement, const QSqlRecord &rec, const QSqlRecord &whereValues); @@ -190,11 +153,6 @@ public: CacheMap cache; }; -class QSqlTableModelSql: public QSqlQueryModelSql -{ -public: -}; - QT_END_NAMESPACE #endif // QSQLTABLEMODEL_P_H |