KickJava   Java API By Example, From Geeks To Geeks.

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


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.MultiplyNode;
17
18 import java.util.Map JavaDoc;
19
20 /**
21  * A multiplicative expression.
22  */

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

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

67     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
68         int i = 0;
69         appendSQL(childList, driver, s, null);
70         SqlExp prev = childList;
71         for (SqlExp e = childList.next; e != null; prev = e, e = e.next) {
72             s.append(' ');
73             switch (ops[i++]) {
74                 case OP_TIMES:
75                     s.append('*');
76                     break;
77                 case OP_DIVIDE:
78                     s.append('/');
79                     break;
80             }
81             s.append(' ');
82             appendSQL(e, driver, s, prev);
83         }
84     }
85
86     private void appendSQL(SqlExp e, SqlDriver driver, CharBuf s,
87             SqlExp leftSibling) {
88         boolean p = e.requiresParensInMultiply();
89         if (p) s.append('(');
90         e.appendSQL(driver, s, leftSibling);
91         if (p) s.append(')');
92     }
93 }
94
Popular Tags