Skip to content
Permalink
Browse files
Introduce VectorLayerType flags
  • Loading branch information
elpaso authored and nyalldawson committed Nov 4, 2021
1 parent d378916 commit c5f956dbef392a6f2f427693210d44818ca43715
@@ -72,6 +72,14 @@
# --
Qgis.DataType.baseClass = Qgis
# monkey patching scoped based enum
Qgis.SqlQuery = Qgis.VectorLayerTypeFlag.SqlQuery
Qgis.SqlQuery.is_monkey_patched = True
Qgis.SqlQuery.__doc__ = "SQL query layer"
Qgis.VectorLayerTypeFlag.__doc__ = 'Vector layer type flags.\n\n.. versionadded:: 3.24\n\n' + '* ``SqlQuery``: ' + Qgis.VectorLayerTypeFlag.SqlQuery.__doc__
# --
Qgis.VectorLayerTypeFlag.baseClass = Qgis
QgsVectorLayer.VectorLayerTypeFlags = Qgis.VectorLayerTypeFlags
# monkey patching scoped based enum
Qgis.Never = Qgis.PythonMacroMode.Never
Qgis.Never.is_monkey_patched = True
Qgis.Never.__doc__ = "Macros are never run"
@@ -103,6 +103,14 @@ The development version
ARGB32_Premultiplied
};

enum class VectorLayerTypeFlag
{
SqlQuery
};
typedef QFlags<Qgis::VectorLayerTypeFlag> VectorLayerTypeFlags;



enum class PythonMacroMode
{
Never,
@@ -128,18 +128,28 @@ Returns ``True`` if the layer does not contain any feature.
.. versionadded:: 3.4
%End

virtual bool isQuery() const;
virtual bool isSqlQuery() const;
%Docstring
Returns ``True`` if the layer is a query (SQL) layer.

.. note::

the default implementation return ``False``, individual providers
must override if they support query layers.
this is simply a shortcut to check if the SqlQuery flag
is set.

.. seealso:: :py:func:`vectorLayerTypeFlags`

.. versionadded:: 3.24
%End

virtual Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const;
%Docstring
Returns the vector layer type flags.

.. seealso:: :py:func:`isSqlQuery`

.. versionadded:: 3.24
%End

virtual QgsFeatureSource::FeatureAvailability hasFeatures() const;

@@ -452,13 +452,30 @@ Capabilities for this layer, comma separated and translated.
%End


virtual bool isQuery() const;
bool isSqlQuery() const;
%Docstring
Returns ``True`` if the layer is a query (SQL) layer.

.. note::

this is simply a shortcut to check if the SqlQuery flag
is set.

.. seealso:: :py:func:`vectorLayerTypeFlags`

.. versionadded:: 3.24
%End

Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const;
%Docstring
Returns ``True`` if the layer is a query (SQL) layer.

.. seealso:: :py:func:`isSqlQuery`

.. versionadded:: 3.24
%End


QString dataComment() const;
%Docstring
Returns a description for this layer as defined in the data provider.
@@ -222,7 +222,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
}

// No raster support in createSqlVectorLayer (yet)
if ( vlayer && vlayer->isQuery() )
if ( vlayer && vlayer->isSqlQuery() )
{
const std::unique_ptr< QgsAbstractDatabaseProviderConnection> conn { QgsMapLayerUtils::databaseConnection( layer ) };
if ( conn )
@@ -3174,9 +3174,14 @@ QgsFeatureSource::SpatialIndexPresence QgsOgrProvider::hasSpatialIndex() const
return QgsFeatureSource::SpatialIndexUnknown;
}

bool QgsOgrProvider::isQuery() const
Qgis::VectorLayerTypeFlags QgsOgrProvider::vectorLayerTypeFlags() const
{
return mSubsetString.trimmed().startsWith( QStringLiteral( "SELECT" ), Qt::CaseSensitivity::CaseInsensitive );
Qgis::VectorLayerTypeFlags flags;
if ( mValid && mSubsetString.trimmed().startsWith( QStringLiteral( "SELECT" ), Qt::CaseSensitivity::CaseInsensitive ) )
{
flags.setFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}
return flags;
}

