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