1 /* JJT: 0.2.2 */
2
3 package mobisnap.mobile_trx;
4
5 /***
6 * Implements primary sql expression : variables, numbers, ...
7 */
8 public class ASTSQLPrimaryExpression extends mobisnap.mobile_trx.SimpleNode {
9 public static final byte EXPR_NULL = 1;
10 public static final byte EXPR_FUNCTION = 2;
11 public static final byte EXPR_OUTERJOIN = 3;
12 public static final byte EXPR_VARIABLE = 4;
13 public static final byte EXPR_SYSDATE = 5;
14 public static final byte EXPR_SQLNUMBER = 6;
15 public static final byte EXPR_SQLCHARLITERAL = 7;
16 public static final byte EXPR_SQLBIND = 8;
17 public static final byte EXPR_SQLEXPR = 9;
18 public static final byte EXPR_CURSYSDATE = 10; //v1 = curdate
19 public static final byte EXPR_NEWUID = 11; //v1 = uid
20
21 public byte type;
22 public Object v1, v2;
23
24 public ASTSQLPrimaryExpression(int id) {
25 super(id);
26 }
27
28 public ASTSQLPrimaryExpression( MobisnapSQL p, int i) {
29 super( p, i);
30 id = i;
31 }
32
33 /*** Accept the visitor. **/
34 public Object jjtAccept(MobisnapSQLVisitor visitor, Object data) {
35 return visitor.visit(this, data);
36 }
37
38 /***
39 * Displays the source code of the node
40 */
41 public void sourceCode( int msql_type, StringBuffer buffer) throws Exception {
42 if( msql_type == mobisnap.MobisnapConstants.MSQL_ORIGINAL) {
43 switch( type) {
44 case EXPR_CURSYSDATE:
45 buffer.append( "'");
46 buffer.append( MSQLTypeUtil.date2String( (java.util.Date)v1));
47 buffer.append( "'");
48 break;
49 case EXPR_NEWUID:
50 buffer.append( "'");
51 buffer.append( ((mobisnap.common.MobisnapUID)v1).toString());
52 buffer.append( "'");
53 break;
54 default:
55 super.sourceCode( msql_type, buffer);
56 break;
57 }
58 return;
59 }
60 switch( type) {
61 case EXPR_VARIABLE:
62 StringBuffer buf = new StringBuffer();
63 ((ASTTableColumn)v1).sourceCode( msql_type, buf);
64 MSQLTName var = null;
65 try {
66 var = MobisnapSQL.names.getName( buf.toString().trim());
67 } catch( Exception e) {
68 var = null;
69 }
70 if( var == null) {
71 buffer.append( buf.toString());
72 } else {
73 Object obj = var.getValue();
74 if( obj instanceof String) {
75 buffer.append( "'");
76 buffer.append( obj.toString());
77 buffer.append( "'");
78 } else if( obj instanceof java.sql.Date)
79 buffer.append( MSQLTypeUtil.date2String( (java.sql.Date)obj));
80 else
81 buffer.append( obj.toString());
82 }
83 break;
84 case EXPR_SYSDATE:
85 buffer.append( "'");
86 buffer.append( MSQLTypeUtil.date2String( new java.util.Date()));
87 buffer.append( "'");
88 break;
89 case EXPR_CURSYSDATE:
90 buffer.append( "'");
91 buffer.append( MSQLTypeUtil.date2String( (java.util.Date)v1));
92 buffer.append( "'");
93 break;
94 case EXPR_NEWUID:
95 buffer.append( "'");
96 buffer.append( ((mobisnap.common.MobisnapUID)v1).toString());
97 buffer.append( "'");
98 break;
99 default:
100 super.sourceCode( msql_type, buffer);
101 break;
102 }
103 }
104
105
106
107 /***
108 * Returns the value of the expression
109 *
110 * @param msql_type Specifies which type of processing should be performed
111 * MobisnapConstants.MSQL_SERVER = 1
112 * MobisnapConstants.MSQL_TENTATIVE_CLIENT = 2
113 * MobisnapConstants.MSQL_STABLE_CLIENT = 3
114 * MobisnapConstants.MSQL_RESERVATION_CLIENT = 4
115 * MobisnapConstants.MSQL_STATIC = 10
116 * @param cond True if reservations associated iwth transaction should be
117 * propagated to the current transaction
118 */
119 public Object value( int msql_type, boolean cond) throws Exception {
120 switch( type) {
121 case EXPR_NULL:
122 return new SQLNull();
123 case EXPR_SQLNUMBER:
124 return MSQLTypeUtil.string2Decimal( (String)v1);
125 case EXPR_VARIABLE:
126 if( msql_type == mobisnap.MobisnapConstants.MSQL_ORIGINAL)
127 return v1;
128 StringBuffer buf = new StringBuffer();
129 ((ASTTableColumn)v1).sourceCode( mobisnap.MobisnapConstants.MSQL_SERVER, buf);
130 MSQLTName var = null;
131 try {
132 var = MobisnapSQL.names.getName( buf.toString().trim());
133 } catch( NoReservationException e) {
134 throw e;
135 } catch( Exception e) {
136 var = null;
137 }
138 if( var == null)
139 throw new UnknownValueException( "Unknown variable - it should be a column");
140 else
141 return var.getValue();
142 case EXPR_SQLCHARLITERAL:
143 return v1;
144 case EXPR_SQLEXPR:
145 return ((SimpleNode)v1).value( msql_type, cond);
146 case EXPR_SYSDATE:
147 return new java.sql.Date( new java.util.Date().getTime());
148 case EXPR_CURSYSDATE:
149 return v1;
150 case EXPR_NEWUID:
151 return ((mobisnap.common.MobisnapUID)v1).toString();
152 default:
153 throw new UnknownValueException( "Unhandled SQLPrimaryExpression");
154 }
155 }
156
157 /***
158 * Returns a simplified node, if possible (usefull in expressions)
159 *
160 * @param msql_type Specifies which type of processing should be performed
161 * MobisnapConstants.MSQL_SERVER = 1
162 * MobisnapConstants.MSQL_TENTATIVE_CLIENT = 2
163 * MobisnapConstants.MSQL_STABLE_CLIENT = 3
164 * MobisnapConstants.MSQL_RESERVATION_CLIENT = 4
165 * MobisnapConstants.MSQL_STATIC = 10
166 * @param cond True if reservations associated iwth transaction should be
167 * propagated to the current transaction
168 */
169 public SimpleNode simplify( int msql_type, boolean cond) {
170 if( type == EXPR_SQLEXPR)
171 return ((SimpleNode)v1).simplify( msql_type, cond);
172 try {
173 return new SimpleNodeValue( value( msql_type, cond));
174 } catch( Exception e) {
175 return this;
176 }
177 }
178
179 }
This page was automatically generated by Maven