QVariant QgsOgrProvider::minimumValue( int index ) const
@@ -124,7 +124,7 @@ class QgsOgrProvider final: public QgsVectorDataProvider
QStringList uniqueStringsMatching( int index, const QString &substring, int limit = -1,
QgsFeedback *feedback = nullptr ) const override;
QgsFeatureSource::SpatialIndexPresence hasSpatialIndex() const override;
bool isQuery() const override;
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const override;

QString name() const override;
static QString providerKey();
@@ -135,6 +135,20 @@ class CORE_EXPORT Qgis
};
Q_ENUM( DataType )

/**
* Vector layer type flags.
*
* \since QGIS 3.24
*/
enum class VectorLayerTypeFlag SIP_MONKEYPATCH_SCOPEENUM_UNNEST( Qgis, VectorLayerTypeFlag ) : int
{
SqlQuery = 1 << 0 //!< SQL query layer
};
Q_ENUM( VectorLayerTypeFlag )
//! Vector layer type flags
Q_DECLARE_FLAGS( VectorLayerTypeFlags, VectorLayerTypeFlag ) SIP_MONKEYPATCH_FLAGS_UNNEST( QgsVectorLayer, VectorLayerTypeFlags )


/**
* Authorisation to run Python Macros
* \since QGIS 3.10
@@ -61,9 +61,14 @@ bool QgsVectorDataProvider::empty() const
return true;
}

bool QgsVectorDataProvider::isQuery() const
bool QgsVectorDataProvider::isSqlQuery() const
{
return false;
return vectorLayerTypeFlags().testFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}

Qgis::VectorLayerTypeFlags QgsVectorDataProvider::vectorLayerTypeFlags() const
{
return Qgis::VectorLayerTypeFlags();
}

QgsFeatureSource::FeatureAvailability QgsVectorDataProvider::hasFeatures() const
@@ -167,13 +167,21 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
/**
* Returns TRUE if the layer is a query (SQL) layer.
*
* \note the default implementation return FALSE, individual providers
* must override if they support query layers.
* \note this is simply a shortcut to check if the SqlQuery flag
* is set.
*
*\see vectorLayerTypeFlags()
* \since QGIS 3.24
*/
virtual bool isQuery() const;
virtual bool isSqlQuery() const;

/**
* Returns the vector layer type flags.
*
* \see isSqlQuery()
* \since QGIS 3.24
*/
virtual Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const;

/**
* Will always return FeatureAvailability::FeaturesAvailable or
@@ -372,9 +372,14 @@ QString QgsVectorLayer::capabilitiesString() const
return QString();
}

bool QgsVectorLayer::isQuery() const
bool QgsVectorLayer::isSqlQuery() const
{
return mDataProvider && mDataProvider->isQuery();
return mDataProvider && mDataProvider->isSqlQuery();
}

Qgis::VectorLayerTypeFlags QgsVectorLayer::vectorLayerTypeFlags() const
{
return mDataProvider ? mDataProvider->vectorLayerTypeFlags() : Qgis::VectorLayerTypeFlags();
}

QString QgsVectorLayer::dataComment() const
@@ -574,9 +574,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
/**
* Returns TRUE if the layer is a query (SQL) layer.
*
* \note this is simply a shortcut to check if the SqlQuery flag
* is set.
*
* \see vectorLayerTypeFlags()
* \since QGIS 3.24
*/
bool isSqlQuery() const;

/**
* Returns TRUE if the layer is a query (SQL) layer.
*
* \see isSqlQuery()
* \since QGIS 3.24
*/
virtual bool isQuery() const;
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const;


