diff --git a/autotest/ogr/ogr_sql_test.py b/autotest/ogr/ogr_sql_test.py index 1e4f31bfa449..330b27df7789 100755 --- a/autotest/ogr/ogr_sql_test.py +++ b/autotest/ogr/ogr_sql_test.py @@ -151,6 +151,32 @@ def data_ds(): yield ds +############################################################################### +# Test an unrecognized dialect name + + +def test_ogr_sql_unrecognized_dialect(data_ds): + class MyHandler: + def __init__(self): + self.warning_raised = False + + def callback(self, err_type, err_no, err_msg): + if ( + err_type == gdal.CE_Warning + and err_msg + == "Dialect 'unknown' is unsupported. Only supported dialects are 'OGRSQL', 'SQLITE'. Defaulting to OGRSQL" + ): + self.warning_raised = True + + my_error_handler = MyHandler() + with gdaltest.error_handler(my_error_handler.callback): + with data_ds.ExecuteSQL( + "SELECT * FROM poly WHERE eas_id < 167", dialect="unknown" + ) as sql_lyr: + assert sql_lyr.GetFeatureCount(force=1) == 3 + assert my_error_handler.warning_raised + + ############################################################################### # Test a simple query with a where clause. 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..cb0360b541fa 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, "") && + !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..8426d2cd885e 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, "") && + !EQUAL(pszDialect, "NATIVE") && !EQUAL(pszDialect, "SQLITE")) + + return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, + pszDialect); /* -------------------------------------------------------------------- */ /* Special case DELLAYER: command. */