Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsqltablemodel_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QSQLTABLEMODEL_P_H
6#define QSQLTABLEMODEL_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists for the convenience
13// of qsql*model.h . This header file may change from version to version
14// without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtSql/private/qtsqlglobal_p.h>
20#include "private/qsqlquerymodel_p.h"
21#include "QtSql/qsqlindex.h"
22#include "QtCore/qmap.h"
23
25
27
28class Q_AUTOTEST_EXPORT QSqlTableModelPrivate: public QSqlQueryModelPrivate
29{
30 Q_DECLARE_PUBLIC(QSqlTableModel)
31
32public:
33 QSqlTableModelPrivate()
34 : sortColumn(-1),
35 sortOrder(Qt::AscendingOrder),
36 strategy(QSqlTableModel::OnRowChange),
37 busyInsertingRows(false)
38 {}
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(m_rec, 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(c, v);
92 m_rec.setGenerated(c, true);
93 }
94 inline bool submitted() const { return m_submitted; }
95 inline void setSubmitted()
96 {
97 m_submitted = true;
98 setGenerated(m_rec, 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(m_db_values, 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(m_rec, 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(m_rec, 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(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, 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
157QT_END_NAMESPACE
158
159#endif // QSQLTABLEMODEL_P_H
QRelatedTableModel(QRelation *rel, QObject *parent, const QSqlDatabase &db)
bool select() override
Populates the model with data from the table that was set via setTable(), using the specified filter ...
QList< QSharedPointer< QRelation > > relations
int nameToIndex(const QString &name) const override
void translateFieldNames(QSqlRecord &values) const
static const QString relTablePrefix(int i)
The QSqlRelationalTableModel class provides an editable data model for a single database table,...
QT_REQUIRE_CONFIG(animation)
QT_REQUIRE_CONFIG(sqlmodel)
QSqlRelationalTableModelSql SqlrTm
QRelatedTableModel * model
bool isDictionaryInitialized() const
QHash< QString, QVariant > dictionary
void init(QSqlRelationalTableModel *parent, const QSqlRelation &relation, int column)