/**
* Returns a description for this layer as defined in the data provider.
@@ -1555,9 +1555,15 @@ void QgsHanaProvider::updateFeatureIdMap( QgsFeatureId fid, const QgsAttributeMa
mPrimaryKeyCntx->insertFid( fid, values );
}

bool QgsHanaProvider::isQuery() const

Qgis::VectorLayerTypeFlags QgsHanaProvider::vectorLayerTypeFlags() const
{
return mIsQuery;
Qgis::VectorLayerTypeFlags flags;
if ( mValid && mIsQuery )
{
flags.setFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}
return flags;
}

QgsCoordinateReferenceSystem QgsHanaProvider::crs() const
@@ -102,7 +102,7 @@ class QgsHanaProvider final : public QgsVectorDataProvider
const QMap<QString, QVariant> *options = nullptr
);

bool isQuery() const override;
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const override;

private:
QgsHanaConnectionRef createConnection() const;
@@ -219,6 +219,16 @@ QString QgsOracleProvider::getWorkspace() const
return mUri.param( "dbworkspace" );
}

Qgis::VectorLayerTypeFlags QgsOracleProvider::vectorLayerTypeFlags() const
{
Qgis::VectorLayerTypeFlags flags;
if ( mValid && mIsQuery )
{
flags.setFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}
return flags;
}

void QgsOracleProvider::setWorkspace( const QString &workspace )
{
QgsDataSourceUri prevUri( mUri );
@@ -2814,10 +2824,6 @@ bool QgsOracleProvider::convertField( QgsField &field )
return true;
}

bool QgsOracleProvider::isQuery() const
{
return mIsQuery;
}

Qgis::VectorExportResult QgsOracleProvider::createEmptyLayer(
const QString &uri,
@@ -189,7 +189,7 @@ class QgsOracleProvider final: public QgsVectorDataProvider
*/
QString getWorkspace() const;

bool isQuery() const override;
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const override;

private:
QString whereClause( QgsFeatureId featureId, QVariantList &args ) const;
@@ -309,18 +309,22 @@ void QgsPostgresProvider::setListening( bool isListening )
}
}

void QgsPostgresProvider::reloadProviderData()
Qgis::VectorLayerTypeFlags QgsPostgresProvider::vectorLayerTypeFlags() const
{
mShared->setFeaturesCounted( -1 );
mLayerExtent.setMinimal();
Qgis::VectorLayerTypeFlags flags;
if ( mValid && mIsQuery )
{
flags.setFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}
return flags;
}

bool QgsPostgresProvider::isQuery() const
void QgsPostgresProvider::reloadProviderData()
{
return mIsQuery;
mShared->setFeaturesCounted( -1 );
mLayerExtent.setMinimal();
}


QgsPostgresConn *QgsPostgresProvider::connectionRW()
{
if ( mTransaction )
@@ -243,7 +243,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
*/
void setListening( bool isListening ) override;

bool isQuery() const override;
Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const override;

private:

@@ -5962,9 +5962,14 @@ void QgsSpatiaLiteProvider::setTransaction( QgsTransaction *transaction )
mTransaction = static_cast<QgsSpatiaLiteTransaction *>( transaction );
}

bool QgsSpatiaLiteProvider::isQuery() const
Qgis::VectorLayerTypeFlags QgsSpatiaLiteProvider::vectorLayerTypeFlags() const
{
return mIsQuery;
Qgis::VectorLayerTypeFlags flags;
if ( mValid && mIsQuery )
{
flags.setFlag( Qgis::VectorLayerTypeFlag::SqlQuery );
}
return flags;
}

QgsTransaction *QgsSpatiaLiteProvider::transaction( ) const
@@ -414,7 +414,9 @@ class QgsSpatiaLiteProvider final: public QgsVectorDataProvider
// QgsVectorDataProvider interface
public:
virtual QString defaultValueClause( int fieldIndex ) const override;
bool isQuery() const override;

Qgis::VectorLayerTypeFlags vectorLayerTypeFlags() const override;

};

class QgsSpatiaLiteProviderMetadata final: public QgsProviderMetadata
Loading

0 comments on commit c5f956d

Please sign in to comment.