KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > InsertVTIResultSet


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.InsertVTIResultSet
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
29
30 import org.apache.derby.iapi.types.DataValueDescriptor;
31
32 import org.apache.derby.iapi.sql.execute.CursorResultSet;
33 import org.apache.derby.iapi.sql.execute.ExecRow;
34 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
35
36 import org.apache.derby.iapi.sql.Activation;
37 import org.apache.derby.iapi.sql.ResultDescription;
38
39 import org.apache.derby.vti.DeferModification;
40
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43
44 import java.util.Properties JavaDoc;
45
46 /**
47  * Insert the rows from the source into the specified
48  * base table. This will cause constraints to be checked
49  * and triggers to be executed based on the c's and t's
50  * compiled into the insert plan.
51  */

52 class InsertVTIResultSet extends DMLVTIResultSet
53 {
54
55     private PreparedStatement JavaDoc ps;
56     private VTIResultSet vtiRS;
57     private java.sql.ResultSet JavaDoc rs;
58
59     private TemporaryRowHolderImpl rowHolder;
60
61     /**
62      *
63      * @exception StandardException Thrown on error
64      */

65     public InsertVTIResultSet(NoPutResultSet source,
66                               NoPutResultSet vtiRS,
67                            Activation activation)
68         throws StandardException
69     {
70         super(source, activation);
71         this.vtiRS = (VTIResultSet) vtiRS;
72     }
73     
74     /**
75         @exception StandardException Standard Cloudscape error policy
76     */

77     protected void openCore() throws StandardException
78     {
79         /* We must instantiate the VTI on each execution if any of the
80          * parameters contain a ?.
81          */

82         if (ps == null)
83         {
84             ps = (PreparedStatement JavaDoc) vtiRS.getVTIConstructor().invoke(activation);
85         }
86
87         if( ps instanceof DeferModification)
88         {
89             try
90             {
91                 ((DeferModification) ps).modificationNotify( DeferModification.INSERT_STATEMENT, constants.deferred);
92             }
93             catch (Throwable JavaDoc t)
94             {
95                 throw StandardException.unexpectedUserException(t);
96             }
97         }
98
99         ExecRow row = getNextRowCore(sourceResultSet);
100
101         try
102         {
103             rs = ps.executeQuery();
104         }
105         catch (Throwable JavaDoc t)
106         {
107             throw StandardException.unexpectedUserException(t);
108         }
109
110         /* Get or re-use the row changer.
111          * NOTE: We need to set ourself as the top result set
112          * if this is not the 1st execution. (Done in constructor
113          * for 1st execution.)
114          */

115         if (! firstExecute)
116         {
117             lcc.getStatementContext().setTopResultSet(this, subqueryTrackingArray);
118         }
119
120         /* The source does not know whether or not we are doing a
121          * deferred mode insert. If we are, then we must clear the
122          * index scan info from the activation so that the row changer
123          * does not re-use that information (which won't be valid for
124          * a deferred mode insert).
125          */

126         if (constants.deferred)
127         {
128             activation.clearIndexScanInfo();
129         }
130
131         if (firstExecute && constants.deferred)
132         {
133             Properties JavaDoc properties = new Properties JavaDoc();
134
135             /*
136             ** If deferred we save a copy of the entire row.
137             */

138             rowHolder =
139                 new TemporaryRowHolderImpl(activation, properties,
140                                            resultDescription);
141         }
142
143         while ( row != null )
144         {
145             /*
146             ** If we're doing a deferred insert, insert into the temporary
147             ** conglomerate. Otherwise, insert directly into the permanent
148             ** conglomerates using the rowChanger.
149             */

150             if (constants.deferred)
151             {
152                 rowHolder.insert(row);
153             }
154             else
155             {
156                 insertIntoVTI(rs, row);
157             }
158
159             rowCount++;
160
161             // No need to do a next on a single row source
162
if (constants.singleRowSource)
163             {
164                 row = null;
165             }
166             else
167             {
168                 row = getNextRowCore(sourceResultSet);
169             }
170         }
171
172         /*
173         ** If it's a deferred insert, scan the temporary conglomerate and
174         ** insert the rows into the permanent conglomerates using rowChanger.
175         */

176         if (constants.deferred)
177         {
178             CursorResultSet tempRS = rowHolder.getResultSet();
179             try
180             {
181                 tempRS.open();
182                 while ((row = tempRS.getNextRow()) != null)
183                 {
184                     insertIntoVTI(rs, row);
185                 }
186             } finally
187             {
188                 sourceResultSet.clearCurrentRow();
189                 tempRS.close();
190             }
191         }
192
193         if (rowHolder != null)
194         {
195             rowHolder.close();
196             // rowHolder kept across opens
197
}
198     } // end of normalInsertCore
199

200     private void insertIntoVTI(ResultSet target, ExecRow row)
201         throws StandardException
202     {
203         try
204         {
205             target.moveToInsertRow();
206
207             DataValueDescriptor[] rowArray = row.getRowArray();
208             for (int index = 0; index < rowArray.length; index++)
209             {
210                 DataValueDescriptor dvd = rowArray[index];
211
212                 try {
213                     if (dvd.isNull())
214                         target.updateNull(index + 1);
215                     else
216                         dvd.setInto(target, index + 1);
217                 } catch (Throwable JavaDoc t) {
218                     // backwards compatibility - 5.0 and before used
219
// updateObject always.
220
target.updateObject(index + 1, dvd.getObject());
221                 }
222             }
223
224             target.insertRow();
225         }
226         catch (Throwable JavaDoc t)
227         {
228             throw StandardException.unexpectedUserException(t);
229         }
230     }
231
232     /**
233      * @see org.apache.derby.iapi.sql.ResultSet#cleanUp
234      *
235      * @exception StandardException Thrown on error
236      */

237     public void cleanUp() throws StandardException
238     {
239         if (rowHolder != null)
240         {
241             rowHolder.close();
242         }
243
244         if (rs != null)
245         {
246             try
247             {
248                 rs.close();
249             }
250             catch (Throwable JavaDoc t)
251             {
252                 throw StandardException.unexpectedUserException(t);
253             }
254             rs = null;
255         }
256
257         // Close the ps if it needs to be instantiated on each execution
258
if (!vtiRS.isReuseablePs() && ps != null)
259         {
260             try
261             {
262                 ps.close();
263                 ps = null;
264             }
265             catch (Throwable JavaDoc t)
266             {
267                 throw StandardException.unexpectedUserException(t);
268             }
269         }
270         super.cleanUp();
271     } // end of cleanUp
272

273     // Class implementation
274

275     public void finish() throws StandardException {
276
277         if ((ps != null) && !vtiRS.isReuseablePs())
278         {
279             try
280             {
281                 ps.close();
282                 ps = null;
283             }
284             catch (Throwable JavaDoc t)
285             {
286                 throw StandardException.unexpectedUserException(t);
287             }
288         }
289         super.finish();
290     } // end of finish
291
}
292
293
Popular Tags