KickJava   Java API By Example, From Geeks To Geeks.

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


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: UpdateRequest.java,v 1.7 2004/02/01 18:22:42 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.util.IntArrayList;
17 import java.sql.Connection JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import org.apache.log4j.Category;
21
22
23 class UpdateRequest extends RequestUsingFields
24 {
25     private static final Category LOG = Category.getInstance(UpdateRequest.class);
26
27     private final int idParamNumber;
28     private final int[] paramNumbersByField;
29     private final String JavaDoc textStmt;
30
31     /**
32      * The fields involved in this request whose ColumnMappings require post-
33      * processing after UPDATE.
34      * Null if there are no such fields.
35      */

36     private final int[] postFields;
37
38     /**
39      * The ColumnMappings requiring post-processing after UPDATE, indexed by
40      * absolute field number.
41      * Null if {@link #postFields} is null.
42      */

43     private final PostUpdateProcessing[] postFieldMappings;
44
45
46     public UpdateRequest(ClassBaseTable table, int[] fieldNumbers)
47     {
48         super(table, fieldNumbers);
49
50         if (colFields != null)
51         {
52             StringBuffer JavaDoc assignments = new StringBuffer JavaDoc();
53             paramNumbersByField = new int[colFieldMappings.length];
54             int paramNumber = 1;
55
56             IntArrayList postfn = new IntArrayList(colFields.length);
57             PostUpdateProcessing[] postfm = new PostUpdateProcessing[colFieldMappings.length];
58
59             for (int i = 0; i < colFields.length; ++i)
60             {
61                 int fn = colFields[i];
62                 ColumnMapping cm = colFieldMappings[fn];
63                 String JavaDoc val = cm.getSQLUpdateValue();
64
65                 if (i > 0)
66                     assignments.append(", ");
67
68                 assignments.append(cm.getColumn().getName()).append(" = ").append(val);
69
70                 if (val.equals("?"))
71                     paramNumbersByField[fn] = paramNumber++;
72
73                 if (cm instanceof PostUpdateProcessing)
74                 {
75                     postfn.add(fn);
76                     postfm[fn] = (PostUpdateProcessing)cm;
77                 }
78             }
79
80             idParamNumber = paramNumber;
81
82             textStmt = "UPDATE " + table.getName() + " SET " + assignments + " WHERE " + idMapping.getColumn().getName() + " = ?";
83
84             if (postfn.isEmpty())
85             {
86                 postFields = null;
87                 postFieldMappings = null;
88             }
89             else
90             {
91                 postFields = postfn.toArray();
92                 postFieldMappings = postfm;
93             }
94         }
95         else
96         {
97             idParamNumber = -1;
98             paramNumbersByField = null;
99             textStmt = null;
100             postFields = null;
101             postFieldMappings = null;
102         }
103     }
104
105     public void execute(final StateManager sm)
106     {
107         if (textStmt != null)
108         {
109             PersistenceManager pm = sm.getPersistenceManager();
110
111             try
112             {
113                 final Connection JavaDoc conn = pm.getConnection(true);
114
115                 try
116                 {
117                     PreparedStatement JavaDoc ps = conn.prepareStatement(textStmt);
118
119                     try
120                     {
121                         sm.provideFields(colFields, new ParameterSetter(pm, ps, colFieldMappings, paramNumbersByField));
122                         idMapping.setObject(pm, ps, idParamNumber, sm.getObjectId());
123
124                         long startTime = System.currentTimeMillis();
125
126                         ps.executeUpdate();
127
128                         if (LOG.isDebugEnabled())
129                             LOG.debug("Time = " + (System.currentTimeMillis() - startTime) + " ms: " + textStmt);
130                     }
131                     finally
132                     {
133                         ps.close();
134                     }
135
136                     if (postFields != null)
137                     {
138                         sm.provideFields(postFields, new GenericFieldManager()
139                         {
140                             public Object JavaDoc fetchObjectField(int field) { return null; } // not possible
141

142                             public void storeObjectField(int field, Object JavaDoc value)
143                             {
144                                 postFieldMappings[field].postUpdate(sm, conn, value);
145                             }
146                         });
147                     }
148                 }
149                 finally
150                 {
151                     pm.releaseConnection(conn);
152                 }
153             }
154             catch (SQLException JavaDoc e)
155             {
156                 throw pm.getStoreManager().getDatabaseAdapter().newDataStoreException("Update request failed: " + textStmt, e);
157             }
158         }
159
160         if (cpxFields != null)
161         {
162             sm.provideFields(cpxFields, new GenericFieldManager()
163             {
164                 public Object JavaDoc fetchObjectField(int field) { return null; } // not possible
165

166                 public void storeObjectField(int field, Object JavaDoc value)
167                 {
168                     cpxFieldMappings[field].updateObject(sm, value);
169                 }
170             });
171         }
172     }
173 }
174
Popular Tags