Skip to content

Commit

Permalink
Merge pull request #9281 from OSGeo/backport-9278-to-release/3.8
Browse files Browse the repository at this point in the history
[Backport release/3.8] [gpkg] Ensure that tables present in gpkgext_relations can be read
  • Loading branch information
rouault authored Feb 22, 2024
2 parents 43b77c0 + 8a3337f commit 0eb4293
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6951,6 +6951,13 @@ def test_ogr_gpkg_relations(tmp_vsimem, tmp_path):
assert rel.GetRightMappingTableFields() == ["related_id"]
assert rel.GetRelatedTableType() == "attributes"

# ensure that the mapping table, which is present in gpkgext_relations but
# NOT gpkg_contents can be opened as a layer
lyr = ds.GetLayer("my_mapping_table")
assert lyr is not None
assert lyr.GetLayerDefn().GetFieldDefn(0).GetName() == "base_id"
assert lyr.GetLayerDefn().GetFieldDefn(1).GetName() == "related_id"

lyr = ds.GetLayer("a")
lyr.Rename("a_renamed")
lyr.AlterFieldDefn(
Expand Down Expand Up @@ -7212,6 +7219,21 @@ def get_query_row_count(query):
)
== 1
)
# force delete from gpkg_contents, and then ensure that we CAN successfully
# load layers which are present ONLY in gpkgext_relations but NOT
# gpkg_contents (i.e. datasources which follow the Related Tables specification
# exactly)
ds.ExecuteSQL(
"DELETE FROM gpkg_contents WHERE table_name='origin_table_dest_table'"
)

ds = gdal.OpenEx(filename, gdal.OF_VECTOR | gdal.OF_UPDATE)
# ensure that the mapping table, which is present in gpkgext_relations but
# NOT gpkg_contents can be opened as a layer
lyr = ds.GetLayer("origin_table_dest_table")
assert lyr is not None
assert lyr.GetLayerDefn().GetFieldDefn(0).GetName() == "base_id"
assert lyr.GetLayerDefn().GetFieldDefn(1).GetName() == "related_id"

lyr = ds.CreateLayer("origin_table2", geom_type=ogr.wkbNone)
fld_defn = ogr.FieldDefn("o_pkey", ogr.OFTInteger)
Expand Down
20 changes: 20 additions & 0 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ int GDALGeoPackageDataset::Open(GDALOpenInfo *poOpenInfo,
CheckUnknownExtensions();

int bRet = FALSE;
bool bHasGPKGExtRelations = false;
if (poOpenInfo->nOpenFlags & GDAL_OF_VECTOR)
{
m_bHasGPKGGeometryColumns =
Expand All @@ -1606,6 +1607,7 @@ int GDALGeoPackageDataset::Open(GDALOpenInfo *poOpenInfo,
"name = 'gpkg_geometry_columns' AND "
"type IN ('table', 'view')",
nullptr) == 1;
bHasGPKGExtRelations = HasGpkgextRelationsTable();
}
if (m_bHasGPKGGeometryColumns)
{
Expand Down Expand Up @@ -1645,6 +1647,18 @@ int GDALGeoPackageDataset::Open(GDALOpenInfo *poOpenInfo,
bHasASpatialOrAttributes =
(oResultTable && oResultTable->RowCount() == 1);
}
if (bHasGPKGExtRelations)
{
osSQL += "UNION ALL "
"SELECT mapping_table_name, mapping_table_name, 0 as "
"is_spatial, NULL, NULL, 0, 0, 0 AS "
"xmin, 0 AS ymin, 0 AS xmax, 0 AS ymax, 0 AS "
"is_in_gpkg_contents, 'table' AS object_type "
"FROM gpkgext_relations WHERE "
"lower(mapping_table_name) NOT IN (SELECT "
"lower(table_name) FROM "
"gpkg_contents)";
}
if (EQUAL(pszListAllTables, "YES") ||
(!bHasASpatialOrAttributes && EQUAL(pszListAllTables, "AUTO")))
{
Expand All @@ -1663,6 +1677,12 @@ int GDALGeoPackageDataset::Open(GDALOpenInfo *poOpenInfo,
"'st_geometry_columns', 'geometry_columns') "
"AND lower(name) NOT IN (SELECT lower(table_name) FROM "
"gpkg_contents)";
if (bHasGPKGExtRelations)
{
osSQL += " AND lower(name) NOT IN (SELECT "
"lower(mapping_table_name) FROM "
"gpkgext_relations)";
}
}
const int nTableLimit = GetOGRTableLimit();
if (nTableLimit > 0)
Expand Down

0 comments on commit 0eb4293

Please sign in to comment.