Skip to content

Commit

Permalink
allow PG provider to delete a layer
Browse files Browse the repository at this point in the history
  • Loading branch information
brushtyler committed Apr 15, 2012
1 parent 3e6ea5c commit 2e3db0c
Showing 1 changed file with 76 additions and 3 deletions.
79 changes: 76 additions & 3 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -2934,9 +2934,9 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
}
schemaTableName += quotedIdentifier( tableName );

QgsDebugMsg( QString( "Connection info is " ).arg( dsUri.connectionInfo() ) );
QgsDebugMsg( QString( "Geometry column is: " ).arg( geometryColumn ) );
QgsDebugMsg( QString( "Schema is: " ).arg( schemaName ) );
QgsDebugMsg( QString( "Connection info is: %1" ).arg( dsUri.connectionInfo() ) );
QgsDebugMsg( QString( "Geometry column is: %1" ).arg( geometryColumn ) );
QgsDebugMsg( QString( "Schema is: %1" ).arg( schemaName ) );
QgsDebugMsg( QString( "Table name is: %1" ).arg( tableName ) );

// create the table
Expand Down Expand Up @@ -3248,3 +3248,76 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
oldToNewAttrIdxMap, errorMessage, options
);
}

QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )
{
QgsDebugMsg( "deleting layer " + uri );

QgsDataSourceURI dsUri( uri );
QString schemaName = dsUri.schema();
QString tableName = dsUri.table();
QString geometryCol = dsUri.geometryColumn();

QString schemaTableName;
if ( !schemaName.isEmpty() )
{
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + ".";
}
schemaTableName += QgsPostgresConn::quotedIdentifier( tableName );

QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return false;
}

// check the geometry column count
QString sql = QString( "SELECT count(*) "
"FROM geometry_columns, pg_class, pg_namespace "
"WHERE f_table_name=relname AND f_table_schema=nspname "
"AND pg_class.relnamespace=pg_namespace.oid "
"AND f_table_schema=%1 AND f_table_name=%2" )
.arg( QgsPostgresConn::quotedValue( schemaName ) )
.arg( QgsPostgresConn::quotedValue( tableName ) );
QgsPostgresResult result = conn->PQexec( sql );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
.arg( schemaTableName )
.arg( result.PQresultErrorMessage() );
conn->disconnect();
return false;
}

int count = result.PQgetvalue( 0, 0 ).toInt();

if ( !geometryCol.isEmpty() && count > 1 )
{
// the table has more geometry columns, drop just the geometry column
sql = QString( "SELECT DropGeometryColumn(%1,%2,%3)" )
.arg( QgsPostgresConn::quotedValue( schemaName ) )
.arg( QgsPostgresConn::quotedValue( tableName ) )
.arg( QgsPostgresConn::quotedValue( geometryCol ) );
}
else
{
// drop the table
sql = QString( "SELECT DropGeometryTable(%1,%2)" )
.arg( QgsPostgresConn::quotedValue( schemaName ) )
.arg( QgsPostgresConn::quotedValue( tableName ) );
}

result = conn->PQexec( sql );
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
{
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
.arg( schemaTableName )
.arg( result.PQresultErrorMessage() );
conn->disconnect();
return false;
}

conn->disconnect();
return true;
}

0 comments on commit 2e3db0c

Please sign in to comment.