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 java.util.Vector ; 28 import java.util.Collection ; 29 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.queryframework.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall; 34 import oracle.toplink.essentials.internal.helper.DatabaseField; 35 import oracle.toplink.essentials.internal.helper.DatabaseTable; 36 import java.util.Map ; 37 38 42 public class SQLUpdateAllStatementForOracleAnonymousBlock extends SQLModifyStatement { 43 protected HashMap tables_databaseFieldsToValues; 44 protected HashMap tablesToPrimaryKeyFields; 45 protected SQLCall selectCall; 46 47 protected static final String varSuffix = "_VAR"; 48 protected static final String typeSuffix = "_TYPE"; 49 protected static final String tab = " "; 50 protected static final String dbltab = tab + tab; 51 protected static final String trpltab = dbltab + tab; 52 53 public void setSelectCall(SQLCall selectCall) { 54 this.selectCall = selectCall; 55 } 56 public SQLCall getSelectCall() { 57 return selectCall; 58 } 59 public void setTablesToPrimaryKeyFields(HashMap tablesToPrimaryKeyFields) { 60 this.tablesToPrimaryKeyFields = tablesToPrimaryKeyFields; 61 } 62 public HashMap getTablesToPrimaryKeyFields() { 63 return tablesToPrimaryKeyFields; 64 } 65 public void setTables_databaseFieldsToValues(HashMap tables_databaseFieldsToValues) { 66 this.tables_databaseFieldsToValues = tables_databaseFieldsToValues; 67 } 68 public HashMap getTables_databaseFieldsToValues() { 69 return tables_databaseFieldsToValues; 70 } 71 72 75 public DatabaseCall buildCall(AbstractSession session) { 76 SQLCall call = new SQLCall(); 77 call.returnNothing(); 78 79 Writer writer = new CharArrayWriter(100); 80 81 Vector mainPrimaryKeys = new Vector (); 82 mainPrimaryKeys.addAll((Collection )tablesToPrimaryKeyFields.get(table)); 83 84 Vector allFields = (Vector )mainPrimaryKeys.clone(); 85 Iterator itDatabaseFieldsToValues = tables_databaseFieldsToValues.values().iterator(); 86 while(itDatabaseFieldsToValues.hasNext()) { 87 Iterator itDatabaseFields = ((HashMap )itDatabaseFieldsToValues.next()).keySet().iterator(); 88 while(itDatabaseFields.hasNext()) { 89 allFields.addElement(itDatabaseFields.next()); 90 } 91 } 92 93 try { 94 writer.write("DECLARE\n"); 96 97 for(int i=0; i < allFields.size(); i++) { 98 writeDeclareTypeAndVar(writer, (DatabaseField)allFields.elementAt(i)); 99 } 100 101 writer.write("BEGIN\n"); 103 104 String selectStr = selectCall.getSQLString(); 106 int index = selectStr.toUpperCase().indexOf(" FROM "); 107 String firstPart = selectStr.substring(0, index); 108 String secondPart = selectStr.substring(index, selectStr.length()); 109 110 writer.write(tab); 111 writer.write(firstPart); 112 writer.write(" BULK COLLECT INTO "); 113 114 for(int i=0; i < allFields.size(); i++) { 115 writeVar(writer, (DatabaseField)allFields.elementAt(i)); 116 if(i < allFields.size() - 1) { 117 writer.write(", "); 118 } 119 } 120 writer.write(secondPart); 121 writer.write(";\n"); 122 123 call.getParameters().addAll(selectCall.getParameters()); 124 call.getParameterTypes().addAll(selectCall.getParameterTypes()); 125 126 DatabaseField firstMainPrimaryKey = (DatabaseField)mainPrimaryKeys.firstElement(); 127 writer.write(tab); 128 writer.write("IF "); 129 writeVar(writer, firstMainPrimaryKey); 130 writer.write(".COUNT > 0 THEN\n"); 131 132 Iterator itEntries = tables_databaseFieldsToValues.entrySet().iterator(); 133 while(itEntries.hasNext()) { 134 writeForAll(writer, firstMainPrimaryKey); 135 writer.write(trpltab); 136 writer.write("UPDATE "); 137 Map.Entry entry = (Map.Entry )itEntries.next(); 138 DatabaseTable t = (DatabaseTable)entry.getKey(); 139 writer.write(t.getQualifiedName()); 140 writer.write(" SET "); 141 HashMap databaseFieldsToValues = (HashMap )entry.getValue(); 142 int counter = 0; 143 Iterator itDatabaseFields = databaseFieldsToValues.keySet().iterator(); 144 while(itDatabaseFields.hasNext()) { 145 counter++; 146 DatabaseField field = (DatabaseField)itDatabaseFields.next(); 147 writer.write(field.getName()); 148 writer.write(" = "); 149 writeVar(writer, field); 150 writer.write("(i)"); 151 if(counter < databaseFieldsToValues.size()) { 152 writer.write(", "); 153 } 154 } 155 156 writer.write(" WHERE "); 157 158 Vector tablePrimaryKeys = new Vector (); 159 tablePrimaryKeys.addAll((Collection )tablesToPrimaryKeyFields.get(t)); 160 for(int i=0; i < mainPrimaryKeys.size(); i++) { 161 DatabaseField tableField = (DatabaseField)tablePrimaryKeys.elementAt(i); 162 writer.write(tableField.getName()); 163 writer.write(" = "); 164 DatabaseField mainField = (DatabaseField )mainPrimaryKeys.elementAt(i); 165 writeVar(writer, mainField); 166 writer.write("(i)"); 167 if(i < mainPrimaryKeys.size()-1) { 168 writer.write(" AND "); 169 } else { 170 writer.write(";\n"); 171 } 172 } 173 } 174 175 writer.write(tab); 176 writer.write("END IF;\n"); 177 178 writer.write(tab); 179 DatabaseField outField = new DatabaseField("ROW_COUNT"); 180 outField.setType(Integer .class); 181 call.appendOut(writer, outField); 182 writer.write(" := "); 183 writeVar(writer, firstMainPrimaryKey); 184 writer.write(".COUNT;\n"); 185 186 writer.write("END;"); 187 188 call.setSQLString(writer.toString()); 189 190 } catch (IOException exception) { 191 throw ValidationException.fileError(exception); 192 } 193 194 return call; 195 } 196 197 protected static void writeUniqueFieldName(Writer writer, DatabaseField field) throws IOException { 198 writer.write(field.getTableName()); 200 writer.write("_"); 201 writer.write(field.getName()); 202 } 203 204 protected static void writeType(Writer writer, DatabaseField field) throws IOException { 205 writeUniqueFieldName(writer, field); 207 writer.write(typeSuffix); 208 } 209 210 protected static void writeVar(Writer writer, DatabaseField field) throws IOException { 211 writeUniqueFieldName(writer, field); 213 writer.write(varSuffix); 214 } 215 216 protected static void writeDeclareTypeAndVar(Writer writer, DatabaseField field) throws IOException { 217 writer.write(tab); 219 writer.write("TYPE "); 220 writeType(writer, field); 221 writer.write(" IS TABLE OF "); 222 writer.write(field.getQualifiedName()); 223 writer.write("%TYPE;\n"); 224 225 writer.write(tab); 227 writeVar(writer, field); 228 writer.write(" "); 229 writeType(writer, field); 230 writer.write(";\n"); 231 } 232 233 protected static void writeForAll(Writer writer, DatabaseField field) throws IOException { 234 writer.write(dbltab); 236 writer.write("FORALL i IN "); 237 writeVar(writer, field); 238 writer.write(".FIRST.."); 239 writeVar(writer, field); 240 writer.write(".LAST\n"); 241 } 242 } 243 | Popular Tags |