KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ArithmeticNode.java,v 1.2 2005/07/19 16:41:11 oneovthafew Exp $
2
package org.hibernate.hql.ast.tree;
3
4 import org.hibernate.Hibernate;
5 import org.hibernate.hql.ast.util.ColumnHelper;
6 import org.hibernate.type.Type;
7
8 import antlr.SemanticException;
9 import antlr.collections.AST;
10
11 /**
12  * @author Gavin King
13  */

14 public class ArithmeticNode extends AbstractSelectExpression {
15     
16     /**
17      * Figure out the type of the binary expression by looking at
18      * the types of the operands. Sometimes we don't know both types,
19      * if, for example, one is a parameter.
20      */

21     public Type getDataType() {
22         AST first = getFirstChild();
23         AST second = getFirstChild().getNextSibling();
24         if ( !( second instanceof SelectExpression ) ) {
25             if ( first instanceof SelectExpression ) {
26                 //we only know the type of the first
27
return ( (SelectExpression) first ).getDataType();
28             }
29             else {
30                 return Hibernate.DOUBLE; //BLIND GUESS!
31
}
32         }
33         else {
34             if ( first instanceof SelectExpression) {
35                 //both have known type
36
Type x = ( (SelectExpression) first ).getDataType();
37                 Type y = ( (SelectExpression) second ).getDataType();
38                 if ( x==Hibernate.DOUBLE || y==Hibernate.DOUBLE ) return Hibernate.DOUBLE;
39                 if ( x==Hibernate.FLOAT || y==Hibernate.FLOAT ) return Hibernate.FLOAT;
40                 if ( x==Hibernate.BIG_DECIMAL || y==Hibernate.BIG_DECIMAL ) return Hibernate.BIG_DECIMAL;
41                 if ( x==Hibernate.BIG_INTEGER || y==Hibernate.BIG_INTEGER ) return Hibernate.BIG_INTEGER;
42                 if ( x==Hibernate.LONG || y==Hibernate.LONG ) return Hibernate.LONG;
43                 if ( x==Hibernate.INTEGER || y==Hibernate.INTEGER ) return Hibernate.INTEGER;
44                 return x;
45             }
46             else {
47                 //we only know the type of the second
48
return ( (SelectExpression) second ).getDataType();
49             }
50         }
51     }
52
53     public void setScalarColumnText(int i) throws SemanticException {
54         ColumnHelper.generateSingleScalarColumn( this, i );
55     }
56
57 }
58
Popular Tags