| Michael Bouschen 2006-07-26, 7:11 pm |
| Hi Craig,
below I try summarize my understanding of what needs to be added in
order to support subqueries:
- Adding support for defining an alias for the elements of the candidate
collection. If there is no explicit alias defined, 'this' is the
default. This allows to disambiguate the candidate collection elements
from the outer query and the subquery. In single-string JDOQL the alias
follows the candidate class name in the FROM clause: 'FROM Employee e'.
For the Query API I propose to add a new overloaded method
Query.setClass(Class candidateClass, String alias).
- The variable declaration supports adding an initializer expression:
q.declareVariables("type name = expr"). This allows using a subquery to
define the value of a variable.
- The FROM clause of the subquery allows a collection relationship field
or a collection variable as candidate collection.
- Subquery enclosed in parenthesis is a regular expression and can can
appear everywhere as long as the typing is correct. So some subqueries
might need to include a UNIQUE clause such that the typing is correct,
e.g. if the result of a subquery is compared with a regular field:
... WHERE this.salary > (SELECT UNIQUE e.salary FROM Employee e WHERE
e.pk == param)
> Hi Michael,
>
> I came up with some more queries that I'd like to discuss as we
> consider how to do subqueries. I haven't found a use case for
> subqueries that themselves contain subqueries...
Below you find my ideas for the single-string and Query API JDOQL for
the queries you mentioned.
>
> Select employees who make more than the average of their department
> employees?
Single-string JDOQL:
SELECT FROM Employee WHERE this.salary > (SELECT AVG(e.salary) FROM
this.department.employees e)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(e.salary) FROM
this.department.employees e");
> Select employees who make more than the average of the employees in
> their department at the same pay scale?
Single-string JDOQL:
SELECT FROM Employee WHERE this.salary >
(SELECT AVG(e.salary) FROM this.department.employees e WHERE
e.payScale == this.payScale)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(e.salary) FROM
this.department.employees e WHERE e.payScale == this.payScale");
> Select employees who make more than the average of all employees?
Single-string JDOQL:
SELECT FROM Employee WHERE this.salary > (SELECT AVG(e.salary) FROM
Employee e)
SELECT FROM Employee WHERE this.salary > (SELECT AVG(this.salary)
FROM Employee)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(this.salary) FROM
Employee");
> Select employees named Joe who make more than the average of all
> employees?
Single-string JDOQL:
SELECT FROM Employee WHERE this.name == 'Joe' && this.salary > (SELECT
AVG(e.salary) FROM Employee e)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.name == 'Joe' && this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(this.salary) FROM
Employee");
>
> Select employees named Joe who make more than the average of all
> employees at the same pay scale?
Single-string JDOQL:
SELECT FROM Employee WHERE this.name == 'Joe' && this.salary >
(SELECT AVG(e.salary) FROM Employee e WHERE e.payScale ==
this.payScale)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.name == 'Joe' && this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(e.salary) FROM
Employee e WHERE e.payScale == this.payScale");
> Select employees who make more than the average of all employees at
> the same pay scale?
Single-string JDOQL:
SELECT FROM Employee WHERE this.salary > (SELECT AVG(e.salary) FROM
Employee e WHERE e.payScale == this.payScale)
Query API:
Query q = pm.newQuery(Employee.class);
q.setFilter("this.salary > averageSalary");
q.declareVariables("float averageSalary = SELECT AVG(e.salary) FROM
Employee e WHERE e.payScale == this.payScale");
Regards Michael
>
> Craig
>
> On Jun 22, 2006, at 3:18 PM, Michael Bouschen wrote:
>
>
> Craig Russell
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
> 408 276-5638 mailto:Craig.Russell@sun.com
> P.S. A good JDO? O, Gasp!
>
--
Michael Bouschen Tech@Spree Engineering GmbH
mailto:mbo.tech@spree.de http://www.tech.spree.de/
Tel.:++49/30/235 520-33 Buelowstr. 66
Fax.:++49/30/2175 2012 D-10783 Berlin
|