Navigation Menu

Skip to content

Commit

Permalink
Fetch more in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jul 6, 2021
1 parent 2c9662b commit eea1eb4
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 144 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsqueryresultmodel.sip.in
Expand Up @@ -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
Expand Down Expand Up @@ -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();

};

/************************************************************************
Expand Down
26 changes: 23 additions & 3 deletions src/core/qgsqueryresultmodel.cpp
Expand Up @@ -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 )
{
Expand All @@ -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 )
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/core/qgsqueryresultmodel.h
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
28 changes: 19 additions & 9 deletions src/gui/qgsqueryresultwidget.cpp
Expand Up @@ -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 )
Expand All @@ -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 )
{
Expand All @@ -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 )
Expand Down
6 changes: 4 additions & 2 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit eea1eb4

Please sign in to comment.