Skip to content

Commit

Permalink
Merge pull request #30184 from elpaso/bugfix-gh30041-pg-oid-overflow
Browse files Browse the repository at this point in the history
An attempt to fix oid overflow in regclass

Cherry-picked from master ffe3c31
  • Loading branch information
elpaso committed Jun 13, 2019
1 parent 2742609 commit 0eadac6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -107,7 +107,7 @@ QString QgsPostgresResult::PQfname( int col )
return QString::fromUtf8( ::PQfname( mRes, col ) );
}

int QgsPostgresResult::PQftable( int col )
unsigned int QgsPostgresResult::PQftable( int col )
{
Q_ASSERT( mRes );
return ::PQftable( mRes, col );
Expand All @@ -119,7 +119,7 @@ int QgsPostgresResult::PQftablecol( int col )
return ::PQftablecol( mRes, col );
}

int QgsPostgresResult::PQftype( int col )
Oid QgsPostgresResult::PQftype( int col )
{
Q_ASSERT( mRes );
return ::PQftype( mRes, col );
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresconn.h
Expand Up @@ -167,8 +167,8 @@ class QgsPostgresResult

int PQnfields();
QString PQfname( int col );
int PQftable( int col );
int PQftype( int col );
unsigned int PQftable( int col );
Oid PQftype( int col );
int PQfmod( int col );
int PQftablecol( int col );
Oid PQoidValue();
Expand Down
31 changes: 16 additions & 15 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -752,7 +752,7 @@ bool QgsPostgresProvider::loadFields()
sql = QStringLiteral( "SELECT oid,typname,typtype,typelem,typlen FROM pg_type" );
QgsPostgresResult typeResult( connectionRO()->PQexec( sql ) );

QMap<int, PGTypeInfo> typeMap;
QMap<Oid, PGTypeInfo> typeMap;
for ( int i = 0; i < typeResult.PQntuples(); ++i )
{
PGTypeInfo typeInfo =
Expand All @@ -762,20 +762,20 @@ bool QgsPostgresProvider::loadFields()
/* typeElem = */ typeResult.PQgetvalue( i, 3 ),
/* typeLen = */ typeResult.PQgetvalue( i, 4 ).toInt()
};
typeMap.insert( typeResult.PQgetvalue( i, 0 ).toInt(), typeInfo );
typeMap.insert( typeResult.PQgetvalue( i, 0 ).toUInt(), typeInfo );
}


QMap<int, QMap<int, QString> > fmtFieldTypeMap, descrMap, defValMap;
QMap<int, QMap<int, int> > attTypeIdMap;
QMap<int, QMap<int, bool> > notNullMap, uniqueMap;
QMap<Oid, QMap<int, QString> > fmtFieldTypeMap, descrMap, defValMap;
QMap<Oid, QMap<int, Oid> > attTypeIdMap;
QMap<Oid, QMap<int, bool> > notNullMap, uniqueMap;
if ( result.PQnfields() > 0 )
{
// Collect table oids
QSet<int> tableoids;
QSet<unsigned int> tableoids;
for ( int i = 0; i < result.PQnfields(); i++ )
{
int tableoid = result.PQftable( i );
Oid tableoid = result.PQftable( i );
if ( tableoid > 0 )
{
tableoids.insert( tableoid );
Expand All @@ -785,7 +785,8 @@ bool QgsPostgresProvider::loadFields()
if ( !tableoids.isEmpty() )
{
QStringList tableoidsList;
Q_FOREACH ( int tableoid, tableoids )
const auto constTableoids = tableoids;
for ( Oid tableoid : constTableoids )
{
tableoidsList.append( QString::number( tableoid ) );
}
Expand All @@ -804,12 +805,12 @@ bool QgsPostgresProvider::loadFields()
QgsPostgresResult fmtFieldTypeResult( connectionRO()->PQexec( sql ) );
for ( int i = 0; i < fmtFieldTypeResult.PQntuples(); ++i )
{
int attrelid = fmtFieldTypeResult.PQgetvalue( i, 0 ).toInt();
int attnum = fmtFieldTypeResult.PQgetvalue( i, 1 ).toInt();
Oid attrelid = fmtFieldTypeResult.PQgetvalue( i, 0 ).toUInt();
int attnum = fmtFieldTypeResult.PQgetvalue( i, 1 ).toInt(); // Int2
QString formatType = fmtFieldTypeResult.PQgetvalue( i, 2 );
QString descr = fmtFieldTypeResult.PQgetvalue( i, 3 );
QString defVal = fmtFieldTypeResult.PQgetvalue( i, 4 );
int attType = fmtFieldTypeResult.PQgetvalue( i, 5 ).toInt();
Oid attType = fmtFieldTypeResult.PQgetvalue( i, 5 ).toUInt();
bool attNotNull = fmtFieldTypeResult.PQgetvalue( i, 6 ).toInt();
bool uniqueConstraint = fmtFieldTypeResult.PQgetvalue( i, 7 ).toInt();
fmtFieldTypeMap[attrelid][attnum] = formatType;
Expand All @@ -830,12 +831,12 @@ bool QgsPostgresProvider::loadFields()
if ( fieldName == mGeometryColumn )
continue;

int fldtyp = result.PQftype( i );
Oid fldtyp = result.PQftype( i );
int fldMod = result.PQfmod( i );
int fieldPrec = -1;
int tableoid = result.PQftable( i );
unsigned int tableoid = result.PQftable( i );
int attnum = result.PQftablecol( i );
int atttypid = attTypeIdMap[tableoid][attnum];
Oid atttypid = attTypeIdMap[tableoid][attnum];

const PGTypeInfo &typeInfo = typeMap.value( fldtyp );
QString fieldTypeName = typeInfo.typeName;
Expand Down Expand Up @@ -3169,7 +3170,7 @@ bool QgsPostgresProvider::setSubsetString( const QString &theSQL, bool updateFea
*/
long QgsPostgresProvider::featureCount() const
{
int featuresCounted = mShared->featuresCounted();
long featuresCounted = mShared->featuresCounted();
if ( featuresCounted >= 0 )
return featuresCounted;

Expand Down

0 comments on commit 0eadac6

Please sign in to comment.