KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > AddExp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import com.versant.core.jdbc.sql.SqlDriver;
15 import com.versant.core.util.CharBuf;
16 import com.versant.core.jdo.query.AddNode;
17 import com.versant.core.metadata.MDStatics;
18
19 import java.util.Map JavaDoc;
20
21 /**
22  * A additive expression.
23  */

24 public class AddExp extends SqlExp {
25
26     public static final int OP_PLUS = AddNode.OP_PLUS;
27     public static final int OP_MINUS = AddNode.OP_MINUS;
28
29     /**
30      * The operators. There will be one less entry here than the childList.
31      * Example: ops[0] is between childList and childList.next.
32      */

33     private int[] ops;
34     private String JavaDoc expAlias;
35
36     public AddExp() {
37     }
38
39     public AddExp(SqlExp children, int[] ops) {
40         super(children);
41         this.ops = ops;
42     }
43
44     public SqlExp createInstance() {
45         return new AddExp();
46     }
47
48     public SqlExp getClone(SqlExp clone, Map JavaDoc cloneMap) {
49         AddExp cst = (AddExp) clone;
50         super.getClone(cst, cloneMap);
51
52         if (ops != null) {
53             int n = ops.length;
54             int[] cOps = new int[n];
55             for (int i = 0; i < n; i++) {
56                 cOps[i] = ops[i];
57             }
58             cst.ops = cOps;
59         }
60         return cst;
61     }
62
63     /**
64      * Append SQL for this node to s.
65      *
66      * @param driver The driver being used
67      * @param s Append the SQL here
68      * @param leftSibling
69      */

70     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
71         if (driver.isExtraParens()) s.append('(');
72         int i = 0;
73         childList.appendSQL(driver, s, null);
74         boolean useConcat = (childList.getJavaTypeCode() == MDStatics.STRING);
75         for (SqlExp e = childList.next; e != null && !useConcat; e = e.next) {
76             useConcat = e.getJavaTypeCode() == MDStatics.STRING;
77         }
78         String JavaDoc concatOp = null;
79         if (useConcat) {
80             concatOp = driver.getSqlBinaryOp(BinaryOpExp.CONCAT);
81         }
82         SqlExp prev = childList;
83         for (SqlExp e = childList.next; e != null; prev = e, e = e.next) {
84             s.append(' ');
85             if (useConcat) {
86                 s.append(concatOp);
87             } else {
88                 switch (ops[i++]) {
89                     case OP_PLUS:
90                         s.append('+');
91                         break;
92                     case OP_MINUS:
93                         s.append('-');
94                         break;
95                 }
96             }
97             s.append(' ');
98             e.appendSQL(driver, s, prev);
99         }
100         if (driver.isExtraParens()) s.append(')');
101         if (expAlias != null) {
102             s.append(driver.getAliasPrepend() + " " + expAlias);
103         }
104     }
105
106     /**
107      * If this expression is added to an MultiplyExp should it be enclosed in
108      * parenthesis?
109      */

110     public boolean requiresParensInMultiply() {
111         return true;
112     }
113
114     public void setExpAlias(String JavaDoc asValue) {
115         if (asValue != null && asValue.length() > 0) {
116             expAlias = asValue;
117         } else {
118             expAlias = null;
119         }
120     }
121 }
122
123
Popular Tags