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