Skip to content

Commit

Permalink
ExecuteSQL(): add a warning if the dialect name isn't recognized (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 27, 2023
1 parent a09033a commit 34d6ba5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
26 changes: 26 additions & 0 deletions autotest/ogr/ogr_sql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
30 changes: 30 additions & 0 deletions gcore/gdaldataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
/* -------------------------------------------------------------------- */
Expand Down
7 changes: 4 additions & 3 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit 34d6ba5

Please sign in to comment.