17#include "moc_qgsqueryresultmodel.cpp"
24const int QgsQueryResultModel::FETCH_MORE_ROWS_COUNT = 400;
27 : QAbstractTableModel( parent )
28 , mQueryResult( queryResult )
29 , mColumns( queryResult.columns() )
31 qRegisterMetaType< QList<QList<QVariant>>>(
"QList<QList<QVariant>>" );
32 mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
33 mWorker->moveToThread( &mWorkerThread );
35 connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady,
this, &QgsQueryResultModel::rowsReady );
36 connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete,
this, &QgsQueryResultModel::fetchingComplete );
37 connect(
this, &QgsQueryResultModel::fetchMoreRows, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
38 mWorkerThread.start();
39 if ( mQueryResult.rowCount() > 0 )
41 mRows.reserve( mQueryResult.rowCount() );
45void QgsQueryResultModel::rowsReady(
const QList<QList<QVariant>> &rows )
47 beginInsertRows( QModelIndex(), mRows.count( ), mRows.count( ) + rows.count() - 1 );
53bool QgsQueryResultModel::canFetchMore(
const QModelIndex &parent )
const
55 if ( parent.isValid() )
57 return mQueryResult.rowCount() < 0 || mRows.length() < mQueryResult.rowCount();
61void QgsQueryResultModel::fetchMore(
const QModelIndex &parent )
63 if ( ! parent.isValid() )
65 emit fetchingStarted();
66 emit fetchMoreRows( FETCH_MORE_ROWS_COUNT );
70void QgsQueryResultModel::cancel()
74 mWorker->stopFetching();
83QStringList QgsQueryResultModel::columns()
const
88QgsQueryResultModel::~QgsQueryResultModel()
92 mWorker->stopFetching();
98 emit fetchingComplete();
102int QgsQueryResultModel::rowCount(
const QModelIndex &parent )
const
104 if ( parent.isValid() )
106 return mRows.count();
109int QgsQueryResultModel::columnCount(
const QModelIndex &parent )
const
111 if ( parent.isValid() )
113 return mColumns.count();
116QVariant QgsQueryResultModel::data(
const QModelIndex &index,
int role )
const
118 if ( !index.isValid() || index.row() < 0 || index.column() >= mColumns.count() ||
119 index.row() >= mRows.count( ) )
124 case Qt::DisplayRole:
126 const QList<QVariant> result = mRows.at( index.row() );
127 if ( index.column() < result.count( ) )
129 const QVariant value = result.at( index.column() );
135 else if ( value.type() == QVariant::ByteArray )
149 const QList<QVariant> result = mRows.at( index.row() );
150 if ( index.column() < result.count( ) )
152 const QVariant value = result.at( index.column() );
164 case Qt::ForegroundRole:
166 const QList<QVariant> result = mRows.at( index.row() );
167 if ( index.column() < result.count( ) )
169 const QVariant value = result.at( index.column() );
173 return QColor( 128, 128, 128 );
179 case Qt::ToolTipRole:
181 const QList<QVariant> result = mRows.at( index.row() );
182 if ( index.column() < result.count( ) )
184 const QVariant value = result.at( index.column() );
203QVariant QgsQueryResultModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
205 if ( orientation == Qt::Orientation::Horizontal && section < mColumns.count() )
209 case Qt::ItemDataRole::DisplayRole:
210 case Qt::ItemDataRole::ToolTipRole:
211 return mColumns.at( section );
217 return QAbstractTableModel::headerData( section, orientation, role );
222const int QgsQueryResultFetcher::ROWS_BATCH_COUNT = 200;
224void QgsQueryResultFetcher::fetchRows(
long long maxRows )
226 long long rowCount { 0 };
227 QList<QList<QVariant>> newRows;
228 newRows.reserve( ROWS_BATCH_COUNT );
229 while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
231 newRows.append( mQueryResult->nextRow() );
233 if ( rowCount % ROWS_BATCH_COUNT == 0 && mStopFetching == 0 )
235 emit rowsReady( newRows );
240 if ( rowCount % ROWS_BATCH_COUNT && mStopFetching == 0 )
242 emit rowsReady( newRows );
245 emit fetchingComplete();
248void 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()