Skip to content

Commit

Permalink
flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jan 13, 2021
1 parent 3b7cc24 commit 322a925
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 60 deletions.
Expand Up @@ -49,7 +49,8 @@ is not supported or cannot be performed without errors.
{
SIP_PYOBJECT __repr__();
%MethodCode
QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.QueryResult: %1 rows>" ).arg( sipCpp->rowCount() );
const qlonglong rowCount = sipCpp->rowCount();
const QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.QueryResult: %1 rows>" ).arg( rowCount > -1 ? QString::number( rowCount ) : QStringLiteral( "unknown" ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
%End

Expand All @@ -58,10 +59,10 @@ is not supported or cannot be performed without errors.
Returns the column names
%End

QList<QList<QVariant> > rows(QgsFeedback* feedback = 0) const;
QList<QList<QVariant> > rows( QgsFeedback *feedback = 0 ) const;
%Docstring
Returns the results rows by calling the iterator internally,
an optional ``feedback`` can be used to interrupt the fetching loop.
Returns the result rows by calling the iterator internally and fetching
all the rows, an optional ``feedback`` can be used to interrupt the fetching loop.

.. note::

Expand All @@ -71,24 +72,42 @@ an optional ``feedback`` can be used to interrupt the fetching loop.

qlonglong rowCount() const;
%Docstring
Returns the estimated row count, the value may not be exact or it may be -1 if not known.
Returns the maximum value between the estimated row count (this value may not be exact or
it may be -1 if not known) and the actual number of fetched rows

.. note::

When all the results have been retrieved returns the actual number of rows

.. seealso:: :py:func:`fetchedRowCount`
%End

bool hasNextRow() const;
%Docstring
Returns ``True`` if there are more rows to fetch

.. seealso:: :py:func:`nextRow`

.. seealso:: :py:func:`rewind`
%End

QList<QVariant> nextRow() const;
%Docstring
Returns the next result row or an empty row if there are no rows left

.. seealso:: :py:func:`hasNextRow`

.. seealso:: :py:func:`rewind`
%End

void rewind();
%Docstring
Resets the iterator counter used by :py:func:`~QgsAbstractDatabaseProviderConnection.hasNextRow` and :py:func:`~QgsAbstractDatabaseProviderConnection.nextRow`
Resets the internal iterator counter, the next call to :py:func:`~QgsAbstractDatabaseProviderConnection.nextRow`
will return the first row (if any)

.. seealso:: :py:func:`hasNextRow`

.. seealso:: :py:func:`nextRow`
%End

qlonglong fetchedRowCount( ) const;
Expand All @@ -100,7 +119,7 @@ Returns the number of fetched rows

QList<QVariant> at( qlonglong index ) const;
%Docstring
Returns the row data at 0-based index ``index``, if index is not valid, an empty list is returned.
Returns the row at 0-based index ``index``, if index is not valid, an empty row is returned

.. note::

Expand Down
9 changes: 5 additions & 4 deletions python/core/auto_generated/qgsqueryresultmodel.sip.in
Expand Up @@ -13,8 +13,6 @@





class QgsQueryResultModel : QAbstractListModel
{
%Docstring
Expand All @@ -28,7 +26,7 @@ The QgsQueryResultModel class is a model for QgsAbstractDatabaseProviderConnecti
%End
public:

QgsQueryResultModel(const QgsAbstractDatabaseProviderConnection::QueryResult& queryResult, QObject *parent = 0 );
QgsQueryResultModel( const QgsAbstractDatabaseProviderConnection::QueryResult &queryResult, QObject *parent = 0 );
%Docstring
Constructs a QgsQueryResultModel from a ``queryResult`` with optional ``parent``
%End
Expand All @@ -46,7 +44,10 @@ Constructs a QgsQueryResultModel from a ``queryResult`` with optional ``parent``

public slots:

void newRowsReady(int newRowsCount );
void newRowsReady( int newRowsCount );
%Docstring
Triggered when ``newRowsCount`` have been fetched and can be added to the model
%End

};

Expand Down
24 changes: 14 additions & 10 deletions src/core/qgsabstractdatabaseproviderconnection.cpp
Expand Up @@ -444,14 +444,13 @@ QStringList QgsAbstractDatabaseProviderConnection::QueryResult::columns() const
return mColumns;
}

