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_P_H
5#define QSQLTABLEMODEL_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of qsql*model.h . This header file may change from version to version
13// without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtSql/private/qtsqlglobal_p.h>
19#include "private/qsqlquerymodel_p.h"
20#include "QtSql/qsqlindex.h"
21#include "QtCore/qmap.h"
22
23QT_REQUIRE_CONFIG(sqlmodel);
24
25QT_BEGIN_NAMESPACE
26
27class Q_AUTOTEST_EXPORT QSqlTableModelPrivate: public QSqlQueryModelPrivate
28{
29 Q_DECLARE_PUBLIC(QSqlTableModel)
30
31public:
32 QSqlTableModelPrivate()
33 : sortColumn(-1),
34 sortOrder(Qt::AscendingOrder),
35 strategy(QSqlTableModel::OnRowChange),
36 busyInsertingRows(false)
37 {}
38 ~QSqlTableModelPrivate();
39
40 void clear();
41 virtual void clearCache();
42 QSqlRecord record(const QList<QVariant> &values) const;
43
44 bool exec(const QString &stmt, bool prepStatement,
45 const QSqlRecord &rec, const QSqlRecord &whereValues);
46 virtual void revertCachedRow(int row);
47 virtual int nameToIndex(const QString &name) const;
48 QString strippedFieldName(const QString &name) const;
49 int insertCount(int maxRow = -1) const;
50 void initRecordAndPrimaryIndex();
51
52 QSqlDatabase db;
53
54 int sortColumn;
55 Qt::SortOrder sortOrder;
56
57 QSqlTableModel::EditStrategy strategy;
58 bool busyInsertingRows;
59
60 QSqlQuery editQuery = { QSqlQuery(nullptr) };
61 QSqlIndex primaryIndex;
62 QString tableName;
63 QString filter;
64 QString autoColumn;
65
66 enum Op { None, Insert, Update, Delete };
67
68 class ModifiedRow
69 {
70 public:
71 inline ModifiedRow(Op o = None, const QSqlRecord &r = QSqlRecord())
72 : m_op(None), m_db_values(r), m_insert(o == Insert)
73 { setOp(o); }
74 inline Op op() const { return m_op; }
75 inline void setOp(Op o)
76 {
77 if (o == None)
78 m_submitted = true;
79 if (o == m_op)
80 return;
81 m_submitted = (o != Insert && o != Delete);
82 m_op = o;
83 m_rec = m_db_values;
84 setGenerated(r&: m_rec, g: m_op == Delete);
85 }
86 inline const QSqlRecord &rec() const { return m_rec; }
87 inline QSqlRecord& recRef() { return m_rec; }
88 inline void setValue(int c, const QVariant &v)
89 {
90 m_submitted = false;
91 m_rec.setValue(i: c, val: v);
92 m_rec.setGenerated(i: c, generated: true);
93 }
94 inline bool submitted() const { return m_submitted; }
95 inline void setSubmitted()
96 {
97 m_submitted = true;
98 setGenerated(r&: m_rec, g: false);
99 if (m_op == Delete) {
100 m_rec.clearValues();
101 }
102 else {
103 m_op = Update;
104 m_db_values = m_rec;
105 setGenerated(r&: m_db_values, g: true);
106 }
107 }
108 inline void refresh(bool exists, const QSqlRecord& newvals)
109 {
110 m_submitted = true;
111 if (exists) {
112 m_op = Update;
113 m_db_values = newvals;
114 m_rec = newvals;
115 setGenerated(r&: m_rec, g: false);
116 } else {
117 m_op = Delete;
118 m_rec.clear();
119 m_db_values.clear();
120 }
121 }
122 inline bool insert() const { return m_insert; }
123 inline void revert()
124 {
125 if (m_submitted)
126 return;
127 if (m_op == Delete)
128 m_op = Update;
129 m_rec = m_db_values;
130 setGenerated(r&: m_rec, g: false);
131 m_submitted = true;
132 }
133 inline QSqlRecord primaryValues(const QSqlRecord& pi) const
134 {
135 if (m_op == None || m_op == Insert)
136 return QSqlRecord();
137
138 return m_db_values.keyValues(keyFields: pi);
139 }
140 private:
141 inline static void setGenerated(QSqlRecord& r, bool g)
142 {
143 for (int i = r.count() - 1; i >= 0; --i)
144 r.setGenerated(i, generated: g);
145 }
146 Op m_op;
147 QSqlRecord m_rec;
148 QSqlRecord m_db_values;
149 bool m_submitted;
150 bool m_insert;
151 };
152
153 typedef QMap<int, ModifiedRow> CacheMap;
154 CacheMap cache;
155};
156
157class QSqlTableModelSql: public QSqlQueryModelSql
158{
159public:
160};
161
162QT_END_NAMESPACE
163
164#endif // QSQLTABLEMODEL_P_H
165

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