Skip to content

Commit

Permalink
postgres provider: use default values for primary key
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9382 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Sep 23, 2008
1 parent fc11ff6 commit 840930c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
63 changes: 40 additions & 23 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1612,34 +1612,38 @@ bool QgsPostgresProvider::isValid()
return valid;
}

QVariant QgsPostgresProvider::getDefaultValue( int fieldId )
QVariant QgsPostgresProvider::getDefaultValue( QString fieldName )
{
try
{
// Get the default column value from the Postgres information
// schema. If there is no default we return an empty string.
// Get the default column value from the Postgres information
// schema. If there is no default we return an empty string.

// Maintaining a cache of the results of this query would be quite
// simple and if this query is called lots, could save some time.
QString fieldName = field( fieldId ).name();
// Maintaining a cache of the results of this query would be quite
// simple and if this query is called lots, could save some time.

QString sql( "SELECT column_default FROM"
" information_schema.columns WHERE"
" column_default IS NOT NULL"
" AND table_schema = " + quotedValue( mSchemaName ) +
" AND table_name = " + quotedValue( mTableName ) +
" AND column_name = " + quotedValue( fieldName ) );
QString sql( "SELECT column_default FROM"
" information_schema.columns WHERE"
" column_default IS NOT NULL"
" AND table_schema = " + quotedValue( mSchemaName ) +
" AND table_name = " + quotedValue( mTableName ) +
" AND column_name = " + quotedValue( fieldName ) );

QVariant defaultValue( QString::null );
QVariant defaultValue( QString::null );

Result result = connectionRO->PQexec( sql );
Result result = connectionRO->PQexec( sql );

if ( PQntuples( result ) == 1 && !PQgetisnull( result, 0, 0 ) )
defaultValue = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );

if ( PQntuples( result ) == 1 && !PQgetisnull( result, 0, 0 ) )
defaultValue = QString::fromUtf8( PQgetvalue( result, 0, 0 ) );
// QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );

// QgsDebugMsg( QString("defaultValue for %1 is NULL: %2").arg(fieldId).arg( defaultValue.isNull() ) );
return defaultValue;
}

return defaultValue;
QVariant QgsPostgresProvider::getDefaultValue( int fieldId )
{
try
{
return getDefaultValue( field( fieldId ).name() );
}
catch ( PGFieldNotFound )
{
Expand Down Expand Up @@ -1820,7 +1824,10 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList & flist )
throw PGException( stmt );
PQclear( stmt );

int primaryKeyHighWater = maxPrimaryKeyValue();
QString keyDefault = getDefaultValue( primaryKey ).toString();
int primaryKeyHighWater = -1;
if ( keyDefault.isNull() )
primaryKeyHighWater = maxPrimaryKeyValue();
QList<int> newIds;

for ( QgsFeatureList::iterator features = flist.begin(); features != flist.end(); features++ )
Expand All @@ -1832,9 +1839,19 @@ bool QgsPostgresProvider::addFeatures( QgsFeatureList & flist )

QStringList params;
params << geomParam;
params << QString( "%1" ).arg( ++primaryKeyHighWater );

newIds << primaryKeyHighWater;
if ( keyDefault.isNull() )
{
++primaryKeyHighWater;
params << QString::number( primaryKeyHighWater );
newIds << primaryKeyHighWater;
}
else
{
QByteArray key = paramValue( keyDefault, keyDefault );
params << key;
newIds << key.toInt();
}

for ( int i = 0; i < fieldId.size(); i++ )
params << paramValue( attributevec[ fieldId[i] ].toString(), defaultValue[i] );
Expand Down
3 changes: 3 additions & 0 deletions src/providers/postgres/qgspostgresprovider.h
Expand Up @@ -197,6 +197,9 @@ class QgsPostgresProvider : public QgsVectorDataProvider

QgsAttributeList allAttributesList();

/**Returns the default value for field specified by @c fieldName */
QVariant getDefaultValue( QString fieldName );

/**Returns the default value for field specified by @c fieldId */
QVariant getDefaultValue( int fieldId );

Expand Down

0 comments on commit 840930c

Please sign in to comment.