QList<QList<QVariant> > QgsAbstractDatabaseProviderConnection::QueryResult::rows( QgsFeedback* feedback) const
QList<QList<QVariant> > QgsAbstractDatabaseProviderConnection::QueryResult::rows( QgsFeedback *feedback ) const
{
if ( ! mResultIterator )
{
return QList<QList<QVariant> >();
}

// mRowCount might be -1 (unknown)
while ( mResultIterator &&
mResultIterator->hasNextRow() &&
( ! feedback || ! feedback->isCanceled() ) )
Expand Down Expand Up @@ -496,11 +495,12 @@ QList<QVariant> QgsAbstractDatabaseProviderConnection::QueryResult::at( qlonglon
}

// Fetch rows until the index
while ( index >= mResultIterator->fetchedRowCount() &&
( mRowCount < 0 || mResultIterator->fetchedRowCount() < mRowCount ) &&
mResultIterator->hasNextRow() )
while ( index >= mResultIterator->fetchedRowCount() )
{
mResultIterator->nextRow();
if ( mResultIterator->nextRow().isEmpty() )
{
break;
}
}

if ( index >= mResultIterator->fetchedRowCount() )
Expand All @@ -514,6 +514,10 @@ QList<QVariant> QgsAbstractDatabaseProviderConnection::QueryResult::at( qlonglon

qlonglong QgsAbstractDatabaseProviderConnection::QueryResult::rowCount() const
{
if ( mResultIterator )
{
return std::max( mResultIterator->fetchedRowCount(), mRowCount );
}
return mRowCount;
}

Expand Down Expand Up @@ -543,7 +547,7 @@ QgsAbstractDatabaseProviderConnection::QueryResult::QueryResult( std::shared_ptr

QVariantList QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::nextRow()
{
QMutexLocker lock(&mMutex);
QMutexLocker lock( &mMutex );
const QVariantList row { nextRowPrivate() };
if ( ! row.isEmpty() )
{
Expand All @@ -554,19 +558,19 @@ QVariantList QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIter

bool QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::hasNextRow() const
{
QMutexLocker lock(&mMutex);
QMutexLocker lock( &mMutex );
return hasNextRowPrivate();
}

qlonglong QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::fetchedRowCount()
{
QMutexLocker lock(&mMutex);
QMutexLocker lock( &mMutex );
return mRows.count();
}

QList<QList<QVariant> > QgsAbstractDatabaseProviderConnection::QueryResult::QueryResultIterator::rows() const
{
QMutexLocker lock(&mMutex);
QMutexLocker lock( &mMutex );
return mRows;
}

Expand Down
52 changes: 34 additions & 18 deletions src/core/qgsabstractdatabaseproviderconnection.h
Expand Up @@ -87,7 +87,8 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
#ifdef SIP_RUN
SIP_PYOBJECT __repr__();
% MethodCode
QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.QueryResult: %1 rows>" ).arg( sipCpp->rowCount() );
const qlonglong rowCount = sipCpp->rowCount();
const QString str = QStringLiteral( "<QgsAbstractDatabaseProviderConnection.QueryResult: %1 rows>" ).arg( rowCount > -1 ? QString::number( rowCount ) : QStringLiteral( "unknown" ) );
sipRes = PyUnicode_FromString( str.toUtf8().constData() );
% End
#endif
Expand All @@ -98,43 +99,58 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
QStringList columns() const;

/**
* Returns the results rows by calling the iterator internally,
* an optional \a feedback can be used to interrupt the fetching loop.
* Returns the result rows by calling the iterator internally and fetching
* all the rows, an optional \a feedback can be used to interrupt the fetching loop.
*
* \note results are cached internally and subsequent calls to this method
* will return the cached results.
*/
QList<QList<QVariant> > rows(QgsFeedback* feedback = nullptr) const;
QList<QList<QVariant> > rows( QgsFeedback *feedback = nullptr ) const;

/**
* Returns the estimated row count, the value may not be exact or it may be -1 if not known.
* Returns the maximum value between the estimated row count (this value may not be exact or
* it may be -1 if not known) and the actual number of fetched rows
*
* \note When all the results have been retrieved returns the actual number of rows
* \see fetchedRowCount()
*/
qlonglong rowCount() const;

/**
* Returns TRUE if there are more rows to fetch
*
* \see nextRow()
* \see rewind()
*/
bool hasNextRow() const;

/**
* Returns the next result row or an empty row if there are no rows left
*
* \see hasNextRow()
* \see rewind()
*/
QList<QVariant> nextRow() const;

/**
* Resets the iterator counter used by hasNextRow() and nextRow()
* Resets the internal iterator counter, the next call to nextRow()
* will return the first row (if any)
*
* \see hasNextRow()
* \see nextRow()
*/
void rewind();

/**
* Returns the number of fetched rows
*
* \see rowCount()
*/
qlonglong fetchedRowCount( ) const;

/**
* Returns the row data at 0-based index \a index, if index is not valid, an empty list is returned.
* Returns the row at 0-based index \a index, if index is not valid, an empty row is returned
*
* \note this method calls the iterator internally until \a index row is retrieved
*/
QList<QVariant> at( qlonglong index ) const;
Expand Down Expand Up @@ -174,18 +190,18 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv
*/
struct QueryResultIterator SIP_SKIP
{
QVariantList nextRow();
bool hasNextRow() const;
qlonglong fetchedRowCount();
virtual ~QueryResultIterator() = default;
QList<QList<QVariant> > rows() const;
QVariantList nextRow();
bool hasNextRow() const;
qlonglong fetchedRowCount();
virtual ~QueryResultIterator() = default;
QList<QList<QVariant> > rows() const;

private:
private:

virtual QVariantList nextRowPrivate() = 0;
virtual bool hasNextRowPrivate() const = 0;
mutable QList<QList<QVariant>> mRows;
mutable QMutex mMutex;
virtual QVariantList nextRowPrivate() = 0;
virtual bool hasNextRowPrivate() const = 0;
mutable QList<QList<QVariant>> mRows;
mutable QMutex mMutex;

};

Expand Down Expand Up @@ -215,7 +231,7 @@ class CORE_EXPORT QgsAbstractDatabaseProviderConnection : public QgsAbstractProv

///@endcond private

private:
private:

mutable std::shared_ptr<QueryResultIterator> mResultIterator;
QStringList mColumns;
Expand Down
23 changes: 14 additions & 9 deletions src/core/qgsqueryresultmodel.cpp
Expand Up @@ -15,9 +15,7 @@
***************************************************************************/
#include "qgsqueryresultmodel.h"

const int ResultWorker::ROWS_TO_FETCH = 200;

QgsQueryResultModel::QgsQueryResultModel(const QgsAbstractDatabaseProviderConnection::QueryResult& queryResult, QObject *parent )
QgsQueryResultModel::QgsQueryResultModel( const QgsAbstractDatabaseProviderConnection::QueryResult &queryResult, QObject *parent )
: QAbstractListModel( parent )
, mQueryResult( queryResult )
, mColumns( queryResult.columns() )
Expand All @@ -26,11 +24,11 @@ QgsQueryResultModel::QgsQueryResultModel(const QgsAbstractDatabaseProviderConnec
if ( mQueryResult.hasNextRow() )
{
mWorker = new ResultWorker( &mQueryResult );
mWorker->moveToThread(&mWorkerThread);
connect(&mWorkerThread, &QThread::finished, mWorker, &ResultWorker::stopFetching );
connect(&mWorkerThread, &QThread::finished, mWorker, &QObject::deleteLater);
connect(&mWorkerThread, &QThread::started, mWorker, &ResultWorker::fetchRows);
connect(mWorker, &ResultWorker::rowsReady, this, &QgsQueryResultModel::newRowsReady );
mWorker->moveToThread( &mWorkerThread );
connect( &mWorkerThread, &QThread::finished, mWorker, &ResultWorker::stopFetching );
connect( &mWorkerThread, &QThread::finished, mWorker, &QObject::deleteLater );
connect( &mWorkerThread, &QThread::started, mWorker, &ResultWorker::fetchRows );
connect( mWorker, &ResultWorker::rowsReady, this, &QgsQueryResultModel::newRowsReady );
mWorkerThread.start();
}
}
Expand Down Expand Up @@ -83,6 +81,10 @@ QVariant QgsQueryResultModel::data( const QModelIndex &index, int role ) const
return QVariant();
}

///@cond private

const int ResultWorker::ROWS_TO_FETCH = 200;

void ResultWorker::fetchRows()
{
qlonglong rowCount { 0 };
Expand All @@ -95,7 +97,7 @@ void ResultWorker::fetchRows()
++rowCount;
if ( rowCount % ROWS_TO_FETCH == 0 && mStopFetching == 0 )
{
emit rowsReady( ROWS_TO_FETCH );
emit rowsReady( ROWS_TO_FETCH );
}
}

Expand All @@ -109,3 +111,6 @@ void ResultWorker::stopFetching()
{
mStopFetching = 1;
}


///@endcond private

0 comments on commit 322a925

Please sign in to comment.