KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.InsertConstantAction
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.stream.HeaderPrintWriter;
25
26
27 import org.apache.derby.iapi.services.io.ArrayUtil;
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29 import org.apache.derby.iapi.services.io.FormatIdUtil;
30
31 import org.apache.derby.iapi.services.context.ContextManager;
32
33 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
34 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
35 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
36 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
37 import org.apache.derby.iapi.sql.execute.ConstantAction;
38 import org.apache.derby.iapi.sql.execute.ExecRow;
39
40 import org.apache.derby.iapi.error.StandardException;
41
42 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;
43 import org.apache.derby.iapi.types.RowLocation;
44
45 import org.apache.derby.catalog.UUID;
46
47 import java.io.ObjectOutput JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 import java.util.Properties JavaDoc;
52
53 /**
54  * This class describes compiled constants that are passed into
55  * InsertResultSets.
56  *
57  * @author Rick Hillegas
58  */

59
60 public class InsertConstantAction extends WriteCursorConstantAction
61 {
62     /********************************************************
63     **
64     ** This class implements Formatable. But it is NOT used
65     ** across either major or minor releases. It is only
66     ** written persistently in stored prepared statements,
67     ** not in the replication stage. SO, IT IS OK TO CHANGE
68     ** ITS read/writeExternal.
69     **
70     ********************************************************/

71
72     /* Which (0-based) columns are indexed */
73     boolean[] indexedCols;
74
75     /* These variables are needed to support Autoincrement-- after an insert
76      * we need to remember the last autoincrement value inserted into the
77      * table and the user could do a search based on schema,table,columnname
78      */

79     private String JavaDoc schemaName;
80     private String JavaDoc tableName;
81     private String JavaDoc columnNames[];
82
83     /**
84      * An array of row location objects (0 based), one for each
85      * column in the table. If the column is an
86      * autoincrement table then the array points to
87      * the row location of the column in SYSCOLUMNS.
88      * if not, then it contains null.
89      */

90     protected RowLocation[] autoincRowLocation;
91     private long[] autoincIncrement;
92     
93     // CONSTRUCTORS
94

95     /**
96      * Public niladic constructor. Needed for Formatable interface to work.
97      *
98      */

99     public InsertConstantAction() { super(); }
100
101     /**
102      * Make the ConstantAction for an INSERT statement.
103      *
104      * @param conglomId Conglomerate ID.
105      * @param heapSCOCI StaticCompiledOpenConglomInfo for heap.
106      * @param irgs Index descriptors
107      * @param indexCIDS Conglomerate IDs of indices
108      * @param indexSCOCIs StaticCompiledOpenConglomInfos for indexes.
109      * @param indexNames Names of indices on this table for error reporting.
110      * @param deferred True means process as a deferred insert.
111      * @param targetProperties Properties on the target table.
112      * @param targetUUID UUID of target table
113      * @param lockMode The lockMode to use on the target table
114      * @param fkInfo Array of structures containing foreign key info,
115      * if any (may be null)
116      * @param triggerInfo Array of structures containing trigger info,
117      * if any (may be null)
118      * @param streamStorableHeapColIds Null for non rep. (0 based)
119      * @param indexedCols boolean[] of which (0-based) columns are indexed.
120      * @param singleRowSource Whether or not source is a single row source
121      * @param autoincRowLocation Array of rowlocations of autoincrement values
122      * in SYSCOLUMNS for each ai column.
123      */

124     public InsertConstantAction(TableDescriptor tableDescriptor,
125                                 long conglomId,
126                                 StaticCompiledOpenConglomInfo heapSCOCI,
127                                 IndexRowGenerator[] irgs,
128                                 long[] indexCIDS,
129                                 StaticCompiledOpenConglomInfo[] indexSCOCIs,
130                                 String JavaDoc[] indexNames,
131                                 boolean deferred,
132                                 Properties JavaDoc targetProperties,
133                                 UUID targetUUID,
134                                 int lockMode,
135                                 FKInfo[] fkInfo,
136                                 TriggerInfo triggerInfo,
137                                 int[] streamStorableHeapColIds,
138                                 boolean[] indexedCols,
139                                 boolean singleRowSource,
140                                 RowLocation[] autoincRowLocation)
141     {
142         super(conglomId,
143               heapSCOCI,
144               irgs,
145               indexCIDS,
146               indexSCOCIs,
147               indexNames,
148               deferred,
149               targetProperties,
150               targetUUID,
151               lockMode,
152               fkInfo,
153               triggerInfo,
154               (ExecRow)null, // never need to pass in a heap row
155
null,
156               null,
157               streamStorableHeapColIds,
158               singleRowSource
159               );
160         this.indexedCols = indexedCols;
161         this.autoincRowLocation = autoincRowLocation;
162         this.schemaName = tableDescriptor.getSchemaName();
163         this.tableName = tableDescriptor.getName();
164         this.columnNames = tableDescriptor.getColumnNamesArray();
165         this.autoincIncrement = tableDescriptor.getAutoincIncrementArray();
166         this.indexNames = indexNames;
167     }
168
169     // INTERFACE METHODS
170

171     // Formatable methods
172
public void readExternal (ObjectInput JavaDoc in)
173          throws IOException JavaDoc, ClassNotFoundException JavaDoc
174     {
175         Object JavaDoc[] objectArray = null;
176         super.readExternal(in);
177         indexedCols = ArrayUtil.readBooleanArray(in);
178
179         // RESOLVEAUTOINCREMENT: this is the new stuff-- probably version!!
180
objectArray = ArrayUtil.readObjectArray(in);
181         
182         if (objectArray != null)
183         {
184             // is there a better way to do cast the whole array?
185
autoincRowLocation = new RowLocation[objectArray.length];
186             for (int i = 0; i < objectArray.length; i++)
187                 autoincRowLocation[i] = (RowLocation)objectArray[i];
188         }
189         
190         schemaName = (String JavaDoc)in.readObject();
191         tableName = (String JavaDoc)in.readObject();
192         objectArray = ArrayUtil.readObjectArray(in);
193         if (objectArray != null)
194         {
195             // is there a better way to do cast the whole array?
196
columnNames = new String JavaDoc[objectArray.length];
197             for (int i = 0; i < objectArray.length; i++)
198                 columnNames[i] = (String JavaDoc)objectArray[i];
199         }
200         
201         autoincIncrement = ArrayUtil.readLongArray(in);
202     }
203
204
205
206     /**
207      * Write this object to a stream of stored objects.
208      *
209      * @param out write bytes here.
210      *
211      * @exception IOException thrown on error
212      */

213     public void writeExternal( ObjectOutput JavaDoc out )
214          throws IOException JavaDoc
215     {
216         super.writeExternal(out);
217         ArrayUtil.writeBooleanArray(out, indexedCols);
218         ArrayUtil.writeArray(out, autoincRowLocation);
219         out.writeObject(schemaName);
220         out.writeObject(tableName);
221         ArrayUtil.writeArray(out, columnNames);
222         ArrayUtil.writeLongArray(out, autoincIncrement);
223     }
224
225     /**
226       * Gets the name of the schema that the table is in
227       *
228       * @return schema name
229       */

230     public String JavaDoc getSchemaName() { return schemaName; }
231
232     /**
233       * Gets the name of the table being inserted into
234       *
235       * @return name of table being inserted into
236       */

237     public String JavaDoc getTableName() { return tableName; }
238
239
240     /**
241      * gets the name of the desired column in the taget table.
242      *
243      * @param i the column number
244      */

245     public String JavaDoc getColumnName(int i) { return columnNames[i]; }
246
247     /**
248      * gets the increment value for a column.
249      *
250      * @param i the column number
251      */

252     public long getAutoincIncrement(int i) { return autoincIncrement[i]; }
253
254     /**
255      * Does the target table has autoincrement columns.
256      *
257      * @return True if the table has ai columns
258      */

259     public boolean hasAutoincrement()
260     {
261         return (autoincRowLocation != null);
262     }
263
264     /**
265      * gets the row location
266      */

267     public RowLocation[] getAutoincRowLocation()
268     {
269         return autoincRowLocation;
270     }
271     
272     /**
273      * Get the formatID which corresponds to this class.
274      *
275      * @return the formatID of this class
276      */

277     public int getTypeFormatId() { return StoredFormatIds.INSERT_CONSTANT_ACTION_V01_ID; }
278
279     // CLASS METHODS
280

281 }
282
Popular Tags