Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/models/qsqlrelationaltablemodel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtSql module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qsqlrelationaltablemodel.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include "qhash.h" | - | ||||||||||||||||||
37 | #include "qstringlist.h" | - | ||||||||||||||||||
38 | #include "qsqldatabase.h" | - | ||||||||||||||||||
39 | #include "qsqldriver.h" | - | ||||||||||||||||||
40 | #include "qsqlerror.h" | - | ||||||||||||||||||
41 | #include "qsqlfield.h" | - | ||||||||||||||||||
42 | #include "qsqlindex.h" | - | ||||||||||||||||||
43 | #include "qsqlquery.h" | - | ||||||||||||||||||
44 | #include "qsqlrecord.h" | - | ||||||||||||||||||
45 | - | |||||||||||||||||||
46 | #include "qsqltablemodel_p.h" | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | #include "qdebug.h" | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
51 | - | |||||||||||||||||||
52 | class QSqlRelationalTableModelSql: public QSqlTableModelSql | - | ||||||||||||||||||
53 | { | - | ||||||||||||||||||
54 | public: | - | ||||||||||||||||||
55 | inline const static QString relTablePrefix(int i) { return QString::number(i).prepend(QLatin1String("relTblAl_")); } executed 107 times by 1 test: return QString::number(i).prepend(QLatin1String("relTblAl_")); Executed by:
| 107 | ||||||||||||||||||
56 | }; | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | typedef QSqlRelationalTableModelSql Sql; | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | /*! | - | ||||||||||||||||||
61 | \class QSqlRelation | - | ||||||||||||||||||
62 | \inmodule QtSql | - | ||||||||||||||||||
63 | \brief The QSqlRelation class stores information about an SQL foreign key. | - | ||||||||||||||||||
64 | - | |||||||||||||||||||
65 | QSqlRelation is a helper class for QSqlRelationalTableModel. See | - | ||||||||||||||||||
66 | QSqlRelationalTableModel::setRelation() and | - | ||||||||||||||||||
67 | QSqlRelationalTableModel::relation() for details. | - | ||||||||||||||||||
68 | - | |||||||||||||||||||
69 | \sa QSqlRelationalTableModel, QSqlRelationalDelegate, | - | ||||||||||||||||||
70 | {Relational Table Model Example} | - | ||||||||||||||||||
71 | */ | - | ||||||||||||||||||
72 | - | |||||||||||||||||||
73 | /*! | - | ||||||||||||||||||
74 | \fn QSqlRelation::QSqlRelation() | - | ||||||||||||||||||
75 | - | |||||||||||||||||||
76 | Constructs an invalid QSqlRelation object. | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | For such an object, the tableName(), indexColumn(), and | - | ||||||||||||||||||
79 | displayColumn() functions return an empty string. | - | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | \sa isValid() | - | ||||||||||||||||||
82 | */ | - | ||||||||||||||||||
83 | - | |||||||||||||||||||
84 | /*! | - | ||||||||||||||||||
85 | \fn QSqlRelation::QSqlRelation(const QString &tableName, const QString &indexColumn, | - | ||||||||||||||||||
86 | const QString &displayColumn) | - | ||||||||||||||||||
87 | - | |||||||||||||||||||
88 | Constructs a QSqlRelation object, where \a tableName is the SQL | - | ||||||||||||||||||
89 | table name to which a foreign key refers, \a indexColumn is the | - | ||||||||||||||||||
90 | foreign key, and \a displayColumn is the field that should be | - | ||||||||||||||||||
91 | presented to the user. | - | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | \sa tableName(), indexColumn(), displayColumn() | - | ||||||||||||||||||
94 | */ | - | ||||||||||||||||||
95 | - | |||||||||||||||||||
96 | /*! | - | ||||||||||||||||||
97 | \fn QString QSqlRelation::tableName() const | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | Returns the name of the table to which a foreign key refers. | - | ||||||||||||||||||
100 | */ | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | /*! | - | ||||||||||||||||||
103 | \fn QString QSqlRelation::indexColumn() const | - | ||||||||||||||||||
104 | - | |||||||||||||||||||
105 | Returns the index column from table tableName() to which a | - | ||||||||||||||||||
106 | foreign key refers. | - | ||||||||||||||||||
107 | */ | - | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | /*! | - | ||||||||||||||||||
110 | \fn QString QSqlRelation::displayColumn() const | - | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | Returns the column from table tableName() that should be | - | ||||||||||||||||||
113 | presented to the user instead of a foreign key. | - | ||||||||||||||||||
114 | */ | - | ||||||||||||||||||
115 | - | |||||||||||||||||||
116 | /*! | - | ||||||||||||||||||
117 | \fn bool QSqlRelation::isValid() const | - | ||||||||||||||||||
118 | - | |||||||||||||||||||
119 | Returns \c true if the QSqlRelation object is valid; otherwise | - | ||||||||||||||||||
120 | returns \c false. | - | ||||||||||||||||||
121 | */ | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | class QRelatedTableModel; | - | ||||||||||||||||||
124 | - | |||||||||||||||||||
125 | struct QRelation | - | ||||||||||||||||||
126 | { | - | ||||||||||||||||||
127 | public: | - | ||||||||||||||||||
128 | QRelation(): model(0), m_parent(0), m_dictInitialized(false) {} executed 266 times by 1 test: end of block Executed by:
| 266 | ||||||||||||||||||
129 | void init(QSqlRelationalTableModel *parent, const QSqlRelation &relation); | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | void populateModel(); | - | ||||||||||||||||||
132 | - | |||||||||||||||||||
133 | bool isDictionaryInitialized(); | - | ||||||||||||||||||
134 | void populateDictionary(); | - | ||||||||||||||||||
135 | void clearDictionary(); | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | void clear(); | - | ||||||||||||||||||
138 | bool isValid(); | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | QSqlRelation rel; | - | ||||||||||||||||||
141 | QRelatedTableModel *model; | - | ||||||||||||||||||
142 | QHash<QString, QVariant> dictionary;//maps keys to display values | - | ||||||||||||||||||
143 | - | |||||||||||||||||||
144 | private: | - | ||||||||||||||||||
145 | QSqlRelationalTableModel *m_parent; | - | ||||||||||||||||||
146 | bool m_dictInitialized; | - | ||||||||||||||||||
147 | }; | - | ||||||||||||||||||
148 | - | |||||||||||||||||||
149 | class QRelatedTableModel : public QSqlTableModel | - | ||||||||||||||||||
150 | { | - | ||||||||||||||||||
151 | public: | - | ||||||||||||||||||
152 | QRelatedTableModel(QRelation *rel, QObject *parent = 0, QSqlDatabase db = QSqlDatabase()); | - | ||||||||||||||||||
153 | bool select() Q_DECL_OVERRIDE; | - | ||||||||||||||||||
154 | private: | - | ||||||||||||||||||
155 | bool firstSelect; | - | ||||||||||||||||||
156 | QRelation *relation; | - | ||||||||||||||||||
157 | }; | - | ||||||||||||||||||
158 | /* | - | ||||||||||||||||||
159 | A QRelation must be initialized before it is considered valid. | - | ||||||||||||||||||
160 | Note: population of the model and dictionary are kept separate | - | ||||||||||||||||||
161 | from initialization, and are populated on an as needed basis. | - | ||||||||||||||||||
162 | */ | - | ||||||||||||||||||
163 | void QRelation::init(QSqlRelationalTableModel *parent, const QSqlRelation &relation) | - | ||||||||||||||||||
164 | { | - | ||||||||||||||||||
165 | Q_ASSERT(parent != NULL); | - | ||||||||||||||||||
166 | m_parent = parent; | - | ||||||||||||||||||
167 | rel = relation; | - | ||||||||||||||||||
168 | } executed 44 times by 1 test: end of block Executed by:
| 44 | ||||||||||||||||||
169 | - | |||||||||||||||||||
170 | void QRelation::populateModel() | - | ||||||||||||||||||
171 | { | - | ||||||||||||||||||
172 | if (!isValid())
| 0-40 | ||||||||||||||||||
173 | return; never executed: return; | 0 | ||||||||||||||||||
174 | Q_ASSERT(m_parent != NULL); | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | if (!model) {
| 0-40 | ||||||||||||||||||
177 | model = new QRelatedTableModel(this, m_parent, m_parent->database()); | - | ||||||||||||||||||
178 | model->setTable(rel.tableName()); | - | ||||||||||||||||||
179 | model->select(); | - | ||||||||||||||||||
180 | } executed 40 times by 1 test: end of block Executed by:
| 40 | ||||||||||||||||||
181 | } executed 40 times by 1 test: end of block Executed by:
| 40 | ||||||||||||||||||
182 | - | |||||||||||||||||||
183 | bool QRelation::isDictionaryInitialized() | - | ||||||||||||||||||
184 | { | - | ||||||||||||||||||
185 | return m_dictInitialized; executed 191 times by 1 test: return m_dictInitialized; Executed by:
| 191 | ||||||||||||||||||
186 | } | - | ||||||||||||||||||
187 | - | |||||||||||||||||||
188 | void QRelation::populateDictionary() | - | ||||||||||||||||||
189 | { | - | ||||||||||||||||||
190 | if (!isValid())
| 0-66 | ||||||||||||||||||
191 | return; never executed: return; | 0 | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | if (model == NULL)
| 29-37 | ||||||||||||||||||
194 | populateModel(); executed 37 times by 1 test: populateModel(); Executed by:
| 37 | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | QSqlRecord record; | - | ||||||||||||||||||
197 | QString indexColumn; | - | ||||||||||||||||||
198 | QString displayColumn; | - | ||||||||||||||||||
199 | for (int i=0; i < model->rowCount(); ++i) {
| 66-136 | ||||||||||||||||||
200 | record = model->record(i); | - | ||||||||||||||||||
201 | - | |||||||||||||||||||
202 | indexColumn = rel.indexColumn(); | - | ||||||||||||||||||
203 | if (m_parent->database().driver()->isIdentifierEscaped(indexColumn, QSqlDriver::FieldName))
| 14-122 | ||||||||||||||||||
204 | indexColumn = m_parent->database().driver()->stripDelimiters(indexColumn, QSqlDriver::FieldName); executed 14 times by 1 test: indexColumn = m_parent->database().driver()->stripDelimiters(indexColumn, QSqlDriver::FieldName); Executed by:
| 14 | ||||||||||||||||||
205 | - | |||||||||||||||||||
206 | displayColumn = rel.displayColumn(); | - | ||||||||||||||||||
207 | if (m_parent->database().driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName))
| 18-118 | ||||||||||||||||||
208 | displayColumn = m_parent->database().driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); executed 18 times by 1 test: displayColumn = m_parent->database().driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); Executed by:
| 18 | ||||||||||||||||||
209 | - | |||||||||||||||||||
210 | dictionary[record.field(indexColumn).value().toString()] = | - | ||||||||||||||||||
211 | record.field(displayColumn).value(); | - | ||||||||||||||||||
212 | } executed 136 times by 1 test: end of block Executed by:
| 136 | ||||||||||||||||||
213 | m_dictInitialized = true; | - | ||||||||||||||||||
214 | } executed 66 times by 1 test: end of block Executed by:
| 66 | ||||||||||||||||||
215 | - | |||||||||||||||||||
216 | void QRelation::clearDictionary() | - | ||||||||||||||||||
217 | { | - | ||||||||||||||||||
218 | dictionary.clear(); | - | ||||||||||||||||||
219 | m_dictInitialized = false; | - | ||||||||||||||||||
220 | } executed 216 times by 1 test: end of block Executed by:
| 216 | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | void QRelation::clear() | - | ||||||||||||||||||
223 | { | - | ||||||||||||||||||
224 | delete model; | - | ||||||||||||||||||
225 | model = 0; | - | ||||||||||||||||||
226 | clearDictionary(); | - | ||||||||||||||||||
227 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
228 | - | |||||||||||||||||||
229 | bool QRelation::isValid() | - | ||||||||||||||||||
230 | { | - | ||||||||||||||||||
231 | return (rel.isValid() && m_parent != NULL); executed 497 times by 1 test: return (rel.isValid() && m_parent != __null); Executed by:
| 0-497 | ||||||||||||||||||
232 | } | - | ||||||||||||||||||
233 | - | |||||||||||||||||||
234 | - | |||||||||||||||||||
235 | - | |||||||||||||||||||
236 | QRelatedTableModel::QRelatedTableModel(QRelation *rel, QObject *parent, QSqlDatabase db) : | - | ||||||||||||||||||
237 | QSqlTableModel(parent, db), firstSelect(true), relation(rel) | - | ||||||||||||||||||
238 | { | - | ||||||||||||||||||
239 | } executed 40 times by 1 test: end of block Executed by:
| 40 | ||||||||||||||||||
240 | - | |||||||||||||||||||
241 | bool QRelatedTableModel::select() | - | ||||||||||||||||||
242 | { | - | ||||||||||||||||||
243 | if (firstSelect) {
| 1-40 | ||||||||||||||||||
244 | firstSelect = false; | - | ||||||||||||||||||
245 | return QSqlTableModel::select(); executed 40 times by 1 test: return QSqlTableModel::select(); Executed by:
| 40 | ||||||||||||||||||
246 | } | - | ||||||||||||||||||
247 | relation->clearDictionary(); | - | ||||||||||||||||||
248 | bool res = QSqlTableModel::select(); | - | ||||||||||||||||||
249 | if (res)
| 0-1 | ||||||||||||||||||
250 | relation->populateDictionary(); executed 1 time by 1 test: relation->populateDictionary(); Executed by:
| 1 | ||||||||||||||||||
251 | return res; executed 1 time by 1 test: return res; Executed by:
| 1 | ||||||||||||||||||
252 | } | - | ||||||||||||||||||
253 | - | |||||||||||||||||||
254 | - | |||||||||||||||||||
255 | class QSqlRelationalTableModelPrivate: public QSqlTableModelPrivate | - | ||||||||||||||||||
256 | { | - | ||||||||||||||||||
257 | Q_DECLARE_PUBLIC(QSqlRelationalTableModel) | - | ||||||||||||||||||
258 | public: | - | ||||||||||||||||||
259 | QSqlRelationalTableModelPrivate() | - | ||||||||||||||||||
260 | : QSqlTableModelPrivate(), | - | ||||||||||||||||||
261 | joinMode( QSqlRelationalTableModel::InnerJoin ) | - | ||||||||||||||||||
262 | {} executed 31 times by 1 test: end of block Executed by:
| 31 | ||||||||||||||||||
263 | QString fullyQualifiedFieldName(const QString &tableName, const QString &fieldName) const; | - | ||||||||||||||||||
264 | - | |||||||||||||||||||
265 | int nameToIndex(const QString &name) const Q_DECL_OVERRIDE; | - | ||||||||||||||||||
266 | mutable QVector<QRelation> relations; | - | ||||||||||||||||||
267 | QSqlRecord baseRec; // the record without relations | - | ||||||||||||||||||
268 | void clearChanges(); | - | ||||||||||||||||||
269 | void clearCache() Q_DECL_OVERRIDE; | - | ||||||||||||||||||
270 | void revertCachedRow(int row) Q_DECL_OVERRIDE; | - | ||||||||||||||||||
271 | - | |||||||||||||||||||
272 | void translateFieldNames(QSqlRecord &values) const; | - | ||||||||||||||||||
273 | QSqlRelationalTableModel::JoinMode joinMode; | - | ||||||||||||||||||
274 | }; | - | ||||||||||||||||||
275 | - | |||||||||||||||||||
276 | void QSqlRelationalTableModelPrivate::clearChanges() | - | ||||||||||||||||||
277 | { | - | ||||||||||||||||||
278 | for (int i = 0; i < relations.count(); ++i) {
| 4-32 | ||||||||||||||||||
279 | QRelation &rel = relations[i]; | - | ||||||||||||||||||
280 | rel.clear(); | - | ||||||||||||||||||
281 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
282 | } executed 32 times by 1 test: end of block Executed by:
| 32 | ||||||||||||||||||
283 | - | |||||||||||||||||||
284 | void QSqlRelationalTableModelPrivate::revertCachedRow(int row) | - | ||||||||||||||||||
285 | { | - | ||||||||||||||||||
286 | QSqlTableModelPrivate::revertCachedRow(row); | - | ||||||||||||||||||
287 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
288 | - | |||||||||||||||||||
289 | int QSqlRelationalTableModelPrivate::nameToIndex(const QString &name) const | - | ||||||||||||||||||
290 | { | - | ||||||||||||||||||
291 | const QString fieldname = strippedFieldName(name); | - | ||||||||||||||||||
292 | int idx = baseRec.indexOf(fieldname); | - | ||||||||||||||||||
293 | if (idx == -1) {
| 1-16 | ||||||||||||||||||
294 | // If the name is an alias we can find it here. | - | ||||||||||||||||||
295 | idx = QSqlTableModelPrivate::nameToIndex(name); | - | ||||||||||||||||||
296 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
297 | return idx; executed 17 times by 1 test: return idx; Executed by:
| 17 | ||||||||||||||||||
298 | } | - | ||||||||||||||||||
299 | - | |||||||||||||||||||
300 | /*! | - | ||||||||||||||||||
301 | \reimp | - | ||||||||||||||||||
302 | */ | - | ||||||||||||||||||
303 | void QSqlRelationalTableModelPrivate::clearCache() | - | ||||||||||||||||||
304 | { | - | ||||||||||||||||||
305 | for (int i = 0; i < relations.count(); ++i)
| 74-211 | ||||||||||||||||||
306 | relations[i].clearDictionary(); executed 211 times by 1 test: relations[i].clearDictionary(); Executed by:
| 211 | ||||||||||||||||||
307 | - | |||||||||||||||||||
308 | QSqlTableModelPrivate::clearCache(); | - | ||||||||||||||||||
309 | } executed 74 times by 1 test: end of block Executed by:
| 74 | ||||||||||||||||||
310 | - | |||||||||||||||||||
311 | /*! | - | ||||||||||||||||||
312 | \class QSqlRelationalTableModel | - | ||||||||||||||||||
313 | \brief The QSqlRelationalTableModel class provides an editable | - | ||||||||||||||||||
314 | data model for a single database table, with foreign key support. | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | \ingroup database | - | ||||||||||||||||||
317 | \inmodule QtSql | - | ||||||||||||||||||
318 | - | |||||||||||||||||||
319 | QSqlRelationalTableModel acts like QSqlTableModel, but allows | - | ||||||||||||||||||
320 | columns to be set as foreign keys into other database tables. | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | \table | - | ||||||||||||||||||
323 | \row \li \inlineimage noforeignkeys.png | - | ||||||||||||||||||
324 | \li \inlineimage foreignkeys.png | - | ||||||||||||||||||
325 | \endtable | - | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | The screenshot on the left shows a plain QSqlTableModel in a | - | ||||||||||||||||||
328 | QTableView. Foreign keys (\c city and \c country) aren't resolved | - | ||||||||||||||||||
329 | to human-readable values. The screenshot on the right shows a | - | ||||||||||||||||||
330 | QSqlRelationalTableModel, with foreign keys resolved into | - | ||||||||||||||||||
331 | human-readable text strings. | - | ||||||||||||||||||
332 | - | |||||||||||||||||||
333 | The following code snippet shows how the QSqlRelationalTableModel | - | ||||||||||||||||||
334 | was set up: | - | ||||||||||||||||||
335 | - | |||||||||||||||||||
336 | \snippet relationaltablemodel/relationaltablemodel.cpp 0 | - | ||||||||||||||||||
337 | \codeline | - | ||||||||||||||||||
338 | \snippet relationaltablemodel/relationaltablemodel.cpp 1 | - | ||||||||||||||||||
339 | \snippet relationaltablemodel/relationaltablemodel.cpp 2 | - | ||||||||||||||||||
340 | - | |||||||||||||||||||
341 | The setRelation() function calls establish a relationship between | - | ||||||||||||||||||
342 | two tables. The first call specifies that column 2 in table \c | - | ||||||||||||||||||
343 | employee is a foreign key that maps with field \c id of table \c | - | ||||||||||||||||||
344 | city, and that the view should present the \c{city}'s \c name | - | ||||||||||||||||||
345 | field to the user. The second call does something similar with | - | ||||||||||||||||||
346 | column 3. | - | ||||||||||||||||||
347 | - | |||||||||||||||||||
348 | If you use a read-write QSqlRelationalTableModel, you probably | - | ||||||||||||||||||
349 | want to use QSqlRelationalDelegate on the view. Unlike the default | - | ||||||||||||||||||
350 | delegate, QSqlRelationalDelegate provides a combobox for fields | - | ||||||||||||||||||
351 | that are foreign keys into other tables. To use the class, simply | - | ||||||||||||||||||
352 | call QAbstractItemView::setItemDelegate() on the view with an | - | ||||||||||||||||||
353 | instance of QSqlRelationalDelegate: | - | ||||||||||||||||||
354 | - | |||||||||||||||||||
355 | \snippet relationaltablemodel/relationaltablemodel.cpp 4 | - | ||||||||||||||||||
356 | - | |||||||||||||||||||
357 | The \l{relationaltablemodel} example illustrates how to use | - | ||||||||||||||||||
358 | QSqlRelationalTableModel in conjunction with | - | ||||||||||||||||||
359 | QSqlRelationalDelegate to provide tables with foreign key | - | ||||||||||||||||||
360 | support. | - | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | \image relationaltable.png | - | ||||||||||||||||||
363 | - | |||||||||||||||||||
364 | Notes: | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | \list | - | ||||||||||||||||||
367 | \li The table must have a primary key declared. | - | ||||||||||||||||||
368 | \li The table's primary key may not contain a relation to | - | ||||||||||||||||||
369 | another table. | - | ||||||||||||||||||
370 | \li If a relational table contains keys that refer to non-existent | - | ||||||||||||||||||
371 | rows in the referenced table, the rows containing the invalid | - | ||||||||||||||||||
372 | keys will not be exposed through the model. The user or the | - | ||||||||||||||||||
373 | database is responsible for keeping referential integrity. | - | ||||||||||||||||||
374 | \li If a relation's display column name is also used as a column | - | ||||||||||||||||||
375 | name in the relational table, or if it is used as display column | - | ||||||||||||||||||
376 | name in more than one relation it will be aliased. The alias is | - | ||||||||||||||||||
377 | the relation's table name, display column name and a unique id | - | ||||||||||||||||||
378 | joined by an underscore (e.g. tablename_columnname_id). | - | ||||||||||||||||||
379 | QSqlRecord::fieldName() will return the aliased column name. | - | ||||||||||||||||||
380 | All occurrences of the duplicate display column name are aliased when | - | ||||||||||||||||||
381 | duplication is detected, but no aliasing is done to the column | - | ||||||||||||||||||
382 | names in the main table. The aliasing doesn't affect | - | ||||||||||||||||||
383 | QSqlRelation, so QSqlRelation::displayColumn() will return the | - | ||||||||||||||||||
384 | original display column name. | - | ||||||||||||||||||
385 | \li The reference table name is aliased. The alias is the word "relTblAl" | - | ||||||||||||||||||
386 | and the relationed column index joined by an underscore | - | ||||||||||||||||||
387 | (e.g. relTblAl_2). The alias can be used to filter the table | - | ||||||||||||||||||
388 | (For example, setFilter("relTblAl_2='Oslo' OR | - | ||||||||||||||||||
389 | relTblAl_3='USA'")). | - | ||||||||||||||||||
390 | \li When using setData() the role should always be Qt::EditRole, | - | ||||||||||||||||||
391 | and when using data() the role should always be Qt::DisplayRole. | - | ||||||||||||||||||
392 | \endlist | - | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | \sa QSqlRelation, QSqlRelationalDelegate, | - | ||||||||||||||||||
395 | {Relational Table Model Example} | - | ||||||||||||||||||
396 | */ | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | - | |||||||||||||||||||
399 | /*! | - | ||||||||||||||||||
400 | Creates an empty QSqlRelationalTableModel and sets the parent to \a parent | - | ||||||||||||||||||
401 | and the database connection to \a db. If \a db is not valid, the | - | ||||||||||||||||||
402 | default database connection will be used. | - | ||||||||||||||||||
403 | */ | - | ||||||||||||||||||
404 | QSqlRelationalTableModel::QSqlRelationalTableModel(QObject *parent, QSqlDatabase db) | - | ||||||||||||||||||
405 | : QSqlTableModel(*new QSqlRelationalTableModelPrivate, parent, db) | - | ||||||||||||||||||
406 | { | - | ||||||||||||||||||
407 | } executed 31 times by 1 test: end of block Executed by:
| 31 | ||||||||||||||||||
408 | - | |||||||||||||||||||
409 | /*! | - | ||||||||||||||||||
410 | Destroys the object and frees any allocated resources. | - | ||||||||||||||||||
411 | */ | - | ||||||||||||||||||
412 | QSqlRelationalTableModel::~QSqlRelationalTableModel() | - | ||||||||||||||||||
413 | { | - | ||||||||||||||||||
414 | } | - | ||||||||||||||||||
415 | - | |||||||||||||||||||
416 | /*! | - | ||||||||||||||||||
417 | \reimp | - | ||||||||||||||||||
418 | */ | - | ||||||||||||||||||
419 | QVariant QSqlRelationalTableModel::data(const QModelIndex &index, int role) const | - | ||||||||||||||||||
420 | { | - | ||||||||||||||||||
421 | Q_D(const QSqlRelationalTableModel); | - | ||||||||||||||||||
422 | - | |||||||||||||||||||
423 | if (role == Qt::DisplayRole && index.column() >= 0 && index.column() < d->relations.count() &&
| 5-248 | ||||||||||||||||||
424 | d->relations.value(index.column()).isValid()) {
| 76-147 | ||||||||||||||||||
425 | QRelation &relation = d->relations[index.column()]; | - | ||||||||||||||||||
426 | if (!relation.isDictionaryInitialized())
| 54-93 | ||||||||||||||||||
427 | relation.populateDictionary(); executed 54 times by 1 test: relation.populateDictionary(); Executed by:
| 54 | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | //only perform a dictionary lookup for the display value | - | ||||||||||||||||||
430 | //when the value at index has been changed or added. | - | ||||||||||||||||||
431 | //At an unmodified index, the underlying model will | - | ||||||||||||||||||
432 | //already have the correct display value. | - | ||||||||||||||||||
433 | if (d->strategy != OnFieldChange) {
| 1-146 | ||||||||||||||||||
434 | const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); | - | ||||||||||||||||||
435 | if (row.op() != QSqlTableModelPrivate::None && row.rec().isGenerated(index.column())) {
| 29-86 | ||||||||||||||||||
436 | if (d->strategy == OnManualSubmit || row.op() != QSqlTableModelPrivate::Delete) {
| 0-16 | ||||||||||||||||||
437 | QVariant v = row.rec().value(index.column()); | - | ||||||||||||||||||
438 | if (v.isValid())
| 0-29 | ||||||||||||||||||
439 | return relation.dictionary[v.toString()]; executed 29 times by 1 test: return relation.dictionary[v.toString()]; Executed by:
| 29 | ||||||||||||||||||
440 | } never executed: end of block | 0 | ||||||||||||||||||
441 | } never executed: end of block | 0 | ||||||||||||||||||
442 | } executed 117 times by 1 test: end of block Executed by:
| 117 | ||||||||||||||||||
443 | } executed 118 times by 1 test: end of block Executed by:
| 118 | ||||||||||||||||||
444 | return QSqlTableModel::data(index, role); executed 307 times by 1 test: return QSqlTableModel::data(index, role); Executed by:
| 307 | ||||||||||||||||||
445 | } | - | ||||||||||||||||||
446 | - | |||||||||||||||||||
447 | /*! | - | ||||||||||||||||||
448 | Sets the data for the \a role in the item with the specified \a | - | ||||||||||||||||||
449 | index to the \a value given. Depending on the edit strategy, the | - | ||||||||||||||||||
450 | value might be applied to the database at once, or it may be | - | ||||||||||||||||||
451 | cached in the model. | - | ||||||||||||||||||
452 | - | |||||||||||||||||||
453 | Returns \c true if the value could be set, or false on error (for | - | ||||||||||||||||||
454 | example, if \a index is out of bounds). | - | ||||||||||||||||||
455 | - | |||||||||||||||||||
456 | For relational columns, \a value must be the index, not the | - | ||||||||||||||||||
457 | display value. The index must also exist in the referenced | - | ||||||||||||||||||
458 | table, otherwise the function returns \c false. | - | ||||||||||||||||||
459 | - | |||||||||||||||||||
460 | \sa editStrategy(), data(), submit(), revertRow() | - | ||||||||||||||||||
461 | */ | - | ||||||||||||||||||
462 | bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant &value, | - | ||||||||||||||||||
463 | int role) | - | ||||||||||||||||||
464 | { | - | ||||||||||||||||||
465 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
466 | if ( role == Qt::EditRole && index.column() > 0 && index.column() < d->relations.count()
| 0-85 | ||||||||||||||||||
467 | && d->relations.value(index.column()).isValid()) {
| 15-44 | ||||||||||||||||||
468 | QRelation &relation = d->relations[index.column()]; | - | ||||||||||||||||||
469 | if (!relation.isDictionaryInitialized())
| 11-33 | ||||||||||||||||||
470 | relation.populateDictionary(); executed 11 times by 1 test: relation.populateDictionary(); Executed by:
| 11 | ||||||||||||||||||
471 | if (!relation.dictionary.contains(value.toString()))
| 2-42 | ||||||||||||||||||
472 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||||||||
473 | } executed 42 times by 1 test: end of block Executed by:
| 42 | ||||||||||||||||||
474 | return QSqlTableModel::setData(index, value, role); executed 83 times by 1 test: return QSqlTableModel::setData(index, value, role); Executed by:
| 83 | ||||||||||||||||||
475 | } | - | ||||||||||||||||||
476 | - | |||||||||||||||||||
477 | /*! | - | ||||||||||||||||||
478 | Lets the specified \a column be a foreign index specified by \a relation. | - | ||||||||||||||||||
479 | - | |||||||||||||||||||
480 | Example: | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | \snippet relationaltablemodel/relationaltablemodel.cpp 0 | - | ||||||||||||||||||
483 | \codeline | - | ||||||||||||||||||
484 | \snippet relationaltablemodel/relationaltablemodel.cpp 1 | - | ||||||||||||||||||
485 | - | |||||||||||||||||||
486 | The setRelation() call specifies that column 2 in table \c | - | ||||||||||||||||||
487 | employee is a foreign key that maps with field \c id of table \c | - | ||||||||||||||||||
488 | city, and that the view should present the \c{city}'s \c name | - | ||||||||||||||||||
489 | field to the user. | - | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | Note: The table's primary key may not contain a relation to another table. | - | ||||||||||||||||||
492 | - | |||||||||||||||||||
493 | \sa relation() | - | ||||||||||||||||||
494 | */ | - | ||||||||||||||||||
495 | void QSqlRelationalTableModel::setRelation(int column, const QSqlRelation &relation) | - | ||||||||||||||||||
496 | { | - | ||||||||||||||||||
497 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
498 | if (column < 0)
| 0-44 | ||||||||||||||||||
499 | return; never executed: return; | 0 | ||||||||||||||||||
500 | if (d->relations.size() <= column)
| 3-41 | ||||||||||||||||||
501 | d->relations.resize(column + 1); executed 41 times by 1 test: d->relations.resize(column + 1); Executed by:
| 41 | ||||||||||||||||||
502 | d->relations[column].init(this, relation); | - | ||||||||||||||||||
503 | } executed 44 times by 1 test: end of block Executed by:
| 44 | ||||||||||||||||||
504 | - | |||||||||||||||||||
505 | /*! | - | ||||||||||||||||||
506 | Returns the relation for the column \a column, or an invalid | - | ||||||||||||||||||
507 | relation if no relation is set. | - | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | \sa setRelation(), QSqlRelation::isValid() | - | ||||||||||||||||||
510 | */ | - | ||||||||||||||||||
511 | QSqlRelation QSqlRelationalTableModel::relation(int column) const | - | ||||||||||||||||||
512 | { | - | ||||||||||||||||||
513 | Q_D(const QSqlRelationalTableModel); | - | ||||||||||||||||||
514 | return d->relations.value(column).rel; never executed: return d->relations.value(column).rel; | 0 | ||||||||||||||||||
515 | } | - | ||||||||||||||||||
516 | - | |||||||||||||||||||
517 | QString QSqlRelationalTableModelPrivate::fullyQualifiedFieldName(const QString &tableName, | - | ||||||||||||||||||
518 | const QString &fieldName) const | - | ||||||||||||||||||
519 | { | - | ||||||||||||||||||
520 | QString ret; | - | ||||||||||||||||||
521 | ret.reserve(tableName.size() + fieldName.size() + 1); | - | ||||||||||||||||||
522 | ret.append(tableName).append(QLatin1Char('.')).append(fieldName); | - | ||||||||||||||||||
523 | - | |||||||||||||||||||
524 | return ret; executed 512 times by 1 test: return ret; Executed by:
| 512 | ||||||||||||||||||
525 | } | - | ||||||||||||||||||
526 | - | |||||||||||||||||||
527 | /*! | - | ||||||||||||||||||
528 | \reimp | - | ||||||||||||||||||
529 | */ | - | ||||||||||||||||||
530 | QString QSqlRelationalTableModel::selectStatement() const | - | ||||||||||||||||||
531 | { | - | ||||||||||||||||||
532 | Q_D(const QSqlRelationalTableModel); | - | ||||||||||||||||||
533 | - | |||||||||||||||||||
534 | if (tableName().isEmpty())
| 0-89 | ||||||||||||||||||
535 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
536 | if (d->relations.isEmpty())
| 5-84 | ||||||||||||||||||
537 | return QSqlTableModel::selectStatement(); executed 5 times by 1 test: return QSqlTableModel::selectStatement(); Executed by:
| 5 | ||||||||||||||||||
538 | - | |||||||||||||||||||
539 | // Count how many times each field name occurs in the record | - | ||||||||||||||||||
540 | QHash<QString, int> fieldNames; | - | ||||||||||||||||||
541 | QStringList fieldList; | - | ||||||||||||||||||
542 | for (int i = 0; i < d->baseRec.count(); ++i) {
| 84-302 | ||||||||||||||||||
543 | QSqlRelation relation = d->relations.value(i).rel; | - | ||||||||||||||||||
544 | QString name; | - | ||||||||||||||||||
545 | if (relation.isValid()) {
| 103-199 | ||||||||||||||||||
546 | // Count the display column name, not the original foreign key | - | ||||||||||||||||||
547 | name = relation.displayColumn(); | - | ||||||||||||||||||
548 | if (d->db.driver()->isIdentifierEscaped(name, QSqlDriver::FieldName))
| 10-93 | ||||||||||||||||||
549 | name = d->db.driver()->stripDelimiters(name, QSqlDriver::FieldName); executed 10 times by 1 test: name = d->db.driver()->stripDelimiters(name, QSqlDriver::FieldName); Executed by:
| 10 | ||||||||||||||||||
550 | - | |||||||||||||||||||
551 | const QSqlRecord rec = database().record(relation.tableName()); | - | ||||||||||||||||||
552 | for (int i = 0; i < rec.count(); ++i) {
| 0-206 | ||||||||||||||||||
553 | if (name.compare(rec.fieldName(i), Qt::CaseInsensitive) == 0) {
| 103 | ||||||||||||||||||
554 | name = rec.fieldName(i); | - | ||||||||||||||||||
555 | break; executed 103 times by 1 test: break; Executed by:
| 103 | ||||||||||||||||||
556 | } | - | ||||||||||||||||||
557 | } executed 103 times by 1 test: end of block Executed by:
| 103 | ||||||||||||||||||
558 | } executed 103 times by 1 test: end of block Executed by:
| 103 | ||||||||||||||||||
559 | else { | - | ||||||||||||||||||
560 | name = d->baseRec.fieldName(i); | - | ||||||||||||||||||
561 | } executed 199 times by 1 test: end of block Executed by:
| 199 | ||||||||||||||||||
562 | fieldNames[name] = fieldNames.value(name, 0) + 1; | - | ||||||||||||||||||
563 | fieldList.append(name); | - | ||||||||||||||||||
564 | } executed 302 times by 1 test: end of block Executed by:
| 302 | ||||||||||||||||||
565 | - | |||||||||||||||||||
566 | QString fList; | - | ||||||||||||||||||
567 | QString conditions; | - | ||||||||||||||||||
568 | QString from = Sql::from(tableName()); | - | ||||||||||||||||||
569 | for (int i = 0; i < d->baseRec.count(); ++i) {
| 84-302 | ||||||||||||||||||
570 | QSqlRelation relation = d->relations.value(i).rel; | - | ||||||||||||||||||
571 | const QString tableField = d->fullyQualifiedFieldName(tableName(), d->db.driver()->escapeIdentifier(d->baseRec.fieldName(i), QSqlDriver::FieldName)); | - | ||||||||||||||||||
572 | if (relation.isValid()) {
| 103-199 | ||||||||||||||||||
573 | const QString relTableAlias = Sql::relTablePrefix(i); | - | ||||||||||||||||||
574 | QString displayTableField = d->fullyQualifiedFieldName(relTableAlias, relation.displayColumn()); | - | ||||||||||||||||||
575 | - | |||||||||||||||||||
576 | // Duplicate field names must be aliased | - | ||||||||||||||||||
577 | if (fieldNames.value(fieldList[i]) > 1) {
| 23-80 | ||||||||||||||||||
578 | QString relTableName = relation.tableName().section(QChar::fromLatin1('.'), -1, -1); | - | ||||||||||||||||||
579 | if (d->db.driver()->isIdentifierEscaped(relTableName, QSqlDriver::TableName))
| 0-23 | ||||||||||||||||||
580 | relTableName = d->db.driver()->stripDelimiters(relTableName, QSqlDriver::TableName); never executed: relTableName = d->db.driver()->stripDelimiters(relTableName, QSqlDriver::TableName); | 0 | ||||||||||||||||||
581 | QString displayColumn = relation.displayColumn(); | - | ||||||||||||||||||
582 | if (d->db.driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName))
| 0-23 | ||||||||||||||||||
583 | displayColumn = d->db.driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); never executed: displayColumn = d->db.driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); | 0 | ||||||||||||||||||
584 | const QString alias = QString::fromLatin1("%1_%2_%3").arg(relTableName).arg(displayColumn).arg(fieldNames.value(fieldList[i])); | - | ||||||||||||||||||
585 | displayTableField = Sql::as(displayTableField, alias); | - | ||||||||||||||||||
586 | --fieldNames[fieldList[i]]; | - | ||||||||||||||||||
587 | } executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||||||||
588 | - | |||||||||||||||||||
589 | fList = Sql::comma(fList, displayTableField); | - | ||||||||||||||||||
590 | - | |||||||||||||||||||
591 | // Join related table | - | ||||||||||||||||||
592 | const QString tblexpr = Sql::concat(relation.tableName(), relTableAlias); | - | ||||||||||||||||||
593 | const QString relTableField = d->fullyQualifiedFieldName(relTableAlias, relation.indexColumn()); | - | ||||||||||||||||||
594 | const QString cond = Sql::eq(tableField, relTableField); | - | ||||||||||||||||||
595 | if (d->joinMode == QSqlRelationalTableModel::InnerJoin) {
| 30-73 | ||||||||||||||||||
596 | // FIXME: InnerJoin code is known to be broken. | - | ||||||||||||||||||
597 | // Use LeftJoin mode if you want correct behavior. | - | ||||||||||||||||||
598 | from = Sql::comma(from, tblexpr); | - | ||||||||||||||||||
599 | conditions = Sql::et(conditions, cond); | - | ||||||||||||||||||
600 | } else { executed 73 times by 1 test: end of block Executed by:
| 73 | ||||||||||||||||||
601 | from = Sql::concat(from, Sql::leftJoin(tblexpr)); | - | ||||||||||||||||||
602 | from = Sql::concat(from, Sql::on(cond)); | - | ||||||||||||||||||
603 | } executed 30 times by 1 test: end of block Executed by:
| 30 | ||||||||||||||||||
604 | } else { | - | ||||||||||||||||||
605 | fList = Sql::comma(fList, tableField); | - | ||||||||||||||||||
606 | } executed 199 times by 1 test: end of block Executed by:
| 199 | ||||||||||||||||||
607 | } | - | ||||||||||||||||||
608 | - | |||||||||||||||||||
609 | if (fList.isEmpty())
| 0-84 | ||||||||||||||||||
610 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
611 | - | |||||||||||||||||||
612 | const QString stmt = Sql::concat(Sql::select(fList), from); | - | ||||||||||||||||||
613 | const QString where = Sql::where(Sql::et(Sql::paren(conditions), Sql::paren(filter()))); | - | ||||||||||||||||||
614 | return Sql::concat(Sql::concat(stmt, where), orderByClause()); executed 84 times by 1 test: return Sql::concat(Sql::concat(stmt, where), orderByClause()); Executed by:
| 84 | ||||||||||||||||||
615 | } | - | ||||||||||||||||||
616 | - | |||||||||||||||||||
617 | /*! | - | ||||||||||||||||||
618 | Returns a QSqlTableModel object for accessing the table for which | - | ||||||||||||||||||
619 | \a column is a foreign key, or 0 if there is no relation for the | - | ||||||||||||||||||
620 | given \a column. | - | ||||||||||||||||||
621 | - | |||||||||||||||||||
622 | The returned object is owned by the QSqlRelationalTableModel. | - | ||||||||||||||||||
623 | - | |||||||||||||||||||
624 | \sa setRelation(), relation() | - | ||||||||||||||||||
625 | */ | - | ||||||||||||||||||
626 | QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const | - | ||||||||||||||||||
627 | { | - | ||||||||||||||||||
628 | Q_D(const QSqlRelationalTableModel); | - | ||||||||||||||||||
629 | if ( column < 0 || column >= d->relations.count())
| 0-20 | ||||||||||||||||||
630 | return 0; executed 4 times by 1 test: return 0; Executed by:
| 4 | ||||||||||||||||||
631 | - | |||||||||||||||||||
632 | QRelation &relation = const_cast<QSqlRelationalTableModelPrivate *>(d)->relations[column]; | - | ||||||||||||||||||
633 | if (!relation.isValid())
| 6-10 | ||||||||||||||||||
634 | return 0; executed 6 times by 1 test: return 0; Executed by:
| 6 | ||||||||||||||||||
635 | - | |||||||||||||||||||
636 | if (!relation.model)
| 3-7 | ||||||||||||||||||
637 | relation.populateModel(); executed 3 times by 1 test: relation.populateModel(); Executed by:
| 3 | ||||||||||||||||||
638 | return relation.model; executed 10 times by 1 test: return relation.model; Executed by:
| 10 | ||||||||||||||||||
639 | } | - | ||||||||||||||||||
640 | - | |||||||||||||||||||
641 | /*! | - | ||||||||||||||||||
642 | \reimp | - | ||||||||||||||||||
643 | */ | - | ||||||||||||||||||
644 | void QSqlRelationalTableModel::revertRow(int row) | - | ||||||||||||||||||
645 | { | - | ||||||||||||||||||
646 | QSqlTableModel::revertRow(row); | - | ||||||||||||||||||
647 | } executed 19 times by 1 test: end of block Executed by:
| 19 | ||||||||||||||||||
648 | - | |||||||||||||||||||
649 | /*! | - | ||||||||||||||||||
650 | \reimp | - | ||||||||||||||||||
651 | */ | - | ||||||||||||||||||
652 | void QSqlRelationalTableModel::clear() | - | ||||||||||||||||||
653 | { | - | ||||||||||||||||||
654 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
655 | beginResetModel(); | - | ||||||||||||||||||
656 | d->clearChanges(); | - | ||||||||||||||||||
657 | d->relations.clear(); | - | ||||||||||||||||||
658 | QSqlTableModel::clear(); | - | ||||||||||||||||||
659 | endResetModel(); | - | ||||||||||||||||||
660 | } executed 32 times by 1 test: end of block Executed by:
| 32 | ||||||||||||||||||
661 | - | |||||||||||||||||||
662 | - | |||||||||||||||||||
663 | /*! \enum QSqlRelationalTableModel::JoinMode | - | ||||||||||||||||||
664 | - | |||||||||||||||||||
665 | \value InnerJoin - Inner join mode, return rows when there is at least one match in both tables. | - | ||||||||||||||||||
666 | \value LeftJoin - Left join mode, returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2). | - | ||||||||||||||||||
667 | - | |||||||||||||||||||
668 | \sa QSqlRelationalTableModel::setJoinMode() | - | ||||||||||||||||||
669 | \since 4.8 | - | ||||||||||||||||||
670 | */ | - | ||||||||||||||||||
671 | - | |||||||||||||||||||
672 | /*! | - | ||||||||||||||||||
673 | Sets the SQL \a joinMode to show or hide rows with NULL foreign keys. | - | ||||||||||||||||||
674 | In InnerJoin mode (the default) these rows will not be shown: use the | - | ||||||||||||||||||
675 | LeftJoin mode if you want to show them. | - | ||||||||||||||||||
676 | - | |||||||||||||||||||
677 | \sa QSqlRelationalTableModel::JoinMode | - | ||||||||||||||||||
678 | \since 4.8 | - | ||||||||||||||||||
679 | */ | - | ||||||||||||||||||
680 | void QSqlRelationalTableModel::setJoinMode( QSqlRelationalTableModel::JoinMode joinMode ) | - | ||||||||||||||||||
681 | { | - | ||||||||||||||||||
682 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
683 | d->joinMode = joinMode; | - | ||||||||||||||||||
684 | } executed 23 times by 1 test: end of block Executed by:
| 23 | ||||||||||||||||||
685 | /*! | - | ||||||||||||||||||
686 | \reimp | - | ||||||||||||||||||
687 | */ | - | ||||||||||||||||||
688 | bool QSqlRelationalTableModel::select() | - | ||||||||||||||||||
689 | { | - | ||||||||||||||||||
690 | return QSqlTableModel::select(); executed 74 times by 1 test: return QSqlTableModel::select(); Executed by:
| 74 | ||||||||||||||||||
691 | } | - | ||||||||||||||||||
692 | - | |||||||||||||||||||
693 | /*! | - | ||||||||||||||||||
694 | \reimp | - | ||||||||||||||||||
695 | */ | - | ||||||||||||||||||
696 | void QSqlRelationalTableModel::setTable(const QString &table) | - | ||||||||||||||||||
697 | { | - | ||||||||||||||||||
698 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
699 | - | |||||||||||||||||||
700 | // memorize the table before applying the relations | - | ||||||||||||||||||
701 | d->baseRec = d->db.record(table); | - | ||||||||||||||||||
702 | - | |||||||||||||||||||
703 | QSqlTableModel::setTable(table); | - | ||||||||||||||||||
704 | } executed 32 times by 1 test: end of block Executed by:
| 32 | ||||||||||||||||||
705 | - | |||||||||||||||||||
706 | /*! \internal | - | ||||||||||||||||||
707 | */ | - | ||||||||||||||||||
708 | void QSqlRelationalTableModelPrivate::translateFieldNames(QSqlRecord &values) const | - | ||||||||||||||||||
709 | { | - | ||||||||||||||||||
710 | for (int i = 0; i < values.count(); ++i) {
| 26-93 | ||||||||||||||||||
711 | if (relations.value(i).isValid()) {
| 34-59 | ||||||||||||||||||
712 | QVariant v = values.value(i); | - | ||||||||||||||||||
713 | bool gen = values.isGenerated(i); | - | ||||||||||||||||||
714 | values.replace(i, baseRec.field(i)); | - | ||||||||||||||||||
715 | values.setValue(i, v); | - | ||||||||||||||||||
716 | values.setGenerated(i, gen); | - | ||||||||||||||||||
717 | } executed 34 times by 1 test: end of block Executed by:
| 34 | ||||||||||||||||||
718 | } executed 93 times by 1 test: end of block Executed by:
| 93 | ||||||||||||||||||
719 | } executed 26 times by 1 test: end of block Executed by:
| 26 | ||||||||||||||||||
720 | - | |||||||||||||||||||
721 | /*! | - | ||||||||||||||||||
722 | \reimp | - | ||||||||||||||||||
723 | */ | - | ||||||||||||||||||
724 | bool QSqlRelationalTableModel::updateRowInTable(int row, const QSqlRecord &values) | - | ||||||||||||||||||
725 | { | - | ||||||||||||||||||
726 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
727 | - | |||||||||||||||||||
728 | QSqlRecord rec = values; | - | ||||||||||||||||||
729 | d->translateFieldNames(rec); | - | ||||||||||||||||||
730 | - | |||||||||||||||||||
731 | return QSqlTableModel::updateRowInTable(row, rec); executed 19 times by 1 test: return QSqlTableModel::updateRowInTable(row, rec); Executed by:
| 19 | ||||||||||||||||||
732 | } | - | ||||||||||||||||||
733 | - | |||||||||||||||||||
734 | /*! | - | ||||||||||||||||||
735 | \reimp | - | ||||||||||||||||||
736 | */ | - | ||||||||||||||||||
737 | bool QSqlRelationalTableModel::insertRowIntoTable(const QSqlRecord &values) | - | ||||||||||||||||||
738 | { | - | ||||||||||||||||||
739 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
740 | - | |||||||||||||||||||
741 | QSqlRecord rec = values; | - | ||||||||||||||||||
742 | d->translateFieldNames(rec); | - | ||||||||||||||||||
743 | - | |||||||||||||||||||
744 | return QSqlTableModel::insertRowIntoTable(rec); executed 7 times by 1 test: return QSqlTableModel::insertRowIntoTable(rec); Executed by:
| 7 | ||||||||||||||||||
745 | } | - | ||||||||||||||||||
746 | - | |||||||||||||||||||
747 | /*! | - | ||||||||||||||||||
748 | \reimp | - | ||||||||||||||||||
749 | */ | - | ||||||||||||||||||
750 | QString QSqlRelationalTableModel::orderByClause() const | - | ||||||||||||||||||
751 | { | - | ||||||||||||||||||
752 | Q_D(const QSqlRelationalTableModel); | - | ||||||||||||||||||
753 | - | |||||||||||||||||||
754 | const QSqlRelation rel = d->relations.value(d->sortColumn).rel; | - | ||||||||||||||||||
755 | if (!rel.isValid())
| 4-85 | ||||||||||||||||||
756 | return QSqlTableModel::orderByClause(); executed 85 times by 1 test: return QSqlTableModel::orderByClause(); Executed by:
| 85 | ||||||||||||||||||
757 | - | |||||||||||||||||||
758 | QString f = d->fullyQualifiedFieldName(Sql::relTablePrefix(d->sortColumn), rel.displayColumn()); | - | ||||||||||||||||||
759 | f = d->sortOrder == Qt::AscendingOrder ? Sql::asc(f) : Sql::desc(f);
| 2 | ||||||||||||||||||
760 | return Sql::orderBy(f); executed 4 times by 1 test: return Sql::orderBy(f); Executed by:
| 4 | ||||||||||||||||||
761 | } | - | ||||||||||||||||||
762 | - | |||||||||||||||||||
763 | /*! | - | ||||||||||||||||||
764 | \reimp | - | ||||||||||||||||||
765 | */ | - | ||||||||||||||||||
766 | bool QSqlRelationalTableModel::removeColumns(int column, int count, const QModelIndex &parent) | - | ||||||||||||||||||
767 | { | - | ||||||||||||||||||
768 | Q_D(QSqlRelationalTableModel); | - | ||||||||||||||||||
769 | - | |||||||||||||||||||
770 | if (parent.isValid() || column < 0 || column + count > d->rec.count())
| 0-4 | ||||||||||||||||||
771 | return false; never executed: return false; | 0 | ||||||||||||||||||
772 | - | |||||||||||||||||||
773 | for (int i = 0; i < count; ++i) {
| 4-6 | ||||||||||||||||||
774 | d->baseRec.remove(column); | - | ||||||||||||||||||
775 | if (d->relations.count() > column)
| 2-4 | ||||||||||||||||||
776 | d->relations.remove(column); executed 4 times by 1 test: d->relations.remove(column); Executed by:
| 4 | ||||||||||||||||||
777 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||||||||
778 | return QSqlTableModel::removeColumns(column, count, parent); executed 4 times by 1 test: return QSqlTableModel::removeColumns(column, count, parent); Executed by:
| 4 | ||||||||||||||||||
779 | } | - | ||||||||||||||||||
780 | - | |||||||||||||||||||
781 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |