Skip to content
Permalink
Browse files

Cache relkind to avoid requesting it every time we need it

(cherry picked from commit 20fcf50)
  • Loading branch information
troopa81 authored and nyalldawson committed Dec 21, 2020
1 parent 961a4b4 commit c522a04167f2124843109195d25bebd32dc7b200
Showing with 75 additions and 53 deletions.
  1. +60 −53 src/providers/postgres/qgspostgresprovider.cpp
  2. +15 −0 src/providers/postgres/qgspostgresprovider.h
@@ -143,22 +143,15 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
if ( mSchemaName.isEmpty() && mTableName.startsWith( '(' ) && mTableName.endsWith( ')' ) )
{
mIsQuery = true;
mQuery = mTableName;
setQuery( mTableName );
mTableName.clear();
}
else
{
mIsQuery = false;

if ( !mSchemaName.isEmpty() )
{
mQuery += quotedIdentifier( mSchemaName ) + '.';
}

if ( !mTableName.isEmpty() )
{
mQuery += quotedIdentifier( mTableName );
}
setQuery( ( !mSchemaName.isEmpty() ? quotedIdentifier( mSchemaName ) + '.' : QString() )
+ ( !mTableName.isEmpty() ? quotedIdentifier( mTableName ) : QString() ) );
}

mUseEstimatedMetadata = mUri.useEstimatedMetadata();
@@ -1459,9 +1452,9 @@ bool QgsPostgresProvider::hasSufficientPermsAndCapabilities()
while ( mQuery.contains( regex ) );

// convert the custom query into a subquery
mQuery = QStringLiteral( "%1 AS %2" )
.arg( mQuery,
quotedIdentifier( alias ) );
setQuery( QStringLiteral( "%1 AS %2" )
.arg( mQuery,
quotedIdentifier( alias ) ) );

QString sql = QStringLiteral( "SELECT * FROM %1 LIMIT 1" ).arg( mQuery );

@@ -5031,55 +5024,69 @@ QgsAttrPalIndexNameHash QgsPostgresProvider::palAttributeIndexNames() const
return mAttrPalIndexName;
}

QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
void QgsPostgresProvider::setQuery( const QString &query )
{
if ( mIsQuery || !connectionRO() )
return Relkind::Unknown;
mQuery = query;

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 );
mKind = Relkind::NotSet;
}

QgsPostgresProvider::Relkind kind = Relkind::Unknown;
QgsPostgresProvider::Relkind QgsPostgresProvider::relkind() const
{
if ( mKind != Relkind::NotSet )
return mKind;

if ( type == QLatin1String( "r" ) )
{
kind = Relkind::OrdinaryTable;
}
else if ( type == QLatin1String( "i" ) )
{
kind = Relkind::Index;
}
else if ( type == QLatin1String( "s" ) )
{
kind = Relkind::Sequence;
}
else if ( type == QLatin1String( "v" ) )
{
kind = Relkind::View;
}
else if ( type == QLatin1String( "m" ) )
{
kind = Relkind::MaterializedView;
}
else if ( type == QLatin1String( "c" ) )
{
kind = Relkind::CompositeType;
}
else if ( type == QLatin1String( "t" ) )
{
kind = Relkind::ToastTable;
}
else if ( type == QLatin1String( "f" ) )
if ( mIsQuery || !connectionRO() )
{
kind = Relkind::ForeignTable;
mKind = Relkind::Unknown;
}
else if ( type == QLatin1String( "p" ) )
else
{
kind = Relkind::PartitionedTable;
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 );

mKind = Relkind::Unknown;

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

return kind;
return mKind;
}

bool QgsPostgresProvider::hasMetadata() const
@@ -56,6 +56,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider

enum Relkind
{
NotSet,
Unknown,
OrdinaryTable, // r
Index, // i
@@ -244,8 +245,17 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
void setListening( bool isListening ) override;

private:

/**
* \returns relation kind
*/
Relkind relkind() const;

/**
* Change internal query with \a query
*/
void setQuery( const QString &query );

bool declareCursor( const QString &cursorName,
const QgsAttributeList &fetchAttributes,
bool fetchGeometry,
@@ -377,6 +387,11 @@ class QgsPostgresProvider final: public QgsVectorDataProvider
*/
QString mSqlWhereClause;

/**
* Kind of relation
*/
mutable Relkind mKind = Relkind::NotSet;

/**
* Data type for the primary key
*/

0 comments on commit c522a04

Please sign in to comment.
You can’t perform that action at this time.