Skip to content
Permalink
Browse files
Fetch more in the model
  • Loading branch information
elpaso committed Jul 6, 2021
1 parent 2c9662b commit eea1eb48fa0ecb1414285f0c5e8fd75bddfabc27
@@ -44,6 +44,9 @@ Constructs a QgsQueryResultModel from a ``queryResult`` with optional ``parent``
virtual QVariant headerData( int section, Qt::Orientation orientation, int role ) const;


void fetchMore( const QModelIndex &parent );
bool canFetchMore( const QModelIndex &parent ) const;

QStringList columns() const;
%Docstring
Returns the column names
@@ -76,6 +79,10 @@ Cancels the row fetching.
Emitted when all rows have been fetched or when the fetching has been stopped
%End

void fetchMoreRows( qlonglong maxRows );

void fetchingStarted();

};

/************************************************************************
@@ -23,10 +23,11 @@ QgsQueryResultModel::QgsQueryResultModel( const QgsAbstractDatabaseProviderConne
qRegisterMetaType< QList<QList<QVariant>>>( "QList<QList<QVariant>>" );
mWorker = std::make_unique<QgsQueryResultFetcher>( &mQueryResult );
mWorker->moveToThread( &mWorkerThread );
connect( &mWorkerThread, &QThread::started, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
//connect( &mWorkerThread, &QThread::started, mWorker.get(), [ = ] () { mWorker->fetchRows( 1000 ); }, Qt::ConnectionType::QueuedConnection );
// Forward signals to the model
connect( mWorker.get(), &QgsQueryResultFetcher::rowsReady, this, &QgsQueryResultModel::rowsReady );
connect( mWorker.get(), &QgsQueryResultFetcher::fetchingComplete, this, &QgsQueryResultModel::fetchingComplete );
connect( this, &QgsQueryResultModel::fetchMoreRows, mWorker.get(), &QgsQueryResultFetcher::fetchRows );
mWorkerThread.start();
if ( mQueryResult.rowCount() > 0 )
{
@@ -41,6 +42,24 @@ void QgsQueryResultModel::rowsReady( const QList<QList<QVariant>> &rows )
endInsertRows();
}


bool QgsQueryResultModel::canFetchMore( const QModelIndex &parent ) const
{
if ( parent.isValid() )
return false;
return mQueryResult.rowCount() < 0 || mRows.length() < mQueryResult.rowCount();
}


void QgsQueryResultModel::fetchMore( const QModelIndex &parent )
{
if ( ! parent.isValid() )
{
emit fetchingStarted();
emit fetchMoreRows( 1000 );
}
}

void QgsQueryResultModel::cancel()
{
if ( mWorker )
@@ -121,11 +140,12 @@ QVariant QgsQueryResultModel::headerData( int section, Qt::Orientation orientati

const int QgsQueryResultFetcher::ROWS_TO_FETCH = 200;

void QgsQueryResultFetcher::fetchRows()
void QgsQueryResultFetcher::fetchRows( qlonglong maxRows )
{
qlonglong rowCount { 0 };
QList<QList<QVariant>> newRows;
while ( mStopFetching == 0 && mQueryResult->hasNextRow() )
newRows.reserve( ROWS_TO_FETCH );
while ( mStopFetching == 0 && mQueryResult->hasNextRow() && ( maxRows < 0 || rowCount < maxRows ) )
{
newRows.append( mQueryResult->nextRow() );
++rowCount;
@@ -43,8 +43,8 @@ class QgsQueryResultFetcher: public QObject
: mQueryResult( queryResult )
{}

//! Start fetching
void fetchRows();
//! Start fetching at most \a maxRows, default value of -1 fetches all rows.
void fetchRows( qlonglong maxRows = -1 );

//! Stop fetching
void stopFetching();
@@ -97,6 +97,9 @@ class CORE_EXPORT QgsQueryResultModel : public QAbstractTableModel
QVariant data( const QModelIndex &index, int role ) const override;
QVariant headerData( int section, Qt::Orientation orientation, int role ) const override;

void fetchMore( const QModelIndex &parent );
bool canFetchMore( const QModelIndex &parent ) const;

//! Returns the column names
QStringList columns() const;

@@ -124,6 +127,10 @@ class CORE_EXPORT QgsQueryResultModel : public QAbstractTableModel
//! Emitted when all rows have been fetched or when the fetching has been stopped
void fetchingComplete();

void fetchMoreRows( qlonglong maxRows );

void fetchingStarted();

private:

QgsAbstractDatabaseProviderConnection::QueryResult mQueryResult;
@@ -241,13 +241,27 @@ void QgsQueryResultWidget::startFetching()
}
else
{
if ( mQueryResultWatcher.result().rowCount() > 0 )
{
mStatusLabel->setText( QStringLiteral( "Query executed successfully (%1 rows)" ).arg( QLocale().toString( mQueryResultWatcher.result().rowCount() ) ) );
}
else
{
mStatusLabel->setText( QStringLiteral( "Query executed successfully" ) );
}
mModel = std::make_unique<QgsQueryResultModel>( mQueryResultWatcher.result() );
connect( mFeedback.get(), &QgsFeedback::canceled, mModel.get(), [ = ]
{
mModel->cancel();
mWasCanceled = true;
} );


connect( mModel.get(), &QgsQueryResultModel::fetchingStarted, this, [ = ]
{
mProgressBar->show();
} );

connect( mModel.get(), &QgsQueryResultModel::rowsInserted, this, [ = ]( const QModelIndex &, int, int )
{
if ( ! mFirstRowFetched )
@@ -263,8 +277,9 @@ void QgsQueryResultWidget::startFetching()
mProgressBar->setRange( 0, mActualRowCount );
}
}
mStatusLabel->setText( tr( "Fetched rows: %1 %2" )
.arg( mModel->rowCount( mModel->index( -1, -1 ) ) )
mStatusLabel->setText( tr( "Fetched rows: %1/%2 %3" )
.arg( QLocale().toString( mModel->rowCount( mModel->index( -1, -1 ) ) ) )
.arg( mActualRowCount != -1 ? QLocale().toString( mActualRowCount ) : tr( "unknown" ) )
.arg( mWasCanceled ? tr( "(stopped)" ) : QString() ) );
if ( mActualRowCount != -1 )
{
@@ -278,25 +293,20 @@ void QgsQueryResultWidget::startFetching()
connect( mModel.get(), &QgsQueryResultModel::fetchingComplete, mStopButton, [ = ]
{
mStopButton->setEnabled( false );
if ( ! mWasCanceled )
{
mStatusLabel->setText( "Query executed successfully." );
mProgressBar->hide();
}
} );
}
}
else
{
mStatusLabel->setText( tr( "SQL command aborted." ) );
mStatusLabel->setText( tr( "SQL command aborted" ) );
mProgressBar->hide();
}
}

void QgsQueryResultWidget::showError( const QString &title, const QString &message, bool isSqlError )
{
mStatusLabel->show();
mStatusLabel->setText( tr( "There was an error executing the query." ) );
mStatusLabel->setText( tr( "An error occurred while executing the query" ) );
mProgressBar->hide();
mQueryResultsTableView->hide();
if ( isSqlError )
@@ -212,6 +212,10 @@ class QgsPostgresConn : public QObject
*/
static QgsPostgresConn *connectDb( const QString &connInfo, bool readOnly, bool shared = true, bool transaction = false );


QgsPostgresConn( const QString &conninfo, bool readOnly, bool shared, bool transaction );
~QgsPostgresConn() override;

void ref();
void unref();

@@ -403,8 +407,6 @@ class QgsPostgresConn : public QObject
void unlock() { mLock.unlock(); }

private:
QgsPostgresConn( const QString &conninfo, bool readOnly, bool shared, bool transaction );
~QgsPostgresConn() override;

int mRef;
int mOpenCursors;

0 comments on commit eea1eb4

Please sign in to comment.