Skip to content

Commit 794907c

Browse files
committed
[BACKPORT] fix #4089
- postgres provider: fix detection of field length and precision for char and numeric fields. - attribute editor: fix validation in case the field length is unknown
1 parent 17e864b commit 794907c

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/gui/qgsfieldvalidator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
5858
}
5959
else if ( mField.precision() > 0 )
6060
{
61-
QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() );
61+
QString re = QString( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
6262
mValidator = new QRegExpValidator( QRegExp( re ), parent );
6363
}
6464
else

src/providers/postgres/qgspostgresprovider.cpp

100644100755
+42-5
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ bool QgsPostgresProvider::loadFields()
909909

910910
int fldtyp = PQftype( result, i );
911911
QString typOid = QString().setNum( fldtyp );
912-
int fieldModifier = PQfmod( result, i );
912+
int fieldPrec = -1;
913913
QString fieldComment( "" );
914914
int tableoid = PQftable( result, i );
915915

@@ -924,13 +924,15 @@ bool QgsPostgresProvider::loadFields()
924924
QString fieldElem = QString::fromUtf8( PQgetvalue( oidResult, 0, 2 ) );
925925
int fieldSize = QString::fromUtf8( PQgetvalue( oidResult, 0, 3 ) ).toInt();
926926

927+
QString formattedFieldType;
927928
if ( tableoid > 0 )
928929
{
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" )
930931
.arg( tableoid ).arg( quotedValue( fieldName ) );
931932

932933
Result tresult = connectionRO->PQexec( sql );
933934
QString attnum = QString::fromUtf8( PQgetvalue( tresult, 0, 0 ) );
935+
formattedFieldType = QString::fromUtf8( PQgetvalue( tresult, 0, 1 ) );
934936

935937
sql = QString( "SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" )
936938
.arg( tableoid ).arg( attnum );
@@ -953,20 +955,41 @@ bool QgsPostgresProvider::loadFields()
953955
{
954956
fieldType = QVariant::LongLong;
955957
fieldSize = -1;
958+
fieldPrec = 0;
956959
}
957960
else if ( fieldTypeName.startsWith( "int" ) ||
958961
fieldTypeName == "serial" )
959962
{
960963
fieldType = QVariant::Int;
961964
fieldSize = -1;
965+
fieldPrec = 0;
962966
}
963967
else if ( fieldTypeName == "real" ||
964968
fieldTypeName == "double precision" ||
965-
fieldTypeName.startsWith( "float" ) ||
966-
fieldTypeName == "numeric" )
969+
fieldTypeName.startsWith( "float" ) )
967970
{
968971
fieldType = QVariant::Double;
969972
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+
}
970993
}
971994
else if ( fieldTypeName == "text" ||
972995
fieldTypeName == "bpchar" ||
@@ -983,6 +1006,20 @@ bool QgsPostgresProvider::loadFields()
9831006
else if ( fieldTypeName == "char" )
9841007
{
9851008
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+
}
9861023
}
9871024
else
9881025
{
@@ -1018,7 +1055,7 @@ bool QgsPostgresProvider::loadFields()
10181055

10191056
fields << fieldName;
10201057

1021-
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldModifier, fieldComment ) );
1058+
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment ) );
10221059
}
10231060

10241061
return true;

0 commit comments

Comments
 (0)