17void QSqlQueryModelPrivate::prefetch(
int limit)
21 if (atEnd || limit <= bottom.row() || bottom.column() == -1)
24 QModelIndex newBottom;
25 const int oldBottomRow = qMax(bottom.row(), 0);
28 if (query.seek(limit)) {
29 newBottom = q->createIndex(limit, bottom.column());
36 newBottom = q->createIndex(i, bottom.column());
39 newBottom = q->createIndex(-1, bottom.column());
43 if (newBottom.row() >= 0 && newBottom.row() > bottom.row()) {
44 q->beginInsertRows(QModelIndex(), bottom.row() + 1, newBottom.row());
326QVariant QSqlQueryModel::data(
const QModelIndex &item,
int role)
const
328 Q_D(
const QSqlQueryModel);
333 if (role & ~(Qt::DisplayRole | Qt::EditRole))
336 if (!d->rec.isGenerated(item.column()))
338 QModelIndex dItem = indexInQuery(item);
339 if (dItem.row() > d->bottom.row())
340 const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row());
342 if (!d->query.seek(dItem.row())) {
343 d->error = d->query.lastError();
347 return d->query.value(dItem.column());
354QVariant QSqlQueryModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
356 Q_D(
const QSqlQueryModel);
357 if (orientation == Qt::Horizontal) {
358 QVariant val = d->headers.value(section).value(role);
359 if (role == Qt::DisplayRole && !val.isValid())
360 val = d->headers.value(section).value(Qt::EditRole);
363 if (role == Qt::DisplayRole && d->rec.count() > section && d->columnInQuery(section) != -1)
364 return d->rec.fieldName(section);
366 return QAbstractItemModel::headerData(section, orientation, role);
408void QSqlQueryModel::setQuery(QSqlQuery &&query)
413 QSqlRecord newRec = query.record();
414 bool columnsChanged = (newRec != d->rec);
416 if (d->colOffsets.size() != newRec.count() || columnsChanged)
417 d->initColOffsets(newRec.count());
419 d->bottom = QModelIndex();
420 d->error = QSqlError();
421 d->query = std::move(query);
425 if (d->query.isForwardOnly()) {
426 d->error = QSqlError(
"Forward-only queries cannot be used in a data model"_L1,
427 QString(), QSqlError::ConnectionError);
432 if (!d->query.isActive()) {
433 d->error = d->query.lastError();
438 if (d->query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) {
439 d->bottom = createIndex(d->query.size() - 1, d->rec.count() - 1);
441 d->bottom = createIndex(-1, d->rec.count() - 1);
503bool QSqlQueryModel::setHeaderData(
int section, Qt::Orientation orientation,
504 const QVariant &value,
int role)
507 if (orientation != Qt::Horizontal || section < 0 || columnCount() <= section)
510 if (d->headers.size() <= section)
511 d->headers.resize(qMax(section + 1, 16));
512 d->headers[section][role] = value;
513 emit headerDataChanged(orientation, section, section);
604bool QSqlQueryModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
607 if (count <= 0 || parent.isValid() || column < 0 || column > d->rec.count())
610 beginInsertColumns(parent, column, column + count - 1);
611 for (
int c = 0; c < count; ++c) {
613 field.setReadOnly(
true);
614 field.setGenerated(
false);
615 d->rec.insert(column, field);
616 if (d->colOffsets.size() < d->rec.count()) {
617 int nVal = d->colOffsets.isEmpty() ? 0 : d->colOffsets[d->colOffsets.size() - 1];
618 d->colOffsets.append(nVal);
619 Q_ASSERT(d->colOffsets.size() >= d->rec.count());
621 for (qsizetype i = column + 1; i < d->colOffsets.size(); ++i)
639bool QSqlQueryModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
642 if (count <= 0 || parent.isValid() || column < 0 || column >= d->rec.count())
645 beginRemoveColumns(parent, column, column + count - 1);
647 for (
int i = 0; i < count; ++i)
648 d->rec.remove(column);
649 for (qsizetype i = column; i < d->colOffsets.size(); ++i)
650 d->colOffsets[i] -= count;