@@ -656,25 +656,32 @@ bool QgsPostgresProvider::loadFields()
656
656
657
657
QgsPostgresResult result = connectionRO ()->PQexec ( sql );
658
658
659
- QSet<QString> fields;
660
-
661
- /* Collect type info */
662
- sql = " SELECT oid, typname,typtype,typelem,typlen FROM pg_type" ;
659
+ // Collect type info
660
+ sql = " SELECT oid,typname,typtype,typelem,typlen FROM pg_type" ;
663
661
QgsPostgresResult typeResult = connectionRO ()->PQexec ( sql );
662
+
663
+ struct PGTypeInfo
664
+ {
665
+ QString typeName;
666
+ QString typeType;
667
+ QString typeElem;
668
+ int typeLen;
669
+ };
670
+
664
671
QMap<int , PGTypeInfo> typeMap;
665
672
for ( int i = 0 ; i < typeResult.PQntuples (); ++i )
666
673
{
667
674
PGTypeInfo typeInfo =
668
675
{
669
- /* typeName = */ typeResult.PQgetvalue ( i, 1 ),
670
- /* typeType = */ typeResult.PQgetvalue ( i, 2 ),
671
- /* typeElem = */ typeResult.PQgetvalue ( i, 3 ),
672
- /* typeLen = */ typeResult.PQgetvalue ( i, 4 ).toInt ()
676
+ /* typeName = */ typeResult.PQgetvalue ( i, 1 ),
677
+ /* typeType = */ typeResult.PQgetvalue ( i, 2 ),
678
+ /* typeElem = */ typeResult.PQgetvalue ( i, 3 ),
679
+ /* typeLen = */ typeResult.PQgetvalue ( i, 4 ).toInt ()
673
680
};
674
681
typeMap.insert ( typeResult.PQgetvalue ( i, 0 ).toInt (), typeInfo );
675
682
}
676
683
677
- /* Collect table oids */
684
+ // Collect table oids
678
685
QSet<int > tableoids;
679
686
for ( int i = 0 ; i < result.PQnfields (); i++ )
680
687
{
@@ -692,7 +699,7 @@ bool QgsPostgresProvider::loadFields()
692
699
693
700
QString tableoidsFilter = " (" + tableoidsList.join ( " ," ) + " )" ;
694
701
695
- /* Collect formatted field types */
702
+ // Collect formatted field types
696
703
sql = " SELECT attrelid, attnum, pg_catalog.format_type(atttypid,atttypmod) FROM pg_attribute WHERE attrelid IN " + tableoidsFilter;
697
704
QgsPostgresResult fmtFieldTypeResult = connectionRO ()->PQexec ( sql );
698
705
QMap<int , QMap<int , QString> > fmtFieldTypeMap;
@@ -704,7 +711,7 @@ bool QgsPostgresProvider::loadFields()
704
711
fmtFieldTypeMap[attrelid][attnum] = formatType;
705
712
}
706
713
707
- /* Collect descriptions */
714
+ // Collect descriptions
708
715
sql = " SELECT objoid, objsubid, description FROM pg_description WHERE objoid IN " + tableoidsFilter;
709
716
QgsPostgresResult descrResult = connectionRO ()->PQexec ( sql );
710
717
QMap<int , QMap<int , QString> > descrMap;
@@ -716,6 +723,7 @@ bool QgsPostgresProvider::loadFields()
716
723
descrMap[objoid][objsubid] = descr;
717
724
}
718
725
726
+ QSet<QString> fields;
719
727
mAttributeFields .clear ();
720
728
for ( int i = 0 ; i < result.PQnfields (); i++ )
721
729
{
@@ -1952,6 +1960,7 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes )
1952
1960
{
1953
1961
conn->begin ();
1954
1962
1963
+ QString delim = " " ;
1955
1964
QString sql = QString ( " ALTER TABLE %1 " ).arg ( mQuery );
1956
1965
for ( QList<QgsField>::const_iterator iter = attributes.begin (); iter != attributes.end (); ++iter )
1957
1966
{
@@ -1966,11 +1975,9 @@ bool QgsPostgresProvider::addAttributes( const QList<QgsField> &attributes )
1966
1975
if ( iter->length () > 0 && iter->precision () >= 0 )
1967
1976
type = QString ( " %1(%2,%3)" ).arg ( type ).arg ( iter->length () ).arg ( iter->precision () );
1968
1977
}
1969
- sql.append ( QString ( " ADD COLUMN %1 %2, " ).arg ( quotedIdentifier ( iter->name () ) ).arg ( type ) );
1978
+ sql.append ( QString ( " %1ADD COLUMN %2 %3" ).arg ( delim ).arg ( quotedIdentifier ( iter->name () ) ).arg ( type ) );
1979
+ delim = " ," ;
1970
1980
}
1971
- sql.chop ( 2 ); /* ", " */
1972
- sql.append ( " ;" );
1973
- QgsDebugMsg ( sql );
1974
1981
1975
1982
// send sql statement and do error handling
1976
1983
QgsPostgresResult result = conn->PQexec ( sql );
0 commit comments