1 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 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 30 public int getStatementType() { 31 return HqlSqlTokenTypes.QUERY; 32 } 33 34 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 58 public final SelectClause getSelectClause() { 59 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 ( orderByClause == null ) { 77 log.debug( "getOrderByClause() : Creating a new ORDER BY clause" ); 78 orderByClause = ( OrderByClause ) ASTUtil.create( getWalker().getASTFactory(), SqlTokenTypes.ORDER, "ORDER" ); 79 80 AST prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.WHERE ); 82 if ( prevSibling == null ) { 83 prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.FROM ); 84 } 85 86 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 alias; 100 101 public String 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 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 |