Skip to content

Commit

Permalink
postgres provider: support NULL in composite keys (fixes #13641)
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 20, 2015
1 parent a8765a3 commit 55babc3
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ QString QgsPostgresProvider::storageType() const
#if QT_VERSION < 0x050000
static bool operator<( const QVariant &a, const QVariant &b )
{
if ( a.isNull() || b.isNull() )
// invalid < NULL < any value
if ( !a.isValid() )
return b.isValid();
else if ( a.isNull() )
return b.isValid() && !b.isNull();
else if ( !b.isValid() || b.isNull() )
return false;

if ( a.type() == b.type() )
Expand All @@ -303,11 +308,11 @@ static bool operator<( const QVariant &a, const QVariant &b )

case QVariant::List:
{
QList<QVariant> al = a.toList();
QList<QVariant> bl = b.toList();
const QList<QVariant> &al = a.toList();
const QList<QVariant> &bl = b.toList();

int i, n = qMin( al.size(), bl.size() );
for ( i = 0; i < n && al[i] == bl[i]; i++ )
for ( i = 0; i < n && al[i].type() == bl[i].type() && al[i].isNull() == bl[i].isNull() && al[i] == bl[i]; i++ )
;

if ( i == n )
Expand All @@ -319,8 +324,8 @@ static bool operator<( const QVariant &a, const QVariant &b )

case QVariant::StringList:
{
QStringList al = a.toStringList();
QStringList bl = b.toStringList();
const QStringList &al = a.toStringList();
const QStringList &bl = b.toStringList();

int i, n = qMin( al.size(), bl.size() );
for ( i = 0; i < n && al[i] == bl[i]; i++ )
Expand Down Expand Up @@ -516,7 +521,12 @@ QString QgsPostgresUtils::whereClause( QgsFeatureId featureId, const QgsFields&
int idx = pkAttrs[i];
const QgsField &fld = fields[ idx ];

whereClause += delim + QString( "%1=%2" ).arg( conn->fieldExpression( fld ), QgsPostgresConn::quotedValue( pkVals[i].toString() ) );
whereClause += delim + conn->fieldExpression( fld );
if ( pkVals[i].isNull() )
whereClause += " IS NULL";
else
whereClause += "=" + QgsPostgresConn::quotedValue( pkVals[i].toString() );

delim = " AND ";
}
}
Expand Down

0 comments on commit 55babc3

Please sign in to comment.