KickJava   Java API By Example, From Geeks To Geeks.

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


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: InsertRequest.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 InsertRequest extends RequestUsingFields
24 {
25     private static final Category LOG = Category.getInstance(InsertRequest.class);
26
27     private static final int IDPARAMNUMBER = 1;
28
29     private final int[] paramNumbersByField;
30     private final String JavaDoc textStmt;
31
32     /**
33      * The fields involved in this request whose ColumnMappings require post-
34      * processing after INSERT.
35      * Null if there are no such fields.
36      */

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

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

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

161                 public void storeObjectField(int field, Object JavaDoc value)
162                 {
163                     cpxFieldMappings[field].insertObject(sm, value);
164                 }
165             });
166         }
167     }
168 }
169
Popular Tags