18 const int QgsQueryResultModel::FETCH_MORE_ROWS_COUNT = 400;
21 : QAbstractTableModel( parent )
22 , mQueryResult( queryResult )
23 , mColumns( queryResult.columns() )
25 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
26 mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
27 mWorker->moveToThread( &mWorkerThread );
32 mWorkerThread.start();
35 mRows.reserve( mQueryResult.
rowCount() );
41 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
49 if ( parent.isValid() )
51 return mQueryResult.
rowCount() < 0 || mRows.length() < mQueryResult.
rowCount();
57 if ( ! parent.isValid() )
68 mWorker->stopFetching();
86 mWorker->stopFetching();
98 if ( parent.isValid() )
100 return mRows.count();
105 if ( parent.isValid() )
107 return mColumns.count();
112 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
113 index.row() >= mRows.count( ) )
118 case Qt::DisplayRole:
120 const QList<QVariant> result = mRows.at( index.row() );
121 if ( index.column() < result.count( ) )
123 return result.at( index.column() );
133 if ( orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole && section < mColumns.count() )
135 return mColumns.at( section );
137 return QAbstractTableModel::headerData( section, orientation, role );
142 const int QgsQueryResultFetcher::ROWS_BATCH_COUNT = 200;
144 void QgsQueryResultFetcher::fetchRows(
long long maxRows )
146 long long rowCount { 0 };
147 QList<QList<QVariant>> newRows;
148 newRows.reserve( ROWS_BATCH_COUNT );
149 while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
151 newRows.append( mQueryResult->nextRow() );
153 if ( rowCount % ROWS_BATCH_COUNT == 0 && mStopFetching == 0 )
155 emit rowsReady( newRows );
160 if ( rowCount % ROWS_BATCH_COUNT && mStopFetching == 0 )
162 emit rowsReady( newRows );
165 emit fetchingComplete();
168 void QgsQueryResultFetcher::stopFetching()
QgsAbstractDatabaseProviderConnection::QueryResult queryResult() const
Returns the query result.
int rowCount(const QModelIndex &parent) const override
QgsQueryResultModel(const QgsAbstractDatabaseProviderConnection::QueryResult &queryResult, QObject *parent=nullptr)
Constructs a QgsQueryResultModel from a queryResult with optional parent.
void rowsReady(const QList< QList< QVariant > > &rows)
Triggered when newRows have been fetched and can be added to the model.
void fetchingStarted()
Emitted when fetching of rows has started.
QVariant data(const QModelIndex &index, int role) const override
void fetchMore(const QModelIndex &parent) override
void fetchMoreRows(qlonglong maxRows)
Emitted when more rows are requested.
void fetchingComplete()
Emitted when rows have been fetched (all of them or a batch if maxRows was passed to fetchMoreRows() ...
int columnCount(const QModelIndex &parent) const override
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
void cancel()
Cancels the row fetching.
QStringList columns() const
Returns the column names.
bool canFetchMore(const QModelIndex &parent) const override
The QueryResult class represents the result of a query executed by execSql()
long long rowCount() const
Returns the number of rows returned by a SELECT query or Qgis::FeatureCountState::UnknownCount if unk...