Skip to content

Commit

Permalink
[gpkg] Implement ALTER TABLE RENAME for rasters (#10213)
Browse files Browse the repository at this point in the history
Fix #10201
  • Loading branch information
elpaso committed Jun 18, 2024
1 parent 8979e64 commit 5818dea
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 95 deletions.
50 changes: 50 additions & 0 deletions autotest/gdrivers/gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4298,3 +4298,53 @@ def test_gpkg_gti_gpkg_ext(tmp_vsimem):
ds = gdal.Open(filename)
assert ds.GetDriver().ShortName == "GPKG"
assert ds.GetRasterBand(1).Checksum() == 4672


###############################################################################
# Test rename a raster table with SQL


@pytest.mark.parametrize("data_type", [gdal.GDT_Byte, gdal.GDT_UInt16])
def test_gpkg_rename_raster_table(data_type, tmp_vsimem):

test_layer_path = str(tmp_vsimem / "test_gpkg_rename_raster_table.gpkg")

if data_type == gdal.GDT_UInt16:
src_ds = gdal.Open("data/int16.tif")
else:
src_ds = gdal.Open("data/small_world.tif")

ds = gdaltest.gpkg_dr.CreateCopy(
test_layer_path,
src_ds,
options=[
"TILE_FORMAT=PNG",
"RASTER_TABLE=weird'layer\"name",
],
)
ds = None
src_ds = None

ds = gdal.OpenEx(test_layer_path, gdal.OF_RASTER | gdal.OF_UPDATE)
# Get layer name
layer_name = ds.GetMetadataItem("IDENTIFIER")
assert layer_name == "weird'layer\"name"

checksum = ds.GetRasterBand(1).Checksum()

ds.ExecuteSQL('ALTER TABLE "weird\'layer""name" RENAME TO bar')
ds.ExecuteSQL("VACUUM")
ds = None

ds = gdal.Open(test_layer_path, gdal.OF_RASTER)
layer_name = ds.GetMetadataItem("IDENTIFIER")
assert layer_name == "bar"
assert ds.GetRasterBand(1).Checksum() == checksum
ds = None

# Check that there is no more any reference to the layer
f = gdal.VSIFOpenL(test_layer_path, "rb")
content = gdal.VSIFReadL(1, 1000000, f).decode("latin1")
gdal.VSIFCloseL(f)

assert "weird" not in content
39 changes: 39 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10510,3 +10510,42 @@ def test_ogr_gpkg_launder(tmp_vsimem):
assert lyr.GetGeometryColumn() == "my_geom"
lyr.CreateField(ogr.FieldDefn("_"))
assert lyr.GetLayerDefn().GetFieldDefn(0).GetNameRef() == "x_"


###############################################################################
# Test rename a "hidden" table with SQL


def test_gpkg_rename_hidden_table(tmp_vsimem):

test_layer_path = str(tmp_vsimem / "test_gpkg_rename_hidden_table.gpkg")

src_ds = gdal.OpenEx("../ogr/data/poly.shp")
gdal.VectorTranslate(test_layer_path, src_ds)
src_ds = None

dst_ds = gdal.OpenEx(
test_layer_path, gdal.OF_UPDATE, open_options=["LIST_ALL_TABLES=NO"]
)
dst_ds.ExecuteSQL("CREATE TABLE hidden_foo_table(id integer primary key);")
dst_ds = None

dst_ds = gdal.OpenEx(
test_layer_path, gdal.OF_UPDATE, open_options=["LIST_ALL_TABLES=NO"]
)
dst_ds.ExecuteSQL("ALTER TABLE hidden_foo_table RENAME TO hidden_bar_table")
dst_ds.ExecuteSQL("VACUUM")
dst_ds = None

dst_ds = gdal.OpenEx(test_layer_path)
# Verify that layer exists
lyr = dst_ds.GetLayerByName("hidden_bar_table")
assert lyr is not None
dst_ds = None

# Check that there is no more any reference to the layer
f = gdal.VSIFOpenL(test_layer_path, "rb")
content = gdal.VSIFReadL(1, 1000000, f).decode("latin1")
gdal.VSIFCloseL(f)

assert "hidden_foo_table" not in content
5 changes: 5 additions & 0 deletions ogr/ogrsf_frmts/gpkg/ogr_geopackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ class GDALGeoPackageDataset final : public OGRSQLiteBaseDataSource,
OGRErr DeleteLayerCommon(const char *pszLayerName);
OGRErr DeleteRasterLayer(const char *pszLayerName);
bool DeleteVectorOrRasterLayer(const char *pszLayerName);
bool RenameVectorOrRasterLayer(const char *pszLayerName,
const char *pszNewName);
bool RenameRasterLayer(const char *pszLayerName,
const char *pszNewLayerName);

bool ConvertGpkgSpatialRefSysToExtensionWkt2(bool bForceEpoch);
void DetectSpatialRefSysColumns();
Expand Down Expand Up @@ -477,6 +481,7 @@ class GDALGeoPackageDataset final : public OGRSQLiteBaseDataSource,
bool OpenOrCreateDB(int flags);
void InstallSQLFunctions();
bool HasGDALAspatialExtension();
std::string CreateRasterTriggersSQL(const std::string &osTableName);
};

/************************************************************************/
Expand Down
Loading

0 comments on commit 5818dea

Please sign in to comment.