Skip to content

Commit

Permalink
Merge pull request #11788 from rouault/fix_11782
Browse files Browse the repository at this point in the history
SQLite/GPKG: run PRELUDE_STATEMENTS after end of initialization, in particular after Spatialite loading
  • Loading branch information
rouault authored Feb 4, 2025
2 parents 50d1393 + ef56800 commit 8d8fb57
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
21 changes: 21 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5869,6 +5869,27 @@ def test_ogr_gpkg_prelude_statements(tmp_vsimem):
assert sql_lyr.GetFeatureCount() == 10


###############################################################################
# Test PRELUDE_STATEMENTS open option


def test_ogr_gpkg_prelude_statements_after_spatialite_loading(tmp_vsimem):

gdal.VectorTranslate(tmp_vsimem / "test.gpkg", "data/poly.shp", format="GPKG")

with ogr.Open(tmp_vsimem / "test.gpkg") as ds:
if not _has_spatialite_4_3_or_later(ds):
pytest.skip("spatialite missing")

with gdal.OpenEx(
tmp_vsimem / "test.gpkg",
open_options=["PRELUDE_STATEMENTS=SELECT setdecimalprecision(1)"],
) as ds:
with ds.ExecuteSQL("SELECT ST_AsText(geom) FROM poly LIMIT 1") as sql_lyr:
f = sql_lyr.GetNextFeature()
assert f[0].startswith("POLYGON((479819.8 4765180.5,")


###############################################################################
# Test DATETIME_FORMAT

Expand Down
8 changes: 8 additions & 0 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9643,6 +9643,14 @@ bool GDALGeoPackageDataset::OpenOrCreateDB(int flags)
}
}

const char *pszPreludeStatements =
CSLFetchNameValue(papszOpenOptions, "PRELUDE_STATEMENTS");
if (pszPreludeStatements)
{
if (SQLCommand(hDB, pszPreludeStatements) != OGRERR_NONE)
return false;
}

return true;
}

Expand Down
16 changes: 8 additions & 8 deletions ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1557,14 +1557,6 @@ bool OGRSQLiteBaseDataSource::OpenOrCreateDB(int flagsIn,
}
#endif

const char *pszPreludeStatements =
CSLFetchNameValue(papszOpenOptions, "PRELUDE_STATEMENTS");
if (pszPreludeStatements)
{
if (SQLCommand(hDB, pszPreludeStatements) != OGRERR_NONE)
return false;
}

if (pszSqlitePragma != nullptr)
{
char **papszTokens =
Expand Down Expand Up @@ -1821,6 +1813,14 @@ bool OGRSQLiteDataSource::OpenOrCreateDB(int flagsIn,
// above OGR2SQLITE_Setup()
LoadExtensions();

const char *pszPreludeStatements =
CSLFetchNameValue(papszOpenOptions, "PRELUDE_STATEMENTS");
if (pszPreludeStatements)
{
if (SQLCommand(hDB, pszPreludeStatements) != OGRERR_NONE)
return false;
}

return true;
}

Expand Down

0 comments on commit 8d8fb57

Please sign in to comment.