18void QSqlQueryModelPrivate::prefetch(
int limit)
22 if (atEnd || limit <= bottom.row() || bottom.column() == -1)
25 QModelIndex newBottom;
26 const int oldBottomRow = qMax(bottom.row(), 0);
29 if (query.seek(limit)) {
30 newBottom = q->createIndex(limit, bottom.column());
37 newBottom = q->createIndex(i, bottom.column());
40 newBottom = q->createIndex(-1, bottom.column());
44 if (newBottom.row() >= 0 && newBottom.row() > bottom.row()) {
45 q->beginInsertRows(QModelIndex(), bottom.row() + 1, newBottom.row());
328QVariant QSqlQueryModel::data(
const QModelIndex &item,
int role)
const
330 Q_D(
const QSqlQueryModel);
334 if (role & ~(Qt::DisplayRole | Qt::EditRole))
337 if (!d->rec.isGenerated(item.column()))
339 QModelIndex dItem = indexInQuery(item);
340 if (dItem.row() > d->bottom.row())
341 const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row());
343 if (!d->query.seek(dItem.row())) {
344 d->error = d->query.lastError();
348 return d->query.value(dItem.column());
355QVariant QSqlQueryModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
357 Q_D(
const QSqlQueryModel);
358 if (orientation == Qt::Horizontal) {
359 QVariant val = d->headers.value(section).value(role);
360 if (role == Qt::DisplayRole && !val.isValid())
361 val = d->headers.value(section).value(Qt::EditRole);
364 if (role == Qt::DisplayRole && d->rec.count() > section && d->columnInQuery(section) != -1)
365 return d->rec.fieldName(section);
367 return QAbstractItemModel::headerData(section, orientation, role);
411void QSqlQueryModel::setQuery(QSqlQuery &&query)
416 QSqlRecord newRec = query.record();
417 bool columnsChanged = (newRec != d->rec);
419 if (d->colOffsets.size() != newRec.count() || columnsChanged)
420 d->initColOffsets(newRec.count());
422 d->bottom = QModelIndex();
423 d->error = QSqlError();
424 d->query = std::move(query);
428 if (d->query.isForwardOnly()) {
429 d->error = QSqlError(
"Forward-only queries cannot be used in a data model"_L1,
430 QString(), QSqlError::ConnectionError);
435 if (!d->query.isActive()) {
436 d->error = d->query.lastError();
441 if (d->query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) {
442 d->bottom = createIndex(d->query.size() - 1, d->rec.count() - 1);
444 d->bottom = createIndex(-1, d->rec.count() - 1);
483void QSqlQueryModel::refresh()
486 const auto connName = d->query.driver()
487 ? d->query.driver()->connectionName() : QString();
488 setQuery(d->query.executedQuery(), QSqlDatabase::database(connName));
522bool QSqlQueryModel::setHeaderData(
int section, Qt::Orientation orientation,
523 const QVariant &value,
int role)
526 if (orientation != Qt::Horizontal || section < 0 || columnCount() <= section)
529 if (d->headers.size() <= section)
530 d->headers.resize(qMax(section + 1, 16));
531 d->headers[section][role] = value;
532 emit headerDataChanged(orientation, section, section);
623bool QSqlQueryModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
626 if (count <= 0 || parent.isValid() || column < 0 || column > d->rec.count())
629 beginInsertColumns(parent, column, column + count - 1);
630 for (
int c = 0; c < count; ++c) {
632 field.setReadOnly(
true);
633 field.setGenerated(
false);
634 d->rec.insert(column, field);
635 if (d->colOffsets.size() < d->rec.count()) {
636 int nVal = d->colOffsets.isEmpty() ? 0 : d->colOffsets[d->colOffsets.size() - 1];
637 d->colOffsets.append(nVal);
638 Q_ASSERT(d->colOffsets.size() >= d->rec.count());
640 for (qsizetype i = column + 1; i < d->colOffsets.size(); ++i)
658bool QSqlQueryModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
661 if (count <= 0 || parent.isValid() || column < 0 || column >= d->rec.count())
664 beginRemoveColumns(parent, column, column + count - 1);
666 for (
int i = 0; i < count; ++i)
667 d->rec.remove(column);
668 for (qsizetype i = column; i < d->colOffsets.size(); ++i)
669 d->colOffsets[i] -= count;