Skip to content

Commit

Permalink
postgres provider:
Browse files Browse the repository at this point in the history
use same field expression in feature queries and min()/max()/distinct.
char fields were trimmed differently otherwise.



git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11771 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Oct 8, 2009
1 parent aede963 commit d3aba7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,31 @@ QString QgsPostgresProvider::storageType() const
return "PostgreSQL database with PostGIS extension";
}

QString QgsPostgresProvider::fieldExpression( const QgsField &fld ) const
{
const QString &type = fld.typeName();
if ( type == "money" )
{
return QString( "cash_out(%1)" ).arg( quotedIdentifier( fld.name() ) );
}
else if ( type.startsWith( "_" ) )
{
return QString( "array_out(%1)" ).arg( quotedIdentifier( fld.name() ) );
}
else if ( type == "bool" )
{
return QString( "boolout(%1)" ).arg( quotedIdentifier( fld.name() ) );
}
else if ( type == "geometry" )
{
return QString( "asewkt(%1)" ).arg( quotedIdentifier( fld.name() ) );
}
else
{
return quotedIdentifier( fld.name() ) + "::text";
}
}

bool QgsPostgresProvider::declareCursor(
const QString &cursorName,
const QgsAttributeList &fetchAttributes,
Expand All @@ -430,31 +455,10 @@ bool QgsPostgresProvider::declareCursor(
{
const QgsField &fld = field( *it );

const QString &fieldname = fld.name();
if ( fieldname == primaryKey )
if ( fld.name() == primaryKey )
continue;

const QString &type = fld.typeName();
if ( type == "money" )
{
query += QString( ",cash_out(%1)" ).arg( quotedIdentifier( fieldname ) );
}
else if ( type.startsWith( "_" ) )
{
query += QString( ",array_out(%1)" ).arg( quotedIdentifier( fieldname ) );
}
else if ( type == "bool" )
{
query += QString( ",boolout(%1)" ).arg( quotedIdentifier( fieldname ) );
}
else if ( type == "geometry" )
{
query += QString( ",asewkt(%1)" ).arg( quotedIdentifier( fieldname ) );
}
else
{
query += "," + quotedIdentifier( fieldname ) + "::text";
}
query += "," + fieldExpression( fld );
}

query += " from " + mSchemaTableName;
Expand Down Expand Up @@ -1698,13 +1702,13 @@ void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues
if ( sqlWhereClause.isEmpty() )
{
sql = QString( "select distinct %1 from %2 order by %1" )
.arg( quotedIdentifier( fld.name() ) )
.arg( fieldExpression( fld ) )
.arg( mSchemaTableName );
}
else
{
sql = QString( "select distinct %1 from %2 where %3 order by %1" )
.arg( quotedIdentifier( fld.name() ) )
.arg( fieldExpression( fld ) )
.arg( mSchemaTableName )
.arg( sqlWhereClause );
}
Expand All @@ -1718,7 +1722,7 @@ void QgsPostgresProvider::uniqueValues( int index, QList<QVariant> &uniqueValues
if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
{
for ( int i = 0; i < PQntuples( res ); i++ )
uniqueValues.append( QString::fromUtf8( PQgetvalue( res, i, 0 ) ) );
uniqueValues.append( convertValue( fld.type(), QString::fromUtf8( PQgetvalue( res, i, 0 ) ) ) );
}
}
catch ( PGFieldNotFound )
Expand Down Expand Up @@ -1865,13 +1869,13 @@ QVariant QgsPostgresProvider::maximumValue( int index )
if ( sqlWhereClause.isEmpty() )
{
sql = QString( "select max(%1) from %2" )
.arg( quotedIdentifier( fld.name() ) )
.arg( fieldExpression( fld ) )
.arg( mSchemaTableName );
}
else
{
sql = QString( "select max(%1) from %2 where %3" )
.arg( quotedIdentifier( fld.name() ) )
.arg( fieldExpression( fld ) )
.arg( mSchemaTableName )
.arg( sqlWhereClause );
}
Expand Down
4 changes: 4 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ class QgsPostgresProvider : public QgsVectorDataProvider
*/
QString quotedValue( QString value ) const;

/** expression to retrieve value
*/
QString fieldExpression( const QgsField &fld ) const;

/** Load the field list
*/
void loadFields();
Expand Down

0 comments on commit d3aba7e

Please sign in to comment.