Skip to content
Permalink
Browse files
postgres provider:
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 d3aba7eeede5b0a4958afa7b21e21154a3e43304
Showing with 36 additions and 28 deletions.
  1. +32 −28 src/providers/postgres/qgspostgresprovider.cpp
  2. +4 −0 src/providers/postgres/qgspostgresprovider.h
@@ -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,
@@ -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;
@@ -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 );
}
@@ -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 )
@@ -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 );
}
@@ -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();

0 comments on commit d3aba7e

Please sign in to comment.