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 InsertRequest extends RequestUsingFields 24 { 25 private static final Category LOG = Category.getInstance(InsertRequest.class); 26 27 private static final int IDPARAMNUMBER = 1; 28 29 private final int[] paramNumbersByField; 30 private final String textStmt; 31 32 37 private final int[] postFields; 38 39 44 private final PostInsertProcessing[] postFieldMappings; 45 46 47 public InsertRequest(ClassBaseTable table) 48 { 49 super(table); 50 51 StringBuffer colNames = new StringBuffer (idMapping.getColumn().getName().toString()); 52 StringBuffer colValues = new StringBuffer ("?"); 53 54 if (colFields != null) 55 { 56 paramNumbersByField = new int[colFieldMappings.length]; 57 int paramNumber = IDPARAMNUMBER + 1; 58 59 IntArrayList postfn = new IntArrayList(colFields.length); 60 PostInsertProcessing[] postfm = new PostInsertProcessing[colFieldMappings.length]; 61 62 for (int i = 0; i < colFields.length; ++i) 63 { 64 int fn = colFields[i]; 65 ColumnMapping cm = colFieldMappings[fn]; 66 String val = cm.getSQLInsertionValue(); 67 colNames.append(',').append(cm.getColumn().getName()); 68 colValues.append(',').append(val); 69 70 if (val.equals("?")) 71 paramNumbersByField[fn] = paramNumber++; 72 73 if (cm instanceof PostInsertProcessing) 74 { 75 postfn.add(fn); 76 postfm[fn] = (PostInsertProcessing)cm; 77 } 78 } 79 80 if (postfn.isEmpty()) 81 { 82 postFields = null; 83 postFieldMappings = null; 84 } 85 else 86 { 87 postFields = postfn.toArray(); 88 postFieldMappings = postfm; 89 } 90 } 91 else 92 { 93 paramNumbersByField = null; 94 postFields = null; 95 postFieldMappings = null; 96 } 97 98 textStmt = "INSERT INTO " + table.getName() + " (" + colNames + ") VALUES (" + colValues + ")"; 99 } 100 101 public void execute(final StateManager sm) 102 { 103 PersistenceManager pm = sm.getPersistenceManager(); 104 105 try 106 { 107 final Connection conn = pm.getConnection(true); 108 109 try 110 { 111 PreparedStatement ps = conn.prepareStatement(textStmt); 112 113 try 114 { 115 idMapping.setObject(pm, ps, IDPARAMNUMBER, sm.getObjectId()); 116 117 if (colFields != null) 118 sm.provideFields(colFields, new ParameterSetter(pm, ps, colFieldMappings, paramNumbersByField)); 119 120 long startTime = System.currentTimeMillis(); 121 122 ps.executeUpdate(); 123 124 if (LOG.isDebugEnabled()) 125 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + textStmt); 126 } 127 finally 128 { 129 ps.close(); 130 } 131 132 if (postFields != null) 133 { 134 sm.provideFields(postFields, new GenericFieldManager() 135 { 136 public Object fetchObjectField(int field) { return null; } 138 public void storeObjectField(int field, Object value) 139 { 140 postFieldMappings[field].postInsert(sm, conn, value); 141 } 142 }); 143 } 144 } 145 finally 146 { 147 pm.releaseConnection(conn); 148 } 149 } 150 catch (SQLException e) 151 { 152 throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Insert request failed: " + textStmt, e); 153 } 154 155 if (cpxFields != null) 156 { 157 sm.provideFields(cpxFields, new GenericFieldManager() 158 { 159 public Object fetchObjectField(int field) { return null; } 161 public void storeObjectField(int field, Object value) 162 { 163 cpxFieldMappings[field].insertObject(sm, value); 164 } 165 }); 166 } 167 } 168 } 169 | Popular Tags |