1 21 package oracle.toplink.essentials.internal.expressions; 23 24 import java.io.*; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import oracle.toplink.essentials.exceptions.*; 28 import oracle.toplink.essentials.expressions.*; 29 import oracle.toplink.essentials.queryframework.*; 30 import oracle.toplink.essentials.internal.sessions.AbstractSession; 31 import java.util.Collection ; 32 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall; 33 import oracle.toplink.essentials.internal.helper.DatabaseField; 34 35 39 public class SQLUpdateAllStatement extends SQLModifyStatement { 40 protected HashMap m_updateClauses; 41 protected HashMap databaseFieldsToTableAliases; 42 43 protected SQLCall selectCallForExist; 44 protected String tableAliasInSelectCallForExist; 45 protected Collection primaryKeyFields; 46 47 public void setSelectCallForExist(SQLCall selectCallForExist) { 48 this.selectCallForExist = selectCallForExist; 49 } 50 public SQLCall getSelectCallForExist() { 51 return selectCallForExist; 52 } 53 public void setTableAliasInSelectCallForExist(String tableAliasInSelectCallForExist) { 54 this.tableAliasInSelectCallForExist = tableAliasInSelectCallForExist; 55 } 56 public String getTableAliasInSelectCallForExist() { 57 return tableAliasInSelectCallForExist; 58 } 59 public void setPrimaryKeyFieldsForAutoJoin(Collection primaryKeyFields) { 60 this.primaryKeyFields = primaryKeyFields; 61 } 62 public Collection getPrimaryKeyFieldsForAutoJoin() { 63 return primaryKeyFields; 64 } 65 public void setUpdateClauses(HashMap updateClauses) { 66 m_updateClauses = updateClauses; 67 } 68 public HashMap getUpdateClauses() { 69 return m_updateClauses; 70 } 71 public void setDatabaseFieldsToTableAliases(HashMap databaseFieldsToTableAliases) { 72 this.databaseFieldsToTableAliases = databaseFieldsToTableAliases; 73 } 74 public HashMap getDatabaseFieldsToTableAliases() { 75 return databaseFieldsToTableAliases; 76 } 77 78 81 public DatabaseCall buildCall(AbstractSession session) { 82 SQLCall call = buildSimple(session); 83 if(selectCallForExist == null) { 84 return call; 85 } 86 Writer writer = new CharArrayWriter(100); 87 try { 88 writer.write(call.getSQLString()); 89 90 if(selectCallForExist != null) { 91 writer.write(" WHERE EXISTS("); 92 writeSelect(writer, selectCallForExist, tableAliasInSelectCallForExist, call); 95 writer.write(")"); 97 } 98 99 call.setSQLString(writer.toString()); 100 101 } catch (IOException exception) { 102 throw ValidationException.fileError(exception); 103 } 104 105 return call; 106 } 107 108 protected SQLCall buildSimple(AbstractSession session) { 109 SQLCall call = new SQLCall(); 110 call.returnNothing(); 111 Writer writer = new CharArrayWriter(100); 112 ExpressionSQLPrinter printer = new ExpressionSQLPrinter(session, getTranslationRow(), call, false); 113 printer.setWriter(writer); 114 115 try { 116 writer.write("UPDATE "); 118 writer.write(getTable().getQualifiedName()); 119 writer.write(" SET "); 121 122 Iterator i = m_updateClauses.keySet().iterator(); 123 boolean commaNeeded = false; 124 125 while (i.hasNext()) { 126 if (commaNeeded) { 127 writer.write(", "); 128 } 129 130 DatabaseField field = (DatabaseField)i.next(); 131 Object value = m_updateClauses.get(field); 132 133 writer.write(field.getName()); 134 writer.write(" = "); 135 if(value instanceof Expression) { 136 printer.printExpression((Expression)value); 137 } else { 138 SQLCall selCall = (SQLCall)value; 140 String tableAlias = (String )getDatabaseFieldsToTableAliases().get(field); 141 writer.write("("); 143 writeSelect(writer, selCall, tableAlias, call); 144 writer.write(")"); 145 } 146 147 commaNeeded = true; 148 } 149 150 if (getWhereClause() != null) { 152 writer.write(" WHERE "); 153 printer.printExpression(getWhereClause()); 154 } 155 156 call.setSQLString(writer.toString()); 157 return call; 158 } catch (IOException exception) { 159 throw ValidationException.fileError(exception); 160 } 161 } 162 163 protected void writeSelect(Writer writer, SQLCall selectCall, String tableAliasInSelectCall, SQLCall call) throws IOException { 164 writer.write(selectCall.getSQLString()); 165 166 Iterator it = getPrimaryKeyFieldsForAutoJoin().iterator(); 169 while(it.hasNext()) { 170 writer.write(" AND "); 171 String fieldName = ((DatabaseField)it.next()).getName(); 172 if(tableAliasInSelectCall != null) { 173 writer.write(tableAliasInSelectCall); 174 writer.write('.'); 175 } 176 writer.write(fieldName); 177 writer.write(" = "); 178 writer.write(table.getQualifiedName()); 179 writer.write('.'); 180 writer.write(fieldName); 181 } 182 183 call.getParameters().addAll(selectCall.getParameters()); 184 call.getParameterTypes().addAll(selectCall.getParameterTypes()); 185 } 186 } 187 | Popular Tags |