KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > ast > tree > QueryNode


1 // $Id: QueryNode.java,v 1.2 2005/07/15 04:39:40 oneovthafew Exp $
2
package org.hibernate.hql.ast.tree;
3
4 import org.hibernate.hql.antlr.HqlSqlTokenTypes;
5 import org.hibernate.hql.antlr.SqlTokenTypes;
6 import org.hibernate.hql.ast.util.ASTUtil;
7 import org.hibernate.hql.ast.util.ColumnHelper;
8 import org.hibernate.type.Type;
9
10 import antlr.SemanticException;
11 import antlr.collections.AST;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 /**
17  * Defines a top-level AST node representing an HQL select statement.
18  *
19  * @author Joshua Davis
20  */

21 public class QueryNode extends AbstractRestrictableStatement implements SelectExpression {
22
23     private static final Log log = LogFactory.getLog( QueryNode.class );
24
25     private OrderByClause orderByClause;
26
27     /**
28      * @see Statement#getStatementType()
29      */

30     public int getStatementType() {
31         return HqlSqlTokenTypes.QUERY;
32     }
33
34     /**
35      * @see Statement#needsExecutor()
36      */

37     public boolean needsExecutor() {
38         return false;
39     }
40
41     protected int getWhereClauseParentTokenType() {
42         return SqlTokenTypes.FROM;
43     }
44
45     protected Log getLog() {
46         return log;
47     }
48
49     /**
50      * Locate the select clause that is part of this select statement.
51      * </p>
52      * Note, that this might return null as derived select clauses (i.e., no
53      * select clause at the HQL-level) get generated much later than when we
54      * get created; thus it depends upon lifecycle.
55      *
56      * @return Our select clause, or null.
57      */

58     public final SelectClause getSelectClause() {
59         // Due to the complexity in initializing the SelectClause, do not generate one here.
60
// If it is not found; simply return null...
61
//
62
// Also, do not cache since it gets generated well after we are created.
63
return ( SelectClause ) ASTUtil.findTypeInChildren( this, SqlTokenTypes.SELECT_CLAUSE );
64     }
65
66     public final boolean hasOrderByClause() {
67         OrderByClause orderByClause = locateOrderByClause();
68         return orderByClause != null && orderByClause.getNumberOfChildren() > 0;
69     }
70
71     public final OrderByClause getOrderByClause() {
72         if ( orderByClause == null ) {
73             orderByClause = locateOrderByClause();
74
75             // if there is no order by, make one
76
if ( orderByClause == null ) {
77                 log.debug( "getOrderByClause() : Creating a new ORDER BY clause" );
78                 orderByClause = ( OrderByClause ) ASTUtil.create( getWalker().getASTFactory(), SqlTokenTypes.ORDER, "ORDER" );
79
80                 // Find the WHERE; if there is no WHERE, find the FROM...
81
AST prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.WHERE );
82                 if ( prevSibling == null ) {
83                     prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.FROM );
84                 }
85
86                 // Now, inject the newly built ORDER BY into the tree
87
orderByClause.setNextSibling( prevSibling.getNextSibling() );
88                 prevSibling.setNextSibling( orderByClause );
89             }
90         }
91         return orderByClause;
92     }
93
94     private OrderByClause locateOrderByClause() {
95         return ( OrderByClause ) ASTUtil.findTypeInChildren( this, SqlTokenTypes.ORDER );
96     }
97     
98     
99     private String JavaDoc alias;
100
101     public String JavaDoc getAlias() {
102         return alias;
103     }
104
105     public FromElement getFromElement() {
106         return null;
107     }
108
109     public boolean isConstructor() {
110         return false;
111     }
112
113     public boolean isReturnableEntity() throws SemanticException {
114         return false;
115     }
116
117     public boolean isScalar() throws SemanticException {
118         return true;
119     }
120
121     public void setAlias(String JavaDoc alias) {
122         this.alias = alias;
123     }
124
125     public void setScalarColumnText(int i) throws SemanticException {
126         ColumnHelper.generateSingleScalarColumn( this, i );
127     }
128
129     public Type getDataType() {
130         return ( (SelectExpression) getSelectClause().getFirstSelectExpression() ).getDataType();
131     }
132
133 }
134
Popular Tags