KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ClauseParser.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
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * Parses the Hibernate query into its constituent clauses.
12  */

13 public class ClauseParser implements Parser {
14
15     private Parser child;
16     private List JavaDoc selectTokens;
17     private boolean cacheSelectTokens = false;
18     private boolean byExpected = false;
19     private int parenCount = 0;
20
21     public void token(String JavaDoc token, QueryTranslatorImpl q) throws QueryException {
22         String JavaDoc lcToken = token.toLowerCase();
23
24         if ( "(".equals( token ) ) {
25             parenCount++;
26         }
27         else if ( ")".equals( token ) ) {
28             parenCount--;
29         }
30
31         if ( byExpected && !lcToken.equals( "by" ) ) {
32             throw new QueryException( "BY expected after GROUP or ORDER: " + token );
33         }
34
35         boolean isClauseStart = parenCount == 0; //ignore subselect keywords
36

37         if ( isClauseStart ) {
38             if ( lcToken.equals( "select" ) ) {
39                 selectTokens = new ArrayList JavaDoc();
40                 cacheSelectTokens = true;
41             }
42             else if ( lcToken.equals( "from" ) ) {
43                 child = new FromParser();
44                 child.start( q );
45                 cacheSelectTokens = false;
46             }
47             else if ( lcToken.equals( "where" ) ) {
48                 endChild( q );
49                 child = new WhereParser();
50                 child.start( q );
51             }
52             else if ( lcToken.equals( "order" ) ) {
53                 endChild( q );
54                 child = new OrderByParser();
55                 byExpected = true;
56             }
57             else if ( lcToken.equals( "having" ) ) {
58                 endChild( q );
59                 child = new HavingParser();
60                 child.start( q );
61             }
62             else if ( lcToken.equals( "group" ) ) {
63                 endChild( q );
64                 child = new GroupByParser();
65                 byExpected = true;
66             }
67             else if ( lcToken.equals( "by" ) ) {
68                 if ( !byExpected ) throw new QueryException( "GROUP or ORDER expected before BY" );
69                 child.start( q );
70                 byExpected = false;
71             }
72             else {
73                 isClauseStart = false;
74             }
75         }
76
77         if ( !isClauseStart ) {
78             if ( cacheSelectTokens ) {
79                 selectTokens.add( token );
80             }
81             else {
82                 if ( child == null ) {
83                     throw new QueryException( "query must begin with SELECT or FROM: " + token );
84                 }
85                 else {
86                     child.token( token, q );
87                 }
88             }
89         }
90
91     }
92
93     private void endChild(QueryTranslatorImpl q) throws QueryException {
94         if ( child == null ) {
95             //null child could occur for no from clause in a filter
96
cacheSelectTokens = false;
97         }
98         else {
99             child.end( q );
100         }
101     }
102
103     public void start(QueryTranslatorImpl q) {
104     }
105
106     public void end(QueryTranslatorImpl q) throws QueryException {
107         endChild( q );
108         if ( selectTokens != null ) {
109             child = new SelectParser();
110             child.start( q );
111             Iterator JavaDoc iter = selectTokens.iterator();
112             while ( iter.hasNext() ) {
113                 token( ( String JavaDoc ) iter.next(), q );
114             }
115             child.end( q );
116         }
117         byExpected = false;
118         parenCount = 0;
119         cacheSelectTokens = false;
120     }
121
122 }
123
124
125
126
127
128
129
130
Popular Tags