KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.DeleteVTIResultSet
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 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
27 import org.apache.derby.iapi.sql.execute.CursorResultSet;
28 import org.apache.derby.iapi.sql.execute.ExecRow;
29 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
30 import org.apache.derby.iapi.sql.Activation;
31 import org.apache.derby.iapi.sql.ResultDescription;
32
33 import org.apache.derby.iapi.store.access.TransactionController;
34
35 import org.apache.derby.iapi.types.SQLInteger;
36
37 import java.util.Properties JavaDoc;
38
39 /**
40  * Delete the rows from the specified
41  * base table. This will cause constraints to be checked
42  * and triggers to be executed based on the c's and t's
43  * compiled into the insert plan.
44  */

45 class DeleteVTIResultSet extends DMLVTIResultSet
46 {
47
48     private java.sql.ResultSet JavaDoc rs;
49     private TemporaryRowHolderImpl rowHolder;
50     /* If the delete is deferred use a row holder to keep the list of IDs of the rows to be deleted.
51      * A RowHolder is used instead of a simple list because a RowHolder will spill to disk when it becomes
52      * too large. The row will consist of just one column -- an integer.
53      */

54
55     /*
56      * class interface
57      *
58      */

59     /**
60      *
61      * @exception StandardException Thrown on error
62      */

63     public DeleteVTIResultSet
64     (
65         NoPutResultSet source,
66         Activation activation
67     )
68         throws StandardException
69     {
70         super(source, activation);
71     }
72
73     /**
74         @exception StandardException Standard Cloudscape error policy
75     */

76     protected void openCore() throws StandardException
77     {
78         lcc.getStatementContext().setTopResultSet(this, subqueryTrackingArray);
79
80         ExecRow row = getNextRowCore(sourceResultSet);
81
82         if (row != null)
83         {
84             rs = activation.getTargetVTI();
85
86             if (SanityManager.DEBUG)
87             {
88                 SanityManager.ASSERT(rs != null,
89                     "rs expected to be non-null");
90             }
91         }
92
93
94         /* The source does not know whether or not we are doing a
95          * deferred mode delete. If we are, then we must clear the
96          * index scan info from the activation so that the row changer
97          * does not re-use that information (which won't be valid for
98          * a deferred mode delete).
99          */

100         if (constants.deferred)
101         {
102             activation.clearIndexScanInfo();
103             if( null == rowHolder)
104                 rowHolder =
105                     new TemporaryRowHolderImpl(activation, new Properties JavaDoc(),
106                                                (ResultDescription) null);
107         }
108
109         try
110         {
111             while ( row != null )
112             {
113                 if( !constants.deferred)
114                     rs.deleteRow();
115                 else
116                 {
117                     ExecRow rowId = new ValueRow(1);
118                     rowId.setColumn( 1, new SQLInteger( rs.getRow()));
119                     rowHolder.insert( rowId);
120                 }
121
122                 rowCount++;
123
124                 // No need to do a next on a single row source
125
if (constants.singleRowSource)
126                 {
127                     row = null;
128                 }
129                 else
130                 {
131                     row = getNextRowCore(sourceResultSet);
132                 }
133             }
134         }
135         catch (StandardException se)
136         {
137             throw se;
138         }
139         catch (Throwable JavaDoc t)
140         {
141             throw StandardException.unexpectedUserException(t);
142         }
143
144         if (constants.deferred)
145         {
146             CursorResultSet tempRS = rowHolder.getResultSet();
147             try
148             {
149                 ExecRow deferredRowBuffer = null;
150
151                 tempRS.open();
152                 while ((deferredRowBuffer = tempRS.getNextRow()) != null)
153                 {
154                     int rowNumber = deferredRowBuffer.getColumn( 1).getInt();
155                     rs.absolute( rowNumber);
156                     rs.deleteRow();
157                 }
158             }
159             catch (Throwable JavaDoc t)
160             {
161                 throw StandardException.unexpectedUserException(t);
162             }
163             finally
164             {
165                 sourceResultSet.clearCurrentRow();
166                 tempRS.close();
167             }
168         }
169
170         if (rowHolder != null)
171         {
172             rowHolder.close();
173             // rowHolder kept across opens
174
}
175     } // end of openCore
176
}
177
Popular Tags