Skip to content
Permalink
Browse files

Moved the code to determine the primary key for a view using the URI …

…keyColumn to a separate function.
  • Loading branch information
SebDieBln authored and jef-n committed Oct 17, 2015
1 parent 3e0badc commit a721949040d56fb6c7ffdeeca48dc6649c2d5d4d
Showing with 92 additions and 83 deletions.
  1. +88 −83 src/providers/postgres/qgspostgresprovider.cpp
  2. +4 −0 src/providers/postgres/qgspostgresprovider.h
@@ -1175,89 +1175,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
}
else if ( type == "v" || type == "m" ) // the relation is a view
{
QString primaryKey = mUri.keyColumn();
mPrimaryKeyType = pktUnknown;

if ( !primaryKey.isEmpty() )
{
QStringList cols;

// remove quotes from key list
if ( primaryKey.startsWith( '"' ) && primaryKey.endsWith( '"' ) )
{
int i = 1;
QString col;
while ( i < primaryKey.size() )
{
if ( primaryKey[i] == '"' )
{
if ( i + 1 < primaryKey.size() && primaryKey[i+1] == '"' )
{
i++;
}
else
{
cols << col;
col = "";

if ( ++i == primaryKey.size() )
break;

Q_ASSERT( primaryKey[i] == ',' );
i++;
Q_ASSERT( primaryKey[i] == '"' );
i++;
col = "";
continue;
}
}

col += primaryKey[i++];
}
}
else if ( primaryKey.contains( "," ) )
{
cols = primaryKey.split( "," );
}
else
{
cols << primaryKey;
primaryKey = quotedIdentifier( primaryKey );
}

Q_FOREACH ( const QString& col, cols )
{
int idx = fieldNameIndex( col );
if ( idx < 0 )
{
QgsMessageLog::logMessage( tr( "Key field '%1' for view not found." ).arg( col ), tr( "PostGIS" ) );
mPrimaryKeyAttrs.clear();
break;
}

mPrimaryKeyAttrs << idx;
}

if ( mPrimaryKeyAttrs.size() > 0 )
{
if ( mUseEstimatedMetadata || uniqueData( mQuery, primaryKey ) )
{
mPrimaryKeyType = ( mPrimaryKeyAttrs.size() == 1 && ( mAttributeFields[ mPrimaryKeyAttrs[0] ].type() == QVariant::Int || mAttributeFields[ mPrimaryKeyAttrs[0] ].type() == QVariant::LongLong ) ) ? pktInt : pktFidMap;
}
else
{
QgsMessageLog::logMessage( tr( "Primary key field '%1' for view not unique." ).arg( primaryKey ), tr( "PostGIS" ) );
}
}
else
{
QgsMessageLog::logMessage( tr( "Keys for view undefined." ).arg( primaryKey ), tr( "PostGIS" ) );
}
}
else
{
QgsMessageLog::logMessage( tr( "No key field for view given." ), tr( "PostGIS" ) );
}
determinePrimaryKeyFromUriKeyColumn();
}
else
{
@@ -1324,6 +1242,93 @@ bool QgsPostgresProvider::determinePrimaryKey()
return mValid;
}

void QgsPostgresProvider::determinePrimaryKeyFromUriKeyColumn()
{
QString primaryKey = mUri.keyColumn();
mPrimaryKeyType = pktUnknown;

if ( !primaryKey.isEmpty() )
{
QStringList cols;

// remove quotes from key list
if ( primaryKey.startsWith( '"' ) && primaryKey.endsWith( '"' ) )
{
int i = 1;
QString col;
while ( i < primaryKey.size() )
{
if ( primaryKey[i] == '"' )
{
if ( i + 1 < primaryKey.size() && primaryKey[i+1] == '"' )
{
i++;
}
else
{
cols << col;
col = "";

if ( ++i == primaryKey.size() )
break;

Q_ASSERT( primaryKey[i] == ',' );
i++;
Q_ASSERT( primaryKey[i] == '"' );
i++;
col = "";
continue;
}
}

col += primaryKey[i++];
}
}
else if ( primaryKey.contains( "," ) )
{
cols = primaryKey.split( "," );
}
else
{
cols << primaryKey;
primaryKey = quotedIdentifier( primaryKey );
}

Q_FOREACH ( const QString& col, cols )
{
int idx = fieldNameIndex( col );
if ( idx < 0 )
{
QgsMessageLog::logMessage( tr( "Key field '%1' for view not found." ).arg( col ), tr( "PostGIS" ) );
mPrimaryKeyAttrs.clear();
break;
}

mPrimaryKeyAttrs << idx;
}

if ( mPrimaryKeyAttrs.size() > 0 )
{
if ( mUseEstimatedMetadata || uniqueData( mQuery, primaryKey ) )
{
mPrimaryKeyType = ( mPrimaryKeyAttrs.size() == 1 && ( mAttributeFields[ mPrimaryKeyAttrs[0] ].type() == QVariant::Int || mAttributeFields[ mPrimaryKeyAttrs[0] ].type() == QVariant::LongLong ) ) ? pktInt : pktFidMap;
}
else
{
QgsMessageLog::logMessage( tr( "Primary key field '%1' for view not unique." ).arg( primaryKey ), tr( "PostGIS" ) );
}
}
else
{
QgsMessageLog::logMessage( tr( "Keys for view undefined." ).arg( primaryKey ), tr( "PostGIS" ) );
}
}
else
{
QgsMessageLog::logMessage( tr( "No key field for view given." ), tr( "PostGIS" ) );
}
}

bool QgsPostgresProvider::uniqueData( QString query, QString quotedColName )
{
Q_UNUSED( query );
@@ -136,6 +136,10 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
bool determinePrimaryKey();

/** Determine the fields making up the primary key from the uri attribute keyColumn
*/
void determinePrimaryKeyFromUriKeyColumn();

/**
* Get the field information for the layer
* @return vector of QgsField objects

0 comments on commit a721949

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