001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.InsertConstantAction
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
025:
026: import org.apache.derby.iapi.services.io.ArrayUtil;
027: import org.apache.derby.iapi.services.io.StoredFormatIds;
028: import org.apache.derby.iapi.services.io.FormatIdUtil;
029:
030: import org.apache.derby.iapi.services.context.ContextManager;
031:
032: import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
033: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
034: import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
035: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
036: import org.apache.derby.iapi.sql.execute.ConstantAction;
037: import org.apache.derby.iapi.sql.execute.ExecRow;
038:
039: import org.apache.derby.iapi.error.StandardException;
040:
041: import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;
042: import org.apache.derby.iapi.types.RowLocation;
043:
044: import org.apache.derby.catalog.UUID;
045:
046: import java.io.ObjectOutput;
047: import java.io.ObjectInput;
048: import java.io.IOException;
049:
050: import java.util.Properties;
051:
052: /**
053: * This class describes compiled constants that are passed into
054: * InsertResultSets.
055: *
056: * @author Rick Hillegas
057: */
058:
059: public class InsertConstantAction extends WriteCursorConstantAction {
060: /********************************************************
061: **
062: ** This class implements Formatable. But it is NOT used
063: ** across either major or minor releases. It is only
064: ** written persistently in stored prepared statements,
065: ** not in the replication stage. SO, IT IS OK TO CHANGE
066: ** ITS read/writeExternal.
067: **
068: ********************************************************/
069:
070: /* Which (0-based) columns are indexed */
071: boolean[] indexedCols;
072:
073: /* These variables are needed to support Autoincrement-- after an insert
074: * we need to remember the last autoincrement value inserted into the
075: * table and the user could do a search based on schema,table,columnname
076: */
077: private String schemaName;
078: private String tableName;
079: private String columnNames[];
080:
081: /**
082: * An array of row location objects (0 based), one for each
083: * column in the table. If the column is an
084: * autoincrement table then the array points to
085: * the row location of the column in SYSCOLUMNS.
086: * if not, then it contains null.
087: */
088: protected RowLocation[] autoincRowLocation;
089: private long[] autoincIncrement;
090:
091: // CONSTRUCTORS
092:
093: /**
094: * Public niladic constructor. Needed for Formatable interface to work.
095: *
096: */
097: public InsertConstantAction() {
098: super ();
099: }
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, StaticCompiledOpenConglomInfo heapSCOCI,
126: IndexRowGenerator[] irgs, long[] indexCIDS,
127: StaticCompiledOpenConglomInfo[] indexSCOCIs,
128: String[] indexNames, boolean deferred,
129: Properties targetProperties, UUID targetUUID, int lockMode,
130: FKInfo[] fkInfo, TriggerInfo triggerInfo,
131: int[] streamStorableHeapColIds, boolean[] indexedCols,
132: boolean singleRowSource, RowLocation[] autoincRowLocation) {
133: super (conglomId, heapSCOCI, irgs, indexCIDS, indexSCOCIs,
134: indexNames, deferred, targetProperties, targetUUID,
135: lockMode, fkInfo, triggerInfo, (ExecRow) null, // never need to pass in a heap row
136: null, null, streamStorableHeapColIds, singleRowSource);
137: this .indexedCols = indexedCols;
138: this .autoincRowLocation = autoincRowLocation;
139: this .schemaName = tableDescriptor.getSchemaName();
140: this .tableName = tableDescriptor.getName();
141: this .columnNames = tableDescriptor.getColumnNamesArray();
142: this .autoincIncrement = tableDescriptor
143: .getAutoincIncrementArray();
144: this .indexNames = indexNames;
145: }
146:
147: // INTERFACE METHODS
148:
149: // Formatable methods
150: public void readExternal(ObjectInput in) throws IOException,
151: ClassNotFoundException {
152: Object[] objectArray = null;
153: super .readExternal(in);
154: indexedCols = ArrayUtil.readBooleanArray(in);
155:
156: // RESOLVEAUTOINCREMENT: this is the new stuff-- probably version!!
157: objectArray = ArrayUtil.readObjectArray(in);
158:
159: if (objectArray != null) {
160: // is there a better way to do cast the whole array?
161: autoincRowLocation = new RowLocation[objectArray.length];
162: for (int i = 0; i < objectArray.length; i++)
163: autoincRowLocation[i] = (RowLocation) objectArray[i];
164: }
165:
166: schemaName = (String) in.readObject();
167: tableName = (String) in.readObject();
168: objectArray = ArrayUtil.readObjectArray(in);
169: if (objectArray != null) {
170: // is there a better way to do cast the whole array?
171: columnNames = new String[objectArray.length];
172: for (int i = 0; i < objectArray.length; i++)
173: columnNames[i] = (String) objectArray[i];
174: }
175:
176: autoincIncrement = ArrayUtil.readLongArray(in);
177: }
178:
179: /**
180: * Write this object to a stream of stored objects.
181: *
182: * @param out write bytes here.
183: *
184: * @exception IOException thrown on error
185: */
186: public void writeExternal(ObjectOutput out) throws IOException {
187: super .writeExternal(out);
188: ArrayUtil.writeBooleanArray(out, indexedCols);
189: ArrayUtil.writeArray(out, autoincRowLocation);
190: out.writeObject(schemaName);
191: out.writeObject(tableName);
192: ArrayUtil.writeArray(out, columnNames);
193: ArrayUtil.writeLongArray(out, autoincIncrement);
194: }
195:
196: /**
197: * Gets the name of the schema that the table is in
198: *
199: * @return schema name
200: */
201: public String getSchemaName() {
202: return schemaName;
203: }
204:
205: /**
206: * Gets the name of the table being inserted into
207: *
208: * @return name of table being inserted into
209: */
210: public String getTableName() {
211: return tableName;
212: }
213:
214: /**
215: * gets the name of the desired column in the taget table.
216: *
217: * @param i the column number
218: */
219: public String getColumnName(int i) {
220: return columnNames[i];
221: }
222:
223: /**
224: * gets the increment value for a column.
225: *
226: * @param i the column number
227: */
228: public long getAutoincIncrement(int i) {
229: return autoincIncrement[i];
230: }
231:
232: /**
233: * Does the target table has autoincrement columns.
234: *
235: * @return True if the table has ai columns
236: */
237: public boolean hasAutoincrement() {
238: return (autoincRowLocation != null);
239: }
240:
241: /**
242: * gets the row location
243: */
244: public RowLocation[] getAutoincRowLocation() {
245: return autoincRowLocation;
246: }
247:
248: /**
249: * Get the formatID which corresponds to this class.
250: *
251: * @return the formatID of this class
252: */
253: public int getTypeFormatId() {
254: return StoredFormatIds.INSERT_CONSTANT_ACTION_V01_ID;
255: }
256:
257: // CLASS METHODS
258:
259: }
|