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: InsertRequest.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 InsertRequest extends RequestUsingFields {
023: private static final Category LOG = Category
024: .getInstance(InsertRequest.class);
025:
026: private static final int IDPARAMNUMBER = 1;
027:
028: private final int[] paramNumbersByField;
029: private final String textStmt;
030:
031: /**
032: * The fields involved in this request whose ColumnMappings require post-
033: * processing after INSERT.
034: * Null if there are no such fields.
035: */
036: private final int[] postFields;
037:
038: /**
039: * The ColumnMappings requiring post-processing after INSERT, indexed by
040: * absolute field number.
041: * Null if {@link #postFields} is null.
042: */
043: private final PostInsertProcessing[] postFieldMappings;
044:
045: public InsertRequest(ClassBaseTable table) {
046: super (table);
047:
048: StringBuffer colNames = new StringBuffer(idMapping.getColumn()
049: .getName().toString());
050: StringBuffer colValues = new StringBuffer("?");
051:
052: if (colFields != null) {
053: paramNumbersByField = new int[colFieldMappings.length];
054: int paramNumber = IDPARAMNUMBER + 1;
055:
056: IntArrayList postfn = new IntArrayList(colFields.length);
057: PostInsertProcessing[] postfm = new PostInsertProcessing[colFieldMappings.length];
058:
059: for (int i = 0; i < colFields.length; ++i) {
060: int fn = colFields[i];
061: ColumnMapping cm = colFieldMappings[fn];
062: String val = cm.getSQLInsertionValue();
063: colNames.append(',').append(cm.getColumn().getName());
064: colValues.append(',').append(val);
065:
066: if (val.equals("?"))
067: paramNumbersByField[fn] = paramNumber++;
068:
069: if (cm instanceof PostInsertProcessing) {
070: postfn.add(fn);
071: postfm[fn] = (PostInsertProcessing) cm;
072: }
073: }
074:
075: if (postfn.isEmpty()) {
076: postFields = null;
077: postFieldMappings = null;
078: } else {
079: postFields = postfn.toArray();
080: postFieldMappings = postfm;
081: }
082: } else {
083: paramNumbersByField = null;
084: postFields = null;
085: postFieldMappings = null;
086: }
087:
088: textStmt = "INSERT INTO " + table.getName() + " (" + colNames
089: + ") VALUES (" + colValues + ")";
090: }
091:
092: public void execute(final StateManager sm) {
093: PersistenceManager pm = sm.getPersistenceManager();
094:
095: try {
096: final Connection conn = pm.getConnection(true);
097:
098: try {
099: PreparedStatement ps = conn.prepareStatement(textStmt);
100:
101: try {
102: idMapping.setObject(pm, ps, IDPARAMNUMBER, sm
103: .getObjectId());
104:
105: if (colFields != null)
106: sm.provideFields(colFields,
107: new ParameterSetter(pm, ps,
108: colFieldMappings,
109: paramNumbersByField));
110:
111: long startTime = System.currentTimeMillis();
112:
113: ps.executeUpdate();
114:
115: if (LOG.isDebugEnabled())
116: LOG
117: .debug("Time = "
118: + (System.currentTimeMillis() - startTime)
119: + " ms: " + textStmt);
120: } finally {
121: ps.close();
122: }
123:
124: if (postFields != null) {
125: sm.provideFields(postFields,
126: new GenericFieldManager() {
127: public Object fetchObjectField(int field) {
128: return null;
129: } // not possible
130:
131: public void storeObjectField(int field,
132: Object value) {
133: postFieldMappings[field]
134: .postInsert(sm, conn, value);
135: }
136: });
137: }
138: } finally {
139: pm.releaseConnection(conn);
140: }
141: } catch (SQLException e) {
142: throw pm.getStoreManager().getDatabaseAdapter()
143: .newDataStoreException(
144: "Insert request failed: " + textStmt, e);
145: }
146:
147: if (cpxFields != null) {
148: sm.provideFields(cpxFields, new GenericFieldManager() {
149: public Object fetchObjectField(int field) {
150: return null;
151: } // not possible
152:
153: public void storeObjectField(int field, Object value) {
154: cpxFieldMappings[field].insertObject(sm, value);
155: }
156: });
157: }
158: }
159: }
|