Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
reindent postgres provider
git-svn-id: http://svn.osgeo.org/qgis/trunk@10968 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Jun 23, 2009
1 parent 6386084 commit 6faf04d
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1686,8 +1686,8 @@ void QgsPostgresProvider::enumValues( int index, QStringList& enumList )

QString typeName;
//find out type of index
QgsFieldMap::const_iterator f_it = attributeFields.find(index);
if(f_it != attributeFields.constEnd())
QgsFieldMap::const_iterator f_it = attributeFields.find( index );
if ( f_it != attributeFields.constEnd() )
{
typeName = f_it.value().typeName();
}
Expand All @@ -1697,57 +1697,57 @@ void QgsPostgresProvider::enumValues( int index, QStringList& enumList )
}

//is type an enum?
QString typeSql = QString("SELECT typtype FROM pg_type where typname = %1").arg(quotedValue(typeName));
QString typeSql = QString( "SELECT typtype FROM pg_type where typname = %1" ).arg( quotedValue( typeName ) );
Result typeRes = connectionRO->PQexec( typeSql );
if ( PQresultStatus( typeRes ) != PGRES_TUPLES_OK || PQntuples(typeRes) < 1)
if ( PQresultStatus( typeRes ) != PGRES_TUPLES_OK || PQntuples( typeRes ) < 1 )
{
return;
}


QString typtype = PQgetvalue( typeRes, 0, 0 );
if(typtype.compare("e", Qt::CaseInsensitive) == 0)
if ( typtype.compare( "e", Qt::CaseInsensitive ) == 0 )
{
//try to read enum_range of attribute
if(!parseEnumRange(enumList, f_it->name()))
if ( !parseEnumRange( enumList, f_it->name() ) )
{
enumList.clear();
}
}
else
{
//is there a domain check constraint for the attribute?
if(!parseDomainCheckConstraint(enumList, f_it->name()))
if ( !parseDomainCheckConstraint( enumList, f_it->name() ) )
{
enumList.clear();
enumList.clear();
}
}
}

