KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.UpdateVTIResultSet
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 java.sql.PreparedStatement JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41
42 import java.util.Properties JavaDoc;
43
44 /**
45  * Update the rows from the source into the specified
46  * base table.
47  */

48 class UpdateVTIResultSet extends DMLVTIResultSet
49 {
50     private java.sql.ResultSet JavaDoc rs;
51
52     private TemporaryRowHolderImpl rowHolder;
53
54     /**
55      *
56      * @exception StandardException Thrown on error
57      */

58     public UpdateVTIResultSet(NoPutResultSet source,
59                            Activation activation)
60         throws StandardException
61     {
62         super(source, activation);
63     }
64     
65     
66     /**
67         @exception StandardException Standard Cloudscape error policy
68     */

69     protected void openCore() throws StandardException
70     {
71         int rowLocationColumn = -1;
72         boolean firstRow = true;
73         
74         rs = activation.getTargetVTI();
75         ExecRow row = getNextRowCore(sourceResultSet);
76
77         if( null != row)
78             rowLocationColumn = row.nColumns();
79         if (!firstExecute)
80             lcc.getStatementContext().setTopResultSet(this, subqueryTrackingArray);
81
82         /* The source does not know whether or not we are doing a
83          * deferred mode insert. If we are, then we must clear the
84          * index scan info from the activation so that the row changer
85          * does not re-use that information (which won't be valid for
86          * a deferred mode insert).
87          */

88         if (constants.deferred)
89         {
90             activation.clearIndexScanInfo();
91         }
92
93         if (null == rowHolder && constants.deferred)
94         {
95             Properties JavaDoc properties = new Properties JavaDoc();
96
97             /*
98             ** If deferred we save a copy of the entire row.
99             */

100             rowHolder =
101                 new TemporaryRowHolderImpl(activation, properties,
102                                            resultDescription);
103         }
104
105         try
106         {
107             while ( row != null )
108             {
109                 if (constants.deferred)
110                 {
111                     // Add the row number to the row.
112
if( firstRow)
113                     {
114                         row.getColumn( rowLocationColumn).setValue( rs.getRow());
115                         firstRow = false;
116                     }
117                     else
118                     {
119                         DataValueDescriptor rowLocation = row.cloneColumn( rowLocationColumn);
120                         rowLocation.setValue( rs.getRow());
121                         row.setColumn( rowLocationColumn, rowLocation);
122                     }
123                     rowHolder.insert(row);
124                 }
125                 else
126                     updateVTI(rs, row);
127                 rowCount++;
128
129                 // No need to do a next on a single row source
130
if (constants.singleRowSource)
131                 {
132                     row = null;
133                 }
134                 else
135                 {
136                     row = getNextRowCore(sourceResultSet);
137                 }
138             }
139         }
140         catch (StandardException se)
141         {
142             throw se;
143         }
144         catch (Throwable JavaDoc t)
145         {
146             throw StandardException.unexpectedUserException(t);
147         }
148
149         /*
150         ** If it's a deferred update, scan the temporary conglomerate and
151         ** insert the rows into the VTI using rowChanger.
152         */

153         if (constants.deferred)
154         {
155             CursorResultSet tempRS = rowHolder.getResultSet();
156             try
157             {
158                 tempRS.open();
159                 while ((row = tempRS.getNextRow()) != null)
160                 {
161                     int rowNumber = row.getColumn( rowLocationColumn).getInt();
162                     rs.absolute( rowNumber);
163                     updateVTI(rs, row);
164                 }
165             }
166             catch (Throwable JavaDoc t)
167             {
168                 throw StandardException.unexpectedUserException(t);
169             }
170             finally
171             {
172                 sourceResultSet.clearCurrentRow();
173                 tempRS.close();
174             }
175         }
176
177         if (rowHolder != null)
178         {
179             rowHolder.close();
180             // rowHolder kept across opens
181
}
182     } // end of openCore
183

184     private void updateVTI(ResultSet target, ExecRow row)
185         throws StandardException
186     {
187         int[] changedColumnIds = constants.changedColumnIds;
188         try
189         {
190             for( int i = 0; i < changedColumnIds.length; i++)
191             {
192                 int columnId = changedColumnIds[i];
193                 DataValueDescriptor newValue = row.getColumn( i + 1);
194                 if( newValue.isNull())
195                     target.updateNull( columnId);
196                 else
197                     newValue.setInto( target, columnId);
198             }
199             target.updateRow();
200         }
201         catch (Throwable JavaDoc t)
202         {
203             throw StandardException.unexpectedUserException(t);
204         }
205     } // end of updateVTI
206
}
207
Popular Tags