KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.WriteCursorConstantAction
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.sql.execute.ExecutionContext;
25
26 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
27
28 import org.apache.derby.iapi.services.io.ArrayUtil;
29 import org.apache.derby.iapi.services.io.Formatable;
30
31 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
32 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
33 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
34 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
36 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
37 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
38 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
39
40 import org.apache.derby.iapi.sql.execute.ConstantAction;
41 import org.apache.derby.iapi.sql.execute.ExecRow;
42
43 import org.apache.derby.iapi.sql.Activation;
44
45 import org.apache.derby.iapi.error.StandardException;
46
47 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;
48
49 import org.apache.derby.iapi.services.sanity.SanityManager;
50
51 import org.apache.derby.iapi.services.io.FormatableBitSet;
52
53 import org.apache.derby.catalog.UUID;
54
55 import java.io.ObjectOutput JavaDoc;
56 import java.io.ObjectInput JavaDoc;
57 import java.io.IOException JavaDoc;
58
59 import java.util.Properties JavaDoc;
60
61
62 /**
63  * This abstract class describes compiled constants that are passed into
64  * Delete, Insert, and Update ResultSets.
65  *
66  * @author Rick Hillegas
67  */

68
69 abstract class WriteCursorConstantAction implements ConstantAction, Formatable
70 {
71
72     /********************************************************
73     **
74     ** This class implements Formatable. But it is NOT used
75     ** across either major or minor releases. It is only
76     ** written persistently in stored prepared statements,
77     ** not in the replication stage. SO, IT IS OK TO CHANGE
78     ** ITS read/writeExternal.
79     **
80     ********************************************************/

81
82     long conglomId;
83     StaticCompiledOpenConglomInfo heapSCOCI;
84     IndexRowGenerator[] irgs;
85     long[] indexCIDS;
86     StaticCompiledOpenConglomInfo[] indexSCOCIs;
87     String JavaDoc[] indexNames;
88     boolean deferred;
89     private Properties JavaDoc targetProperties;
90     UUID targetUUID;
91     int lockMode;
92     private FKInfo[] fkInfo;
93     private TriggerInfo triggerInfo;
94
95     private ExecRow emptyHeapRow;
96     private FormatableBitSet baseRowReadList;
97     private int[] baseRowReadMap;
98     private int[] streamStorableHeapColIds;
99     boolean singleRowSource;
100
101
102     // CONSTRUCTORS
103

104     /**
105      * Public niladic constructor. Needed for Formatable interface to work.
106      *
107      */

108     public WriteCursorConstantAction() {}
109
110     /**
111      * Make the ConstantAction for a DELETE, INSERT, or UPDATE statement.
112      *
113      * @param conglomId Conglomerate ID of heap.
114      * @param heapSCOCI StaticCompiledOpenConglomInfo for heap.
115      * @param irgs Index descriptors
116      * @param indexCIDS Conglomerate IDs of indices
117      * @param indexSCOCIs StaticCompiledOpenConglomInfos for indexes.
118      * @param indexNames Names of indices on this table for error reporting.
119      * @param deferred True means process as a deferred update
120      * @param targetProperties Properties on the target table
121      * @param targetUUID UUID of target table
122      * @param lockMode The lock mode to use on the target table
123      * @param fkInfo Structure containing foreign key info, if any (may be null)
124      * @param triggerInfo Structure containing trigger info, if any (may be null)
125      * @param emptyHeapRow an empty heap row
126      * @param baseRowReadMap BaseRowReadMap[heapColId]->ReadRowColumnId. (0 based)
127      * @param streamStorableHeapColIds Null for non rep. (0 based)
128      * @param singleRowSource Whether or not source is a single row source
129      */

130     public WriteCursorConstantAction(
131                                 long conglomId,
132                                 StaticCompiledOpenConglomInfo heapSCOCI,
133                                 IndexRowGenerator[] irgs,
134                                 long[] indexCIDS,
135                                 StaticCompiledOpenConglomInfo[] indexSCOCIs,
136                                 String JavaDoc[] indexNames,
137                                 boolean deferred,
138                                 Properties JavaDoc targetProperties,
139                                 UUID targetUUID,
140                                 int lockMode,
141                                 FKInfo[] fkInfo,
142                                 TriggerInfo triggerInfo,
143                                 ExecRow emptyHeapRow,
144                                 FormatableBitSet baseRowReadList,
145                                 int[] baseRowReadMap,
146                                 int[] streamStorableHeapColIds,
147                                 boolean singleRowSource
148                                 )
149     {
150         this.conglomId = conglomId;
151         this.heapSCOCI = heapSCOCI;
152         this.irgs = irgs;
153         this.indexSCOCIs = indexSCOCIs;
154         this.indexCIDS = indexCIDS;
155         this.indexSCOCIs = indexSCOCIs;
156         this.deferred = deferred;
157         this.targetProperties = targetProperties;
158         this.targetUUID = targetUUID;
159         this.lockMode = lockMode;
160         this.emptyHeapRow = emptyHeapRow;
161         this.fkInfo = fkInfo;
162         this.triggerInfo = triggerInfo;
163         this.baseRowReadList = baseRowReadList;
164         this.baseRowReadMap = baseRowReadMap;
165         this.streamStorableHeapColIds = streamStorableHeapColIds;
166         this.singleRowSource = singleRowSource;
167         this.indexNames = indexNames;
168         if (SanityManager.DEBUG)
169         {
170             if (fkInfo != null)
171             {
172                 SanityManager.ASSERT(fkInfo.length != 0, "fkinfo array has no elements, if there are no foreign keys, then pass in null");
173             }
174         }
175     }
176
177     ///////////////////////////////////////////////////////////////////
178
//
179
// ACCESSORS
180
//
181
///////////////////////////////////////////////////////////////////
182

183     /**
184       * Gets the foreign key information for this constant action.
185       * A full list of foreign keys was compiled into this constant
186       * action. However, we prune this list at run time so that we
187       * enforce only the foreign keys that we absolutely must.
188       *
189       * What's going on here? Well, in certain contexts (REFRESH and
190       * when intially booting a Target), we don't have to enforce some
191       * foreign keys. We allow the ExecutionContext to rule on which
192       * foreign keys are relevant and which aren't.
193       *
194       * @param ec the ExecutionContext
195       *
196       * @return the list of foreign keys to enforce for this action
197       *
198       * @exception StandardException Thrown on failure
199       */

200     public FKInfo[] getFKInfo
201     (
202         ExecutionContext ec
203     )
204         throws StandardException
205     {
206         FKInfo[] siftedArray = (FKInfo[]) ec.siftForeignKeys( fkInfo );
207
208         return siftedArray;
209     }
210
211     /**
212      * Basically, the same as getFKInfo but for triggers.
213      *
214      * @return the triggers that should be fired
215      *
216      * @exception StandardException Thrown on failure
217      */

218     public TriggerInfo getTriggerInfo(ExecutionContext ec)
219         throws StandardException
220     {
221         return (TriggerInfo)ec.siftTriggers(triggerInfo);
222     }
223
224
225     ///////////////////////////////////////////////////////////////////
226
//
227
// INTERFACE METHODS
228
//
229
///////////////////////////////////////////////////////////////////
230

231     /**
232      * NOP routine. The work is done in InsertResultSet.
233      *
234      * @see ConstantAction#executeConstantAction
235      *
236      * @exception StandardException Thrown on failure
237      */

238     public final void executeConstantAction( Activation activation )
239         throws StandardException { }
240
241     // Formatable methods
242
/**
243      * Read this object from a stream of stored objects.
244      *
245      * @param in read this.
246      *
247      * @exception IOException thrown on error
248      * @exception ClassNotFoundException thrown on error
249      */

250     public void readExternal( ObjectInput JavaDoc in )
251          throws IOException JavaDoc, ClassNotFoundException JavaDoc
252     {
253         conglomId = in.readLong();
254         heapSCOCI = (StaticCompiledOpenConglomInfo) in.readObject();
255         irgs = new IndexRowGenerator[ArrayUtil.readArrayLength(in)];
256         ArrayUtil.readArrayItems(in, irgs);
257
258         indexCIDS = ArrayUtil.readLongArray(in);
259         indexSCOCIs = new StaticCompiledOpenConglomInfo[ArrayUtil.readArrayLength(in)];
260         ArrayUtil.readArrayItems(in, indexSCOCIs);
261
262         deferred = in.readBoolean();
263         targetProperties = (Properties JavaDoc) in.readObject();
264         targetUUID = (UUID) in.readObject();
265         lockMode = in.readInt();
266
267         fkInfo = new FKInfo[ArrayUtil.readArrayLength(in)];
268         ArrayUtil.readArrayItems(in, fkInfo);
269
270         triggerInfo = (TriggerInfo)in.readObject();
271
272         baseRowReadList = (FormatableBitSet)in.readObject();
273         baseRowReadMap = ArrayUtil.readIntArray(in);
274         streamStorableHeapColIds = ArrayUtil.readIntArray(in);
275         singleRowSource = in.readBoolean();
276         indexNames = ArrayUtil.readStringArray(in);
277     }
278
279     /**
280      * Write this object to a stream of stored objects.
281      *
282      * @param out write bytes here.
283      *
284      * @exception IOException thrown on error
285      */

286     public void writeExternal( ObjectOutput JavaDoc out )
287          throws IOException JavaDoc
288     {
289         out.writeLong(conglomId);
290         out.writeObject(heapSCOCI);
291         ArrayUtil.writeArray(out, irgs);
292         ArrayUtil.writeLongArray(out, indexCIDS);
293         ArrayUtil.writeArray(out, indexSCOCIs);
294         out.writeBoolean(deferred);
295         out.writeObject(targetProperties);
296         out.writeObject(targetUUID);
297         out.writeInt(lockMode);
298         ArrayUtil.writeArray(out, fkInfo);
299
300         //
301
//Added for Xena.
302
out.writeObject(triggerInfo);
303
304         //
305
//Moved from super class for Xena.
306
out.writeObject(baseRowReadList);
307
308         //
309
//Added for Xena
310
ArrayUtil.writeIntArray(out,baseRowReadMap);
311         ArrayUtil.writeIntArray(out,streamStorableHeapColIds);
312
313         //Added for Buffy
314
out.writeBoolean(singleRowSource);
315         
316         // Added for Mulan (Track Bug# 3322)
317
ArrayUtil.writeArray(out, indexNames);
318         
319     }
320
321     // ACCESSORS
322

323     /**
324      * Get the conglomerate id for the changed heap.
325      * @return the conglomerate id.
326      */

327     public long getConglomerateId() { return conglomId; }
328         
329     /**
330      * Get emptyHeapRow
331      *
332      * @param lcc The LanguageConnectionContext to use.
333      *
334      * @return an empty base table row for the table being updated.
335      *
336      * @exception StandardException on error
337      */

338     public ExecRow getEmptyHeapRow(LanguageConnectionContext lcc) throws StandardException
339     {
340         DataDictionary dd;
341         TableDescriptor td;
342
343         if (emptyHeapRow == null)
344         {
345
346             dd = lcc.getDataDictionary();
347     
348             td = dd.getTableDescriptor(targetUUID);
349     
350             emptyHeapRow = td.getEmptyExecRow(lcc.getContextManager());
351         }
352
353         return emptyHeapRow.getClone();
354     }
355
356     /**
357      * Get the targetProperties from the constant action.
358      *
359      * @return The targetProperties
360      */

361     public Properties JavaDoc getTargetProperties()
362     {
363         return targetProperties;
364     }
365
366     /**
367      * The the value of the specified key, if it exists, from
368      * the targetProperties.
369      *
370      * @param key The key to search for
371      *
372      * @return The value for the specified key if it exists, otherwise null.
373      * (Return null if targetProperties is null.)
374      */

375     public String JavaDoc getProperty(String JavaDoc key)
376     {
377         return (targetProperties == null) ? null : targetProperties.getProperty(key);
378     }
379
380     public FormatableBitSet getBaseRowReadList() { return baseRowReadList; }
381     public int[] getBaseRowReadMap() { return baseRowReadMap; }
382     public int[] getStreamStorableHeapColIds() { return streamStorableHeapColIds; }
383
384     /**
385      * get the index name given the conglomerate id of the index.
386      *
387      * @param indexCID conglomerate ID of the index.
388      *
389      * @return index name of given index.
390      */

391     public String JavaDoc getIndexNameFromCID(long indexCID)
392     {
393         int size = indexCIDS.length;
394
395         if (indexNames == null)
396         {
397             return null;
398         }
399         
400         for (int i = 0; i < size; i++)
401         {
402             if (indexCIDS[i] == indexCID)
403                 return indexNames[i];
404         }
405         return null;
406     }
407             
408     public String JavaDoc[] getIndexNames()
409     {
410         return indexNames;
411     }
412 }
413  
414
Popular Tags