Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move postgres relkind enum to Qgis
Avoids having to include qgspostgresprovider header in a bunch
of non-provider related files
  • Loading branch information
nyalldawson committed Apr 6, 2023
1 parent 3ddf444 commit 0bc7bf5
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 65 deletions.
15 changes: 15 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -3427,3 +3427,18 @@
Qgis.LayoutUnitType.__doc__ = 'Types of layout units\n\n.. note::\n\n Prior to QGIS 3.30 this was available as :py:class:`QgsUnitTypes`.LayoutUnitType.\n\n.. versionadded:: 3.30\n\n' + '* ``LayoutPaperUnits``: ' + Qgis.LayoutUnitType.PaperUnits.__doc__ + '\n' + '* ``LayoutScreenUnits``: ' + Qgis.LayoutUnitType.ScreenUnits.__doc__
# --
Qgis.LayoutUnitType.baseClass = Qgis
# monkey patching scoped based enum
Qgis.PostgresRelKind.NotSet.__doc__ = "Not set"
Qgis.PostgresRelKind.Unknown.__doc__ = "Unknown"
Qgis.PostgresRelKind.OrdinaryTable.__doc__ = "Ordinary table"
Qgis.PostgresRelKind.Index.__doc__ = "Index"
Qgis.PostgresRelKind.Sequence.__doc__ = "Sequence"
Qgis.PostgresRelKind.View.__doc__ = "View"
Qgis.PostgresRelKind.MaterializedView.__doc__ = "Materialized view"
Qgis.PostgresRelKind.CompositeType.__doc__ = "Composition type"
Qgis.PostgresRelKind.ToastTable.__doc__ = "TOAST table"
Qgis.PostgresRelKind.ForeignTable.__doc__ = "Foreign table"
Qgis.PostgresRelKind.PartitionedTable.__doc__ = "PartionedTable"
Qgis.PostgresRelKind.__doc__ = 'Postgres database relkind options.\n\n.. versionadded:: 3.32\n\n' + '* ``NotSet``: ' + Qgis.PostgresRelKind.NotSet.__doc__ + '\n' + '* ``Unknown``: ' + Qgis.PostgresRelKind.Unknown.__doc__ + '\n' + '* ``OrdinaryTable``: ' + Qgis.PostgresRelKind.OrdinaryTable.__doc__ + '\n' + '* ``Index``: ' + Qgis.PostgresRelKind.Index.__doc__ + '\n' + '* ``Sequence``: ' + Qgis.PostgresRelKind.Sequence.__doc__ + '\n' + '* ``View``: ' + Qgis.PostgresRelKind.View.__doc__ + '\n' + '* ``MaterializedView``: ' + Qgis.PostgresRelKind.MaterializedView.__doc__ + '\n' + '* ``CompositeType``: ' + Qgis.PostgresRelKind.CompositeType.__doc__ + '\n' + '* ``ToastTable``: ' + Qgis.PostgresRelKind.ToastTable.__doc__ + '\n' + '* ``ForeignTable``: ' + Qgis.PostgresRelKind.ForeignTable.__doc__ + '\n' + '* ``PartitionedTable``: ' + Qgis.PostgresRelKind.PartitionedTable.__doc__
# --
Qgis.PostgresRelKind.baseClass = Qgis
15 changes: 15 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -1928,6 +1928,21 @@ The development version
ScreenUnits
};

enum class PostgresRelKind
{
NotSet,
Unknown,
OrdinaryTable,
Index,
Sequence,
View,
MaterializedView,
CompositeType,
ToastTable,
ForeignTable,
PartitionedTable,
};

static const double DEFAULT_SEARCH_RADIUS_MM;

static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
Expand Down
21 changes: 21 additions & 0 deletions src/core/qgis.h
Expand Up @@ -3339,6 +3339,27 @@ class CORE_EXPORT Qgis
};
Q_ENUM( LayoutUnitType )

/**
* Postgres database relkind options.
*
* \since QGIS 3.32
*/
enum class PostgresRelKind
{
NotSet, //!< Not set
Unknown, //!< Unknown
OrdinaryTable, //!< Ordinary table
Index, //!< Index
Sequence, //!< Sequence
View, //!< View
MaterializedView, //!< Materialized view
CompositeType, //!< Composition type
ToastTable, //!< TOAST table
ForeignTable, //!< Foreign table
PartitionedTable, //!< PartionedTable
};
Q_ENUM( PostgresRelKind )

