1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.GenericFieldManager; 14 import com.triactive.jdo.PersistenceManager; 15 import com.triactive.jdo.StateManager; 16 import com.triactive.jdo.util.IntArrayList; 17 import java.sql.Connection ; 18 import java.sql.PreparedStatement ; 19 import java.sql.SQLException ; 20 import org.apache.log4j.Category; 21 22 23 class UpdateRequest extends RequestUsingFields 24 { 25 private static final Category LOG = Category.getInstance(UpdateRequest.class); 26 27 private final int idParamNumber; 28 private final int[] paramNumbersByField; 29 private final String textStmt; 30 31 36 private final int[] postFields; 37 38 43 private final PostUpdateProcessing[] postFieldMappings; 44 45 46 public UpdateRequest(ClassBaseTable table, int[] fieldNumbers) 47 { 48 super(table, fieldNumbers); 49 50 if (colFields != null) 51 { 52 StringBuffer assignments = new StringBuffer (); 53 paramNumbersByField = new int[colFieldMappings.length]; 54 int paramNumber = 1; 55 56 IntArrayList postfn = new IntArrayList(colFields.length); 57 PostUpdateProcessing[] postfm = new PostUpdateProcessing[colFieldMappings.length]; 58 59 for (int i = 0; i < colFields.length; ++i) 60 { 61 int fn = colFields[i]; 62 ColumnMapping cm = colFieldMappings[fn]; 63 String val = cm.getSQLUpdateValue(); 64 65 if (i > 0) 66 assignments.append(", "); 67 68 assignments.append(cm.getColumn().getName()).append(" = ").append(val); 69 70 if (val.equals("?")) 71 paramNumbersByField[fn] = paramNumber++; 72 73 if (cm instanceof PostUpdateProcessing) 74 { 75 postfn.add(fn); 76 postfm[fn] = (PostUpdateProcessing)cm; 77 } 78 } 79 80 idParamNumber = paramNumber; 81 82 textStmt = "UPDATE " + table.getName() + " SET " + assignments + " WHERE " + idMapping.getColumn().getName() + " = ?"; 83 84 if (postfn.isEmpty()) 85 { 86 postFields = null; 87 postFieldMappings = null; 88 } 89 else 90 { 91 postFields = postfn.toArray(); 92 postFieldMappings = postfm; 93 } 94 } 95 else 96 { 97 idParamNumber = -1; 98 paramNumbersByField = null; 99 textStmt = null; 100 postFields = null; 101 postFieldMappings = null; 102 } 103 } 104 105 public void execute(final StateManager sm) 106 { 107 if (textStmt != null) 108 { 109 PersistenceManager pm = sm.getPersistenceManager(); 110 111 try 112 { 113 final Connection conn = pm.getConnection(true); 114 115 try 116 { 117 PreparedStatement ps = conn.prepareStatement(textStmt); 118 119 try 120 { 121 sm.provideFields(colFields, new ParameterSetter(pm, ps, colFieldMappings, paramNumbersByField)); 122 idMapping.setObject(pm, ps, idParamNumber, sm.getObjectId()); 123 124 long startTime = System.currentTimeMillis(); 125 126 ps.executeUpdate(); 127 128 if (LOG.isDebugEnabled()) 129 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + textStmt); 130 } 131 finally 132 { 133 ps.close(); 134 } 135 136 if (postFields != null) 137 { 138 sm.provideFields(postFields, new GenericFieldManager() 139 { 140 public Object fetchObjectField(int field) { return null; } 142 public void storeObjectField(int field, Object value) 143 { 144 postFieldMappings[field].postUpdate(sm, conn, value); 145 } 146 }); 147 } 148 } 149 finally 150 { 151 pm.releaseConnection(conn); 152 } 153 } 154 catch (SQLException e) 155 { 156 throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Update request failed: " + textStmt, e); 157 } 158 } 159 160 if (cpxFields != null) 161 { 162 sm.provideFields(cpxFields, new GenericFieldManager() 163 { 164 public Object fetchObjectField(int field) { return null; } 166 public void storeObjectField(int field, Object value) 167 { 168 cpxFieldMappings[field].updateObject(sm, value); 169 } 170 }); 171 } 172 } 173 } 174 | Popular Tags |