From 54c8f2b9f336bdb014d5c13a5551bd038025faa1 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 11 Mar 2024 17:37:11 +0100 Subject: [PATCH] introduce 'this', the implicit identification variable see #452 --- .../main/asciidoc/ch04-query-language.adoc | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/spec/src/main/asciidoc/ch04-query-language.adoc b/spec/src/main/asciidoc/ch04-query-language.adoc index ae475cab..3b631610 100644 --- a/spec/src/main/asciidoc/ch04-query-language.adoc +++ b/spec/src/main/asciidoc/ch04-query-language.adoc @@ -294,16 +294,19 @@ clause of a query. Each identification variable is assigned an abstract schema type. Each element of the domain may declare an identification variable. -- If the domain has exactly one named entity abstract schema type and - no joins, then the named entity does not require an identification - variable. +- If the domain has exactly one named entity abstract schema type + and no joins, then the named entity does not require an explicit + identification variable, and its identification variable defaults + to the _implicit identification variable,_ `this`. - Otherwise, every element of the FROM clause—that is, every named entity abstract schema types and every join—must declare an identification variable. ---- from_clause ::= - FROM entity_name | identification_variable_declarations + FROM {this_implicit_variable | identification_variable_declarations} + +this_implicit_variable ::= entity_name identification_variable_declarations ::= identification_variable_declaration @@ -382,12 +385,12 @@ releases of this specification. ==== Identification Variables [[a4765]] -An identification -variable is a valid identifier declared in the FROM clause of a query. +An identification variable is a valid identifier declared in the FROM +clause of a query. -All identification variables must be declared -in the FROM clause. Identification variables cannot be declared in other -clauses. +Every identification variable must be declared in the FROM clause, +except for the implicit identification variable `this`. Identification +variables are never declared in other clauses. An identification variable must not be a reserved identifier. @@ -491,8 +494,8 @@ WHERE o1.quantity > o2.quantity AND o2.customer.firstname= 'John' ---- -If the query domain is a single entity type, the range variable -declaration is optional. These queries are equivalent: +If the query domain is a single entity abstract schema type, the range +variable declaration is optional. These queries are equivalent: [source,sql] ---- @@ -509,9 +512,9 @@ WHERE customer.lastname = 'Smith' AND customer.firstname= 'John' ---- -Otherwise, if the query domain has more than one element, -each entity type listed in the FROM clause must specify -be a range variable declaration. +Otherwise, if the query domain has more than one element, each named +entity abstract schema type listed in the FROM clause must be a range +variable declaration. [[a4792]] @@ -521,21 +524,15 @@ A path expression is a sequence of identifiers uniquely identifying a state field or association field of an element of the query domain. A path expression may begin with a reference to an identification -variable, followed by the navigation operator (`.`). - -- If the query domain has a single element, then the identification - variable is optional, and every path expression which does not begin - with an identification variable is interpreted exactly as if it began - with an identification variable referring to the single element of - the domain. -- Otherwise, every path expression must begin with an identification - variable. +variable, followed by the navigation operator (`.`). If the first +element of a path expression is not an identification variable, then +the path expression is interpreted exactly as if it began with the +implicit identification variable `this`. The remaining elements of the path expression are interpreted as references to state fields or association fields in the context of the -abstract schema type assigned to the identification variable, or in the -context of the single abstract schema type of the query domain, in the -case where the path expression does not begin with an identification +abstract schema type assigned to the identification variable—or +to `this`, if the path expression does not begin with an identification variable. A reference to a state field or association field in a path expression @@ -2498,24 +2495,32 @@ GROUP BY c ORDER BY itemCount ---- -If the query domain is a single entity type, the SELECT clause -declaration is optional. A query with a missing SELECT clause -is interpreted as if it had a SELECT clause with a single item -containing an identification variable referencing the single -element of the domain. These queries are equivalent: +If the query domain is a single named entity abstract schema type, +the SELECT clause is optional. A query with a missing SELECT clause +is interpreted as if it had the following single-item SELECT clause: +`select this`, where `this` is the implicit identification variable. + +Thus, the following queries are equivalent: [source,sql] ---- -SELECT ord -FROM Order AS ord -WHERE ord.customer.lastname = 'Smith' - AND ord.customer.firstname= 'John' +FROM Order +WHERE customer.lastname = 'Smith' + AND customer.firstname= 'John' ---- [source,sql] ---- +SELECT this FROM Order -WHERE customer.lastname = 'Smith' - AND customer.firstname= 'John' +WHERE this.customer.lastname = 'Smith' + AND this.customer.firstname= 'John' +---- +[source,sql] +---- +SELECT ord +FROM Order AS ord +WHERE ord.customer.lastname = 'Smith' + AND ord.customer.firstname= 'John' ---- Otherwise, if the query domain has more than one element, @@ -3175,7 +3180,8 @@ select_query ::= [select_clause] from_clause [where_clause] [groupby_clause] update_statement ::= update_clause [where_clause] delete_statement ::= delete_clause [where_clause] from_clause ::= - FROM entity_name | identification_variable_declarations + FROM {this_implicit_variable | identification_variable_declarations} +this_implicit_variable ::= entity_name identification_variable_declarations ::= identification_variable_declaration {, {identification_variable_declaration | collection_member_declaration}}*