2.5. 实施

2.5.1. BNF语法

终端元素以大写字母表示,非终端元素以小写字母表示。终端元素的值(引号之间)是一个python正则表达式。地址:

statement ::= (select | delete | insert | update) ';'


# select specific rules
select      ::= 'DISTINCT'? E_TYPE selected_terms restriction? group? sort?

selected_terms ::= expression ( ',' expression)*

group       ::= 'GROUPBY' VARIABLE ( ',' VARIABLE)*

sort        ::= 'ORDERBY' sort_term ( ',' sort_term)*

sort_term   ::=  VARIABLE sort_method =?

sort_method ::= 'ASC' | 'DESC'


# delete specific rules
delete ::= 'DELETE' (variables_declaration | relations_declaration) restriction?


# insert specific rules
insert ::= 'INSERT' variables_declaration ( ':' relations_declaration)? restriction?


# update specific rules
update ::= 'SET' relations_declaration restriction


# common rules
variables_declaration ::= E_TYPE VARIABLE (',' E_TYPE VARIABLE)*

relations_declaration ::= simple_relation (',' simple_relation)*

simple_relation ::= VARIABLE R_TYPE expression

restriction ::= 'WHERE' relations

relations   ::= relation (LOGIC_OP relation)*
              | '(' relations')'

relation    ::= 'NOT'? VARIABLE R_TYPE COMP_OP? expression
              | 'NOT'? R_TYPE VARIABLE 'IN' '(' expression (',' expression)* ')'

expression  ::= var_or_func_or_const (MATH_OP var_or_func_or_const) *
              | '(' expression ')'

var_or_func_or_const ::= VARIABLE | function | constant

function    ::= FUNCTION '(' expression ( ',' expression) * ')'

constant    ::= KEYWORD | STRING | FLOAT | INT

# tokens
LOGIC_OP ::= ',' | 'OR' | 'AND'
MATH_OP  ::= '+' | '-' | '/' | '*'
COMP_OP  ::= '>' | '>=' | '=' | '<=' | '<' | '~=' | 'LIKE'

FUNCTION ::= 'MIN' | 'MAX' | 'SUM' | 'AVG' | 'COUNT' | 'UPPER' | 'LOWER'

VARIABLE ::= '[A-Z][A-Z0-9]*'
E_TYPE   ::= '[A-Z]\w*'
R_TYPE   ::= '[a-z_]+'

KEYWORD  ::= 'TRUE' | 'FALSE' | 'NULL' | 'TODAY' | 'NOW'
STRING   ::= "'([^'\]|\\.)*'" |'"([^\"]|\\.)*\"'
FLOAT    ::= '\d+\.\d*'
INT      ::= '\d+'

2.5.2. 内部表示(句法树)

树研究不包含所选变量(例如,只有后面的“where”)。

插入树不包含插入的变量或在这些变量上定义的关系(例如,只有后面的“where”)。

删除树不包含已删除的变量和关系(例如,只有“where”后面的内容)。

更新树不包含更新的变量和关系(例如,只有“where”后面的内容)。

Select         ((Relationship | And | Or)?, Group?, Sort?)
Insert         (Relations | And | Or)?
Delete         (Relationship | And | Or)?
Update         (Relations | And | Or)?

And            ((Relationship | And | Or), (Relationship | And | Or))
Or             ((Relationship | And | Or), (Relationship | And | Or))

Relationship   ((VariableRef, Comparison))

Comparison     ((Function | MathExpression | Keyword | Constant | VariableRef) +)

Function       (())
MathExpression ((MathExpression | Keyword | Constant | VariableRef), (MathExpression | Keyword | Constant | VariableRef))

Group          (VariableRef +)
Sort           (SortTerm +)
SortTerm       (VariableRef +)

VariableRef    ()
Variable       ()
Keyword        ()
Constant       ()

2.5.3. 已知限制

  • 当前实现不支持将“is”类型的两个关系与或链接。我认为这种关系不支持否定(需要确认的xxx)。

  • 错过了联合,当然还有其他事情…

  • 编写RQL查询需要了解所使用的模式(具有真实的关系名和实体,而不是在用户界面中查看的关系名和实体)。另一方面,我们不能真正绕过它,隐藏RQL是用户界面的工作。

2.5.4. 话题

表示模式匹配关系(非递归规则)比较方便:

Document class Type <-> Document occurence_of Fiche class Type
Sheet class Type    <-> Form collection Collection class Type

因此1.变为:

Document X where
X class C, C name 'Cartoon'
X owned_by U, U login 'syt'
X available true

我不确定我们应该在RQL级别处理这个问题…

还有一种特殊的关系“匿名”。