1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSQLRELATIONALTABLEMODEL_H
5#define QSQLRELATIONALTABLEMODEL_H
6
7#include <QtSql/qtsqlglobal.h>
8#include <QtSql/qsqltablemodel.h>
9
10#include <QtCore/qtypeinfo.h>
11
12QT_REQUIRE_CONFIG(sqlmodel);
13
14QT_BEGIN_NAMESPACE
15
16
17class Q_SQL_EXPORT QSqlRelation
18{
19public:
20 QSqlRelation() {}
21 QSqlRelation(const QString &aTableName, const QString &indexCol,
22 const QString &displayCol)
23 : tName(aTableName), iColumn(indexCol), dColumn(displayCol) {}
24
25 void swap(QSqlRelation &other) noexcept
26 {
27 tName.swap(other&: other.tName);
28 iColumn.swap(other&: other.iColumn);
29 dColumn.swap(other&: other.dColumn);
30 }
31
32 inline QString tableName() const
33 { return tName; }
34 inline QString indexColumn() const
35 { return iColumn; }
36 inline QString displayColumn() const
37 { return dColumn; }
38 bool isValid() const noexcept
39 { return !(tName.isEmpty() || iColumn.isEmpty() || dColumn.isEmpty()); }
40private:
41 QString tName, iColumn, dColumn;
42};
43Q_DECLARE_SHARED(QSqlRelation)
44
45class QSqlRelationalTableModelPrivate;
46
47class Q_SQL_EXPORT QSqlRelationalTableModel: public QSqlTableModel
48{
49 Q_OBJECT
50
51public:
52 enum JoinMode {
53 InnerJoin,
54 LeftJoin
55 };
56
57 explicit QSqlRelationalTableModel(QObject *parent = nullptr,
58 const QSqlDatabase &db = QSqlDatabase());
59 virtual ~QSqlRelationalTableModel();
60
61 QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const override;
62 bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole) override;
63 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
64
65 void clear() override;
66 bool select() override;
67
68 void setTable(const QString &tableName) override;
69 virtual void setRelation(int column, const QSqlRelation &relation);
70 QSqlRelation relation(int column) const;
71 virtual QSqlTableModel *relationModel(int column) const;
72 void setJoinMode( QSqlRelationalTableModel::JoinMode joinMode );
73
74public Q_SLOTS:
75 void revertRow(int row) override;
76
77protected:
78 QString selectStatement() const override;
79 bool updateRowInTable(int row, const QSqlRecord &values) override;
80 bool insertRowIntoTable(const QSqlRecord &values) override;
81 QString orderByClause() const override;
82
83private:
84 Q_DECLARE_PRIVATE(QSqlRelationalTableModel)
85};
86
87QT_END_NAMESPACE
88
89#endif // QSQLRELATIONALTABLEMODEL_H
90

source code of qtbase/src/sql/models/qsqlrelationaltablemodel.h