mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-25 00:58:06 -05:00
[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:
parent
17e864bb99
commit
794907cbe0
@ -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
47
src/providers/postgres/qgspostgresprovider.cpp
Normal file → Executable 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user