@@ -682,46 +682,45 @@ bool QgsPostgresProvider::loadFields()
682
682
typeMap.insert ( typeResult.PQgetvalue ( i, 0 ).toInt (), typeInfo );
683
683
}
684
684
685
- // Collect table oids
686
- QSet <int > tableoids ;
687
- for ( int i = 0 ; i < result.PQnfields (); i++ )
685
+
686
+ QMap <int , QMap< int , QString> > fmtFieldTypeMap, descrMap, defValMap ;
687
+ if ( result.PQnfields () > 0 )
688
688
{
689
- int tableoid = result.PQftable ( i );
690
- if ( tableoid > 0 )
689
+ // Collect table oids
690
+ QSet<int > tableoids;
691
+ for ( int i = 0 ; i < result.PQnfields (); i++ )
691
692
{
692
- tableoids.insert ( tableoid );
693
+ int tableoid = result.PQftable ( i );
694
+ if ( tableoid > 0 )
695
+ {
696
+ tableoids.insert ( tableoid );
697
+ }
698
+ }
699
+ QStringList tableoidsList;
700
+ foreach ( int tableoid, tableoids )
701
+ {
702
+ tableoidsList.append ( QString::number ( tableoid ) );
693
703
}
694
- }
695
- QStringList tableoidsList;
696
- foreach ( int tableoid, tableoids )
697
- {
698
- tableoidsList.append ( QString::number ( tableoid ) );
699
- }
700
-
701
- QString tableoidsFilter = " (" + tableoidsList.join ( " ," ) + " )" ;
702
704
703
- // Collect formatted field types
704
- sql = " SELECT attrelid, attnum, pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid IN " + tableoidsFilter;
705
- QgsPostgresResult fmtFieldTypeResult = connectionRO ()->PQexec ( sql );
706
- QMap<int , QMap<int , QString> > fmtFieldTypeMap;
707
- for ( int i = 0 ; i < fmtFieldTypeResult.PQntuples (); ++i )
708
- {
709
- int attrelid = fmtFieldTypeResult.PQgetvalue ( i, 0 ).toInt ();
710
- int attnum = fmtFieldTypeResult.PQgetvalue ( i, 1 ).toInt ();
711
- QString formatType = fmtFieldTypeResult.PQgetvalue ( i, 2 );
712
- fmtFieldTypeMap[attrelid][attnum] = formatType;
713
- }
705
+ QString tableoidsFilter = " (" + tableoidsList.join ( " ," ) + " )" ;
714
706
715
- // Collect descriptions
716
- sql = " SELECT objoid, objsubid, description FROM pg_description WHERE objoid IN " + tableoidsFilter;
717
- QgsPostgresResult descrResult = connectionRO ()->PQexec ( sql );
718
- QMap<int , QMap<int , QString> > descrMap;
719
- for ( int i = 0 ; i < descrResult.PQntuples (); ++i )
720
- {
721
- int objoid = descrResult.PQgetvalue ( i, 0 ).toInt ();
722
- int objsubid = descrResult.PQgetvalue ( i, 1 ).toInt ();
723
- QString descr = descrResult.PQgetvalue ( i, 2 );
724
- descrMap[objoid][objsubid] = descr;
707
+ // Collect formatted field types
708
+ sql = " SELECT attrelid, attnum, pg_catalog.format_type(atttypid,atttypmod), pg_catalog.col_description(attrelid,attnum), adsrc"
709
+ " FROM pg_attribute"
710
+ " LEFT OUTER JOIN pg_attrdef ON attrelid=adrelid AND attnum=adnum"
711
+ " WHERE attrelid IN " + tableoidsFilter;
712
+ QgsPostgresResult fmtFieldTypeResult = connectionRO ()->PQexec ( sql );
713
+ for ( int i = 0 ; i < fmtFieldTypeResult.PQntuples (); ++i )
714
+ {
715
+ int attrelid = fmtFieldTypeResult.PQgetvalue ( i, 0 ).toInt ();
716
+ int attnum = fmtFieldTypeResult.PQgetvalue ( i, 1 ).toInt ();
717
+ QString formatType = fmtFieldTypeResult.PQgetvalue ( i, 2 );
718
+ QString descr = fmtFieldTypeResult.PQgetvalue ( i, 3 );
719
+ QString defVal = fmtFieldTypeResult.PQgetvalue ( i, 4 );
720
+ fmtFieldTypeMap[attrelid][attnum] = formatType;
721
+ descrMap[attrelid][attnum] = descr;
722
+ defValMap[attrelid][attnum] = defVal;
723
+ }
725
724
}
726
725
727
726
QSet<QString> fields;
@@ -907,6 +906,7 @@ bool QgsPostgresProvider::loadFields()
907
906
908
907
mAttrPalIndexName .insert ( i, fieldName );
909
908
mAttributeFields .append ( QgsField ( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment ) );
909
+ mDefaultValues .insert ( i, defValMap[tableoid][attnum] );
910
910
}
911
911
912
912
return true ;
@@ -1475,44 +1475,9 @@ bool QgsPostgresProvider::isValid()
1475
1475
return mValid ;
1476
1476
}
1477
1477
1478
- QVariant QgsPostgresProvider::defaultValue ( QString fieldName, QString tableName, QString schemaName )
1479
- {
1480
- if ( schemaName.isNull () )
1481
- schemaName = mSchemaName ;
1482
- if ( tableName.isNull () )
1483
- tableName = mTableName ;
1484
-
1485
- // Get the default column value from the Postgres information
1486
- // schema. If there is no default we return an empty string.
1487
-
1488
- // Maintaining a cache of the results of this query would be quite
1489
- // simple and if this query is called lots, could save some time.
1490
-
1491
- QString sql = QString ( " SELECT column_default FROM information_schema.columns WHERE column_default IS NOT NULL AND table_schema=%1 AND table_name=%2 AND column_name=%3 " )
1492
- .arg ( quotedValue ( schemaName ) )
1493
- .arg ( quotedValue ( tableName ) )
1494
- .arg ( quotedValue ( fieldName ) );
1495
-
1496
- QVariant defaultValue ( QString::null );
1497
-
1498
- QgsPostgresResult result = connectionRO ()->PQexec ( sql );
1499
-
1500
- if ( result.PQntuples () == 1 )
1501
- defaultValue = result.PQgetvalue ( 0 , 0 );
1502
-
1503
- return defaultValue;
1504
- }
1505
-
1506
1478
QVariant QgsPostgresProvider::defaultValue ( int fieldId )
1507
1479
{
1508
- try
1509
- {
1510
- return defaultValue ( field ( fieldId ).name () );
1511
- }
1512
- catch ( PGFieldNotFound )
1513
- {
1514
- return QVariant ( QString::null );
1515
- }
1480
+ return mDefaultValues .value ( fieldId, QString::null );
1516
1481
}
1517
1482
1518
1483
QString QgsPostgresProvider::paramValue ( QString fieldValue, const QString &defaultValue ) const
0 commit comments