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 );
30 connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady,
this, &QgsQueryResultModel::rowsReady );
31 connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete,
this, &QgsQueryResultModel::fetchingComplete );
32 connect(
this, &QgsQueryResultModel::fetchMoreRows, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
33 mWorkerThread.start();
34 if ( mQueryResult.rowCount() > 0 )
36 mRows.reserve( mQueryResult.rowCount() );
40void QgsQueryResultModel::rowsReady(
const QList<QList<QVariant>> &rows )
42 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
48bool QgsQueryResultModel::canFetchMore(
const QModelIndex &parent )
const
50 if ( parent.isValid() )
52 return mQueryResult.rowCount() < 0 || mRows.length() < mQueryResult.rowCount();
56void QgsQueryResultModel::fetchMore(
const QModelIndex &parent )
58 if ( ! parent.isValid() )
60 emit fetchingStarted();
61 emit fetchMoreRows( FETCH_MORE_ROWS_COUNT );
65void QgsQueryResultModel::cancel()
69 mWorker->stopFetching();
78QStringList QgsQueryResultModel::columns()
const
83QgsQueryResultModel::~QgsQueryResultModel()
87 mWorker->stopFetching();
93 emit fetchingComplete();
97int QgsQueryResultModel::rowCount(
const QModelIndex &parent )
const
99 if ( parent.isValid() )
101 return mRows.count();
104int QgsQueryResultModel::columnCount(
const QModelIndex &parent )
const
106 if ( parent.isValid() )
108 return mColumns.count();
111QVariant QgsQueryResultModel::data(
const QModelIndex &index,
int role )
const
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() );
143QVariant QgsQueryResultModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
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.
The QueryResult class represents the result of a query executed by execSql()