Skip to content
Permalink
Browse files

[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
  • Loading branch information
jef-n committed Jul 15, 2011
1 parent 17e864b commit 794907cbe0a69fcb2a29af4b66e5b5a455c84994
Showing with 43 additions and 6 deletions.
  1. +1 −1 src/gui/qgsfieldvalidator.cpp
  2. +42 −5 src/providers/postgres/qgspostgresprovider.cpp
@@ -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
@@ -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;

0 comments on commit 794907c

Please sign in to comment.
You can’t perform that action at this time.