Skip to content

Commit

Permalink
Accept fields of type "name" in PG 12
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and nyalldawson committed Jun 28, 2020
1 parent 0a84475 commit 0219ef3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -828,14 +828,8 @@ bool QgsPostgresProvider::loadFields()
}
}

// Populate the field vector for this layer. The field vector contains
// field name, type, length, and precision (if numeric)
QString sql = QStringLiteral( "SELECT * FROM %1 LIMIT 0" ).arg( mQuery );

QgsPostgresResult result( connectionRO()->PQexec( sql ) );

// Collect type info
sql = QStringLiteral( "SELECT oid,typname,typtype,typelem,typlen FROM pg_type" );
QString sql = QStringLiteral( "SELECT oid,typname,typtype,typelem,typlen FROM pg_type" );
QgsPostgresResult typeResult( connectionRO()->PQexec( sql ) );

QMap<Oid, PGTypeInfo> typeMap;
Expand All @@ -851,6 +845,11 @@ bool QgsPostgresProvider::loadFields()
typeMap.insert( typeResult.PQgetvalue( i, 0 ).toUInt(), typeInfo );
}

// Populate the field vector for this layer. The field vector contains
// field name, type, length, and precision (if numeric)
sql = QStringLiteral( "SELECT * FROM %1 LIMIT 0" ).arg( mQuery );

QgsPostgresResult result( connectionRO()->PQexec( sql ) );

QMap<Oid, QMap<int, QString> > fmtFieldTypeMap, descrMap, defValMap, identityMap, generatedMap;
QMap<Oid, QMap<int, Oid> > attTypeIdMap;
Expand Down Expand Up @@ -1120,6 +1119,13 @@ bool QgsPostgresProvider::loadFields()
fieldType = QVariant::Bool;
fieldSize = -1;
}
// PG 12 returns "name" type for some system table fields (e.g. information_schema.tables)
else if ( fieldTypeName == QLatin1String( "name" ) )
{
fieldSubType = QVariant::String;
// "name" type lenght is 64 according to: SELECT typlen FROM pg_type WHERE typname = 'name';
fieldSize = 64;
}
else
{
// be tolerant in case of views: this might be a field used as a key
Expand Down
3 changes: 2 additions & 1 deletion src/providers/postgres/qgspostgresproviderconnection.cpp
Expand Up @@ -271,7 +271,8 @@ QList<QVariantList> QgsPostgresProviderConnection::executeSqlPrivate( const QStr
}
else
{
QgsDebugMsg( QStringLiteral( "Unhandled PostgreSQL type %1" ).arg( typName ) );
// Just a warning, usually ok
QgsDebugMsgLevel( QStringLiteral( "Unhandled PostgreSQL type %1, assuming string" ).arg( typName ), 2 );
}
}
typeMap[ rowIdx ] = vType;
Expand Down

0 comments on commit 0219ef3

Please sign in to comment.