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 QSQLTABLEMODEL_H
5#define QSQLTABLEMODEL_H
6
7#include <QtSql/qtsqlglobal.h>
8#include <QtSql/qsqldatabase.h>
9#include <QtSql/qsqlquerymodel.h>
10
11QT_REQUIRE_CONFIG(sqlmodel);
12
13QT_BEGIN_NAMESPACE
14
15
16class QSqlTableModelPrivate;
17class QSqlRecord;
18class QSqlField;
19class QSqlIndex;
20
21class Q_SQL_EXPORT QSqlTableModel: public QSqlQueryModel
22{
23 Q_OBJECT
24 Q_DECLARE_PRIVATE(QSqlTableModel)
25 Q_MOC_INCLUDE(<QtSql/qsqlrecord.h>)
26
27public:
28 enum EditStrategy {OnFieldChange, OnRowChange, OnManualSubmit};
29
30 explicit QSqlTableModel(QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase());
31 virtual ~QSqlTableModel();
32
33 virtual void setTable(const QString &tableName);
34 QString tableName() const;
35
36 Qt::ItemFlags flags(const QModelIndex &index) const override;
37
38 QSqlRecord record() const;
39 QSqlRecord record(int row) const;
40 QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override;
41 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
42 bool clearItemData(const QModelIndex &index) override;
43
44 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
45
46 bool isDirty() const;
47 bool isDirty(const QModelIndex &index) const;
48
49 void clear() override;
50
51 virtual void setEditStrategy(EditStrategy strategy);
52 EditStrategy editStrategy() const;
53
54 QSqlIndex primaryKey() const;
55 QSqlDatabase database() const;
56 int fieldIndex(const QString &fieldName) const;
57
58 void sort(int column, Qt::SortOrder order) override;
59 virtual void setSort(int column, Qt::SortOrder order);
60
61 QString filter() const;
62 virtual void setFilter(const QString &filter);
63
64 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
65
66 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
67 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
68 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
69
70 bool insertRecord(int row, const QSqlRecord &record);
71 bool setRecord(int row, const QSqlRecord &record);
72
73 virtual void revertRow(int row);
74
75public Q_SLOTS:
76 virtual bool select();
77 virtual bool selectRow(int row);
78
79 bool submit() override;
80 void revert() override;
81
82 bool submitAll();
83 void revertAll();
84
85Q_SIGNALS:
86 void primeInsert(int row, QSqlRecord &record);
87
88 void beforeInsert(QSqlRecord &record);
89 void beforeUpdate(int row, QSqlRecord &record);
90 void beforeDelete(int row);
91
92protected:
93 QSqlTableModel(QSqlTableModelPrivate &dd, QObject *parent = nullptr, const QSqlDatabase &db = QSqlDatabase());
94
95 virtual bool updateRowInTable(int row, const QSqlRecord &values);
96 virtual bool insertRowIntoTable(const QSqlRecord &values);
97 virtual bool deleteRowFromTable(int row);
98 virtual QString orderByClause() const;
99 virtual QString selectStatement() const;
100
101 void setPrimaryKey(const QSqlIndex &key);
102#if QT_SQL_REMOVED_SINCE(6, 5)
103 void setQuery(const QSqlQuery &query);
104#endif
105 QModelIndex indexInQuery(const QModelIndex &item) const override;
106 QSqlRecord primaryValues(int row) const;
107};
108
109QT_END_NAMESPACE
110
111#endif // QSQLTABLEMODEL_H
112

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