01: /*
02: * Copyright 2004 (C) TJDO.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the TJDO License version 1.0.
06: * See the terms of the TJDO License in the documentation provided with this software.
07: *
08: * $Id: LookupRequest.java,v 1.1 2004/01/18 03:01:06 jackknifebarber Exp $
09: */
10:
11: package com.triactive.jdo.store;
12:
13: import com.triactive.jdo.PersistenceManager;
14: import com.triactive.jdo.StateManager;
15: import java.sql.Connection;
16: import java.sql.PreparedStatement;
17: import java.sql.ResultSet;
18: import java.sql.SQLException;
19: import org.apache.log4j.Category;
20:
21: class LookupRequest extends Request {
22: private static final Category LOG = Category
23: .getInstance(LookupRequest.class);
24:
25: private final String textStmt;
26:
27: public LookupRequest(ClassBaseTable table) {
28: super (table);
29:
30: FetchStatement fetchStmt = new FetchStatement(table);
31: Column idCol = idMapping.getColumn();
32:
33: fetchStmt.select(idCol);
34: fetchStmt.andCondition(fetchStmt.referenceColumn(idCol)
35: + " = ?");
36:
37: textStmt = fetchStmt.toString();
38: }
39:
40: public void execute(StateManager sm) {
41: PersistenceManager pm = sm.getPersistenceManager();
42:
43: try {
44: Connection conn = pm.getConnection(false);
45:
46: try {
47: PreparedStatement ps = conn.prepareStatement(textStmt);
48:
49: try {
50: idMapping.setObject(pm, ps, 1, sm.getObjectId());
51:
52: long startTime = System.currentTimeMillis();
53:
54: ResultSet rs = ps.executeQuery();
55:
56: try {
57: if (LOG.isDebugEnabled())
58: LOG
59: .debug("Time = "
60: + (System
61: .currentTimeMillis() - startTime)
62: + " ms: " + textStmt);
63:
64: if (!rs.next())
65: throw new ObjectNotFoundException(
66: "No such database row", sm
67: .getObject());
68: } finally {
69: rs.close();
70: }
71: } finally {
72: ps.close();
73: }
74: } finally {
75: pm.releaseConnection(conn);
76: }
77: } catch (SQLException e) {
78: throw pm.getStoreManager().getDatabaseAdapter()
79: .newDataStoreException(
80: "Lookup request failed: " + textStmt, e);
81: }
82: }
83: }
|