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: FetchRequest.java,v 1.7 2004/01/18 03:01:06 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.Transaction;
017: import java.sql.Connection;
018: import java.sql.PreparedStatement;
019: import java.sql.ResultSet;
020: import java.sql.SQLException;
021: import org.apache.log4j.Category;
022:
023: class FetchRequest extends RequestUsingFields {
024: private static final Category LOG = Category
025: .getInstance(FetchRequest.class);
026:
027: private final int[] columnNumbersByField;
028: private final String textStmt;
029:
030: public FetchRequest(ClassBaseTable table, int[] fieldNumbers) {
031: super (table, fieldNumbers);
032:
033: if (colFields != null) {
034: FetchStatement fetchStmt = new FetchStatement(table);
035: columnNumbersByField = new int[colFieldMappings.length];
036:
037: for (int i = 0; i < colFields.length; ++i) {
038: int fn = colFields[i];
039: ColumnMapping cm = colFieldMappings[fn];
040:
041: columnNumbersByField[fn] = fetchStmt.select(cm
042: .getColumn());
043: }
044:
045: fetchStmt.andCondition(fetchStmt.referenceColumn(idMapping
046: .getColumn())
047: + " = ?");
048: textStmt = fetchStmt.toString();
049: } else {
050: columnNumbersByField = null;
051: textStmt = null;
052: }
053: }
054:
055: public void execute(final StateManager sm) {
056: if (textStmt != null) {
057: PersistenceManager pm = sm.getPersistenceManager();
058: Transaction tx = (Transaction) pm.currentTransaction();
059: String stmt = textStmt
060: + (tx.useUpdateLockOnFetch() ? " FOR UPDATE" : "");
061:
062: try {
063: Connection conn = tx.getConnection(false);
064:
065: try {
066: PreparedStatement ps = conn.prepareStatement(stmt);
067:
068: try {
069: OID id = (OID) sm.getObjectId();
070: idMapping.setObject(pm, ps, 1, id);
071:
072: long startTime = System.currentTimeMillis();
073:
074: ResultSet rs = ps.executeQuery();
075:
076: try {
077: if (LOG.isDebugEnabled())
078: LOG
079: .debug("Time = "
080: + (System
081: .currentTimeMillis() - startTime)
082: + " ms: " + stmt);
083:
084: if (!rs.next())
085: throw new ObjectNotFoundException(
086: "No such database row", sm
087: .getObject());
088:
089: sm.replaceFields(colFields,
090: new ResultSetGetter(pm, rs,
091: colFieldMappings,
092: columnNumbersByField));
093: } finally {
094: rs.close();
095: }
096: } finally {
097: ps.close();
098: }
099: } finally {
100: tx.releaseConnection(conn);
101: }
102: } catch (SQLException e) {
103: throw pm.getStoreManager().getDatabaseAdapter()
104: .newDataStoreException(
105: "Fetch request failed: " + stmt, e);
106: }
107: }
108:
109: if (cpxFields != null) {
110: sm.replaceFields(cpxFields, new GenericFieldManager() {
111: public Object fetchObjectField(int field) {
112: return cpxFieldMappings[field].fetchObject(sm);
113: }
114:
115: public void storeObjectField(int field, Object value) {
116: } // not possible
117: });
118: }
119: }
120: }
|