KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > classic > OrderByParser


1 //$Id: OrderByParser.java,v 1.1 2004/12/08 00:22:20 oneovthafew Exp $
2
package org.hibernate.hql.classic;
3
4 import org.hibernate.QueryException;
5 import org.hibernate.util.StringHelper;
6
7 /**
8  * Parses the ORDER BY clause of a query
9  */

10
11 public class OrderByParser implements Parser {
12
13     // This uses a PathExpressionParser but notice that compound paths are not valid,
14
// only bare names and simple paths:
15

16     // SELECT p FROM p IN CLASS eg.Person ORDER BY p.Name, p.Address, p
17

18     // The reason for this is SQL doesn't let you sort by an expression you are
19
// not returning in the result set.
20

21     private final PathExpressionParser pathExpressionParser;
22
23     {
24         pathExpressionParser = new PathExpressionParser();
25         pathExpressionParser.setUseThetaStyleJoin( true ); //TODO: would be nice to use false, but issues with MS SQL
26
}
27
28     public void token(String JavaDoc token, QueryTranslatorImpl q) throws QueryException {
29
30         if ( q.isName( StringHelper.root( token ) ) ) {
31             ParserHelper.parse( pathExpressionParser, q.unalias( token ), ParserHelper.PATH_SEPARATORS, q );
32             q.appendOrderByToken( pathExpressionParser.getWhereColumn() );
33             pathExpressionParser.addAssociation( q );
34         }
35         else if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) { //named query parameter
36
q.addNamedParameter( token.substring( 1 ) );
37             q.appendOrderByToken( "?" );
38         }
39         else {
40             q.appendOrderByToken( token );
41         }
42     }
43
44     public void start(QueryTranslatorImpl q) throws QueryException {
45     }
46
47     public void end(QueryTranslatorImpl q) throws QueryException {
48     }
49
50 }
51
Popular Tags