Opened 19 years ago
Last modified 18 years ago
#1020 closed defect (fixed)
Postgis DeleteLayer(int iLayer) method
Reported by: | warmerdam | Owned by: | warmerdam |
---|---|---|---|
Priority: | high | Milestone: | |
Component: | OGR_SF | Version: | unspecified |
Severity: | normal | Keywords: | |
Cc: |
Description
From Craig Miller: The existing PostGIS driver didn't have support for deleting a layer via the datasource (OGRDatasource::DeleteLayer(int iLayer). It did have a method for deleting a layer via a name, but this isn't exposed via the C++ API. I quickly adapted that function to the DeleteLayer(int iLayer) method, so there would be an implementation in the driver available. I've done very little testing of the method, and I didn't make very good use of the OGRErr/CPLError mechanism. I only implemented enough error handling to get it to work. Here is the code (Just add it to ogrpgdatasource.cpp and delcare the method in ogr_pg.h): /************************************************************************/ /* DeleteLayer() */ /************************************************************************/ OGRErr OGRPGDataSource::DeleteLayer(int iLayer) { if (iLayer >= nLayers) { CPLDebug( "OGR_PG", "Layer index is out of range."); return OGRERR_FAILURE; // Index out of range error. } if (papoLayers[iLayer] == NULL) { CPLDebug( "OGR_PG", "In memory datasource model is corrupt."); return OGRERR_FAILURE; // Corrupt layer } /* -------------------------------------------------------------------- */ /* Blow away our OGR structures related to the layer. This is */ /* pretty dangerous if anything has a reference to this layer! */ /* -------------------------------------------------------------------- */ CPLDebug( "OGR_PG", "DeleteLayer()" ); char* pszLayerName = strdup(papoLayers[iLayer]->GetLayerDefn()->GetName()); delete papoLayers[iLayer]; memmove( papoLayers + iLayer, papoLayers + iLayer + 1, sizeof(void *) * (nLayers - iLayer - 1) ); nLayers--; /* -------------------------------------------------------------------- */ /* Remove from the database. */ /* -------------------------------------------------------------------- */ PGresult *hResult; char szCommand[1024]; hResult = PQexec(hPGConn, "BEGIN"); PQclear( hResult ); if( bHavePostGIS ) { sprintf( szCommand, "SELECT DropGeometryColumn('%s','%s','wkb_geometry')", pszDBName, pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); } sprintf( szCommand, "DROP TABLE %s", pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult ); hResult = PQexec(hPGConn, "BEGIN"); PQclear( hResult ); sprintf( szCommand, "DROP SEQUENCE %s_ogc_fid_seq", pszLayerName ); CPLDebug( "OGR_PG", "PGexec(%s)", szCommand ); hResult = PQexec( hPGConn, szCommand ); PQclear( hResult ); hResult = PQexec(hPGConn, "COMMIT"); PQclear( hResult ); delete pszLayerName; }
Change History (3)
comment:2 by , 18 years ago
Craig, I have committed changes to OGRPGDataSource to implement DeleteLayer() properly. I am embarrassed to admit I hadn't reviewed this bug report before doing so. In any event, it should now be in there.
comment:3 by , 18 years ago
Frank, In your comments below you indicate that you updated DeleteLayer. Did you also add this code to support DeleteLayer(int i)? Thanks, Craig
Note:
See TracTickets
for help on using tickets.