Skip to content

Commit 2e3db0c

Browse files
committed
allow PG provider to delete a layer
1 parent 3e6ea5c commit 2e3db0c

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+76-3
Original file line numberDiff line numberDiff line change
@@ -2934,9 +2934,9 @@ QgsVectorLayerImport::ImportError QgsPostgresProvider::createEmptyLayer(
29342934
}
29352935
schemaTableName += quotedIdentifier( tableName );
29362936

2937-
QgsDebugMsg( QString( "Connection info is " ).arg( dsUri.connectionInfo() ) );
2938-
QgsDebugMsg( QString( "Geometry column is: " ).arg( geometryColumn ) );
2939-
QgsDebugMsg( QString( "Schema is: " ).arg( schemaName ) );
2937+
QgsDebugMsg( QString( "Connection info is: %1" ).arg( dsUri.connectionInfo() ) );
2938+
QgsDebugMsg( QString( "Geometry column is: %1" ).arg( geometryColumn ) );
2939+
QgsDebugMsg( QString( "Schema is: %1" ).arg( schemaName ) );
29402940
QgsDebugMsg( QString( "Table name is: %1" ).arg( tableName ) );
29412941

29422942
// create the table
@@ -3248,3 +3248,76 @@ QGISEXTERN QgsVectorLayerImport::ImportError createEmptyLayer(
32483248
oldToNewAttrIdxMap, errorMessage, options
32493249
);
32503250
}
3251+
3252+
QGISEXTERN bool deleteLayer( const QString& uri, QString& errCause )
3253+
{
3254+
QgsDebugMsg( "deleting layer " + uri );
3255+
3256+
QgsDataSourceURI dsUri( uri );
3257+
QString schemaName = dsUri.schema();
3258+
QString tableName = dsUri.table();
3259+
QString geometryCol = dsUri.geometryColumn();
3260+
3261+
QString schemaTableName;
3262+
if ( !schemaName.isEmpty() )
3263+
{
3264+
schemaTableName = QgsPostgresConn::quotedIdentifier( schemaName ) + ".";
3265+
}
3266+
schemaTableName += QgsPostgresConn::quotedIdentifier( tableName );
3267+
3268+
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
3269+
if ( !conn )
3270+
{
3271+
errCause = QObject::tr( "Connection to database failed" );
3272+
return false;
3273+
}
3274+
3275+
// check the geometry column count
3276+
QString sql = QString( "SELECT count(*) "
3277+
"FROM geometry_columns, pg_class, pg_namespace "
3278+
"WHERE f_table_name=relname AND f_table_schema=nspname "
3279+
"AND pg_class.relnamespace=pg_namespace.oid "
3280+
"AND f_table_schema=%1 AND f_table_name=%2" )
3281+
.arg( QgsPostgresConn::quotedValue( schemaName ) )
3282+
.arg( QgsPostgresConn::quotedValue( tableName ) );
3283+
QgsPostgresResult result = conn->PQexec( sql );
3284+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
3285+
{
3286+
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
3287+
.arg( schemaTableName )
3288+
.arg( result.PQresultErrorMessage() );
3289+
conn->disconnect();
3290+
return false;
3291+
}
3292+
3293+
int count = result.PQgetvalue( 0, 0 ).toInt();
3294+
3295+
if ( !geometryCol.isEmpty() && count > 1 )
3296+
{
3297+
// the table has more geometry columns, drop just the geometry column
3298+
sql = QString( "SELECT DropGeometryColumn(%1,%2,%3)" )
3299+
.arg( QgsPostgresConn::quotedValue( schemaName ) )
3300+
.arg( QgsPostgresConn::quotedValue( tableName ) )
3301+
.arg( QgsPostgresConn::quotedValue( geometryCol ) );
3302+
}
3303+
else
3304+
{
3305+
// drop the table
3306+
sql = QString( "SELECT DropGeometryTable(%1,%2)" )
3307+
.arg( QgsPostgresConn::quotedValue( schemaName ) )
3308+
.arg( QgsPostgresConn::quotedValue( tableName ) );
3309+
}
3310+
3311+
result = conn->PQexec( sql );
3312+
if ( result.PQresultStatus() != PGRES_TUPLES_OK )
3313+
{
3314+
errCause = QObject::tr( "Unable to delete layer %1: \n%2" )
3315+
.arg( schemaTableName )
3316+
.arg( result.PQresultErrorMessage() );
3317+
conn->disconnect();
3318+
return false;
3319+
}
3320+
3321+
conn->disconnect();
3322+
return true;
3323+
}

0 commit comments

Comments
 (0)