001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.catalog.SYSVIEWSRowFactory
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.catalog;
023:
024: import org.apache.derby.iapi.types.DataValueDescriptor;
025:
026: import org.apache.derby.iapi.types.TypeId;
027: import org.apache.derby.iapi.sql.dictionary.SystemColumn;
028: import org.apache.derby.catalog.TypeDescriptor;
029:
030: import org.apache.derby.iapi.types.DataValueFactory;
031: import org.apache.derby.iapi.types.RowLocation;
032:
033: import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
034: import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
035: import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
036: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
037: import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
038: import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
039: import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;
040:
041: import org.apache.derby.iapi.sql.execute.ExecutionContext;
042: import org.apache.derby.iapi.sql.execute.ExecIndexRow;
043: import org.apache.derby.iapi.sql.execute.ExecRow;
044: import org.apache.derby.iapi.sql.execute.ExecutionFactory;
045:
046: import org.apache.derby.iapi.error.StandardException;
047:
048: import org.apache.derby.iapi.services.sanity.SanityManager;
049:
050: import org.apache.derby.iapi.services.uuid.UUIDFactory;
051: import org.apache.derby.catalog.UUID;
052: import org.apache.derby.catalog.IndexDescriptor;
053:
054: import java.util.Properties;
055:
056: /**
057: * Factory for creating a SYSVIEWS row.
058: *
059: * @author jerry
060: */
061:
062: public class SYSVIEWSRowFactory extends CatalogRowFactory {
063: private static final String TABLENAME_STRING = "SYSVIEWS";
064:
065: protected static final int SYSVIEWS_COLUMN_COUNT = 4;
066: protected static final int SYSVIEWS_TABLEID = 1;
067: protected static final int SYSVIEWS_VIEWDEFINITION = 2;
068: protected static final int SYSVIEWS_CHECKOPTION = 3;
069: protected static final int SYSVIEWS_COMPILATION_SCHEMAID = 4;
070:
071: // Column widths
072: protected static final int SYSVIEWS_TABLEID_WIDTH = 36;
073:
074: protected static final int SYSVIEWS_INDEX1_ID = 0;
075:
076: private static final int[][] indexColumnPositions = { { SYSVIEWS_TABLEID } };
077:
078: // if you add a non-unique index allocate this array.
079: private static final boolean[] uniqueness = null;
080:
081: private static final String[] uuids = {
082: "8000004d-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID
083: , "80000050-00d0-fd77-3ed8-000a0a0b1900" // heap UUID
084: , "8000004f-00d0-fd77-3ed8-000a0a0b1900" // SYSVIEWS_INDEX1
085: };
086:
087: /////////////////////////////////////////////////////////////////////////////
088: //
089: // CONSTRUCTORS
090: //
091: /////////////////////////////////////////////////////////////////////////////
092:
093: public SYSVIEWSRowFactory(UUIDFactory uuidf, ExecutionFactory ef,
094: DataValueFactory dvf, boolean convertIdToLower) {
095: super (uuidf, ef, dvf, convertIdToLower);
096: initInfo(SYSVIEWS_COLUMN_COUNT, TABLENAME_STRING,
097: indexColumnPositions, uniqueness, uuids);
098: }
099:
100: /////////////////////////////////////////////////////////////////////////////
101: //
102: // METHODS
103: //
104: /////////////////////////////////////////////////////////////////////////////
105:
106: /**
107: * Make a SYSVIEWS row
108: *
109: * @return Row suitable for inserting into SYSVIEWS.
110: *
111: * @exception StandardException thrown on failure
112: */
113: public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
114: throws StandardException {
115: DataValueDescriptor col;
116: ExecRow row;
117: String tableID = null;
118: String compSchemaId = null;
119: String viewText = null;
120: String checkSType = null;
121: int checkIType;
122:
123: if (td != null) {
124: UUID tableUUID;
125: ViewDescriptor vd = (ViewDescriptor) td;
126:
127: /*
128: ** We only allocate a new UUID if the descriptor doesn't already have one.
129: ** For descriptors replicated from a Source system, we already have an UUID.
130: */
131: tableUUID = vd.getUUID();
132: if (tableUUID == null) {
133: tableUUID = getUUIDFactory().createUUID();
134: vd.setUUID(tableUUID);
135: }
136: tableID = tableUUID.toString();
137: viewText = vd.getViewText();
138:
139: /* RESOLVE - check constraints not supported yet */
140: checkIType = vd.getCheckOptionType();
141:
142: if (SanityManager.DEBUG) {
143: if (checkIType != ViewDescriptor.NO_CHECK_OPTION) {
144: SanityManager
145: .THROWASSERT("checkIType expected to be "
146: + ViewDescriptor.NO_CHECK_OPTION
147: + ", not " + checkIType);
148: }
149: }
150: checkSType = "N";
151:
152: UUID tmpId = vd.getCompSchemaId();
153: compSchemaId = (tmpId == null) ? null : tmpId.toString();
154: }
155:
156: /* Insert info into sysviews */
157:
158: /* RESOLVE - It would be nice to require less knowledge about sysviews
159: * and have this be more table driven.
160: */
161:
162: /* Build the row to insert */
163: row = getExecutionFactory().getValueRow(SYSVIEWS_COLUMN_COUNT);
164:
165: /* 1st column is TABLEID (UUID - char(36)) */
166: row.setColumn(SYSVIEWS_TABLEID, dvf.getCharDataValue(tableID));
167:
168: /* 2nd column is VIEWDEFINITION */
169: row.setColumn(SYSVIEWS_VIEWDEFINITION, dvf
170: .getLongvarcharDataValue(viewText));
171:
172: /* 3rd column is CHECKOPTION (char(1)) */
173: row.setColumn(SYSVIEWS_CHECKOPTION, dvf
174: .getCharDataValue(checkSType));
175:
176: /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
177: row.setColumn(SYSVIEWS_COMPILATION_SCHEMAID, dvf
178: .getCharDataValue(compSchemaId));
179:
180: return row;
181: }
182:
183: ///////////////////////////////////////////////////////////////////////////
184: //
185: // ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
186: //
187: ///////////////////////////////////////////////////////////////////////////
188:
189: /**
190: * Make a ViewDescriptor out of a SYSVIEWS row
191: *
192: * @param row a SYSVIEWS row
193: * @param parentTupleDescriptor Null for this kind of descriptor.
194: * @param dd dataDictionary
195: *
196: * @exception StandardException thrown on failure
197: */
198: public TupleDescriptor buildDescriptor(ExecRow row,
199: TupleDescriptor parentTupleDescriptor, DataDictionary dd)
200: throws StandardException {
201: ViewDescriptor vd = null;
202:
203: if (SanityManager.DEBUG) {
204: SanityManager.ASSERT(
205: row.nColumns() == SYSVIEWS_COLUMN_COUNT,
206: "Wrong number of columns for a SYSVIEWS row");
207: }
208:
209: DataValueDescriptor col;
210: DataDescriptorGenerator ddg;
211: int checkIType;
212: String checkSType;
213: String tableID;
214: String compSchemaId;
215: String viewDefinition;
216: UUID tableUUID;
217: UUID compSchemaUUID = null;
218:
219: ddg = dd.getDataDescriptorGenerator();
220:
221: /* 1st column is TABLEID (UUID - char(36)) */
222: col = row.getColumn(SYSVIEWS_TABLEID);
223: tableID = col.getString();
224: tableUUID = getUUIDFactory().recreateUUID(tableID);
225:
226: /* 2nd column is VIEWDEFINITION */
227: col = row.getColumn(SYSVIEWS_VIEWDEFINITION);
228: viewDefinition = col.getString();
229:
230: /* 3rd column is CHECKOPTION (char(1)) */
231: col = row.getColumn(SYSVIEWS_CHECKOPTION);
232: checkSType = col.getString();
233:
234: if (SanityManager.DEBUG) {
235: if (!checkSType.equals("N")) {
236: SanityManager
237: .THROWASSERT("checkSType expected to be 'N', not "
238: + checkSType);
239: }
240: }
241:
242: /* RESOLVE - no check options for now */
243: checkIType = ViewDescriptor.NO_CHECK_OPTION;
244:
245: /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */
246: col = row.getColumn(SYSVIEWS_COMPILATION_SCHEMAID);
247: compSchemaId = col.getString();
248: if (compSchemaId != null) {
249: compSchemaUUID = getUUIDFactory()
250: .recreateUUID(compSchemaId);
251: }
252:
253: /* now build and return the descriptor */
254: vd = ddg.newViewDescriptor(tableUUID, null, viewDefinition,
255: checkIType, compSchemaUUID);
256: return vd;
257: }
258:
259: /**
260: * Builds a list of columns suitable for creating this Catalog.
261: *
262: *
263: * @return array of SystemColumn suitable for making this catalog.
264: */
265: public SystemColumn[] buildColumnList() {
266: SystemColumn[] columnList = new SystemColumn[SYSVIEWS_COLUMN_COUNT];
267:
268: columnList[0] = new SystemColumnImpl(convertIdCase("TABLEID"), // name
269: SYSVIEWS_TABLEID, // column number
270: 0, // precision
271: 0, // scale
272: false, // nullability
273: "CHAR", // dataType
274: true, // built-in type
275: 36 // maxLength
276: );
277: // describe columns
278:
279: columnList[1] = new SystemColumnImpl(
280: convertIdCase("VIEWDEFINITION"), // column name
281: SYSVIEWS_VIEWDEFINITION, // column number
282: 0, // precision
283: 0, // scale
284: false, // nullability
285: "LONG VARCHAR", // dataType
286: true, // built-in type
287: TypeId.LONGVARCHAR_MAXWIDTH // maxLength
288: );
289: columnList[2] = new SystemColumnImpl(
290: convertIdCase("CHECKOPTION"), // column name
291: SYSVIEWS_CHECKOPTION,// column number
292: 0, // precision
293: 0, // scale
294: false, // nullability
295: "CHAR", // dataType
296: true, // built-in type
297: 1 // maxLength
298: );
299:
300: columnList[3] = new SystemColumnImpl(
301: convertIdCase("COMPILATIONSCHEMAID"), // name
302: SYSVIEWS_COMPILATION_SCHEMAID, // column number
303: 0, // precision
304: 0, // scale
305: false, // nullability
306: "CHAR", // dataType
307: true, // built-in type
308: 36 // maxLength
309: );
310: return columnList;
311: }
312: }
|