17#include "moc_qgsqueryresultmodel.cpp"
20const int QgsQueryResultModel::FETCH_MORE_ROWS_COUNT = 400;
23 : QAbstractTableModel( parent )
24 , mQueryResult( queryResult )
25 , mColumns( queryResult.columns() )
27 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
28 mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
29 mWorker->moveToThread( &mWorkerThread );
31 connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady,
this, &QgsQueryResultModel::rowsReady );
32 connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete,
this, &QgsQueryResultModel::fetchingComplete );
33 connect(
this, &QgsQueryResultModel::fetchMoreRows, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
34 mWorkerThread.start();
35 if ( mQueryResult.rowCount() > 0 )
37 mRows.reserve( mQueryResult.rowCount() );
41void QgsQueryResultModel::rowsReady(
const QList<QList<QVariant>> &rows )
43 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
49bool QgsQueryResultModel::canFetchMore(
const QModelIndex &parent )
const
51 if ( parent.isValid() )
53 return mQueryResult.rowCount() < 0 || mRows.length() < mQueryResult.rowCount();
57void QgsQueryResultModel::fetchMore(
const QModelIndex &parent )
59 if ( ! parent.isValid() )
61 emit fetchingStarted();
62 emit fetchMoreRows( FETCH_MORE_ROWS_COUNT );
66void QgsQueryResultModel::cancel()
70 mWorker->stopFetching();
79QStringList QgsQueryResultModel::columns()
const
84QgsQueryResultModel::~QgsQueryResultModel()
88 mWorker->stopFetching();
94 emit fetchingComplete();
98int QgsQueryResultModel::rowCount(
const QModelIndex &parent )
const
100 if ( parent.isValid() )
102 return mRows.count();
105int QgsQueryResultModel::columnCount(
const QModelIndex &parent )
const
107 if ( parent.isValid() )
109 return mColumns.count();
112QVariant QgsQueryResultModel::data(
const QModelIndex &index,
int role )
const
114 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
115 index.row() >= mRows.count( ) )
120 case Qt::DisplayRole:
122 const QList<QVariant> result = mRows.at( index.row() );
123 if ( index.column() < result.count( ) )
125 return result.at( index.column() );
130 case Qt::ToolTipRole:
132 const QList<QVariant> result = mRows.at( index.row() );
133 if ( index.column() < result.count( ) )
135 const QVariant value = result.at( index.column() );
144QVariant QgsQueryResultModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
146 if ( orientation == Qt::Orientation::Horizontal && role == Qt::ItemDataRole::DisplayRole && section < mColumns.count() )
148 return mColumns.at( section );
150 return QAbstractTableModel::headerData( section, orientation, role );
155const int QgsQueryResultFetcher::ROWS_BATCH_COUNT = 200;
157void QgsQueryResultFetcher::fetchRows(
long long maxRows )
159 long long rowCount { 0 };
160 QList<QList<QVariant>> newRows;
161 newRows.reserve( ROWS_BATCH_COUNT );
162 while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
164 newRows.append( mQueryResult->nextRow() );
166 if ( rowCount % ROWS_BATCH_COUNT == 0 && mStopFetching == 0 )
168 emit rowsReady( newRows );
173 if ( rowCount % ROWS_BATCH_COUNT && mStopFetching == 0 )
175 emit rowsReady( newRows );
178 emit fetchingComplete();
181void QgsQueryResultFetcher::stopFetching()
static QString formatPreviewString(const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60)
Formats an expression result for friendly display to the user.
The QueryResult class represents the result of a query executed by execSql()