1 2 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 ; 20 21 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 33 private int[] ops; 34 private String 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 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 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 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 110 public boolean requiresParensInMultiply() { 111 return true; 112 } 113 114 public void setExpAlias(String asValue) { 115 if (asValue != null && asValue.length() > 0) { 116 expAlias = asValue; 117 } else { 118 expAlias = null; 119 } 120 } 121 } 122 123 | Popular Tags |