001: /*
002: * Copyright 2004 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: UpdateRequest.java,v 1.7 2004/02/01 18:22:42 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.GenericFieldManager;
014: import com.triactive.jdo.PersistenceManager;
015: import com.triactive.jdo.StateManager;
016: import com.triactive.jdo.util.IntArrayList;
017: import java.sql.Connection;
018: import java.sql.PreparedStatement;
019: import java.sql.SQLException;
020: import org.apache.log4j.Category;
021:
022: class UpdateRequest extends RequestUsingFields {
023: private static final Category LOG = Category
024: .getInstance(UpdateRequest.class);
025:
026: private final int idParamNumber;
027: private final int[] paramNumbersByField;
028: private final String textStmt;
029:
030: /**
031: * The fields involved in this request whose ColumnMappings require post-
032: * processing after UPDATE.
033: * Null if there are no such fields.
034: */
035: private final int[] postFields;
036:
037: /**
038: * The ColumnMappings requiring post-processing after UPDATE, indexed by
039: * absolute field number.
040: * Null if {@link #postFields} is null.
041: */
042: private final PostUpdateProcessing[] postFieldMappings;
043:
044: public UpdateRequest(ClassBaseTable table, int[] fieldNumbers) {
045: super (table, fieldNumbers);
046:
047: if (colFields != null) {
048: StringBuffer assignments = new StringBuffer();
049: paramNumbersByField = new int[colFieldMappings.length];
050: int paramNumber = 1;
051:
052: IntArrayList postfn = new IntArrayList(colFields.length);
053: PostUpdateProcessing[] postfm = new PostUpdateProcessing[colFieldMappings.length];
054:
055: for (int i = 0; i < colFields.length; ++i) {
056: int fn = colFields[i];
057: ColumnMapping cm = colFieldMappings[fn];
058: String val = cm.getSQLUpdateValue();
059:
060: if (i > 0)
061: assignments.append(", ");
062:
063: assignments.append(cm.getColumn().getName()).append(
064: " = ").append(val);
065:
066: if (val.equals("?"))
067: paramNumbersByField[fn] = paramNumber++;
068:
069: if (cm instanceof PostUpdateProcessing) {
070: postfn.add(fn);
071: postfm[fn] = (PostUpdateProcessing) cm;
072: }
073: }
074:
075: idParamNumber = paramNumber;
076:
077: textStmt = "UPDATE " + table.getName() + " SET "
078: + assignments + " WHERE "
079: + idMapping.getColumn().getName() + " = ?";
080:
081: if (postfn.isEmpty()) {
082: postFields = null;
083: postFieldMappings = null;
084: } else {
085: postFields = postfn.toArray();
086: postFieldMappings = postfm;
087: }
088: } else {
089: idParamNumber = -1;
090: paramNumbersByField = null;
091: textStmt = null;
092: postFields = null;
093: postFieldMappings = null;
094: }
095: }
096:
097: public void execute(final StateManager sm) {
098: if (textStmt != null) {
099: PersistenceManager pm = sm.getPersistenceManager();
100:
101: try {
102: final Connection conn = pm.getConnection(true);
103:
104: try {
105: PreparedStatement ps = conn
106: .prepareStatement(textStmt);
107:
108: try {
109: sm.provideFields(colFields,
110: new ParameterSetter(pm, ps,
111: colFieldMappings,
112: paramNumbersByField));
113: idMapping.setObject(pm, ps, idParamNumber, sm
114: .getObjectId());
115:
116: long startTime = System.currentTimeMillis();
117:
118: ps.executeUpdate();
119:
120: if (LOG.isDebugEnabled())
121: LOG
122: .debug("Time = "
123: + (System
124: .currentTimeMillis() - startTime)
125: + " ms: " + textStmt);
126: } finally {
127: ps.close();
128: }
129:
130: if (postFields != null) {
131: sm.provideFields(postFields,
132: new GenericFieldManager() {
133: public Object fetchObjectField(
134: int field) {
135: return null;
136: } // not possible
137:
138: public void storeObjectField(
139: int field, Object value) {
140: postFieldMappings[field]
141: .postUpdate(sm, conn,
142: value);
143: }
144: });
145: }
146: } finally {
147: pm.releaseConnection(conn);
148: }
149: } catch (SQLException e) {
150: throw pm
151: .getStoreManager()
152: .getDatabaseAdapter()
153: .newDataStoreException(
154: "Update request failed: " + textStmt, e);
155: }
156: }
157:
158: if (cpxFields != null) {
159: sm.provideFields(cpxFields, new GenericFieldManager() {
160: public Object fetchObjectField(int field) {
161: return null;
162: } // not possible
163:
164: public void storeObjectField(int field, Object value) {
165: cpxFieldMappings[field].updateObject(sm, value);
166: }
167: });
168: }
169: }
170: }
|