19const int QgsQueryResultModel::FETCH_MORE_ROWS_COUNT = 400;
22 : QAbstractTableModel( parent )
23 , mQueryResult( queryResult )
24 , mColumns( queryResult.columns() )
26 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
27 mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
28 mWorker->moveToThread( &mWorkerThread );
33 mWorkerThread.start();
36 mRows.reserve( mQueryResult.
rowCount() );
42 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
50 if ( parent.isValid() )
52 return mQueryResult.
rowCount() < 0 || mRows.length() < mQueryResult.
rowCount();
58 if ( ! parent.isValid() )
69 mWorker->stopFetching();
87 mWorker->stopFetching();
99 if ( parent.isValid() )
101 return mRows.count();
106 if ( parent.isValid() )
108 return mColumns.count();
113 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
114 index.row() >= mRows.count( ) )
119 case Qt::DisplayRole:
121 const QList<QVariant> result = mRows.at( index.row() );
122 if ( index.column() < result.count( ) )
124 return result.at( index.column() );
129 case Qt::ToolTipRole:
131 const QList<QVariant> result = mRows.at( index.row() );
132 if ( index.column() < result.count( ) )
134 const QVariant value = result.at( index.column() );
145 if ( orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole && section < mColumns.count() )
147 return mColumns.at( section );
149 return QAbstractTableModel::headerData( section, orientation, role );
154const int QgsQueryResultFetcher::ROWS_BATCH_COUNT = 200;
156void QgsQueryResultFetcher::fetchRows(
long long maxRows )
158 long long rowCount { 0 };
159 QList<QList<QVariant>> newRows;
160 newRows.reserve( ROWS_BATCH_COUNT );
161 while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
163 newRows.append( mQueryResult->nextRow() );
165 if ( rowCount % ROWS_BATCH_COUNT == 0 && mStopFetching == 0 )
167 emit rowsReady( newRows );
172 if ( rowCount % ROWS_BATCH_COUNT && mStopFetching == 0 )
174 emit rowsReady( newRows );
177 emit fetchingComplete();
180void QgsQueryResultFetcher::stopFetching()
static QString formatPreviewString(const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60)
Formats an expression result for friendly display to the user.
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...