Skip to content

Commit

Permalink
[BACKPORT] fix #4089
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jef-n committed Jul 15, 2011
1 parent 17e864b commit 794907c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/gui/qgsfieldvalidator.cpp
Expand Up @@ -58,7 +58,7 @@ QgsFieldValidator::QgsFieldValidator( QObject *parent, const QgsField &field )
}
else if ( mField.precision() > 0 )
{
QString re = QString( "-?\\d*(\\.\\d{0,%1))?" ).arg( mField.precision() );
QString re = QString( "-?\\d*(\\.\\d{0,%1})?" ).arg( mField.precision() );
mValidator = new QRegExpValidator( QRegExp( re ), parent );
}
else
Expand Down
47 changes: 42 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp 100644 → 100755
Expand Up @@ -909,7 +909,7 @@ bool QgsPostgresProvider::loadFields()

int fldtyp = PQftype( result, i );
QString typOid = QString().setNum( fldtyp );
int fieldModifier = PQfmod( result, i );
int fieldPrec = -1;
QString fieldComment( "" );
int tableoid = PQftable( result, i );

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

QString formattedFieldType;
if ( tableoid > 0 )
{
sql = QString( "SELECT attnum FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
sql = QString( "SELECT attnum,pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid=%1 AND attname=%2" )
.arg( tableoid ).arg( quotedValue( fieldName ) );

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

sql = QString( "SELECT description FROM pg_description WHERE objoid=%1 AND objsubid=%2" )
.arg( tableoid ).arg( attnum );
Expand All @@ -953,20 +955,41 @@ bool QgsPostgresProvider::loadFields()
{
fieldType = QVariant::LongLong;
fieldSize = -1;
fieldPrec = 0;
}
else if ( fieldTypeName.startsWith( "int" ) ||
fieldTypeName == "serial" )
{
fieldType = QVariant::Int;
fieldSize = -1;
fieldPrec = 0;
}
else if ( fieldTypeName == "real" ||
fieldTypeName == "double precision" ||
fieldTypeName.startsWith( "float" ) ||
fieldTypeName == "numeric" )
fieldTypeName.startsWith( "float" ) )
{
fieldType = QVariant::Double;
fieldSize = -1;
fieldPrec = 0;
}
else if ( fieldTypeName == "numeric" )
{
fieldType = QVariant::Double;

QRegExp re( "numeric\\((\\d+),(\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) && re.captureCount() == 2 )
{
fieldSize = re.cap( 1 ).toInt();
fieldPrec = re.cap( 2 ).toInt();
}
else
{
QgsDebugMsg( QString( "unexpected formatted field type '%1' for field %2" )
.arg( formattedFieldType )
.arg( fieldName ) );
fieldSize = -1;
fieldPrec = -1;
}
}
else if ( fieldTypeName == "text" ||
fieldTypeName == "bpchar" ||
Expand All @@ -983,6 +1006,20 @@ bool QgsPostgresProvider::loadFields()
else if ( fieldTypeName == "char" )
{
fieldType = QVariant::String;

QRegExp re( "char\\((\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) && re.captureCount() == 1 )
{
fieldSize = re.cap( 1 ).toInt();
}
else
{
QgsDebugMsg( QString( "unexpected formatted field type '%1' for field %2" )
.arg( formattedFieldType )
.arg( fieldName ) );
fieldSize = -1;
fieldPrec = -1;
}
}
else
{
Expand Down Expand Up @@ -1018,7 +1055,7 @@ bool QgsPostgresProvider::loadFields()

fields << fieldName;

attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldModifier, fieldComment ) );
attributeFields.insert( i, QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment ) );
}

return true;
Expand Down

0 comments on commit 794907c

Please sign in to comment.