@@ -1612,34 +1612,38 @@ bool QgsPostgresProvider::isValid()
16121612 return valid;
16131613}
16141614
1615- QVariant QgsPostgresProvider::getDefaultValue ( int fieldId )
1615+ QVariant QgsPostgresProvider::getDefaultValue ( QString fieldName )
16161616{
1617- try
1618- {
1619- // Get the default column value from the Postgres information
1620- // schema. If there is no default we return an empty string.
1617+ // Get the default column value from the Postgres information
1618+ // schema. If there is no default we return an empty string.
16211619
1622- // Maintaining a cache of the results of this query would be quite
1623- // simple and if this query is called lots, could save some time.
1624- QString fieldName = field ( fieldId ).name ();
1620+ // Maintaining a cache of the results of this query would be quite
1621+ // simple and if this query is called lots, could save some time.
16251622
1626- QString sql ( " SELECT column_default FROM"
1627- " information_schema.columns WHERE"
1628- " column_default IS NOT NULL"
1629- " AND table_schema = " + quotedValue ( mSchemaName ) +
1630- " AND table_name = " + quotedValue ( mTableName ) +
1631- " AND column_name = " + quotedValue ( fieldName ) );
1623+ QString sql ( " SELECT column_default FROM"
1624+ " information_schema.columns WHERE"
1625+ " column_default IS NOT NULL"
1626+ " AND table_schema = " + quotedValue ( mSchemaName ) +
1627+ " AND table_name = " + quotedValue ( mTableName ) +
1628+ " AND column_name = " + quotedValue ( fieldName ) );
16321629
1633- QVariant defaultValue ( QString::null );
1630+ QVariant defaultValue ( QString::null );
16341631
1635- Result result = connectionRO->PQexec ( sql );
1632+ Result result = connectionRO->PQexec ( sql );
1633+
1634+ if ( PQntuples ( result ) == 1 && !PQgetisnull ( result, 0 , 0 ) )
1635+ defaultValue = QString::fromUtf8 ( PQgetvalue ( result, 0 , 0 ) );
16361636
1637- if ( PQntuples ( result ) == 1 && !PQgetisnull ( result, 0 , 0 ) )
1638- defaultValue = QString::fromUtf8 ( PQgetvalue ( result, 0 , 0 ) );
1637+ // QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );
16391638
1640- // QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );
1639+ return defaultValue;
1640+ }
16411641
1642- return defaultValue;
1642+ QVariant QgsPostgresProvider::getDefaultValue ( int fieldId )
1643+ {
1644+ try
1645+ {
1646+ return getDefaultValue ( field ( fieldId ).name () );
16431647 }
16441648 catch ( PGFieldNotFound )
16451649 {
@@ -1820,7 +1824,10 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList & flist )
18201824 throw PGException ( stmt );
18211825 PQclear ( stmt );
18221826
1823- int primaryKeyHighWater = maxPrimaryKeyValue ();
1827+ QString keyDefault = getDefaultValue ( primaryKey ).toString ();
1828+ int primaryKeyHighWater = -1 ;
1829+ if ( keyDefault.isNull () )
1830+ primaryKeyHighWater = maxPrimaryKeyValue ();
18241831 QList<int > newIds;
18251832
18261833 for ( QgsFeatureList::iterator features = flist.begin (); features != flist.end (); features++ )
@@ -1832,9 +1839,19 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList & flist )
18321839
18331840 QStringList params;
18341841 params << geomParam;
1835- params << QString ( " %1" ).arg ( ++primaryKeyHighWater );
18361842
1837- newIds << primaryKeyHighWater;
1843+ if ( keyDefault.isNull () )
1844+ {
1845+ ++primaryKeyHighWater;
1846+ params << QString::number ( primaryKeyHighWater );
1847+ newIds << primaryKeyHighWater;
1848+ }
1849+ else
1850+ {
1851+ QByteArray key = paramValue ( keyDefault, keyDefault );
1852+ params << key;
1853+ newIds << key.toInt ();
1854+ }
18381855
18391856 for ( int i = 0 ; i < fieldId.size (); i++ )
18401857 params << paramValue ( attributevec[ fieldId[i] ].toString (), defaultValue[i] );
0 commit comments