bool QgsPostgresProvider::parseEnumRange(QStringList& enumValues, const QString& attributeName) const
bool QgsPostgresProvider::parseEnumRange( QStringList& enumValues, const QString& attributeName ) const
{
enumValues.clear();
QString enumRangeSql = QString("SELECT enum_range(%1) from %2 limit1").arg(quotedIdentifier(attributeName)).arg(mSchemaTableName);
Result enumRangeRes = connectionRO->PQexec(enumRangeSql);
if ( PQresultStatus( enumRangeRes ) == PGRES_TUPLES_OK && PQntuples(enumRangeRes) > 0)
QString enumRangeSql = QString( "SELECT enum_range(%1) from %2 limit1" ).arg( quotedIdentifier( attributeName ) ).arg( mSchemaTableName );
Result enumRangeRes = connectionRO->PQexec( enumRangeSql );
if ( PQresultStatus( enumRangeRes ) == PGRES_TUPLES_OK && PQntuples( enumRangeRes ) > 0 )
{
QString enumRangeString = PQgetvalue(enumRangeRes, 0, 0);
QString enumRangeString = PQgetvalue( enumRangeRes, 0, 0 );
//strip away the brackets at begin and end
enumRangeString.chop(1);
enumRangeString.remove(0, 1);
QStringList rangeSplit = enumRangeString.split(",");
enumRangeString.chop( 1 );
enumRangeString.remove( 0, 1 );
QStringList rangeSplit = enumRangeString.split( "," );
QStringList::const_iterator range_it = rangeSplit.constBegin();
for(; range_it != rangeSplit.constEnd(); ++range_it)
for ( ; range_it != rangeSplit.constEnd(); ++range_it )
{
QString currentEnumValue = *range_it;
//remove quotes from begin and end of the value
if(currentEnumValue.startsWith("'") || currentEnumValue.startsWith("\""))
if ( currentEnumValue.startsWith( "'" ) || currentEnumValue.startsWith( "\"" ) )
{
currentEnumValue.remove(0, 1);
currentEnumValue.remove( 0, 1 );
}
if(currentEnumValue.endsWith("'") || currentEnumValue.endsWith("\""))
if ( currentEnumValue.endsWith( "'" ) || currentEnumValue.endsWith( "\"" ) )
{
currentEnumValue.chop(1);
currentEnumValue.chop( 1 );
}
enumValues << currentEnumValue;
}
Expand All @@ -1756,49 +1756,49 @@ bool QgsPostgresProvider::parseEnumRange(QStringList& enumValues, const QString&
return false;
}

bool QgsPostgresProvider::parseDomainCheckConstraint(QStringList& enumValues, const QString& attributeName) const
bool QgsPostgresProvider::parseDomainCheckConstraint( QStringList& enumValues, const QString& attributeName ) const
{
enumValues.clear();

//is it a domain type with a check constraint?
QString domainSql = QString("SELECT domain_name from information_schema.columns where table_name = %1 and column_name = %2").arg(quotedValue(mTableName)).arg(quotedValue(attributeName));
Result domainResult = connectionRO->PQexec(domainSql);
if ( PQresultStatus( domainResult ) == PGRES_TUPLES_OK && PQntuples(domainResult) > 0)
QString domainSql = QString( "SELECT domain_name from information_schema.columns where table_name = %1 and column_name = %2" ).arg( quotedValue( mTableName ) ).arg( quotedValue( attributeName ) );
Result domainResult = connectionRO->PQexec( domainSql );
if ( PQresultStatus( domainResult ) == PGRES_TUPLES_OK && PQntuples( domainResult ) > 0 )
{
//a domain type
QString domainCheckDefinitionSql = QString("SELECT consrc FROM pg_constraint where conname = (SELECT constraint_name FROM information_schema.domain_constraints WHERE domain_name = %1)").arg(quotedValue(PQgetvalue(domainResult, 0, 0)));
Result domainCheckRes = connectionRO->PQexec(domainCheckDefinitionSql);
if ( PQresultStatus(domainCheckRes) == PGRES_TUPLES_OK && PQntuples(domainCheckRes) > 0)
QString domainCheckDefinitionSql = QString( "SELECT consrc FROM pg_constraint where conname = (SELECT constraint_name FROM information_schema.domain_constraints WHERE domain_name = %1)" ).arg( quotedValue( PQgetvalue( domainResult, 0, 0 ) ) );
Result domainCheckRes = connectionRO->PQexec( domainCheckDefinitionSql );
if ( PQresultStatus( domainCheckRes ) == PGRES_TUPLES_OK && PQntuples( domainCheckRes ) > 0 )
{
QString checkDefinition = PQgetvalue(domainCheckRes, 0, 0);
QString checkDefinition = PQgetvalue( domainCheckRes, 0, 0 );

//we assume that the constraint is of the following form:
//(VALUE = ANY (ARRAY['a'::text, 'b'::text, 'c'::text, 'd'::text]))
//normally, postgresql creates that if the contstraint has been specified as 'VALUE in ('a', 'b', 'c', 'd')

//todo: ANY must occure before ARRAY
int anyPos = checkDefinition.indexOf("VALUE = ANY");
int arrayPosition = checkDefinition.lastIndexOf("ARRAY[");
int closingBracketPos = checkDefinition.indexOf("]", arrayPosition + 6);
int anyPos = checkDefinition.indexOf( "VALUE = ANY" );
int arrayPosition = checkDefinition.lastIndexOf( "ARRAY[" );
int closingBracketPos = checkDefinition.indexOf( "]", arrayPosition + 6 );

if(anyPos == -1 || anyPos >= arrayPosition)
if ( anyPos == -1 || anyPos >= arrayPosition )
{
return false; //constraint has not the required format
}

if(arrayPosition != -1)
if ( arrayPosition != -1 )
{
QString valueList = checkDefinition.mid(arrayPosition + 6, closingBracketPos);
QStringList commaSeparation = valueList.split(",", QString::SkipEmptyParts);
QString valueList = checkDefinition.mid( arrayPosition + 6, closingBracketPos );
QStringList commaSeparation = valueList.split( ",", QString::SkipEmptyParts );
QStringList::const_iterator cIt = commaSeparation.constBegin();
for(; cIt != commaSeparation.constEnd(); ++cIt)
for ( ; cIt != commaSeparation.constEnd(); ++cIt )
{
//get string between ''
int beginQuotePos = cIt->indexOf("'");
int endQuotePos = cIt->lastIndexOf("'");
if(beginQuotePos != -1 && (endQuotePos - beginQuotePos) > 1)
int beginQuotePos = cIt->indexOf( "'" );
int endQuotePos = cIt->lastIndexOf( "'" );
if ( beginQuotePos != -1 && ( endQuotePos - beginQuotePos ) > 1 )
{
enumValues << cIt->mid(beginQuotePos + 1, endQuotePos - beginQuotePos - 1);
enumValues << cIt->mid( beginQuotePos + 1, endQuotePos - beginQuotePos - 1 );
}
}
}
Expand Down

0 comments on commit 6faf04d

Please sign in to comment.