KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: AbstractRestrictableStatement.java,v 1.1 2005/07/12 20:27:16 steveebersole Exp $
2
package org.hibernate.hql.ast.tree;
3
4 import org.hibernate.hql.antlr.HqlSqlTokenTypes;
5 import org.hibernate.hql.ast.util.ASTUtil;
6
7 import antlr.collections.AST;
8
9 import org.apache.commons.logging.Log;
10
11 /**
12  * Convenience implementation of RestrictableStatement to centralize common functionality.
13  *
14  * @author Steve Ebersole
15  */

16 public abstract class AbstractRestrictableStatement extends AbstractStatement implements RestrictableStatement {
17
18     private FromClause fromClause;
19     private AST whereClause;
20
21     protected abstract int getWhereClauseParentTokenType();
22     protected abstract Log getLog();
23
24     /**
25      * @see org.hibernate.hql.ast.tree.RestrictableStatement#getFromClause
26      */

27     public final FromClause getFromClause() {
28         if ( fromClause == null ) {
29             fromClause = ( FromClause ) ASTUtil.findTypeInChildren( this, HqlSqlTokenTypes.FROM );
30         }
31         return fromClause;
32     }
33
34     /**
35      * @see RestrictableStatement#hasWhereClause
36      */

37     public final boolean hasWhereClause() {
38         AST whereClause = locateWhereClause();
39         return whereClause != null && whereClause.getNumberOfChildren() > 0;
40     }
41
42     /**
43      * @see org.hibernate.hql.ast.tree.RestrictableStatement#getWhereClause
44      */

45     public final AST getWhereClause() {
46         if ( whereClause == null ) {
47             whereClause = locateWhereClause();
48             // If there is no WHERE node, make one.
49
if ( whereClause == null ) {
50                 getLog().debug( "getWhereClause() : Creating a new WHERE clause..." );
51                 whereClause = ASTUtil.create( getWalker().getASTFactory(), HqlSqlTokenTypes.WHERE, "WHERE" );
52                 // inject the WHERE after the parent
53
AST parent = ASTUtil.findTypeInChildren( this, getWhereClauseParentTokenType() );
54                 whereClause.setNextSibling( parent.getNextSibling() );
55                 parent.setNextSibling( whereClause );
56             }
57         }
58         return whereClause;
59     }
60
61     protected AST locateWhereClause() {
62         return ASTUtil.findTypeInChildren( this, HqlSqlTokenTypes.WHERE );
63     }
64 }
65
Popular Tags