Skip to content
Permalink
Browse files

Backport of PR #35832 (PgSQL bigint PKs)

* PostgreSQL provider don't cast bigint PKs to text

* Apply suggestions from code review

Fix merge conflicts

Co-Authored-By: José de Paula Rodrigues N. Assis <espinafre@gmail.com>

* Fix merge conflicts

Co-Authored-By: José de Paula Rodrigues N. Assis <espinafre@gmail.com>

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Matthias Kuhn <matthias@opengis.ch>
Co-authored-by: José de Paula Rodrigues N. Assis <espinafre@gmail.com>
(cherry picked from commit 566afd9)
  • Loading branch information
qgis-bot authored and nyalldawson committed May 15, 2020
1 parent 7839f24 commit 4a639985017a8178e19ab1d684cf6dde7f095881
@@ -203,6 +203,7 @@ QgsPostgresConn::QgsPostgresConn( const QString &conninfo, bool readOnly, bool s
, mOpenCursors( 0 )
, mConnInfo( conninfo )
, mGeosAvailable( false )
, mProjAvailable( false )
, mTopologyAvailable( false )
, mGotPostgisVersion( false )
, mPostgresqlVersion( 0 )
@@ -1052,10 +1053,12 @@ QString QgsPostgresConn::postgisVersion() const
// apparently PostGIS 1.5.2 doesn't report capabilities in postgis_version() anymore
if ( mPostgisVersionMajor > 1 || ( mPostgisVersionMajor == 1 && mPostgisVersionMinor >= 5 ) )
{
result = PQexec( QStringLiteral( "SELECT postgis_geos_version()" ) );
result = PQexec( QStringLiteral( "SELECT postgis_geos_version(), postgis_proj_version()" ) );
mGeosAvailable = result.PQntuples() == 1 && !result.PQgetisnull( 0, 0 );
mProjAvailable = result.PQntuples() == 1 && !result.PQgetisnull( 0, 1 );
QgsDebugMsg( QStringLiteral( "geos:%1 proj:%2" )
.arg( mGeosAvailable ? result.PQgetvalue( 0, 0 ) : "none" ) );
.arg( mGeosAvailable ? result.PQgetvalue( 0, 0 ) : "none" )
.arg( mProjAvailable ? result.PQgetvalue( 0, 1 ) : "none" ) );
}
else
{
@@ -1619,6 +1622,10 @@ QString QgsPostgresConn::fieldExpression( const QgsField &fld, QString expr )
{
return QStringLiteral( "st_astext(%1)" ).arg( expr );
}
else if ( type == QLatin1String( "int8" ) )
{
return expr;
}
//TODO: add support for hstore
//TODO: add support for json/jsonb
else
@@ -51,6 +51,7 @@ enum QgsPostgresPrimaryKeyType
{
PktUnknown,
PktInt,
PktInt64,
PktUint64,
PktTid,
PktOid,
@@ -391,6 +392,9 @@ class QgsPostgresConn : public QObject
//! GEOS capability
mutable bool mGeosAvailable;

//! PROJ capability
mutable bool mProjAvailable;

//! Topology capability
mutable bool mTopologyAvailable;

@@ -611,6 +611,7 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString &whereClause, long
break;

case PktInt:
case PktInt64:
case PktUint64:
query += delim + QgsPostgresConn::quotedIdentifier( mSource->mFields.at( mSource->mPrimaryKeyAttrs.at( 0 ) ).name() );
delim = ',';
@@ -663,7 +664,6 @@ bool QgsPostgresFeatureIterator::declareCursor( const QString &whereClause, long
return true;
}


bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int row, QgsFeature &feature )
{
feature.initAttributes( mSource->mFields.count() );
@@ -744,20 +744,37 @@ bool QgsPostgresFeatureIterator::getFeature( QgsPostgresResult &queryResult, int
break;

case PktInt:
case PktUint64:
fid = mConn->getBinaryInt( queryResult, row, col++ );
if ( !subsetOfAttributes || fetchAttributes.contains( mSource->mPrimaryKeyAttrs.at( 0 ) ) )
{
feature.setAttribute( mSource->mPrimaryKeyAttrs[0], fid );
}
if ( mSource->mPrimaryKeyType == PktInt )
// NOTE: this needs be done _after_ the setAttribute call
// above as we want the attribute value to be 1:1 with
// database value
fid = QgsPostgresUtils::int32pk_to_fid( fid );
break;

case PktUint64:
case PktInt64:
{
QVariantList pkVal;

int idx = mSource->mPrimaryKeyAttrs.at( 0 );
QgsField fld = mSource->mFields.at( idx );

QVariant v = QgsPostgresProvider::convertValue( fld.type(), fld.subType(), QString::number( mConn->getBinaryInt( queryResult, row, col ) ), fld.typeName() );
pkVal << v;

if ( !subsetOfAttributes || fetchAttributes.contains( idx ) )
{
// NOTE: this needs be done _after_ the setAttribute call
// above as we want the attribute value to be 1:1 with
// database value
fid = QgsPostgresUtils::int32pk_to_fid( fid );
feature.setAttribute( idx, v );
}
break;
col++;

fid = mSource->mShared->lookupFid( pkVal );
}
break;

case PktFidMap:
{
@@ -76,11 +76,12 @@ QgsPostgresProvider::pkType( const QgsField &f ) const
switch ( f.type() )
{
case QVariant::LongLong:
// unless we can guarantee all values are unsigned
// (in which case we could use pktUint64)
// we'll have to use a Map type.
// See https://github.com/qgis/QGIS/issues/22258
return PktFidMap; // pktUint64
// PostgreSQL doesn't have native "unsigned" types.
// Unsigned primary keys are emulated by the serial/bigserial
// pseudo-types, in which autogenerated values are always > 0;
// however, the database accepts manually inserted 0 and negative values
// in these fields.
return PktInt64;

case QVariant::Int:
return PktInt;
@@ -275,11 +276,12 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri, const ProviderOpti
key = QStringLiteral( "tid" );
break;
case PktInt:
case PktUint64:
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
Q_ASSERT( mPrimaryKeyAttrs[0] >= 0 && mPrimaryKeyAttrs[0] < mAttributeFields.count() );
key = mAttributeFields.at( mPrimaryKeyAttrs.at( 0 ) ).name();
break;
case PktInt64:
case PktUint64:
case PktFidMap:
{
QString delim;
@@ -435,11 +437,12 @@ QString QgsPostgresProvider::pkParamWhereClause( int offset, const char *alias )
break;

case PktInt:
case PktUint64:
Q_ASSERT( mPrimaryKeyAttrs.size() == 1 );
whereClause = QStringLiteral( "%3%1=$%2" ).arg( quotedIdentifier( field( mPrimaryKeyAttrs[0] ).name() ) ).arg( offset ).arg( aliased );
break;

case PktInt64:
case PktUint64:
case PktFidMap:
{
QString delim;
@@ -476,7 +479,6 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
switch ( mPrimaryKeyType )
{
case PktOid:
case PktUint64:
params << QString::number( featureId );
break;

@@ -488,6 +490,8 @@ void QgsPostgresProvider::appendPkParams( QgsFeatureId featureId, QStringList &p
params << QStringLiteral( "'(%1,%2)'" ).arg( FID_TO_NUMBER( featureId ) >> 16 ).arg( FID_TO_NUMBER( featureId ) & 0xffff );
break;

case PktInt64:
case PktUint64:
case PktFidMap:
{
QVariantList pkVals = mShared->lookupKey( featureId );
@@ -547,10 +551,22 @@ QString QgsPostgresUtils::whereClause( QgsFeatureId featureId, const QgsFields &
whereClause = QStringLiteral( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields.at( pkAttrs[0] ).name() ) ).arg( FID2PKINT( featureId ) );
break;

case PktInt64:
case PktUint64:
{
Q_ASSERT( pkAttrs.size() == 1 );
whereClause = QStringLiteral( "%1=%2" ).arg( QgsPostgresConn::quotedIdentifier( fields.at( pkAttrs[0] ).name() ) ).arg( featureId );
break;
QVariantList pkVals = sharedData->lookupKey( featureId );
if ( !pkVals.isEmpty() )
{
QgsField fld = fields.at( pkAttrs[0] );
whereClause = conn->fieldExpression( fld );
if ( !pkVals[0].isNull() )
whereClause += '=' + pkVals[0].toString();
else
whereClause += QLatin1String( " IS NULL" );
}
}
break;

case PktFidMap:
{
@@ -597,7 +613,6 @@ QString QgsPostgresUtils::whereClause( const QgsFeatureIds &featureIds, const Qg
{
case PktOid:
case PktInt:
case PktUint64:
{
QString expr;

@@ -607,25 +622,49 @@ QString QgsPostgresUtils::whereClause( const QgsFeatureIds &featureIds, const Qg
QString delim;
expr = QStringLiteral( "%1 IN (" ).arg( ( pkType == PktOid ? QStringLiteral( "oid" ) : QgsPostgresConn::quotedIdentifier( fields.at( pkAttrs[0] ).name() ) ) );

const auto constFeatureIds = featureIds;
for ( const QgsFeatureId featureId : constFeatureIds )
for ( const QgsFeatureId featureId : qgis::as_const( featureIds ) )
{
expr += delim + FID_TO_STRING( ( pkType == PktOid ? featureId : pkType == PktUint64 ? featureId : FID2PKINT( featureId ) ) );
expr += delim + FID_TO_STRING( ( pkType == PktOid ? featureId : FID2PKINT( featureId ) ) );
delim = ',';
}
expr += ')';
}

return expr;
}
case PktInt64:
case PktUint64:
{
QString expr;

//simple primary key, so prefer to use an "IN (...)" query. These are much faster then multiple chained ...OR... clauses
if ( !featureIds.isEmpty() )
{
QString delim;
expr = QStringLiteral( "%1 IN (" ).arg( QgsPostgresConn::quotedIdentifier( fields.at( pkAttrs[0] ).name() ) );

for ( const QgsFeatureId featureId : qgis::as_const( featureIds ) )
{
QVariantList pkVals = sharedData->lookupKey( featureId );
if ( !pkVals.isEmpty() )
{
QgsField fld = fields.at( pkAttrs[0] );
expr += delim + pkVals[0].toString();
delim = ',';
}
}
expr += ')';
}

return expr;
}
case PktFidMap:
case PktTid:
case PktUnknown:
{
//complex primary key, need to build up where string
QStringList whereClauses;
const auto constFeatureIds = featureIds;
for ( const QgsFeatureId featureId : constFeatureIds )
for ( const QgsFeatureId featureId : qgis::as_const( featureIds ) )
{
whereClauses << whereClause( featureId, fields, conn, pkType, pkAttrs, sharedData );
}
@@ -2195,7 +2234,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
bool skipSinglePKField = false;
bool overrideIdentity = false;

if ( ( mPrimaryKeyType == PktInt || mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktUint64 ) )
if ( ( mPrimaryKeyType == PktInt || mPrimaryKeyType == PktInt64 || mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktUint64 ) )
{
if ( mPrimaryKeyAttrs.size() == 1 &&
defaultValueClause( mPrimaryKeyAttrs[0] ).startsWith( "nextval(" ) )
@@ -2350,7 +2389,7 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )

if ( !( flags & QgsFeatureSink::FastInsert ) )
{
if ( mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktInt || mPrimaryKeyType == PktUint64 )
if ( mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktInt || mPrimaryKeyType == PktInt64 || mPrimaryKeyType == PktUint64 )
{
insert += QLatin1String( " RETURNING " );

@@ -2431,17 +2470,13 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList &flist, Flags flags )
if ( !( flags & QgsFeatureSink::FastInsert ) )
{
// update feature ids
if ( mPrimaryKeyType == PktInt || mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktUint64 )
if ( mPrimaryKeyType == PktInt || mPrimaryKeyType == PktInt64 || mPrimaryKeyType == PktFidMap || mPrimaryKeyType == PktUint64 )
{
for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); ++features )
{
QgsAttributes attrs = features->attributes();

if ( mPrimaryKeyType == PktUint64 )
{
features->setId( STRING_TO_FID( attrs.at( mPrimaryKeyAttrs.at( 0 ) ) ) );
}
else if ( mPrimaryKeyType == PktInt )
if ( mPrimaryKeyType == PktInt )
{
features->setId( PKINT2FID( STRING_TO_FID( attrs.at( mPrimaryKeyAttrs.at( 0 ) ) ) ) );
}
@@ -493,7 +493,7 @@ class QgsPostgresUtils

static QString andWhereClauses( const QString &c1, const QString &c2 );

static const qint64 INT32PK_OFFSET = 4294967296;
static const qint64 INT32PK_OFFSET = 4294967296; // 2^32

// We shift negative 32bit integers to above the max 32bit
// positive integer to support the whole range of int32 values

0 comments on commit 4a63998

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