KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > FetchRequest


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: FetchRequest.java,v 1.7 2004/01/18 03:01:06 jackknifebarber Exp $
9  */

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.Transaction;
17 import java.sql.Connection JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import org.apache.log4j.Category;
22
23
24 class FetchRequest extends RequestUsingFields
25 {
26     private static final Category LOG = Category.getInstance(FetchRequest.class);
27
28     private final int[] columnNumbersByField;
29     private final String JavaDoc textStmt;
30
31     public FetchRequest(ClassBaseTable table, int[] fieldNumbers)
32     {
33         super(table, fieldNumbers);
34
35         if (colFields != null)
36         {
37             FetchStatement fetchStmt = new FetchStatement(table);
38             columnNumbersByField = new int[colFieldMappings.length];
39
40             for (int i = 0; i < colFields.length; ++i)
41             {
42                 int fn = colFields[i];
43                 ColumnMapping cm = colFieldMappings[fn];
44
45                 columnNumbersByField[fn] = fetchStmt.select(cm.getColumn());
46             }
47
48             fetchStmt.andCondition(fetchStmt.referenceColumn(idMapping.getColumn()) + " = ?");
49             textStmt = fetchStmt.toString();
50         }
51         else
52         {
53             columnNumbersByField = null;
54             textStmt = null;
55         }
56     }
57
58     public void execute(final StateManager sm)
59     {
60         if (textStmt != null)
61         {
62             PersistenceManager pm = sm.getPersistenceManager();
63             Transaction tx = (Transaction)pm.currentTransaction();
64             String JavaDoc stmt = textStmt + (tx.useUpdateLockOnFetch() ? " FOR UPDATE" : "");
65
66             try
67             {
68                 Connection JavaDoc conn = tx.getConnection(false);
69
70                 try
71                 {
72                     PreparedStatement JavaDoc ps = conn.prepareStatement(stmt);
73
74                     try
75                     {
76                         OID id = (OID)sm.getObjectId();
77                         idMapping.setObject(pm, ps, 1, id);
78
79                         long startTime = System.currentTimeMillis();
80
81                         ResultSet JavaDoc rs = ps.executeQuery();
82
83                         try
84                         {
85                             if (LOG.isDebugEnabled())
86                                 LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + stmt);
87
88                             if (!rs.next())
89                                 throw new ObjectNotFoundException("No such database row", sm.getObject());
90
91                             sm.replaceFields(colFields, new ResultSetGetter(pm, rs, colFieldMappings, columnNumbersByField));
92                         }
93                         finally
94                         {
95                             rs.close();
96                         }
97                     }
98                     finally
99                     {
100                         ps.close();
101                     }
102                 }
103                 finally
104                 {
105                     tx.releaseConnection(conn);
106                 }
107             }
108             catch (SQLException JavaDoc e)
109             {
110                 throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Fetch request failed: " + stmt, e);
111             }
112         }
113
114         if (cpxFields != null)
115         {
116             sm.replaceFields(cpxFields, new GenericFieldManager()
117             {
118                 public Object JavaDoc fetchObjectField(int field)
119                 {
120                     return cpxFieldMappings[field].fetchObject(sm);
121                 }
122
123                 public void storeObjectField(int field, Object JavaDoc value) {} // not possible
124
});
125         }
126     }
127 }
128
Popular Tags