From d75fea1cab3a6ee0fc1fdcaae5e83ca2ebcd0de6 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Mon, 27 Nov 2023 23:07:59 +0100 Subject: [PATCH] ExecuteSQL(): add a warning if the dialect name isn't recognized (fixes #8843) --- gcore/gdaldataset.cpp | 30 +++++++++++++++++++ .../gpkg/ogrgeopackagedatasource.cpp | 7 +++-- .../sqlite/ogrsqlitedatasource.cpp | 10 ++++--- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/gcore/gdaldataset.cpp b/gcore/gdaldataset.cpp index 1b622ed06d03..f359c4b9b3b6 100644 --- a/gcore/gdaldataset.cpp +++ b/gcore/gdaldataset.cpp @@ -6478,6 +6478,36 @@ GDALDataset::ExecuteSQL(const char *pszStatement, OGRGeometry *poSpatialFilter, #endif } + if (pszDialect != nullptr && !EQUAL(pszDialect, "") && + !EQUAL(pszDialect, "OGRSQL")) + { + std::string osDialectList = "'OGRSQL'"; +#ifdef SQLITE_ENABLED + osDialectList += ", 'SQLITE'"; +#endif + const char *pszDialects = + GetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS); + if (pszDialects) + { + const CPLStringList aosTokens( + CSLTokenizeString2(pszDialects, " ", 0)); + for (int i = 0; i < aosTokens.size(); ++i) + { + if (!EQUAL(aosTokens[i], "OGRSQL") && + !EQUAL(aosTokens[i], "SQLITE")) + { + osDialectList += ", '"; + osDialectList += aosTokens[i]; + osDialectList += "'"; + } + } + } + CPLError(CE_Warning, CPLE_NotSupported, + "Dialect '%s' is unsupported. Only supported dialects are %s. " + "Defaulting to OGRSQL", + pszDialect, osDialectList.c_str()); + } + /* -------------------------------------------------------------------- */ /* Handle CREATE INDEX statements specially. */ /* -------------------------------------------------------------------- */ diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp index 0a9c9f3ea6fa..34868ed6b6ad 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp @@ -7310,11 +7310,12 @@ OGRLayer *GDALGeoPackageDataset::ExecuteSQL(const char *pszSQLCommand, return nullptr; } - if (pszDialect != nullptr && EQUAL(pszDialect, "OGRSQL")) - return GDALDataset::ExecuteSQL(osSQLCommand, poSpatialFilter, - pszDialect); else if (pszDialect != nullptr && EQUAL(pszDialect, "INDIRECT_SQLITE")) return GDALDataset::ExecuteSQL(osSQLCommand, poSpatialFilter, "SQLITE"); + else if (pszDialect != nullptr && !EQUAL(pszDialect, "NATIVE") && + !EQUAL(pszDialect, "SQLITE")) + return GDALDataset::ExecuteSQL(osSQLCommand, poSpatialFilter, + pszDialect); #if SQLITE_VERSION_NUMBER < 3007017 // Emulate PRAGMA application_id diff --git a/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp b/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp index e845a9b6b8e5..f6d7252a44e9 100644 --- a/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp +++ b/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp @@ -3206,12 +3206,14 @@ OGRLayer *OGRSQLiteDataSource::ExecuteSQL(const char *pszSQLCommand, } } - if (pszDialect != nullptr && EQUAL(pszDialect, "OGRSQL")) - return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, - pszDialect); - else if (pszDialect != nullptr && EQUAL(pszDialect, "INDIRECT_SQLITE")) + if (pszDialect != nullptr && EQUAL(pszDialect, "INDIRECT_SQLITE")) return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, "SQLITE"); + else if (pszDialect != nullptr && !EQUAL(pszDialect, "NATIVE") && + !EQUAL(pszDialect, "SQLITE")) + + return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, + pszDialect); /* -------------------------------------------------------------------- */ /* Special case DELLAYER: command. */