| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSql module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QSQLRELATIONALTABLEMODEL_H |
| 41 | #define QSQLRELATIONALTABLEMODEL_H |
| 42 | |
| 43 | #include <QtSql/qtsqlglobal.h> |
| 44 | #include <QtSql/qsqltablemodel.h> |
| 45 | |
| 46 | #include <QtCore/qtypeinfo.h> |
| 47 | |
| 48 | QT_REQUIRE_CONFIG(sqlmodel); |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | |
| 53 | class Q_SQL_EXPORT QSqlRelation |
| 54 | { |
| 55 | public: |
| 56 | QSqlRelation() {} |
| 57 | QSqlRelation(const QString &aTableName, const QString &indexCol, |
| 58 | const QString &displayCol) |
| 59 | : tName(aTableName), iColumn(indexCol), dColumn(displayCol) {} |
| 60 | |
| 61 | void swap(QSqlRelation &other) noexcept |
| 62 | { |
| 63 | qSwap(value1&: tName, value2&: other.tName); |
| 64 | qSwap(value1&: iColumn, value2&: other.iColumn); |
| 65 | qSwap(value1&: dColumn, value2&: other.dColumn); |
| 66 | } |
| 67 | |
| 68 | inline QString tableName() const |
| 69 | { return tName; } |
| 70 | inline QString indexColumn() const |
| 71 | { return iColumn; } |
| 72 | inline QString displayColumn() const |
| 73 | { return dColumn; } |
| 74 | bool isValid() const noexcept |
| 75 | { return !(tName.isEmpty() || iColumn.isEmpty() || dColumn.isEmpty()); } |
| 76 | private: |
| 77 | QString tName, iColumn, dColumn; |
| 78 | }; |
| 79 | Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QSqlRelation) |
| 80 | |
| 81 | class QSqlRelationalTableModelPrivate; |
| 82 | |
| 83 | class Q_SQL_EXPORT QSqlRelationalTableModel: public QSqlTableModel |
| 84 | { |
| 85 | Q_OBJECT |
| 86 | |
| 87 | public: |
| 88 | enum JoinMode { |
| 89 | InnerJoin, |
| 90 | LeftJoin |
| 91 | }; |
| 92 | |
| 93 | explicit QSqlRelationalTableModel(QObject *parent = nullptr, |
| 94 | QSqlDatabase db = QSqlDatabase()); |
| 95 | virtual ~QSqlRelationalTableModel(); |
| 96 | |
| 97 | QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const override; |
| 98 | bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole) override; |
| 99 | bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; |
| 100 | |
| 101 | void clear() override; |
| 102 | bool select() override; |
| 103 | |
| 104 | void setTable(const QString &tableName) override; |
| 105 | virtual void setRelation(int column, const QSqlRelation &relation); |
| 106 | QSqlRelation relation(int column) const; |
| 107 | virtual QSqlTableModel *relationModel(int column) const; |
| 108 | void setJoinMode( QSqlRelationalTableModel::JoinMode joinMode ); |
| 109 | |
| 110 | public Q_SLOTS: |
| 111 | void revertRow(int row) override; |
| 112 | |
| 113 | protected: |
| 114 | QString selectStatement() const override; |
| 115 | bool updateRowInTable(int row, const QSqlRecord &values) override; |
| 116 | bool insertRowIntoTable(const QSqlRecord &values) override; |
| 117 | QString orderByClause() const override; |
| 118 | |
| 119 | private: |
| 120 | Q_DECLARE_PRIVATE(QSqlRelationalTableModel) |
| 121 | }; |
| 122 | |
| 123 | QT_END_NAMESPACE |
| 124 | |
| 125 | #endif // QSQLRELATIONALTABLEMODEL_H |
| 126 | |