1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.internal.helper.*; 27 import oracle.toplink.essentials.queryframework.*; 28 import oracle.toplink.essentials.exceptions.*; 29 import oracle.toplink.essentials.expressions.*; 30 import oracle.toplink.essentials.internal.databaseaccess.*; 31 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 34 45 public class ExpressionSQLPrinter { 46 47 51 protected AbstractSession session; 52 53 56 protected SQLCall call; 57 58 61 protected AbstractRecord translationRow; 62 63 67 protected boolean shouldPrintQualifiedNames; 68 69 protected Writer writer; 71 72 73 protected boolean requiresDistinct; 74 75 protected boolean isFirstElementPrinted; 77 78 public ExpressionSQLPrinter(AbstractSession session, AbstractRecord translationRow, SQLCall call, boolean printQualifiedNames) { 79 this.session = session; 80 this.translationRow = translationRow; 81 this.call = call; 82 this.shouldPrintQualifiedNames = printQualifiedNames; 83 this.requiresDistinct = false; 84 isFirstElementPrinted = false; 85 } 86 87 90 protected SQLCall getCall() { 91 return call; 92 } 93 94 98 public DatabasePlatform getPlatform() { 99 return session.getPlatform(); 100 } 101 102 protected AbstractSession getSession() { 103 return session; 104 } 105 106 110 protected AbstractRecord getTranslationRow() { 111 return translationRow; 112 } 113 114 public Writer getWriter() { 115 return writer; 116 } 117 118 122 public boolean isFirstElementPrinted() { 123 return isFirstElementPrinted; 124 } 125 126 public void printExpression(Expression expression) { 127 translateExpression(expression); 128 } 129 130 public void printField(DatabaseField field) { 131 if (field == null) { 132 return; 133 } 134 135 try { 136 if (shouldPrintQualifiedNames()) { 138 getWriter().write(field.getQualifiedName()); 139 } else { 140 getWriter().write(field.getName()); 141 } 142 } catch (IOException exception) { 143 throw ValidationException.fileError(exception); 144 } 145 } 146 147 public void printParameter(ParameterExpression expression) { 148 try { 149 getCall().appendTranslationParameter(getWriter(), expression, getPlatform()); 150 151 } catch (IOException exception) { 152 throw ValidationException.fileError(exception); 153 } 154 } 155 156 public void printParameter(DatabaseField field) { 157 getCall().appendTranslation(getWriter(), field); 158 } 159 160 public void printPrimitive(Object value) { 161 if (value instanceof Vector) { 162 printValuelist((Vector)value); 163 return; 164 } 165 166 session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value); 167 } 168 169 public void printString(String value) { 170 try { 171 getWriter().write(value); 172 173 } catch (IOException exception) { 174 throw ValidationException.fileError(exception); 175 } 176 } 177 178 public void printValuelist(Vector values) { 179 try { 180 Enumeration valuesEnum = values.elements(); 181 while (valuesEnum.hasMoreElements()) { 182 Object value = valuesEnum.nextElement(); 183 session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value); 184 if (valuesEnum.hasMoreElements()) { 185 getWriter().write(", "); 186 } 187 } 188 } catch (IOException exception) { 189 throw ValidationException.fileError(exception); 190 } 191 } 192 193 197 public boolean requiresDistinct() { 198 return requiresDistinct; 199 } 200 201 protected void setCall(SQLCall call) { 202 this.call = call; 203 } 204 205 209 public void setIsFirstElementPrinted(boolean isFirstElementPrinted) { 210 this.isFirstElementPrinted = isFirstElementPrinted; 211 } 212 213 217 public void setRequiresDistinct(boolean requiresDistinct) { 218 this.requiresDistinct = requiresDistinct; 219 } 220 221 protected void setSession(AbstractSession theSession) { 222 session = theSession; 223 } 224 225 protected void setShouldPrintQualifiedNames(boolean shouldPrintQualifiedNames) { 226 this.shouldPrintQualifiedNames = shouldPrintQualifiedNames; 227 } 228 229 233 protected void setTranslationRow(AbstractRecord theRow) { 234 translationRow = theRow; 235 } 236 237 public void setWriter(Writer writer) { 238 this.writer = writer; 239 } 240 241 public boolean shouldPrintParameterValues() { 242 return getTranslationRow() != null; 243 } 244 245 protected boolean shouldPrintQualifiedNames() { 246 return shouldPrintQualifiedNames; 247 } 248 249 255 protected void translateExpression(Expression theExpression) { 256 theExpression.printSQL(this); 257 } 258 } 259 | Popular Tags |