You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by "Thomas Fox (JIRA)" <ji...@apache.org> on 2014/10/26 09:26:33 UTC

[jira] [Updated] (TORQUE-290) Improve building from clause in subselect

     [ https://issues.apache.org/jira/browse/TORQUE-290?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thomas Fox updated TORQUE-290:
------------------------------
    Description: 
Currently, it is not easily possible to reference columns in the main select from a subselect.
For example, to select authors which have published exactly one book, one can use the SQL:
SELECT * FROM author WHERE (SELECT COUNT(*) FROM book WHERE book.author_id=author.author_id)=1

One would like to achieve this by

Criteria subselect = new Criteria();
subselect.where(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
subselect.addSelectColumn(new Count("*"));

Criteria criteria = new Criteria();
criteria.where(subselect, 1);

List<Author> result = AuthorPeer.doSelect(criteria);

but the SQL results in (note the additional author in the from clause of the subselect)
SELECT * FROM author WHERE (SELECT COUNT(*) FROM book, author WHERE book.author_id=author.author_id)=1

So the desired behavior is:
If a table also exists in the from clause of the outer criteria, it should not be added to the from clause of the subselect

NOTE1: This change can change the behaviour of Torque in existing code
NOTE2: Workarounds exist, e.g. defining the FROM clause of the subselect manually using Criteria.addFrom() or using "new ColumnImpl(null,null,null,AuthorPeer.AUTHOR_ID.getSqlExpression())" as join column in the WHERE clause of the subselect.

  was:
Currently, it is not easily possible to reference columns in the main select from a subselect.
For example:

Select authors which have published more than three books
SELECT * FROM author WHERE (SELECT COUNT(*) FROM book WHERE book.author_id=author.author_id)=1

One would like to achieve this by
Criteria subselect = new Criteria();
subselect.where(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
subselect.addSelectColumn(new Count("*"));

Criteria criteria = new Criteria();
criteria.where(subselect, 1);

List<Author> result = AuthorPeer.doSelect(criteria);

but the SQL results in (note the additional author in the from clause of the subselect)
SELECT * FROM author WHERE (SELECT COUNT(*) FROM book, author WHERE book.author_id=author.author_id)=1

So the desired behavior is:
If a table also exists in the from clause of the outer criteria, it should not be added to the from clause of the subselect

NOTE1: This change can change the behaviour of Torque in existing code
NOTE2: Workarounds exist, e.g. defining the FROM clause of the subselect manually using Criteria.addFrom() or using "new ColumnImpl(null,null,null,AuthorPeer.AUTHOR_ID.getSqlExpression())" as join column in the WHERE clause of the subselect.


> Improve building from clause in subselect
> -----------------------------------------
>
>                 Key: TORQUE-290
>                 URL: https://issues.apache.org/jira/browse/TORQUE-290
>             Project: Torque
>          Issue Type: Improvement
>          Components: Runtime
>    Affects Versions: 4.0
>            Reporter: Thomas Fox
>            Assignee: Thomas Fox
>              Labels: incompatibleChange
>
> Currently, it is not easily possible to reference columns in the main select from a subselect.
> For example, to select authors which have published exactly one book, one can use the SQL:
> SELECT * FROM author WHERE (SELECT COUNT(*) FROM book WHERE book.author_id=author.author_id)=1
> One would like to achieve this by
> Criteria subselect = new Criteria();
> subselect.where(BookPeer.AUTHOR_ID, AuthorPeer.AUTHOR_ID);
> subselect.addSelectColumn(new Count("*"));
> Criteria criteria = new Criteria();
> criteria.where(subselect, 1);
> List<Author> result = AuthorPeer.doSelect(criteria);
> but the SQL results in (note the additional author in the from clause of the subselect)
> SELECT * FROM author WHERE (SELECT COUNT(*) FROM book, author WHERE book.author_id=author.author_id)=1
> So the desired behavior is:
> If a table also exists in the from clause of the outer criteria, it should not be added to the from clause of the subselect
> NOTE1: This change can change the behaviour of Torque in existing code
> NOTE2: Workarounds exist, e.g. defining the FROM clause of the subselect manually using Criteria.addFrom() or using "new ColumnImpl(null,null,null,AuthorPeer.AUTHOR_ID.getSqlExpression())" as join column in the WHERE clause of the subselect.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org