/**
* Identify search radius in mm
* \since QGIS 2.3
Expand Down
48 changes: 23 additions & 25 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1281,8 +1281,8 @@ bool QgsPostgresProvider::loadFields()
else
{
// be tolerant in case of views: this might be a field used as a key
const QgsPostgresProvider::Relkind type = relkind();
if ( ( type == Relkind::View || type == Relkind::MaterializedView ) && parseUriKey( mUri.keyColumn( ) ).contains( fieldName ) )
const Qgis::PostgresRelKind type = relkind();
if ( ( type == Qgis::PostgresRelKind::View || type == Qgis::PostgresRelKind::MaterializedView ) && parseUriKey( mUri.keyColumn( ) ).contains( fieldName ) )
{
// Assume it is convertible to text
fieldType = QVariant::String;
Expand Down Expand Up @@ -1683,9 +1683,9 @@ bool QgsPostgresProvider::determinePrimaryKey()
// If the relation is a view try to find a suitable column to use as
// the primary key.

const QgsPostgresProvider::Relkind type = relkind();
const Qgis::PostgresRelKind type = relkind();

if ( type == Relkind::OrdinaryTable || type == Relkind::PartitionedTable )
if ( type == Qgis::PostgresRelKind::OrdinaryTable || type == Qgis::PostgresRelKind::PartitionedTable )
{
QgsDebugMsgLevel( QStringLiteral( "Relation is a table. Checking to see if it has an oid column." ), 2 );

Expand Down Expand Up @@ -1742,15 +1742,13 @@ bool QgsPostgresProvider::determinePrimaryKey()
QgsMessageLog::logMessage( tr( "The table has no column suitable for use as a key. QGIS requires a primary key, a PostgreSQL oid column or a ctid for tables." ), tr( "PostGIS" ) );
}
}
else if ( type == Relkind::View || type == Relkind::MaterializedView || type == Relkind::ForeignTable )
else if ( type == Qgis::PostgresRelKind::View || type == Qgis::PostgresRelKind::MaterializedView || type == Qgis::PostgresRelKind::ForeignTable )
{
determinePrimaryKeyFromUriKeyColumn();
}
else
{
const QMetaEnum metaEnum( QMetaEnum::fromType<Relkind>() );
QString typeName = metaEnum.valueToKey( type );
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( typeName ), tr( "PostGIS" ) );
QgsMessageLog::logMessage( tr( "Unexpected relation type '%1'." ).arg( qgsEnumValueToKey( type ) ), tr( "PostGIS" ) );
}
}
else
Expand Down Expand Up @@ -3787,7 +3785,7 @@ long long QgsPostgresProvider::featureCount() const
long long num = -1;
if ( !mIsQuery && mUseEstimatedMetadata )
{
if ( ( relkind() == Relkind::View || !mSqlWhereClause.isEmpty() ) && connectionRO()->pgVersion() >= 90000 )
if ( ( relkind() == Qgis::PostgresRelKind::View || !mSqlWhereClause.isEmpty() ) && connectionRO()->pgVersion() >= 90000 )
{
// parse explain output to estimate feature count
// we don't use pg_class reltuples because it returns 0 for view
Expand Down Expand Up @@ -5242,61 +5240,61 @@ void QgsPostgresProvider::setQuery( const QString &query )
{
mQuery = query;

mKind = Relkind::NotSet;
mKind = Qgis::PostgresRelKind::NotSet;
}

QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
Qgis::PostgresRelKind QgsPostgresProvider::relkind() const
{
if ( mKind != Relkind::NotSet )
if ( mKind != Qgis::PostgresRelKind::NotSet )
return mKind;

if ( mIsQuery || !connectionRO() )
{
mKind = Relkind::Unknown;
mKind = Qgis::PostgresRelKind::Unknown;
}
else
{
QString sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
QgsPostgresResult res( connectionRO()->LoggedPQexec( "QgsPostgresProvider", sql ) );
QString type = res.PQgetvalue( 0, 0 );

mKind = Relkind::Unknown;
mKind = Qgis::PostgresRelKind::Unknown;

if ( type == QLatin1String( "r" ) )
{
mKind = Relkind::OrdinaryTable;
mKind = Qgis::PostgresRelKind::OrdinaryTable;
}
else if ( type == QLatin1String( "i" ) )
{
mKind = Relkind::Index;
mKind = Qgis::PostgresRelKind::Index;
}
else if ( type == QLatin1String( "s" ) )
{
mKind = Relkind::Sequence;
mKind = Qgis::PostgresRelKind::Sequence;
}
else if ( type == QLatin1String( "v" ) )
{
mKind = Relkind::View;
mKind = Qgis::PostgresRelKind::View;
}
else if ( type == QLatin1String( "m" ) )
{
mKind = Relkind::MaterializedView;
mKind = Qgis::PostgresRelKind::MaterializedView;
}
else if ( type == QLatin1String( "c" ) )
{
mKind = Relkind::CompositeType;
mKind = Qgis::PostgresRelKind::CompositeType;
}
else if ( type == QLatin1String( "t" ) )
{
mKind = Relkind::ToastTable;
mKind = Qgis::PostgresRelKind::ToastTable;
}
else if ( type == QLatin1String( "f" ) )
{
mKind = Relkind::ForeignTable;
mKind = Qgis::PostgresRelKind::ForeignTable;
}
else if ( type == QLatin1String( "p" ) )
{
mKind = Relkind::PartitionedTable;
mKind = Qgis::PostgresRelKind::PartitionedTable;
}
}

Expand All @@ -5306,9 +5304,9 @@ QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
bool QgsPostgresProvider::hasMetadata() const
{
bool hasMetadata = true;
QgsPostgresProvider::Relkind kind = relkind();
Qgis::PostgresRelKind kind = relkind();

if ( kind == Relkind::View || kind == Relkind::MaterializedView )
if ( kind == Qgis::PostgresRelKind::View || kind == Qgis::PostgresRelKind::MaterializedView )
{
hasMetadata = false;
}
Expand Down
20 changes: 2 additions & 18 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -56,22 +56,6 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
static const QString POSTGRES_KEY;
static const QString POSTGRES_DESCRIPTION;

enum Relkind
{
NotSet,
Unknown,
OrdinaryTable, // r
Index, // i
Sequence, // s
View, // v
MaterializedView, // m
CompositeType, // c
ToastTable, // t
ForeignTable, // f
PartitionedTable // p - PostgreSQL 10
};
Q_ENUM( Relkind )

/**
* Import a vector layer into the database
* \param options options for provider, specified via a map of option name
Expand Down Expand Up @@ -256,7 +240,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
/**
* \returns relation kind
*/
Relkind relkind() const;
Qgis::PostgresRelKind relkind() const;

