@@ -909,7 +909,7 @@ bool QgsPostgresProvider::loadFields()
909
909
910
910
int fldtyp = PQftype ( result, i );
911
911
QString typOid = QString ().setNum ( fldtyp );
912
- int fieldModifier = PQfmod ( result, i ) ;
912
+ int fieldPrec = - 1 ;
913
913
QString fieldComment ( " " );
914
914
int tableoid = PQftable ( result, i );
915
915
@@ -924,13 +924,15 @@ bool QgsPostgresProvider::loadFields()
924
924
QString fieldElem = QString::fromUtf8 ( PQgetvalue ( oidResult, 0 , 2 ) );
925
925
int fieldSize = QString::fromUtf8 ( PQgetvalue ( oidResult, 0 , 3 ) ).toInt ();
926
926
927
+ QString formattedFieldType;
927
928
if ( tableoid > 0 )
928
929
{
929
- sql = QString ( " SELECT attnum FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
930
+ sql = QString ( " SELECT attnum,pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
930
931
.arg ( tableoid ).arg ( quotedValue ( fieldName ) );
931
932
932
933
Result tresult = connectionRO->PQexec ( sql );
933
934
QString attnum = QString::fromUtf8 ( PQgetvalue ( tresult, 0 , 0 ) );
935
+ formattedFieldType = QString::fromUtf8 ( PQgetvalue ( tresult, 0 , 1 ) );
934
936
935
937
sql = QString ( " SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" )
936
938
.arg ( tableoid ).arg ( attnum );
@@ -953,20 +955,41 @@ bool QgsPostgresProvider::loadFields()
953
955
{
954
956
fieldType = QVariant::LongLong;
955
957
fieldSize = -1 ;
958
+ fieldPrec = 0 ;
956
959
}
957
960
else if ( fieldTypeName.startsWith ( " int" ) ||
958
961
fieldTypeName == " serial" )
959
962
{
960
963
fieldType = QVariant::Int;
961
964
fieldSize = -1 ;
965
+ fieldPrec = 0 ;
962
966
}
963
967
else if ( fieldTypeName == " real" ||
964
968
fieldTypeName == " double precision" ||
965
- fieldTypeName.startsWith ( " float" ) ||
966
- fieldTypeName == " numeric" )
969
+ fieldTypeName.startsWith ( " float" ) )
967
970
{
968
971
fieldType = QVariant::Double;
969
972
fieldSize = -1 ;
973
+ fieldPrec = 0 ;
974
+ }
975
+ else if ( fieldTypeName == " numeric" )
976
+ {
977
+ fieldType = QVariant::Double;
978
+
979
+ QRegExp re ( " numeric\\ ((\\ d+),(\\ d+)\\ )" );
980
+ if ( re.exactMatch ( formattedFieldType ) && re.captureCount () == 2 )
981
+ {
982
+ fieldSize = re.cap ( 1 ).toInt ();
983
+ fieldPrec = re.cap ( 2 ).toInt ();
984
+ }
985
+ else
986
+ {
987
+ QgsDebugMsg ( QString ( " unexpected formatted field type '%1' for field %2" )
988
+ .arg ( formattedFieldType )
989
+ .arg ( fieldName ) );
990
+ fieldSize = -1 ;
991
+ fieldPrec = -1 ;
992
+ }
970
993
}
971
994
else if ( fieldTypeName == " text" ||
972
995
fieldTypeName == " bpchar" ||
@@ -983,6 +1006,20 @@ bool QgsPostgresProvider::loadFields()
983
1006
else if ( fieldTypeName == " char" )
984
1007
{
985
1008
fieldType = QVariant::String;
1009
+
1010
+ QRegExp re ( " char\\ ((\\ d+)\\ )" );
1011
+ if ( re.exactMatch ( formattedFieldType ) && re.captureCount () == 1 )
1012
+ {
1013
+ fieldSize = re.cap ( 1 ).toInt ();
1014
+ }
1015
+ else
1016
+ {
1017
+ QgsDebugMsg ( QString ( " unexpected formatted field type '%1' for field %2" )
1018
+ .arg ( formattedFieldType )
1019
+ .arg ( fieldName ) );
1020
+ fieldSize = -1 ;
1021
+ fieldPrec = -1 ;
1022
+ }
986
1023
}
987
1024
else
988
1025
{
@@ -1018,7 +1055,7 @@ bool QgsPostgresProvider::loadFields()
1018
1055
1019
1056
fields << fieldName;
1020
1057
1021
- attributeFields.insert ( i, QgsField ( fieldName, fieldType, fieldTypeName, fieldSize, fieldModifier , fieldComment ) );
1058
+ attributeFields.insert ( i, QgsField ( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec , fieldComment ) );
1022
1059
}
1023
1060
1024
1061
return true ;
0 commit comments