001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.sql.exp;
012:
013: import com.versant.core.jdbc.sql.SqlDriver;
014: import com.versant.core.util.CharBuf;
015: import com.versant.core.jdo.query.AddNode;
016: import com.versant.core.metadata.MDStatics;
017:
018: import java.util.Map;
019:
020: /**
021: * A additive expression.
022: */
023: public class AddExp extends SqlExp {
024:
025: public static final int OP_PLUS = AddNode.OP_PLUS;
026: public static final int OP_MINUS = AddNode.OP_MINUS;
027:
028: /**
029: * The operators. There will be one less entry here than the childList.
030: * Example: ops[0] is between childList and childList.next.
031: */
032: private int[] ops;
033: private String expAlias;
034:
035: public AddExp() {
036: }
037:
038: public AddExp(SqlExp children, int[] ops) {
039: super (children);
040: this .ops = ops;
041: }
042:
043: public SqlExp createInstance() {
044: return new AddExp();
045: }
046:
047: public SqlExp getClone(SqlExp clone, Map cloneMap) {
048: AddExp cst = (AddExp) clone;
049: super .getClone(cst, cloneMap);
050:
051: if (ops != null) {
052: int n = ops.length;
053: int[] cOps = new int[n];
054: for (int i = 0; i < n; i++) {
055: cOps[i] = ops[i];
056: }
057: cst.ops = cOps;
058: }
059: return cst;
060: }
061:
062: /**
063: * Append SQL for this node to s.
064: *
065: * @param driver The driver being used
066: * @param s Append the SQL here
067: * @param leftSibling
068: */
069: public void appendSQLImp(SqlDriver driver, CharBuf s,
070: SqlExp leftSibling) {
071: if (driver.isExtraParens())
072: s.append('(');
073: int i = 0;
074: childList.appendSQL(driver, s, null);
075: boolean useConcat = (childList.getJavaTypeCode() == MDStatics.STRING);
076: for (SqlExp e = childList.next; e != null && !useConcat; e = e.next) {
077: useConcat = e.getJavaTypeCode() == MDStatics.STRING;
078: }
079: String concatOp = null;
080: if (useConcat) {
081: concatOp = driver.getSqlBinaryOp(BinaryOpExp.CONCAT);
082: }
083: SqlExp prev = childList;
084: for (SqlExp e = childList.next; e != null; prev = e, e = e.next) {
085: s.append(' ');
086: if (useConcat) {
087: s.append(concatOp);
088: } else {
089: switch (ops[i++]) {
090: case OP_PLUS:
091: s.append('+');
092: break;
093: case OP_MINUS:
094: s.append('-');
095: break;
096: }
097: }
098: s.append(' ');
099: e.appendSQL(driver, s, prev);
100: }
101: if (driver.isExtraParens())
102: s.append(')');
103: if (expAlias != null) {
104: s.append(driver.getAliasPrepend() + " " + expAlias);
105: }
106: }
107:
108: /**
109: * If this expression is added to an MultiplyExp should it be enclosed in
110: * parenthesis?
111: */
112: public boolean requiresParensInMultiply() {
113: return true;
114: }
115:
116: public void setExpAlias(String asValue) {
117: if (asValue != null && asValue.length() > 0) {
118: expAlias = asValue;
119: } else {
120: expAlias = null;
121: }
122: }
123: }
|