Skip to content

Commit

Permalink
Cache relkind to avoid requesting it every time we need it
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 committed Nov 26, 2020
1 parent feff180 commit 451064f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 53 deletions.
113 changes: 60 additions & 53 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -1463,9 +1456,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 );

Expand Down Expand Up @@ -5034,55 +5027,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
Expand Down
15 changes: 15 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -56,6 +56,7 @@ class QgsPostgresProvider final: public QgsVectorDataProvider

enum Relkind
{
NotSet,
Unknown,
OrdinaryTable, // r
Index, // i
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 451064f

Please sign in to comment.