1 2 12 package com.versant.core.jdbc.sql.exp; 13 14 import com.versant.core.jdbc.sql.SqlDriver; 15 import com.versant.core.jdbc.sql.conv.DummyPreparedStmt; 16 import com.versant.core.util.CharBuf; 17 import com.versant.core.jdo.query.UnaryOpNode; 18 19 import java.util.Map ; 20 import java.sql.SQLException ; 21 22 import com.versant.core.common.BindingSupportImpl; 23 24 27 public class UnaryOpExp extends UnaryExp { 28 29 public static final int OP_MINUS = UnaryOpNode.OP_MINUS; 30 public static final int OP_PLUS = UnaryOpNode.OP_PLUS; 31 public static final int OP_COMPLEMENT = UnaryOpNode.OP_TILDE; 32 public static final int OP_NOT = UnaryOpNode.OP_BANG; 33 34 private int op; 35 36 public UnaryOpExp(SqlExp child, int op) { 37 super(child); 38 this.op = op; 39 } 40 41 public UnaryOpExp() { 42 } 43 44 public SqlExp createInstance() { 45 return new UnaryOpExp(); 46 } 47 48 public SqlExp getClone(SqlExp clone, Map cloneMap) { 49 super.getClone(clone, cloneMap); 50 51 ((UnaryOpExp)clone).op = op; 52 53 return clone; 54 } 55 56 public String toString() { 57 String s = super.toString() + " "; 58 switch (op) { 59 case OP_PLUS: 60 return s + "+"; 61 case OP_MINUS: 62 return s + "-"; 63 case OP_NOT: 64 return s + "not"; 65 case OP_COMPLEMENT: 66 return s + "~"; 67 } 68 return s + "unknown(" + op + ")"; 69 } 70 71 75 public SqlExp normalize(SqlDriver driver, SelectExp sel, boolean convertExists) { 76 if (childList instanceof UnaryOpExp && ((UnaryOpExp)childList).op == op) { 77 SqlExp r = childList.childList.normalize(driver, sel, convertExists); 80 if (r != null) { 81 return r; 82 } else { 83 return childList.childList; 84 } 85 } else { 86 SqlExp r = null; 87 if ( childList != null ) 88 r = childList.normalize(driver, sel, convertExists); 89 if (r != null) childList = r; 90 return null; 91 } 92 } 93 94 101 public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) { 102 if (childList instanceof UnaryOpExp && ((UnaryOpExp)childList).op == op) { 105 childList.childList.appendSQL(driver, s, null); 106 return; 107 } 108 109 boolean brackets; 110 switch (op) { 111 case OP_PLUS: 112 childList.appendSQL(driver, s, null); 114 break; 115 case OP_MINUS: 116 s.append('-'); 117 s.append(' '); 118 brackets = !(childList instanceof LiteralExp); 120 if (brackets) s.append('('); 121 childList.appendSQL(driver, s, null); 122 if (brackets) s.append(')'); 123 break; 124 case OP_NOT: 125 if (childList instanceof ColumnExp) { 126 ColumnExp cExp = (ColumnExp)childList; 127 childList.appendSQL(driver, s, null); 128 s.append(" = "); 129 if (cExp.col.converter != null) { 130 DummyPreparedStmt pstmt = new DummyPreparedStmt(); 131 try { 132 cExp.col.converter.set(pstmt, 0, cExp.col, new Boolean ("false")); 133 } catch (SQLException e) { 134 } 136 if (pstmt.toQuote) { 137 s.append("'" + pstmt.value + "'"); 138 } else { 139 s.append(pstmt.value); 140 } 141 } else { 142 s.append("false"); 143 } 144 } else { 145 s.append("not "); 146 s.append('('); 147 childList.appendSQL(driver, s, null); 148 s.append(')'); 149 } 150 break; 151 case OP_COMPLEMENT: 152 s.append('('); 153 s.append('-'); 154 s.append(' '); 155 brackets = !(childList instanceof LiteralExp); 157 if (brackets) s.append('('); 158 childList.appendSQL(driver, s, null); 159 if (brackets) s.append(')'); 160 s.append(')'); 161 s.append(' '); 162 s.append('-'); 163 s.append(' '); 164 s.append('1'); 165 break; 166 default: 167 throw BindingSupportImpl.getInstance().internal("Unknown UnaryOpExp op: " + op); 168 } 169 } 170 171 179 public int getConvertToJoin() { 180 if ( childList == null ) 181 return YES; 182 if (op == OP_NOT) { 183 int ans = childList.getConvertToJoin(); 184 if (ans == YES_DISTINCT) return YES_DISTINCT_NOT; 185 return ans; 186 } else { 187 return NO; 188 } 189 } 190 191 } 192 | Popular Tags |