/**
* Change internal query with \a query
Expand Down Expand Up @@ -397,7 +381,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
/**
* Kind of relation
*/
mutable Relkind mKind = Relkind::NotSet;
mutable Qgis::PostgresRelKind mKind = Qgis::PostgresRelKind::NotSet;

/**
* Data type for the primary key
Expand Down
37 changes: 18 additions & 19 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -20,7 +20,6 @@
#include "qgsmessagelog.h"
#include "qgsrectangle.h"
#include "qgspolygon.h"
#include "qgspostgresprovider.h"
#include "qgsgdalutils.h"
#include "qgsstringutils.h"
#include "qgsapplication.h"
Expand Down Expand Up @@ -1865,8 +1864,8 @@ bool QgsPostgresRasterProvider::loadFields()
else
{
// be tolerant in case of views: this might be a field used as a key
const QgsPostgresProvider::Relkind type = relkind();
if ( ( type == QgsPostgresProvider::Relkind::View || type == QgsPostgresProvider::Relkind::MaterializedView )
const Qgis::PostgresRelKind type = relkind();
if ( ( type == Qgis::PostgresRelKind::View || type == Qgis::PostgresRelKind::MaterializedView )
&& parseUriKey( mUri.keyColumn( ) ).contains( fieldName ) )
{
// Assume it is convertible to text
Expand Down Expand Up @@ -2008,52 +2007,52 @@ QStringList QgsPostgresRasterProvider::parseUriKey( const QString &key )
return cols;
}

QgsPostgresProvider::Relkind QgsPostgresRasterProvider::relkind() const
Qgis::PostgresRelKind QgsPostgresRasterProvider::relkind() const
{
if ( mIsQuery || !connectionRO() )
return QgsPostgresProvider::Relkind::Unknown;
return Qgis::PostgresRelKind::Unknown;

QString sql = QStringLiteral( "SELECT relkind FROM pg_class WHERE oid=regclass(%1)::oid" ).arg( quotedValue( mQuery ) );
QgsPostgresResult res( connectionRO()->PQexec( sql ) );
QString type = res.PQgetvalue( 0, 0 );

QgsPostgresProvider::Relkind kind = QgsPostgresProvider::Relkind::Unknown;
Qgis::PostgresRelKind kind = Qgis::PostgresRelKind::Unknown;

if ( type == 'r' )
{
kind = QgsPostgresProvider::Relkind::OrdinaryTable;
kind = Qgis::PostgresRelKind::OrdinaryTable;
}
else if ( type == 'i' )
{
kind = QgsPostgresProvider::Relkind::Index;
kind = Qgis::PostgresRelKind::Index;
}
else if ( type == 's' )
{
kind = QgsPostgresProvider::Relkind::Sequence;
kind = Qgis::PostgresRelKind::Sequence;
}
else if ( type == 'v' )
{
kind = QgsPostgresProvider::Relkind::View;
kind = Qgis::PostgresRelKind::View;
}
else if ( type == 'm' )
{
kind = QgsPostgresProvider::Relkind::MaterializedView;
kind = Qgis::PostgresRelKind::MaterializedView;
}
else if ( type == 'c' )
{
kind = QgsPostgresProvider::Relkind::CompositeType;
kind = Qgis::PostgresRelKind::CompositeType;
}
else if ( type == 't' )
{
kind = QgsPostgresProvider::Relkind::ToastTable;
kind = Qgis::PostgresRelKind::ToastTable;
}
else if ( type == 'f' )
{
kind = QgsPostgresProvider::Relkind::ForeignTable;
kind = Qgis::PostgresRelKind::ForeignTable;
}
else if ( type == 'p' )
{
kind = QgsPostgresProvider::Relkind::PartitionedTable;
kind = Qgis::PostgresRelKind::PartitionedTable;
}

return kind;
Expand Down Expand Up @@ -2098,9 +2097,9 @@ bool QgsPostgresRasterProvider::determinePrimaryKey()
// If the relation is a view try to find a suitable column to use as
// the primary key.

const QgsPostgresProvider::Relkind type = relkind();
const Qgis::PostgresRelKind type = relkind();

if ( type == QgsPostgresProvider::Relkind::OrdinaryTable || type == QgsPostgresProvider::Relkind::PartitionedTable )
if ( type == Qgis::PostgresRelKind::OrdinaryTable || type == Qgis::PostgresRelKind::PartitionedTable )
{
QgsDebugMsgLevel( QStringLiteral( "Relation is a table. Checking to see if it has an oid column." ), 4 );

Expand Down Expand Up @@ -2158,8 +2157,8 @@ bool QgsPostgresRasterProvider::determinePrimaryKey()
QgsMessageLog::logMessage( tr( "The table has no column suitable for use as a key. QGIS requires a primary key, a PostgreSQL oid column or a ctid for tables." ), tr( "PostGIS" ) );
}
}
else if ( type == QgsPostgresProvider::Relkind::View || type == QgsPostgresProvider::Relkind::MaterializedView
|| type == QgsPostgresProvider::Relkind::ForeignTable )
else if ( type == Qgis::PostgresRelKind::View || type == Qgis::PostgresRelKind::MaterializedView
|| type == Qgis::PostgresRelKind::ForeignTable )
{
determinePrimaryKeyFromUriKeyColumn();
}
Expand Down
4 changes: 1 addition & 3 deletions src/providers/postgres/raster/qgspostgresrasterprovider.h
Expand Up @@ -20,8 +20,6 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsprovidermetadata.h"
#include "qgspostgresconn.h"
#include "qgspostgresprovider.h"
#include "qgsogrutils.h"
#include "qgspostgresrastershareddata.h"

#include <exception>
Expand Down Expand Up @@ -206,7 +204,7 @@ class QgsPostgresRasterProvider : public QgsRasterDataProvider
static QString quotedValue( const QVariant &value ) { return QgsPostgresConn::quotedValue( value ); }
static QString quotedJsonValue( const QVariant &value ) { return QgsPostgresConn::quotedJsonValue( value ); }
static QString quotedByteaValue( const QVariant &value );
QgsPostgresProvider::Relkind relkind() const;
Qgis::PostgresRelKind relkind() const;
bool loadFields();

/**
Expand Down

0 comments on commit 0bc7bf5

Please sign in to comment.