Skip to content

Commit cfb8552

Browse files
committedApr 10, 2017
[postgis] Native boolean support
1 parent 5ab8e71 commit cfb8552

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed
 

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ QgsPostgresProvider::QgsPostgresProvider( QString const &uri )
227227
<< QgsVectorDataProvider::NativeType( tr( "Array of number (integer - 64bit)" ), QStringLiteral( "int8[]" ), QVariant::List, -1, -1, -1, -1, QVariant::LongLong )
228228
<< QgsVectorDataProvider::NativeType( tr( "Array of number (double)" ), QStringLiteral( "double precision[]" ), QVariant::List, -1, -1, -1, -1, QVariant::Double )
229229
<< QgsVectorDataProvider::NativeType( tr( "Array of text" ), QStringLiteral( "text[]" ), QVariant::StringList, -1, -1, -1, -1, QVariant::String )
230+
231+
// boolean
232+
<< QgsVectorDataProvider::NativeType( tr( "Boolean" ), QStringLiteral( "bool" ), QVariant::Bool, -1, -1, -1, -1 )
230233
);
231234

232235
QString key;
@@ -898,7 +901,6 @@ bool QgsPostgresProvider::loadFields()
898901
fieldSize = -1;
899902
}
900903
else if ( fieldTypeName == QLatin1String( "text" ) ||
901-
fieldTypeName == QLatin1String( "bool" ) ||
902904
fieldTypeName == QLatin1String( "geometry" ) ||
903905
fieldTypeName == QLatin1String( "inet" ) ||
904906
fieldTypeName == QLatin1String( "money" ) ||
@@ -956,6 +958,12 @@ bool QgsPostgresProvider::loadFields()
956958
fieldSubType = QVariant::String;
957959
fieldSize = -1;
958960
}
961+
else if ( fieldTypeName == QLatin1String( "bool" ) )
962+
{
963+
// enum
964+
fieldType = QVariant::Bool;
965+
fieldSize = -1;
966+
}
959967
else
960968
{
961969
QgsMessageLog::logMessage( tr( "Field %1 ignored, because of unsupported type %2" ).arg( fieldName, fieldTypeName ), tr( "PostGIS" ) );
@@ -3615,6 +3623,12 @@ bool QgsPostgresProvider::convertField( QgsField &field, const QMap<QString, QVa
36153623
fieldPrec = -1;
36163624
break;
36173625

3626+
case QVariant::Bool:
3627+
fieldType = QStringLiteral( "bool" );
3628+
fieldPrec = -1;
3629+
fieldSize = -1;
3630+
break;
3631+
36183632
default:
36193633
return false;
36203634
}
@@ -4106,20 +4120,32 @@ static QVariant parseArray( const QString &txt, QVariant::Type type, QVariant::T
41064120

41074121
QVariant QgsPostgresProvider::convertValue( QVariant::Type type, QVariant::Type subType, const QString &value )
41084122
{
4123+
QVariant result;
41094124
switch ( type )
41104125
{
41114126
case QVariant::Map:
4112-
return parseHstore( value );
4127+
result = parseHstore( value );
4128+
break;
41134129
case QVariant::StringList:
41144130
case QVariant::List:
4115-
return parseArray( value, type, subType );
4131+
result = parseArray( value, type, subType );
4132+
break;
4133+
case QVariant::Bool:
4134+
if ( value == QChar( 't' ) )
4135+
result = true;
4136+
else if ( value == QChar( 'f' ) )
4137+
result = false;
4138+
else
4139+
result = QVariant( type );
4140+
break;
41164141
default:
4117-
{
4118-
QVariant v( value );
4119-
if ( !v.convert( type ) || value.isNull() ) return QVariant( type );
4120-
return v;
4121-
}
4142+
result = value;
4143+
if ( !result.convert( type ) || value.isNull() )
4144+
result = QVariant( type );
4145+
break;
41224146
}
4147+
4148+
return result;
41234149
}
41244150

41254151
QList<QgsVectorLayer *> QgsPostgresProvider::searchLayers( const QList<QgsVectorLayer *> &layers, const QString &connectionInfo, const QString &schema, const QString &tableName )

1 commit comments

Comments
 (1)

3nids commented on Sep 4, 2017

@3nids
Member

@m-kuhn I think this break porting projects having expressions with "my_bool" = 't'. In my project, this breaks layer symbology. Is there any mean to have some project translation for this or is it too tricky?

Please sign in to comment.