Skip to content

Commit

Permalink
GPKG: prevent from creating field with same name, or with the name of…
Browse files Browse the repository at this point in the history
… the geometry field
  • Loading branch information
rouault committed Oct 20, 2024
1 parent dea0d97 commit 3d06180
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
28 changes: 28 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10664,3 +10664,31 @@ def test_gpkg_rename_hidden_table(tmp_vsimem):
gdal.VSIFCloseL(f)

assert "hidden_foo_table" not in content


###############################################################################
# Test creating duplicate field names


@gdaltest.enable_exceptions()
def test_gpkg_create_duplicate_field_names(tmp_vsimem):

filename = str(tmp_vsimem / "test_gpkg_create_duplicate_field_names.gpkg")
with ogr.GetDriverByName("GPKG").CreateDataSource(filename) as ds:
lyr = ds.CreateLayer("test")
lyr.CreateField(ogr.FieldDefn("foo"))
with pytest.raises(
Exception, match="A field with the same name already exists"
):
lyr.CreateField(ogr.FieldDefn("foo"))
assert lyr.GetLayerDefn().GetFieldCount() == 1
with pytest.raises(
Exception, match="A field with the same name already exists"
):
lyr.CreateField(ogr.FieldDefn("FOO"))
assert lyr.GetLayerDefn().GetFieldCount() == 1
with pytest.raises(
Exception, match="It has the same name as the geometry field"
):
lyr.CreateField(ogr.FieldDefn("geom"))
assert lyr.GetLayerDefn().GetFieldCount() == 1
18 changes: 18 additions & 0 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,24 @@ OGRErr OGRGeoPackageTableLayer::CreateField(const OGRFieldDefn *poField,
GDALGeoPackageDataset::LaunderName(oFieldDefn.GetNameRef())
.c_str());

if (m_poFeatureDefn->GetFieldIndex(oFieldDefn.GetNameRef()) >= 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot create field %s. "
"A field with the same name already exists.",
oFieldDefn.GetNameRef());
return OGRERR_FAILURE;
}

if (m_poFeatureDefn->GetGeomFieldIndex(oFieldDefn.GetNameRef()) >= 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot create field %s. "
"It has the same name as the geometry field.",
oFieldDefn.GetNameRef());
return OGRERR_FAILURE;
}

if (m_pszFidColumn != nullptr &&
EQUAL(oFieldDefn.GetNameRef(), m_pszFidColumn) &&
poField->GetType() != OFTInteger &&
Expand Down

0 comments on commit 3d06180

Please sign in to comment.