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