KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.DMLWriteResultSet
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.types.DataValueDescriptor;
25 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
26 import org.apache.derby.iapi.services.io.StreamStorable;
27 import org.apache.derby.iapi.sql.execute.ExecRow;
28 import org.apache.derby.iapi.sql.execute.ConstantAction;
29 import org.apache.derby.iapi.error.StandardException;
30 import org.apache.derby.iapi.sql.Activation;
31 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
32
33 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo;
34 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;
35 import org.apache.derby.iapi.store.access.TransactionController;
36
37 import org.apache.derby.catalog.UUID;
38 import org.apache.derby.iapi.services.io.FormatableBitSet;
39
40 /**
41  * For INSERT/UPDATE/DELETE impls. Used to tag them.
42  */

43 abstract class DMLWriteResultSet extends NoRowsResultSetImpl
44 {
45     protected WriteCursorConstantAction constantAction;
46     protected int[] baseRowReadMap;
47     protected int[] streamStorableHeapColIds;
48     protected ExecRow deferredSparseRow;
49     protected DynamicCompiledOpenConglomInfo heapDCOCI;
50     protected DynamicCompiledOpenConglomInfo[] indexDCOCIs;
51     private boolean needToObjectifyStream;
52
53
54     public int rowCount;
55
56
57     /**
58      * Constructor
59      *
60      * @param activation an activation
61      *
62      * @exception StandardException on error
63      */

64     DMLWriteResultSet(Activation activation)
65         throws StandardException
66     {
67         this(activation, activation.getConstantAction());
68     }
69     DMLWriteResultSet(Activation activation, ConstantAction constantAction)
70         throws StandardException
71     {
72         super(activation);
73
74         this.constantAction = (WriteCursorConstantAction) constantAction;
75         baseRowReadMap = this.constantAction.getBaseRowReadMap();
76         streamStorableHeapColIds = this.constantAction.getStreamStorableHeapColIds();
77
78         TransactionController tc = activation.getTransactionController();
79
80         // Special handling for updatable VTIs
81
if (! (constantAction instanceof UpdatableVTIConstantAction))
82         {
83             heapDCOCI = tc.getDynamicCompiledConglomInfo(this.constantAction.conglomId);
84             if (this.constantAction.indexCIDS.length != 0)
85             {
86                 indexDCOCIs = new DynamicCompiledOpenConglomInfo[this.constantAction.indexCIDS.length];
87                 for (int index = 0; index < this.constantAction.indexCIDS.length; index++)
88                 {
89                     indexDCOCIs[index] = tc.getDynamicCompiledConglomInfo(
90                                                 this.constantAction.indexCIDS[index]);
91                 }
92             }
93         }
94
95         /* We only need to objectify the streams here if they are exposed to the users through the
96          * trigger context. For "before" trigger, we could just return the stream wrapped in
97          * RememberBytesInputStream to the user and reset it after usage, but this wouldn't work
98          * because the user may get the stream in trigger action and do something with it in parallel
99          * with the store doing insert. We could also delay the materializing until the stream is
100          * fetched in before trigger but that would complicate the code. For "after" trigger, we have
101          * to materialize it here because store only keeps a page's amount for each round. For other
102          * reasons of "deferred" operations we don't need to objectify here. Simply going through a
103          * temp table (either in memory part or spilled to disk) is fine for the stream, unless a
104          * same stream appears in two rows in the temp table, which could happen for an "update", in
105          * which case we do the objectifying in UpdateResultSet. Beetle 4896. Related bug entries:
106          * 2432, 3383.
107          */

108         needToObjectifyStream = (this.constantAction.getTriggerInfo(
109                         activation.getLanguageConnectionContext().getExecutionContext()) != null);
110     }
111
112     public final int modifiedRowCount() { return rowCount; }
113
114
115     /**
116      * Get next row from the source result set.
117      *
118      * @param source SourceResultSet
119      * Also look at Track#2432/change 12433
120      */

121     protected ExecRow getNextRowCore(NoPutResultSet source)
122         throws StandardException
123     {
124         ExecRow row = source.getNextRowCore();
125         if (needToObjectifyStream)
126         {
127             /*
128                See comments in the constructor. We also need to load the column
129                if it is part of an index on an insert but that is done in
130                insertResultSet#normalInsertCore or IRS#changedRow
131             */

132             objectifyStreams(row);
133         }
134         return row;
135     }
136
137     private void objectifyStreams(ExecRow row) throws StandardException
138     {
139         // if the column is a streamStorable, we need to materialize the object
140
// therefore, the object can be used to multiple rows.
141
if ((row != null) && (streamStorableHeapColIds != null))
142         {
143             for (int ix=0; ix < streamStorableHeapColIds.length; ix++)
144             {
145                 int heapIx = streamStorableHeapColIds[ix];
146                 int readIx = (baseRowReadMap == null) ?
147                     heapIx :
148                     baseRowReadMap[heapIx];
149
150                 DataValueDescriptor col = row.getColumn(readIx+1);
151                 ((StreamStorable)col).loadStream();
152             }
153         }
154     }
155
156     /**
157      * For deferred update, get a deferred sparse row based on the
158      * deferred non-sparse row. Share the underlying columns. If there
159      * is no column bit map, make them the same row.
160      *
161      * @exception StandardException Thrown on error
162      */

163     protected ExecRow makeDeferredSparseRow(
164                             ExecRow deferredBaseRow,
165                             FormatableBitSet baseRowReadList,
166                             LanguageConnectionContext lcc)
167                 throws StandardException
168     {
169         ExecRow deferredSparseRow;
170
171         if (baseRowReadList == null)
172         {
173             /* No sparse row */
174             deferredSparseRow = deferredBaseRow;
175         }
176         else
177         {
178             /*
179             ** We need to do a fetch doing a partial row
180             ** read. We need to shift our 1-based bit
181             ** set to a zero based bit set like the store
182             ** expects.
183             */

184             deferredSparseRow =
185                 RowUtil.getEmptyValueRow(
186                                 baseRowReadList.getLength() - 1,
187                                 lcc);
188             /*
189             ** getColumn(), setColumn(), and baseRowReadList are
190             ** one-based.
191             */

192             int fromPosition = 1;
193             for (int i = 1; i <= deferredSparseRow.nColumns(); i++)
194             {
195                 if (baseRowReadList.isSet(i))
196                 {
197                     deferredSparseRow.setColumn(
198                         i,
199                         deferredBaseRow.getColumn(fromPosition++)
200                         );
201                 }
202             }
203         }
204
205         return deferredSparseRow;
206     }
207     
208     /**
209      * get the index name given the conglomerate id of the index.
210      *
211      * @param indexCID conglomerate ID of the index.
212      *
213      * @return index name of given index.
214      */

215     String JavaDoc getIndexNameFromCID(long indexCID)
216     {
217         return this.constantAction.getIndexNameFromCID(indexCID);
218     }
219 }
220
Popular Tags