01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.jdbc.sql.exp;
12:
13: import com.versant.core.jdbc.sql.SqlDriver;
14: import com.versant.core.util.CharBuf;
15: import com.versant.core.jdo.query.MultiplyNode;
16:
17: import java.util.Map;
18:
19: /**
20: * A multiplicative expression.
21: */
22: public class MultiplyExp extends SqlExp {
23:
24: public static final int OP_TIMES = MultiplyNode.OP_TIMES;
25: public static final int OP_DIVIDE = MultiplyNode.OP_DIVIDE;
26:
27: /**
28: * The operators. There will be one less entry here than the childList.
29: * Example: ops[0] is between childList and childList.next.
30: */
31: private int[] ops;
32:
33: public MultiplyExp(SqlExp children, int[] ops) {
34: super (children);
35: this .ops = ops;
36: }
37:
38: public MultiplyExp() {
39: }
40:
41: public SqlExp createInstance() {
42: return new MultiplyExp();
43: }
44:
45: public SqlExp getClone(SqlExp clone, Map cloneMap) {
46: super .getClone(clone, cloneMap);
47:
48: if (ops != null) {
49: int n = ops.length;
50: int[] cOps = ((MultiplyExp) clone).ops = new int[ops.length];
51: for (int i = 0; i < n; i++) {
52: cOps[i] = ops[i];
53: }
54: }
55:
56: return clone;
57: }
58:
59: /**
60: * Append SQL for this node to s.
61: *
62: * @param driver The driver being used
63: * @param s Append the SQL here
64: * @param leftSibling
65: */
66: public void appendSQLImp(SqlDriver driver, CharBuf s,
67: 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)
90: s.append('(');
91: e.appendSQL(driver, s, leftSibling);
92: if (p)
93: s.append(')');
94: }
95: }
|