| Line | Source Code | Coverage |
|---|
| 1 | | - |
| 2 | | - |
| 3 | | - |
| 4 | | - |
| 5 | | - |
| 6 | | - |
| 7 | typedef QSqlTableModelSql Sql; | - |
| 8 | | - |
| 9 | | - |
| 10 | | - |
| 11 | | - |
| 12 | QSqlRecord QSqlTableModelPrivate::record(const QVector<QVariant> &values) const | - |
| 13 | { | - |
| 14 | QSqlRecord r = rec; | - |
| 15 | for (int i = 0; i < r.count() && i < values.count(); ++i) | - |
| 16 | r.setValue(i, values.at(i)); | - |
| 17 | return r; | - |
| 18 | } | - |
| 19 | | - |
| 20 | int QSqlTableModelPrivate::nameToIndex(const QString &name) const | - |
| 21 | { | - |
| 22 | return rec.indexOf(strippedFieldName(name)); | - |
| 23 | } | - |
| 24 | | - |
| 25 | QString QSqlTableModelPrivate::strippedFieldName(const QString &name) const | - |
| 26 | { | - |
| 27 | QString fieldname = name; | - |
| 28 | if (db.driver()->isIdentifierEscaped(fieldname, QSqlDriver::FieldName)) | - |
| 29 | fieldname = db.driver()->stripDelimiters(fieldname, QSqlDriver::FieldName); | - |
| 30 | return fieldname; | - |
| 31 | } | - |
| 32 | | - |
| 33 | int QSqlTableModelPrivate::insertCount(int maxRow) const | - |
| 34 | { | - |
| 35 | int cnt = 0; | - |
| 36 | CacheMap::ConstIterator i = cache.constBegin(); | - |
| 37 | const CacheMap::ConstIterator e = cache.constEnd(); | - |
| 38 | for ( ; i != e && (maxRow < 0 || i.key() <= maxRow); ++i) | - |
| 39 | if (i.value().insert()) | - |
| 40 | ++cnt; | - |
| 41 | | - |
| 42 | return cnt; | - |
| 43 | } | - |
| 44 | | - |
| 45 | void QSqlTableModelPrivate::initRecordAndPrimaryIndex() | - |
| 46 | { | - |
| 47 | rec = db.record(tableName); | - |
| 48 | primaryIndex = db.primaryIndex(tableName); | - |
| 49 | initColOffsets(rec.count()); | - |
| 50 | } | - |
| 51 | | - |
| 52 | void QSqlTableModelPrivate::clear() | - |
| 53 | { | - |
| 54 | sortColumn = -1; | - |
| 55 | sortOrder = Qt::AscendingOrder; | - |
| 56 | tableName.clear(); | - |
| 57 | editQuery.clear(); | - |
| 58 | cache.clear(); | - |
| 59 | primaryIndex.clear(); | - |
| 60 | rec.clear(); | - |
| 61 | filter.clear(); | - |
| 62 | } | - |
| 63 | | - |
| 64 | void QSqlTableModelPrivate::clearCache() | - |
| 65 | { | - |
| 66 | cache.clear(); | - |
| 67 | } | - |
| 68 | | - |
| 69 | void QSqlTableModelPrivate::revertCachedRow(int row) | - |
| 70 | { | - |
| 71 | QSqlTableModel * const q = q_func(); | - |
| 72 | ModifiedRow r = cache.value(row); | - |
| 73 | | - |
| 74 | switch (r.op()) { | - |
| 75 | case QSqlTableModelPrivate::None: | - |
| 76 | qt_noop(); | - |
| 77 | return; | - |
| 78 | case QSqlTableModelPrivate::Update: | - |
| 79 | case QSqlTableModelPrivate::Delete: | - |
| 80 | if (!r.submitted()) { | - |
| 81 | cache[row].revert(); | - |
| 82 | q->dataChanged(q->createIndex(row, 0), | - |
| 83 | q->createIndex(row, q->columnCount() - 1)); | - |
| 84 | } | - |
| 85 | break; | - |
| 86 | case QSqlTableModelPrivate::Insert: { | - |
| 87 | QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = cache.find(row); | - |
| 88 | if (it == cache.end()) | - |
| 89 | return; | - |
| 90 | q->beginRemoveRows(QModelIndex(), row, row); | - |
| 91 | it = cache.erase(it); | - |
| 92 | while (it != cache.end()) { | - |
| 93 | int oldKey = it.key(); | - |
| 94 | const QSqlTableModelPrivate::ModifiedRow oldValue = it.value(); | - |
| 95 | cache.erase(it); | - |
| 96 | it = cache.insert(oldKey - 1, oldValue); | - |
| 97 | ++it; | - |
| 98 | } | - |
| 99 | q->endRemoveRows(); | - |
| 100 | break; } | - |
| 101 | } | - |
| 102 | } | - |
| 103 | | - |
| 104 | bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, | - |
| 105 | const QSqlRecord &rec, const QSqlRecord &whereValues) | - |
| 106 | { | - |
| 107 | if (stmt.isEmpty()) | - |
| 108 | return false; | - |
| 109 | | - |
| 110 | | - |
| 111 | if (editQuery.driver() != db.driver()) | - |
| 112 | editQuery = QSqlQuery(db); | - |
| 113 | | - |
| 114 | | - |
| 115 | | - |
| 116 | if (db.driver()->hasFeature(QSqlDriver::SimpleLocking)) | - |
| 117 | const_cast<QSqlResult *>(query.result())->detachFromResultSet(); | - |
| 118 | | - |
| 119 | if (prepStatement) { | - |
| 120 | if (editQuery.lastQuery() != stmt) { | - |
| 121 | if (!editQuery.prepare(stmt)) { | - |
| 122 | error = editQuery.lastError(); | - |
| 123 | return false; | - |
| 124 | } | - |
| 125 | } | - |
| 126 | int i; | - |
| 127 | for (i = 0; i < rec.count(); ++i) | - |
| 128 | if (rec.isGenerated(i)) | - |
| 129 | editQuery.addBindValue(rec.value(i)); | - |
| 130 | for (i = 0; i < whereValues.count(); ++i) | - |
| 131 | if (whereValues.isGenerated(i) && !whereValues.isNull(i)) | - |
| 132 | editQuery.addBindValue(whereValues.value(i)); | - |
| 133 | | - |
| 134 | if (!editQuery.exec()) { | - |
| 135 | error = editQuery.lastError(); | - |
| 136 | return false; | - |
| 137 | } | - |
| 138 | } else { | - |
| 139 | if (!editQuery.exec(stmt)) { | - |
| 140 | error = editQuery.lastError(); | - |
| 141 | return false; | - |
| 142 | } | - |
| 143 | } | - |
| 144 | return true; | - |
| 145 | } | - |
| 146 | | - |
| 147 | QSqlRecord QSqlTableModelPrivate::primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex) | - |
| 148 | { | - |
| 149 | QSqlRecord pValues(pIndex); | - |
| 150 | | - |
| 151 | for (int i = pValues.count() - 1; i >= 0; --i) | - |
| 152 | pValues.setValue(i, rec.value(pValues.fieldName(i))); | - |
| 153 | | - |
| 154 | return pValues; | - |
| 155 | } | - |
| 156 | | - |
| 157 | QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const | - |
| 158 | { | - |
| 159 | const QSqlTableModel * const q = q_func(); | - |
| 160 | | - |
| 161 | const QSqlRecord &pIndex = primaryIndex.isEmpty() ? rec : primaryIndex; | - |
| 162 | | - |
| 163 | ModifiedRow mr = cache.value(row); | - |
| 164 | if (mr.op() != None) | - |
| 165 | return mr.primaryValues(pIndex); | - |
| 166 | else | - |
| 167 | return primaryValues(q->QSqlQueryModel::record(row), pIndex); | - |
| 168 | } | - |
| 169 | QSqlTableModel::QSqlTableModel(QObject *parent, QSqlDatabase db) | - |
| 170 | : QSqlQueryModel(*new QSqlTableModelPrivate, parent) | - |
| 171 | { | - |
| 172 | QSqlTableModelPrivate * const d = d_func(); | - |
| 173 | d->db = db.isValid() ? db : QSqlDatabase::database(); | - |
| 174 | } | - |
| 175 | | - |
| 176 | | - |
| 177 | | - |
| 178 | QSqlTableModel::QSqlTableModel(QSqlTableModelPrivate &dd, QObject *parent, QSqlDatabase db) | - |
| 179 | : QSqlQueryModel(dd, parent) | - |
| 180 | { | - |
| 181 | QSqlTableModelPrivate * const d = d_func(); | - |
| 182 | d->db = db.isValid() ? db : QSqlDatabase::database(); | - |
| 183 | } | - |
| 184 | | - |
| 185 | | - |
| 186 | | - |
| 187 | | - |
| 188 | QSqlTableModel::~QSqlTableModel() | - |
| 189 | { | - |
| 190 | } | - |
| 191 | void QSqlTableModel::setTable(const QString &tableName) | - |
| 192 | { | - |
| 193 | QSqlTableModelPrivate * const d = d_func(); | - |
| 194 | clear(); | - |
| 195 | d->tableName = tableName; | - |
| 196 | d->initRecordAndPrimaryIndex(); | - |
| 197 | | - |
| 198 | if (d->rec.count() == 0) evaluated: d->rec.count() == 0| yes Evaluation Count:1 | yes Evaluation Count:182 |
| 1-182 |
| 199 | d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), | 1 |
| 200 | QSqlError::StatementError); executed: d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), QSqlError::StatementError);Execution Count:1 | 1 |
| 201 | | - |
| 202 | | - |
| 203 | | - |
| 204 | d->autoColumn.clear(); | - |
| 205 | for (int c = 0; c < d->rec.count(); ++c) { evaluated: c < d->rec.count()| yes Evaluation Count:450 | yes Evaluation Count:178 |
| 178-450 |
| 206 | if (d->rec.field(c).isAutoValue()) { evaluated: d->rec.field(c).isAutoValue()| yes Evaluation Count:5 | yes Evaluation Count:445 |
| 5-445 |
| 207 | d->autoColumn = d->rec.fieldName(c); | - |
| 208 | break; executed: break;Execution Count:5 | 5 |
| 209 | } | - |
| 210 | } executed: }Execution Count:445 | 445 |
| 211 | } executed: }Execution Count:183 | 183 |
| 212 | | - |
| 213 | | - |
| 214 | | - |
| 215 | | - |
| 216 | QString QSqlTableModel::tableName() const | - |
| 217 | { | - |
| 218 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 219 | return d->tableName; | - |
| 220 | } | - |
| 221 | bool QSqlTableModel::select() | - |
| 222 | { | - |
| 223 | QSqlTableModelPrivate * const d = d_func(); | - |
| 224 | const QString query = selectStatement(); | - |
| 225 | if (query.isEmpty()) | - |
| 226 | return false; | - |
| 227 | | - |
| 228 | beginResetModel(); | - |
| 229 | | - |
| 230 | d->clearCache(); | - |
| 231 | | - |
| 232 | QSqlQuery qu(query, d->db); | - |
| 233 | setQuery(qu); | - |
| 234 | | - |
| 235 | if (!qu.isActive() || lastError().isValid()) { | - |
| 236 | | - |
| 237 | d->initRecordAndPrimaryIndex(); | - |
| 238 | endResetModel(); | - |
| 239 | return false; | - |
| 240 | } | - |
| 241 | endResetModel(); | - |
| 242 | return true; | - |
| 243 | } | - |
| 244 | bool QSqlTableModel::selectRow(int row) | - |
| 245 | { | - |
| 246 | QSqlTableModelPrivate * const d = d_func(); | - |
| 247 | | - |
| 248 | if (row < 0 || row >= rowCount()) | - |
| 249 | return false; | - |
| 250 | | - |
| 251 | const int table_sort_col = d->sortColumn; | - |
| 252 | d->sortColumn = -1; | - |
| 253 | const QString table_filter = d->filter; | - |
| 254 | d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, | - |
| 255 | d->tableName, | - |
| 256 | d->primaryValues(row), | - |
| 257 | false); | - |
| 258 | static const QString wh = Sql::where() + Sql::sp(); | - |
| 259 | if (d->filter.startsWith(wh, Qt::CaseInsensitive)) | - |
| 260 | d->filter.remove(0, wh.length()); | - |
| 261 | | - |
| 262 | QString stmt; | - |
| 263 | | - |
| 264 | if (!d->filter.isEmpty()) | - |
| 265 | stmt = selectStatement(); | - |
| 266 | | - |
| 267 | d->sortColumn = table_sort_col; | - |
| 268 | d->filter = table_filter; | - |
| 269 | | - |
| 270 | if (stmt.isEmpty()) | - |
| 271 | return false; | - |
| 272 | | - |
| 273 | bool exists; | - |
| 274 | QSqlRecord newValues; | - |
| 275 | | - |
| 276 | { | - |
| 277 | QSqlQuery q(d->db); | - |
| 278 | q.setForwardOnly(true); | - |
| 279 | if (!q.exec(stmt)) | - |
| 280 | return false; | - |
| 281 | | - |
| 282 | exists = q.next(); | - |
| 283 | newValues = q.record(); | - |
| 284 | } | - |
| 285 | | - |
| 286 | bool needsAddingToCache = !exists || d->cache.contains(row); | - |
| 287 | | - |
| 288 | if (!needsAddingToCache) { | - |
| 289 | const QSqlRecord curValues = record(row); | - |
| 290 | needsAddingToCache = curValues.count() != newValues.count(); | - |
| 291 | if (!needsAddingToCache) { | - |
| 292 | | - |
| 293 | | - |
| 294 | for (int f = curValues.count() - 1; f >= 0; --f) { | - |
| 295 | if (curValues.value(f) != newValues.value(f)) | - |
| 296 | needsAddingToCache = true; | - |
| 297 | break; | - |
| 298 | } | - |
| 299 | } | - |
| 300 | } | - |
| 301 | | - |
| 302 | if (needsAddingToCache) { | - |
| 303 | d->cache[row].refresh(exists, newValues); | - |
| 304 | headerDataChanged(Qt::Vertical, row, row); | - |
| 305 | dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1)); | - |
| 306 | } | - |
| 307 | | - |
| 308 | return true; | - |
| 309 | } | - |
| 310 | | - |
| 311 | | - |
| 312 | | - |
| 313 | | - |
| 314 | QVariant QSqlTableModel::data(const QModelIndex &index, int role) const | - |
| 315 | { | - |
| 316 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 317 | if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole)) | - |
| 318 | return QVariant(); | - |
| 319 | | - |
| 320 | const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row()); | - |
| 321 | if (mrow.op() != QSqlTableModelPrivate::None) | - |
| 322 | return mrow.rec().value(index.column()); | - |
| 323 | | - |
| 324 | return QSqlQueryModel::data(index, role); | - |
| 325 | } | - |
| 326 | | - |
| 327 | | - |
| 328 | | - |
| 329 | | - |
| 330 | QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, int role) const | - |
| 331 | { | - |
| 332 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 333 | if (orientation == Qt::Vertical && role == Qt::DisplayRole) { | - |
| 334 | const QSqlTableModelPrivate::Op op = d->cache.value(section).op(); | - |
| 335 | if (op == QSqlTableModelPrivate::Insert) | - |
| 336 | return QLatin1String("*"); | - |
| 337 | else if (op == QSqlTableModelPrivate::Delete) | - |
| 338 | return QLatin1String("!"); | - |
| 339 | } | - |
| 340 | return QSqlQueryModel::headerData(section, orientation, role); | - |
| 341 | } | - |
| 342 | bool QSqlTableModel::isDirty() const | - |
| 343 | { | - |
| 344 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 345 | QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin(); | - |
| 346 | const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd(); | - |
| 347 | for (; i != e; i++) | - |
| 348 | if (!i.value().submitted()) | - |
| 349 | return true; | - |
| 350 | return false; | - |
| 351 | } | - |
| 352 | bool QSqlTableModel::isDirty(const QModelIndex &index) const | - |
| 353 | { | - |
| 354 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 355 | if (!index.isValid()) | - |
| 356 | return false; | - |
| 357 | | - |
| 358 | const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row()); | - |
| 359 | if (row.submitted()) | - |
| 360 | return false; | - |
| 361 | | - |
| 362 | return row.op() == QSqlTableModelPrivate::Insert | - |
| 363 | || row.op() == QSqlTableModelPrivate::Delete | - |
| 364 | || (row.op() == QSqlTableModelPrivate::Update | - |
| 365 | && row.rec().isGenerated(index.column())); | - |
| 366 | } | - |
| 367 | bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | - |
| 368 | { | - |
| 369 | QSqlTableModelPrivate * const d = d_func(); | - |
| 370 | if (d->busyInsertingRows) partially evaluated: d->busyInsertingRows| no Evaluation Count:0 | yes Evaluation Count:270 |
| 0-270 |
| 371 | return false; never executed: return false; | 0 |
| 372 | | - |
| 373 | if (role != Qt::EditRole) evaluated: role != Qt::EditRole| yes Evaluation Count:2 | yes Evaluation Count:268 |
| 2-268 |
| 374 | return QSqlQueryModel::setData(index, value, role); executed: return QSqlQueryModel::setData(index, value, role);Execution Count:2 | 2 |
| 375 | | - |
| 376 | if (!index.isValid() || index.column() >= d->rec.count() || index.row() >= rowCount()) evaluated: !index.isValid()| yes Evaluation Count:3 | yes Evaluation Count:265 |
partially evaluated: index.column() >= d->rec.count()| no Evaluation Count:0 | yes Evaluation Count:265 |
partially evaluated: index.row() >= rowCount()| no Evaluation Count:0 | yes Evaluation Count:265 |
| 0-265 |
| 377 | return false; executed: return false;Execution Count:3 | 3 |
| 378 | | - |
| 379 | if (!(flags(index) & Qt::ItemIsEditable)) evaluated: !(flags(index) & Qt::ItemIsEditable)| yes Evaluation Count:3 | yes Evaluation Count:262 |
| 3-262 |
| 380 | return false; executed: return false;Execution Count:3 | 3 |
| 381 | | - |
| 382 | if (const QVariant oldValue = QSqlTableModel::data(index, role)); | - |
| 383 | if (value == oldValue evaluated: value == oldValue| yes Evaluation Count:33 | yes Evaluation Count:229 |
| 33-229 |
| 384 | && value.isNull() == oldValue.isNull() evaluated: value.isNull() == oldValue.isNull()| yes Evaluation Count:30 | yes Evaluation Count:3 |
| 3-30 |
| 385 | && d->cache.value(index.row()).op() != QSqlTableModelPrivate::Insert) evaluated: d->cache.value(index.row()).op() != QSqlTableModelPrivate::Insert| yes Evaluation Count:23 | yes Evaluation Count:7 |
| 7-23 |
| 386 | return true; executed: return true;Execution Count:23 | 23 |
| 387 | | - |
| 388 | QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()]; | - |
| 389 | | - |
| 390 | if (row.op() == QSqlTableModelPrivate::None) evaluated: row.op() == QSqlTableModelPrivate::None| yes Evaluation Count:47 | yes Evaluation Count:192 |
| 47-192 |
| 391 | row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, | 47 |
| 392 | QSqlQueryModel::record(index.row())); executed: row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(index.row()));Execution Count:47 | 47 |
| 393 | | - |
| 394 | row.setValue(index.column(), value); | - |
| 395 | dataChanged(index, index); | - |
| 396 | | - |
| 397 | if (d->strategy == OnFieldChange && row.op() != QSqlTableModelPrivate::Insert) evaluated: d->strategy == OnFieldChange| yes Evaluation Count:15 | yes Evaluation Count:224 |
evaluated: row.op() != QSqlTableModelPrivate::Insert| yes Evaluation Count:8 | yes Evaluation Count:7 |
| 7-224 |
| 398 | return submit(); executed: return submit();Execution Count:8 | 8 |
| 399 | | - |
| 400 | return true; executed: return true;Execution Count:231 | 231 |
| 401 | } | - |
| 402 | void QSqlTableModel::setQuery(const QSqlQuery &query) | - |
| 403 | { | - |
| 404 | QSqlQueryModel::setQuery(query); | - |
| 405 | } | - |
| 406 | bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values) | - |
| 407 | { | - |
| 408 | QSqlTableModelPrivate * const d = d_func(); | - |
| 409 | QSqlRecord rec(values); | - |
| 410 | beforeUpdate(row, rec); | - |
| 411 | | - |
| 412 | const QSqlRecord whereValues = d->primaryValues(row); | - |
| 413 | const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries); | - |
| 414 | const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::UpdateStatement, d->tableName, | - |
| 415 | rec, prepStatement); | - |
| 416 | const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, d->tableName, | - |
| 417 | whereValues, prepStatement); | - |
| 418 | | - |
| 419 | if (stmt.isEmpty() || where.isEmpty() || row < 0 || row >= rowCount()) { | - |
| 420 | d->error = QSqlError(QLatin1String("No Fields to update"), QString(), | - |
| 421 | QSqlError::StatementError); | - |
| 422 | return false; | - |
| 423 | } | - |
| 424 | | - |
| 425 | return d->exec(Sql::concat(stmt, where), prepStatement, rec, whereValues); | - |
| 426 | } | - |
| 427 | bool QSqlTableModel::insertRowIntoTable(const QSqlRecord &values) | - |
| 428 | { | - |
| 429 | QSqlTableModelPrivate * const d = d_func(); | - |
| 430 | QSqlRecord rec = values; | - |
| 431 | beforeInsert(rec); | - |
| 432 | | - |
| 433 | const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries); | - |
| 434 | const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::InsertStatement, d->tableName, | - |
| 435 | rec, prepStatement); | - |
| 436 | | - |
| 437 | if (stmt.isEmpty()) { | - |
| 438 | d->error = QSqlError(QLatin1String("No Fields to update"), QString(), | - |
| 439 | QSqlError::StatementError); | - |
| 440 | return false; | - |
| 441 | } | - |
| 442 | | - |
| 443 | return d->exec(stmt, prepStatement, rec, QSqlRecord() ); | - |
| 444 | } | - |
| 445 | bool QSqlTableModel::deleteRowFromTable(int row) | - |
| 446 | { | - |
| 447 | QSqlTableModelPrivate * const d = d_func(); | - |
| 448 | beforeDelete(row); | - |
| 449 | | - |
| 450 | const QSqlRecord whereValues = d->primaryValues(row); | - |
| 451 | const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries); | - |
| 452 | const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::DeleteStatement, | - |
| 453 | d->tableName, | - |
| 454 | QSqlRecord(), | - |
| 455 | prepStatement); | - |
| 456 | const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, | - |
| 457 | d->tableName, | - |
| 458 | whereValues, | - |
| 459 | prepStatement); | - |
| 460 | | - |
| 461 | if (stmt.isEmpty() || where.isEmpty()) { | - |
| 462 | d->error = QSqlError(QLatin1String("Unable to delete row"), QString(), | - |
| 463 | QSqlError::StatementError); | - |
| 464 | return false; | - |
| 465 | } | - |
| 466 | | - |
| 467 | return d->exec(Sql::concat(stmt, where), prepStatement, QSqlRecord() , whereValues); | - |
| 468 | } | - |
| 469 | bool QSqlTableModel::submitAll() | - |
| 470 | { | - |
| 471 | QSqlTableModelPrivate * const d = d_func(); | - |
| 472 | | - |
| 473 | bool success = true; | - |
| 474 | | - |
| 475 | for (QForeachContainer<__typeof__(d->cache.keys())> _container_(d->cache.keys()); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (int row = *_container_.i;; __extension__ ({--_container_.brk; break;})) { | - |
| 476 | | - |
| 477 | QSqlTableModelPrivate::CacheMap::iterator it = d->cache.find(row); | - |
| 478 | if (it == d->cache.end()) partially evaluated: it == d->cache.end()| no Evaluation Count:0 | yes Evaluation Count:160 |
| 0-160 |
| 479 | continue; never executed: continue; | 0 |
| 480 | | - |
| 481 | QSqlTableModelPrivate::ModifiedRow &mrow = it.value(); | - |
| 482 | if (mrow.submitted()) evaluated: mrow.submitted()| yes Evaluation Count:36 | yes Evaluation Count:124 |
| 36-124 |
| 483 | continue; executed: continue;Execution Count:36 | 36 |
| 484 | | - |
| 485 | switch (mrow.op()) { | - |
| 486 | case QSqlTableModelPrivate::Insert: | - |
| 487 | success = insertRowIntoTable(mrow.rec()); | - |
| 488 | break; executed: break;Execution Count:53 | 53 |
| 489 | case QSqlTableModelPrivate::Update: | - |
| 490 | success = updateRowInTable(row, mrow.rec()); | - |
| 491 | break; executed: break;Execution Count:49 | 49 |
| 492 | case QSqlTableModelPrivate::Delete: | - |
| 493 | success = deleteRowFromTable(row); | - |
| 494 | break; executed: break;Execution Count:22 | 22 |
| 495 | case QSqlTableModelPrivate::None: | - |
| 496 | qt_noop(); | - |
| 497 | break; | 0 |
| 498 | } | - |
| 499 | | - |
| 500 | if (success) { evaluated: success| yes Evaluation Count:117 | yes Evaluation Count:7 |
| 7-117 |
| 501 | if (d->strategy != OnManualSubmit && mrow.op() == QSqlTableModelPrivate::Insert) { evaluated: d->strategy != OnManualSubmit| yes Evaluation Count:60 | yes Evaluation Count:57 |
evaluated: mrow.op() == QSqlTableModelPrivate::Insert| yes Evaluation Count:23 | yes Evaluation Count:37 |
| 23-60 |
| 502 | int c = mrow.rec().indexOf(d->autoColumn); | - |
| 503 | if (c != -1 && !mrow.rec().isGenerated(c)) evaluated: c != -1| yes Evaluation Count:6 | yes Evaluation Count:17 |
partially evaluated: !mrow.rec().isGenerated(c)| yes Evaluation Count:6 | no Evaluation Count:0 |
| 0-17 |
| 504 | mrow.setValue(c, d->editQuery.lastInsertId()); executed: mrow.setValue(c, d->editQuery.lastInsertId());Execution Count:6 | 6 |
| 505 | } executed: }Execution Count:23 | 23 |
| 506 | mrow.setSubmitted(); | - |
| 507 | if (d->strategy != OnManualSubmit) evaluated: d->strategy != OnManualSubmit| yes Evaluation Count:60 | yes Evaluation Count:57 |
| 57-60 |
| 508 | success = selectRow(row); executed: success = selectRow(row);Execution Count:60 | 60 |
| 509 | } executed: }Execution Count:117 | 117 |
| 510 | | - |
| 511 | if (!success) evaluated: !success| yes Evaluation Count:7 | yes Evaluation Count:117 |
| 7-117 |
| 512 | break; executed: break;Execution Count:7 | 7 |
| 513 | } executed: }Execution Count:117 | 117 |
| 514 | | - |
| 515 | if (success) { evaluated: success| yes Evaluation Count:118 | yes Evaluation Count:7 |
| 7-118 |
| 516 | if (d->strategy == OnManualSubmit) evaluated: d->strategy == OnManualSubmit| yes Evaluation Count:36 | yes Evaluation Count:82 |
| 36-82 |
| 517 | success = select(); executed: success = select();Execution Count:36 | 36 |
| 518 | } executed: }Execution Count:118 | 118 |
| 519 | | - |
| 520 | return success; executed: return success;Execution Count:125 | 125 |
| 521 | } | - |
| 522 | bool QSqlTableModel::submit() | - |
| 523 | { | - |
| 524 | QSqlTableModelPrivate * const d = d_func(); | - |
| 525 | if (d->strategy == OnRowChange || d->strategy == OnFieldChange) | - |
| 526 | return submitAll(); | - |
| 527 | return true; | - |
| 528 | } | - |
| 529 | void QSqlTableModel::revert() | - |
| 530 | { | - |
| 531 | QSqlTableModelPrivate * const d = d_func(); | - |
| 532 | if (d->strategy == OnRowChange || d->strategy == OnFieldChange) evaluated: d->strategy == OnRowChange| yes Evaluation Count:4 | yes Evaluation Count:9 |
evaluated: d->strategy == OnFieldChange| yes Evaluation Count:4 | yes Evaluation Count:5 |
| 4-9 |
| 533 | revertAll(); executed: revertAll();Execution Count:8 | 8 |
| 534 | } executed: }Execution Count:13 | 13 |
| 535 | void QSqlTableModel::setEditStrategy(EditStrategy strategy) | - |
| 536 | { | - |
| 537 | QSqlTableModelPrivate * const d = d_func(); | - |
| 538 | revertAll(); | - |
| 539 | d->strategy = strategy; | - |
| 540 | } | - |
| 541 | | - |
| 542 | | - |
| 543 | | - |
| 544 | | - |
| 545 | | - |
| 546 | | - |
| 547 | QSqlTableModel::EditStrategy QSqlTableModel::editStrategy() const | - |
| 548 | { | - |
| 549 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 550 | return d->strategy; | - |
| 551 | } | - |
| 552 | | - |
| 553 | | - |
| 554 | | - |
| 555 | | - |
| 556 | | - |
| 557 | | - |
| 558 | void QSqlTableModel::revertAll() | - |
| 559 | { | - |
| 560 | QSqlTableModelPrivate * const d = d_func(); | - |
| 561 | | - |
| 562 | const QList<int> rows(d->cache.keys()); | - |
| 563 | for (int i = rows.size() - 1; i >= 0; --i) | - |
| 564 | revertRow(rows.value(i)); | - |
| 565 | } | - |
| 566 | | - |
| 567 | | - |
| 568 | | - |
| 569 | | - |
| 570 | | - |
| 571 | | - |
| 572 | void QSqlTableModel::revertRow(int row) | - |
| 573 | { | - |
| 574 | if (row < 0) | - |
| 575 | return; | - |
| 576 | | - |
| 577 | QSqlTableModelPrivate * const d = d_func(); | - |
| 578 | d->revertCachedRow(row); | - |
| 579 | } | - |
| 580 | | - |
| 581 | | - |
| 582 | | - |
| 583 | | - |
| 584 | | - |
| 585 | | - |
| 586 | | - |
| 587 | QSqlIndex QSqlTableModel::primaryKey() const | - |
| 588 | { | - |
| 589 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 590 | return d->primaryIndex; | - |
| 591 | } | - |
| 592 | void QSqlTableModel::setPrimaryKey(const QSqlIndex &key) | - |
| 593 | { | - |
| 594 | QSqlTableModelPrivate * const d = d_func(); | - |
| 595 | d->primaryIndex = key; | - |
| 596 | } | - |
| 597 | | - |
| 598 | | - |
| 599 | | - |
| 600 | | - |
| 601 | QSqlDatabase QSqlTableModel::database() const | - |
| 602 | { | - |
| 603 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 604 | return d->db; | - |
| 605 | } | - |
| 606 | void QSqlTableModel::sort(int column, Qt::SortOrder order) | - |
| 607 | { | - |
| 608 | setSort(column, order); | - |
| 609 | select(); | - |
| 610 | } | - |
| 611 | void QSqlTableModel::setSort(int column, Qt::SortOrder order) | - |
| 612 | { | - |
| 613 | QSqlTableModelPrivate * const d = d_func(); | - |
| 614 | d->sortColumn = column; | - |
| 615 | d->sortOrder = order; | - |
| 616 | } | - |
| 617 | | - |
| 618 | | - |
| 619 | | - |
| 620 | | - |
| 621 | | - |
| 622 | | - |
| 623 | | - |
| 624 | QString QSqlTableModel::orderByClause() const | - |
| 625 | { | - |
| 626 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 627 | QSqlField f = d->rec.field(d->sortColumn); | - |
| 628 | if (!f.isValid()) | - |
| 629 | return QString(); | - |
| 630 | | - |
| 631 | | - |
| 632 | | - |
| 633 | QString field = d->db.driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName); | - |
| 634 | field.prepend(QLatin1Char('.')).prepend(d->tableName); | - |
| 635 | field = d->sortOrder == Qt::AscendingOrder ? Sql::asc(field) : Sql::desc(field); | - |
| 636 | return Sql::orderBy(field); | - |
| 637 | } | - |
| 638 | | - |
| 639 | | - |
| 640 | | - |
| 641 | | - |
| 642 | | - |
| 643 | int QSqlTableModel::fieldIndex(const QString &fieldName) const | - |
| 644 | { | - |
| 645 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 646 | return d->rec.indexOf(fieldName); | - |
| 647 | } | - |
| 648 | QString QSqlTableModel::selectStatement() const | - |
| 649 | { | - |
| 650 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 651 | if (d->tableName.isEmpty()) { | - |
| 652 | d->error = QSqlError(QLatin1String("No table name given"), QString(), | - |
| 653 | QSqlError::StatementError); | - |
| 654 | return QString(); | - |
| 655 | } | - |
| 656 | if (d->rec.isEmpty()) { | - |
| 657 | d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), | - |
| 658 | QSqlError::StatementError); | - |
| 659 | return QString(); | - |
| 660 | } | - |
| 661 | | - |
| 662 | const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::SelectStatement, | - |
| 663 | d->tableName, | - |
| 664 | d->rec, | - |
| 665 | false); | - |
| 666 | if (stmt.isEmpty()) { | - |
| 667 | d->error = QSqlError(QLatin1String("Unable to select fields from table ") + d->tableName, | - |
| 668 | QString(), QSqlError::StatementError); | - |
| 669 | return stmt; | - |
| 670 | } | - |
| 671 | return Sql::concat(Sql::concat(stmt, Sql::where(d->filter)), orderByClause()); | - |
| 672 | } | - |
| 673 | bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &parent) | - |
| 674 | { | - |
| 675 | QSqlTableModelPrivate * const d = d_func(); | - |
| 676 | if (parent.isValid() || column < 0 || column + count > d->rec.count()) | - |
| 677 | return false; | - |
| 678 | for (int i = 0; i < count; ++i) | - |
| 679 | d->rec.remove(column); | - |
| 680 | if (d->query.isActive()) | - |
| 681 | return select(); | - |
| 682 | return true; | - |
| 683 | } | - |
| 684 | bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) | - |
| 685 | { | - |
| 686 | QSqlTableModelPrivate * const d = d_func(); | - |
| 687 | if (parent.isValid() || row < 0 || count <= 0) | - |
| 688 | return false; | - |
| 689 | else if (row + count > rowCount()) | - |
| 690 | return false; | - |
| 691 | else if (!count) | - |
| 692 | return true; | - |
| 693 | | - |
| 694 | if (d->strategy != OnManualSubmit) | - |
| 695 | if (count > 1 || (d->cache.value(row).submitted() && isDirty())) | - |
| 696 | return false; | - |
| 697 | | - |
| 698 | | - |
| 699 | | - |
| 700 | for (int idx = row + count - 1; idx >= row; --idx) { | - |
| 701 | QSqlTableModelPrivate::ModifiedRow& mrow = d->cache[idx]; | - |
| 702 | if (mrow.op() == QSqlTableModelPrivate::Insert) { | - |
| 703 | revertRow(idx); | - |
| 704 | } else { | - |
| 705 | if (mrow.op() == QSqlTableModelPrivate::None) | - |
| 706 | mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete, | - |
| 707 | QSqlQueryModel::record(idx)); | - |
| 708 | else | - |
| 709 | mrow.setOp(QSqlTableModelPrivate::Delete); | - |
| 710 | if (d->strategy == OnManualSubmit) | - |
| 711 | headerDataChanged(Qt::Vertical, idx, idx); | - |
| 712 | } | - |
| 713 | } | - |
| 714 | | - |
| 715 | if (d->strategy != OnManualSubmit) | - |
| 716 | return submit(); | - |
| 717 | | - |
| 718 | return true; | - |
| 719 | } | - |
| 720 | bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent) | - |
| 721 | { | - |
| 722 | QSqlTableModelPrivate * const d = d_func(); | - |
| 723 | if (row < 0 || count <= 0 || row > rowCount() || parent.isValid()) | - |
| 724 | return false; | - |
| 725 | | - |
| 726 | if (d->strategy != OnManualSubmit) | - |
| 727 | if (count != 1 || isDirty()) | - |
| 728 | return false; | - |
| 729 | | - |
| 730 | d->busyInsertingRows = true; | - |
| 731 | beginInsertRows(parent, row, row + count - 1); | - |
| 732 | | - |
| 733 | if (d->strategy != OnManualSubmit) | - |
| 734 | d->cache.empty(); | - |
| 735 | | - |
| 736 | if (!d->cache.isEmpty()) { | - |
| 737 | QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = d->cache.end(); | - |
| 738 | while (it != d->cache.begin() && (--it).key() >= row) { | - |
| 739 | int oldKey = it.key(); | - |
| 740 | const QSqlTableModelPrivate::ModifiedRow oldValue = it.value(); | - |
| 741 | d->cache.erase(it); | - |
| 742 | it = d->cache.insert(oldKey + count, oldValue); | - |
| 743 | } | - |
| 744 | } | - |
| 745 | | - |
| 746 | for (int i = 0; i < count; ++i) { | - |
| 747 | d->cache[row + i] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Insert, | - |
| 748 | d->rec); | - |
| 749 | primeInsert(row + i, d->cache[row + i].recRef()); | - |
| 750 | } | - |
| 751 | | - |
| 752 | endInsertRows(); | - |
| 753 | d->busyInsertingRows = false; | - |
| 754 | return true; | - |
| 755 | } | - |
| 756 | bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record) | - |
| 757 | { | - |
| 758 | if (row < 0) | - |
| 759 | row = rowCount(); | - |
| 760 | if (!insertRow(row, QModelIndex())) | - |
| 761 | return false; | - |
| 762 | if (!setRecord(row, record)) { | - |
| 763 | revertRow(row); | - |
| 764 | return false; | - |
| 765 | } | - |
| 766 | return true; | - |
| 767 | } | - |
| 768 | | - |
| 769 | | - |
| 770 | | - |
| 771 | int QSqlTableModel::rowCount(const QModelIndex &parent) const | - |
| 772 | { | - |
| 773 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 774 | | - |
| 775 | if (parent.isValid()) | - |
| 776 | return 0; | - |
| 777 | | - |
| 778 | return QSqlQueryModel::rowCount() + d->insertCount(); | - |
| 779 | } | - |
| 780 | QModelIndex QSqlTableModel::indexInQuery(const QModelIndex &item) const | - |
| 781 | { | - |
| 782 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 783 | if (d->cache.value(item.row()).insert()) | - |
| 784 | return QModelIndex(); | - |
| 785 | | - |
| 786 | const int rowOffset = d->insertCount(item.row()); | - |
| 787 | return QSqlQueryModel::indexInQuery(createIndex(item.row() - rowOffset, item.column(), item.internalPointer())); | - |
| 788 | } | - |
| 789 | | - |
| 790 | | - |
| 791 | | - |
| 792 | | - |
| 793 | | - |
| 794 | | - |
| 795 | QString QSqlTableModel::filter() const | - |
| 796 | { | - |
| 797 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 798 | return d->filter; | - |
| 799 | } | - |
| 800 | void QSqlTableModel::setFilter(const QString &filter) | - |
| 801 | { | - |
| 802 | QSqlTableModelPrivate * const d = d_func(); | - |
| 803 | d->filter = filter; | - |
| 804 | if (d->query.isActive()) | - |
| 805 | select(); | - |
| 806 | } | - |
| 807 | | - |
| 808 | | - |
| 809 | | - |
| 810 | void QSqlTableModel::clear() | - |
| 811 | { | - |
| 812 | QSqlTableModelPrivate * const d = d_func(); | - |
| 813 | d->clear(); | - |
| 814 | QSqlQueryModel::clear(); | - |
| 815 | } | - |
| 816 | | - |
| 817 | | - |
| 818 | | - |
| 819 | Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const | - |
| 820 | { | - |
| 821 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 822 | if (index.internalPointer() || index.column() < 0 || index.column() >= d->rec.count() | - |
| 823 | || index.row() < 0) | - |
| 824 | return 0; | - |
| 825 | | - |
| 826 | bool editable = true; | - |
| 827 | | - |
| 828 | if (d->rec.field(index.column()).isReadOnly()) { | - |
| 829 | editable = false; | - |
| 830 | } | - |
| 831 | else { | - |
| 832 | const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row()); | - |
| 833 | if (mrow.op() == QSqlTableModelPrivate::Delete) { | - |
| 834 | editable = false; | - |
| 835 | } | - |
| 836 | else if (d->strategy == OnFieldChange) { | - |
| 837 | if (mrow.op() != QSqlTableModelPrivate::Insert) | - |
| 838 | if (!isDirty(index) && isDirty()) | - |
| 839 | editable = false; | - |
| 840 | } | - |
| 841 | else if (d->strategy == OnRowChange) { | - |
| 842 | if (mrow.submitted() && isDirty()) | - |
| 843 | editable = false; | - |
| 844 | } | - |
| 845 | } | - |
| 846 | | - |
| 847 | if (!editable) | - |
| 848 | return QSqlQueryModel::flags(index); | - |
| 849 | else | - |
| 850 | return QSqlQueryModel::flags(index) | Qt::ItemIsEditable; | - |
| 851 | } | - |
| 852 | | - |
| 853 | QSqlRecord QSqlTableModel::record() const | - |
| 854 | { | - |
| 855 | return QSqlQueryModel::record(); | - |
| 856 | } | - |
| 857 | QSqlRecord QSqlTableModel::record(int row) const | - |
| 858 | { | - |
| 859 | const QSqlTableModelPrivate * const d = d_func(); | - |
| 860 | | - |
| 861 | | - |
| 862 | QSqlRecord rec = QSqlQueryModel::record(row); | - |
| 863 | | - |
| 864 | | - |
| 865 | const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(row); | - |
| 866 | if (mrow.op() != QSqlTableModelPrivate::None) { | - |
| 867 | const QSqlRecord crec = mrow.rec(); | - |
| 868 | for (int i = 0, cnt = rec.count(); i < cnt; ++i) | - |
| 869 | rec.setGenerated(i, crec.isGenerated(i)); | - |
| 870 | } | - |
| 871 | | - |
| 872 | return rec; | - |
| 873 | } | - |
| 874 | bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) | - |
| 875 | { | - |
| 876 | QSqlTableModelPrivate * const d = d_func(); | - |
| 877 | qt_noop(); | - |
| 878 | if (d->busyInsertingRows) | - |
| 879 | return false; | - |
| 880 | | - |
| 881 | if (row >= rowCount()) | - |
| 882 | return false; | - |
| 883 | | - |
| 884 | if (d->cache.value(row).op() == QSqlTableModelPrivate::Delete) | - |
| 885 | return false; | - |
| 886 | | - |
| 887 | if (d->strategy != OnManualSubmit && d->cache.value(row).submitted() && isDirty()) | - |
| 888 | return false; | - |
| 889 | | - |
| 890 | | - |
| 891 | typedef QMap<int, int> Map; | - |
| 892 | Map map; | - |
| 893 | for (int i = 0; i < values.count(); ++i) { | - |
| 894 | int idx = d->nameToIndex(values.fieldName(i)); | - |
| 895 | if (idx == -1) | - |
| 896 | return false; | - |
| 897 | map[i] = idx; | - |
| 898 | } | - |
| 899 | | - |
| 900 | QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row]; | - |
| 901 | if (mrow.op() == QSqlTableModelPrivate::None) | - |
| 902 | mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, | - |
| 903 | QSqlQueryModel::record(row)); | - |
| 904 | | - |
| 905 | Map::const_iterator i = map.constBegin(); | - |
| 906 | const Map::const_iterator e = map.constEnd(); | - |
| 907 | for ( ; i != e; ++i) { | - |
| 908 | | - |
| 909 | EditStrategy strategy = d->strategy; | - |
| 910 | d->strategy = OnManualSubmit; | - |
| 911 | QModelIndex cIndex = createIndex(row, i.value()); | - |
| 912 | setData(cIndex, values.value(i.key())); | - |
| 913 | d->strategy = strategy; | - |
| 914 | | - |
| 915 | if (!values.isGenerated(i.key())) | - |
| 916 | mrow.recRef().setGenerated(i.value(), false); | - |
| 917 | } | - |
| 918 | | - |
| 919 | if (d->strategy != OnManualSubmit) | - |
| 920 | return submit(); | - |
| 921 | | - |
| 922 | return true; | - |
| 923 | } | - |
| 924 | | - |
| 925 | | - |
| 926 | | - |
| | |