Skip to content

Commit

Permalink
GPKG: make sure gpkg_ogr_contents.feature_count = 0 on a newly create…
Browse files Browse the repository at this point in the history
…d empty table

Fixes OSGeo#11274
  • Loading branch information
rouault committed Nov 14, 2024
1 parent 4dd4d08 commit 0ea26b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,27 @@ def test_ogr_gpkg_6(gpkg_ds, tmp_path):


def test_ogr_gpkg_7(gpkg_ds):
def get_feature_count_from_gpkg_contents():
with gpkg_ds.ExecuteSQL(
'SELECT feature_count FROM gpkg_ogr_contents WHERE table_name = "field_test_layer"',
dialect="DEBUG",
) as sql_lyr:
f = sql_lyr.GetNextFeature()
ret = f.GetField(0)
return ret

lyr = gpkg_ds.CreateLayer("field_test_layer", geom_type=ogr.wkbPoint)
field_defn = ogr.FieldDefn("dummy", ogr.OFTString)
lyr.CreateField(field_defn)

gpkg_ds.SyncToDisk()

assert get_feature_count_from_gpkg_contents() == 0

gpkg_ds = gdaltest.reopen(gpkg_ds, update=1)

assert get_feature_count_from_gpkg_contents() == 0

lyr = gpkg_ds.GetLayerByName("field_test_layer")
geom = ogr.CreateGeometryFromWkt("POINT(10 10)")
feat = ogr.Feature(lyr.GetLayerDefn())
Expand All @@ -642,6 +656,9 @@ def test_ogr_gpkg_7(gpkg_ds):

assert lyr.CreateFeature(feat) == 0, "cannot create feature"

# None is expected here since CreateFeature() has temporarily disabled triggers
assert get_feature_count_from_gpkg_contents() is None

# Read back what we just inserted
lyr.ResetReading()
feat_read = lyr.GetNextFeature()
Expand Down Expand Up @@ -690,34 +707,17 @@ def test_ogr_gpkg_7(gpkg_ds):

assert lyr.GetFeatureCount() == 2

def get_feature_count_from_gpkg_contents():
with gpkg_ds.ExecuteSQL(
'SELECT feature_count FROM gpkg_ogr_contents WHERE table_name = "field_test_layer"',
dialect="DEBUG",
) as sql_lyr:
f = sql_lyr.GetNextFeature()
ret = f.GetField(0)
return ret

assert get_feature_count_from_gpkg_contents() == 2

assert lyr.CreateFeature(ogr.Feature(lyr.GetLayerDefn())) == ogr.OGRERR_NONE

assert lyr.GetFeatureCount() == 3

# 2 is expected here since CreateFeature() has temporarily disable triggers
assert get_feature_count_from_gpkg_contents() == 2

# Test upserting an existing feature
feat.SetField("dummy", "updated")
fid = feat.GetFID()
assert lyr.UpsertFeature(feat) == ogr.OGRERR_NONE, "cannot upsert existing feature"

assert feat.GetFID() == fid

# UpsertFeature() has serialized value 3 and re-enables triggers
assert get_feature_count_from_gpkg_contents() == 3

upserted_feat = lyr.GetFeature(feat.GetFID())
assert (
upserted_feat.GetField("dummy") == "updated"
Expand Down Expand Up @@ -5526,7 +5526,7 @@ def test_ogr_gpkg_test_ogrsf(gpkg_ds):
dbname = gpkg_ds.GetDescription()

# Do integrity check first
gpkg_ds = ogr.Open(dbname)
gpkg_ds = gdaltest.reopen(gpkg_ds)
with gpkg_ds.ExecuteSQL("PRAGMA integrity_check") as sql_lyr:
feat = sql_lyr.GetNextFeature()
assert feat.GetField(0) == "ok", "integrity check failed"
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6034,7 +6034,7 @@ OGRErr OGRGeoPackageTableLayer::RunDeferredCreationIfNecessary()

pszSQL = sqlite3_mprintf(
"INSERT INTO gpkg_ogr_contents (table_name, feature_count) "
"VALUES ('%q', NULL)",
"VALUES ('%q', 0)",
pszLayerName);
err = SQLCommand(m_poDS->GetDB(), pszSQL);
sqlite3_free(pszSQL);
Expand Down

0 comments on commit 0ea26b2

Please # to comment.