24#include "moc_qgsqueryresultmodel.cpp"
26const int QgsQueryResultModel::FETCH_MORE_ROWS_COUNT = 400;
29 : QAbstractTableModel( parent )
30 , mQueryResult( queryResult )
31 , mColumns( queryResult.columns() )
33 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
34 mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
35 mWorker->moveToThread( &mWorkerThread );
37 connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady,
this, &QgsQueryResultModel::rowsReady );
38 connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete,
this, &QgsQueryResultModel::fetchingComplete );
39 connect(
this, &QgsQueryResultModel::fetchMoreRows, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
40 mWorkerThread.start();
41 if ( mQueryResult.rowCount() > 0 )
43 mRows.reserve( mQueryResult.rowCount() );
47void QgsQueryResultModel::rowsReady(
const QList<QList<QVariant>> &rows )
49 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
55bool QgsQueryResultModel::canFetchMore(
const QModelIndex &parent )
const
57 if ( parent.isValid() )
59 return mQueryResult.rowCount() < 0 || mRows.length() < mQueryResult.rowCount();
63void QgsQueryResultModel::fetchMore(
const QModelIndex &parent )
65 if ( ! parent.isValid() )
67 emit fetchingStarted();
68 emit fetchMoreRows( FETCH_MORE_ROWS_COUNT );
72void QgsQueryResultModel::cancel()
76 mWorker->stopFetching();
85QStringList QgsQueryResultModel::columns()
const
90QgsQueryResultModel::~QgsQueryResultModel()
94 mWorker->stopFetching();
100 emit fetchingComplete();
104int QgsQueryResultModel::rowCount(
const QModelIndex &parent )
const
106 if ( parent.isValid() )
108 return mRows.count();
111int QgsQueryResultModel::columnCount(
const QModelIndex &parent )
const
113 if ( parent.isValid() )
115 return mColumns.count();
118QVariant QgsQueryResultModel::data(
const QModelIndex &index,
int role )
const
120 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
121 index.row() >= mRows.count( ) )
126 case Qt::DisplayRole:
128 const QList<QVariant> result = mRows.at( index.row() );
129 if ( index.column() < result.count( ) )
131 const QVariant value = result.at( index.column() );
137 else if ( value.type() == QVariant::ByteArray )
151 const QList<QVariant> result = mRows.at( index.row() );
152 if ( index.column() < result.count( ) )
154 const QVariant value = result.at( index.column() );
166 case Qt::ForegroundRole:
168 const QList<QVariant> result = mRows.at( index.row() );
169 if ( index.column() < result.count( ) )
171 const QVariant value = result.at( index.column() );
175 return QColor( 128, 128, 128 );
181 case Qt::ToolTipRole:
183 const QList<QVariant> result = mRows.at( index.row() );
184 if ( index.column() < result.count( ) )
186 const QVariant value = result.at( index.column() );
205QVariant QgsQueryResultModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
207 if ( orientation == Qt::Orientation::Horizontal && section < mColumns.count() )
211 case Qt::ItemDataRole::DisplayRole:
212 case Qt::ItemDataRole::ToolTipRole:
213 return mColumns.at( section );
219 return QAbstractTableModel::headerData( section, orientation, role );
224const int QgsQueryResultFetcher::ROWS_BATCH_COUNT = 200;
226void QgsQueryResultFetcher::fetchRows(
long long maxRows )
228 long long rowCount { 0 };
229 QList<QList<QVariant>> newRows;
230 newRows.reserve( ROWS_BATCH_COUNT );
231 while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
233 newRows.append( mQueryResult->nextRow() );
235 if ( rowCount % ROWS_BATCH_COUNT == 0 && mStopFetching == 0 )
237 emit rowsReady( newRows );
242 if ( rowCount % ROWS_BATCH_COUNT && mStopFetching == 0 )
244 emit rowsReady( newRows );
247 emit fetchingComplete();
250void QgsQueryResultFetcher::stopFetching()
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static QString formatPreviewString(const QVariant &value, bool htmlOutput=true, int maximumPreviewLength=60)
Formats an expression result for friendly display to the user.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
The QueryResult class represents the result of a query executed by execSql().