[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
This commit is contained in:
Juergen E. Fischer 2011-07-15 16:17:47 +02:00
parent 17e864bb99
commit 794907cbe0
2 changed files with 43 additions and 6 deletions

View File

@ -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

47
src/providers/postgres/qgspostgresprovider.cpp Normal file → Executable file
View File

@ -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 );
@ -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 );
@ -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" ||
@ -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
{
@ -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;