RFC 52:严格的OGR SQL引用

作者:连鲁奥

联系人:spatialys.com上的偶数点rouault

状态:采用,在GDAL 2.0中实现

总结

此RFC建议OGR SQL对SQL文本和标识符强制执行严格的引用规则

细节

目前,OGR SQL引擎对单引号字符(')和双引号字符(“)的处理方式不同,尽管它们可以在两种不同的上下文中使用:

  • 指定字符串文字

  • 指定列名或表名(需要引用时)

SQL92要求字符串文字用单引号字符括起来,而带引号的标识符(列、表名)用双引号字符括起来。

http://savage.net.au/SQL/sql-92.bnf.html

<delimited identifier>     ::= <double quote> <delimited identifier body> <double quote>
<character string literal> ::= <quote> [ <character representation> ... ] <quote>

Current OGR behavior is generally fine, except in some situations when specifying columns in a SELECT statement or in a WHERE expression. The OGR SQL engine tries to determine the intent with the following rule : if the quoted string matches a column name, then it is assumed to be a column identifier, otherwise a string literal. But sometimes (see http://trac.osgeo.org/gdal/ticket/5428), there are situations where filters such as "MyField" = 'MYFIELD' are needed, and currently they always evaluate to TRUE.

为了避免任何歧义和更符合SQL,字符串文本应该是单引号,如果与保留关键字冲突,标识符(列/表名称)应该是不带引号的,或者由双引号字符包围。

实施

实施将由甚至鲁奥完成 (Spatialys ②)

建议的实现位于 https://github.com/rouault/gdal2/tree/rfc52_stricter_sql 储存库。

更改列表: https://github.com/rouault/gdal2/compare/rfc52_stricter_sql

兼容性

此更改影响兼容性,因为依赖当前许可行为的用户必须调整其SQL表达式。这可能很难检测,因为它不会总是导致明显的运行时故障。例如,“SELECT'iu thoughtu thisu wouldu expressedu asu au columnu nameu butu nowu itu isu au stringu literal”“FROM atable”将无错误运行,但不会产生先前预期的结果。因此应用程序必须检查它们如何构建SQL过滤器(SetAttributeFilter())和表达式(ExecuteSQL())

修正为符合更严格规则的表达式也适用于旧的GDAL版本。

ogrsql文档页上还应该有一个显著的警告,以公布行为的更改

讨论

使用SQLite进行的测试表明,它有时可以容忍引号字符的误用,但与当前OGR相反。因此,它将从“atable”中接受SELECT“non_existing_column”,并将其解释为从“atable”中选择“non_existing_column”。相反,PostgreSQL不会接受它。我更倾向于遵循PostgreSQL更严格的方法来保持事情简单易懂:“字符串文本应该是单引号,标识符(列/表名)应该是非引号或由双引号字符包围。”

测试

Python自动测试套件将被修复为仍然通过。

投票历史

+1名来自JukkaR,TamasS,DanielM和Ever