--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.catalog.SYSVIEWSRowFactory\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+*/\r
+\r
+package org.apache.derby.impl.sql.catalog;\r
+\r
+import java.sql.Types;\r
+\r
+import org.apache.derby.catalog.UUID;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.services.uuid.UUIDFactory;\r
+import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;\r
+import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+import org.apache.derby.iapi.sql.dictionary.SystemColumn;\r
+import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;\r
+import org.apache.derby.iapi.sql.execute.ExecRow;\r
+import org.apache.derby.iapi.sql.execute.ExecutionFactory;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.types.DataValueFactory;\r
+import org.apache.derby.iapi.types.SQLChar;\r
+import org.apache.derby.iapi.types.TypeId;\r
+\r
+/**\r
+ * Factory for creating a SYSVIEWS row.\r
+ *\r
+ */\r
+\r
+public class SYSVIEWSRowFactory extends CatalogRowFactory\r
+{\r
+ private static final String TABLENAME_STRING = "SYSVIEWS";\r
+\r
+ protected static final int SYSVIEWS_COLUMN_COUNT = 4;\r
+ protected static final int SYSVIEWS_TABLEID = 1;\r
+ protected static final int SYSVIEWS_VIEWDEFINITION = 2;\r
+ protected static final int SYSVIEWS_CHECKOPTION = 3;\r
+ protected static final int SYSVIEWS_COMPILATION_SCHEMAID = 4;\r
+\r
+ // Column widths\r
+ protected static final int SYSVIEWS_TABLEID_WIDTH = 36;\r
+\r
+ protected static final int SYSVIEWS_INDEX1_ID = 0;\r
+\r
+ private static final int[][] indexColumnPositions =\r
+ {\r
+ {SYSVIEWS_TABLEID}\r
+ };\r
+\r
+\r
+ // if you add a non-unique index allocate this array.\r
+ private static final boolean[] uniqueness = null;\r
+\r
+ private static final String[] uuids =\r
+ {\r
+ "8000004d-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID\r
+ ,"80000050-00d0-fd77-3ed8-000a0a0b1900" // heap UUID\r
+ ,"8000004f-00d0-fd77-3ed8-000a0a0b1900" // SYSVIEWS_INDEX1\r
+ };\r
+\r
+ /////////////////////////////////////////////////////////////////////////////\r
+ //\r
+ // CONSTRUCTORS\r
+ //\r
+ /////////////////////////////////////////////////////////////////////////////\r
+\r
+ SYSVIEWSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf)\r
+ {\r
+ super(uuidf,ef,dvf);\r
+ initInfo(SYSVIEWS_COLUMN_COUNT, TABLENAME_STRING, \r
+ indexColumnPositions, uniqueness, uuids );\r
+ }\r
+\r
+ /////////////////////////////////////////////////////////////////////////////\r
+ //\r
+ // METHODS\r
+ //\r
+ /////////////////////////////////////////////////////////////////////////////\r
+\r
+ /**\r
+ * Make a SYSVIEWS row\r
+ *\r
+ * @return Row suitable for inserting into SYSVIEWS.\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+ public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)\r
+ throws StandardException \r
+ {\r
+ DataValueDescriptor col;\r
+ ExecRow row;\r
+ String tableID = null;\r
+ String compSchemaId = null;\r
+ String viewText = null;\r
+ String checkSType = null;\r
+ int checkIType;\r
+\r
+ if (td != null)\r
+ {\r
+ UUID tableUUID;\r
+ ViewDescriptor vd = (ViewDescriptor)td;\r
+\r
+ /*\r
+ ** We only allocate a new UUID if the descriptor doesn't already have one.\r
+ ** For descriptors replicated from a Source system, we already have an UUID.\r
+ */\r
+ tableUUID = vd.getUUID();\r
+ if ( tableUUID == null )\r
+ {\r
+ tableUUID = getUUIDFactory().createUUID();\r
+ vd.setUUID(tableUUID);\r
+ }\r
+ tableID = tableUUID.toString();\r
+ viewText = vd.getViewText();\r
+\r
+ /* RESOLVE - check constraints not supported yet */\r
+ checkIType = vd.getCheckOptionType();\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (checkIType != ViewDescriptor.NO_CHECK_OPTION)\r
+ {\r
+ SanityManager.THROWASSERT("checkIType expected to be " + \r
+ ViewDescriptor.NO_CHECK_OPTION +\r
+ ", not " + checkIType);\r
+ }\r
+ }\r
+ checkSType = "N";\r
+\r
+ UUID tmpId = vd.getCompSchemaId();\r
+ compSchemaId = (tmpId == null) ? null : tmpId.toString();\r
+ }\r
+\r
+ /* Insert info into sysviews */\r
+\r
+ /* RESOLVE - It would be nice to require less knowledge about sysviews\r
+ * and have this be more table driven.\r
+ */\r
+\r
+ /* Build the row to insert */\r
+ row = getExecutionFactory().getValueRow(SYSVIEWS_COLUMN_COUNT);\r
+\r
+ /* 1st column is TABLEID (UUID - char(36)) */\r
+ row.setColumn(SYSVIEWS_TABLEID, new SQLChar(tableID));\r
+\r
+ /* 2nd column is VIEWDEFINITION */\r
+ row.setColumn(SYSVIEWS_VIEWDEFINITION,\r
+ dvf.getLongvarcharDataValue(viewText));\r
+\r
+ /* 3rd column is CHECKOPTION (char(1)) */\r
+ row.setColumn(SYSVIEWS_CHECKOPTION, new SQLChar(checkSType));\r
+\r
+ /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */\r
+ row.setColumn(SYSVIEWS_COMPILATION_SCHEMAID, new SQLChar(compSchemaId));\r
+\r
+ return row;\r
+ }\r
+ \r
+ ///////////////////////////////////////////////////////////////////////////\r
+ //\r
+ // ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory\r
+ //\r
+ ///////////////////////////////////////////////////////////////////////////\r
+\r
+ /**\r
+ * Make a ViewDescriptor out of a SYSVIEWS row\r
+ *\r
+ * @param row a SYSVIEWS row\r
+ * @param parentTupleDescriptor Null for this kind of descriptor.\r
+ * @param dd dataDictionary\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+ public TupleDescriptor buildDescriptor(\r
+ ExecRow row,\r
+ TupleDescriptor parentTupleDescriptor,\r
+ DataDictionary dd )\r
+ throws StandardException\r
+ {\r
+ ViewDescriptor vd = null;\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(\r
+ row.nColumns() == SYSVIEWS_COLUMN_COUNT, \r
+ "Wrong number of columns for a SYSVIEWS row");\r
+ }\r
+\r
+ DataValueDescriptor col;\r
+ DataDescriptorGenerator ddg;\r
+ int checkIType;\r
+ String checkSType;\r
+ String tableID;\r
+ String compSchemaId;\r
+ String viewDefinition;\r
+ UUID tableUUID;\r
+ UUID compSchemaUUID = null;\r
+\r
+ ddg = dd.getDataDescriptorGenerator();\r
+\r
+ /* 1st column is TABLEID (UUID - char(36)) */\r
+ col = row.getColumn(SYSVIEWS_TABLEID);\r
+ tableID = col.getString();\r
+ tableUUID = getUUIDFactory().recreateUUID(tableID);\r
+\r
+ /* 2nd column is VIEWDEFINITION */\r
+ col = row.getColumn(SYSVIEWS_VIEWDEFINITION);\r
+ viewDefinition = col.getString();\r
+\r
+ /* 3rd column is CHECKOPTION (char(1)) */\r
+ col = row.getColumn(SYSVIEWS_CHECKOPTION);\r
+ checkSType = col.getString();\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ if (!checkSType.equals("N"))\r
+ {\r
+ SanityManager.THROWASSERT("checkSType expected to be 'N', not " + checkSType);\r
+ }\r
+ }\r
+\r
+ /* RESOLVE - no check options for now */\r
+ checkIType = ViewDescriptor.NO_CHECK_OPTION;\r
+\r
+ /* 4th column is COMPILATIONSCHEMAID (UUID - char(36)) */\r
+ col = row.getColumn(SYSVIEWS_COMPILATION_SCHEMAID);\r
+ compSchemaId = col.getString();\r
+ if (compSchemaId != null)\r
+ {\r
+ compSchemaUUID = getUUIDFactory().recreateUUID(compSchemaId);\r
+ }\r
+\r
+ /* now build and return the descriptor */\r
+ vd = ddg.newViewDescriptor(tableUUID, null, viewDefinition, \r
+ checkIType, compSchemaUUID);\r
+ return vd;\r
+ }\r
+\r
+ /**\r
+ * Builds a list of columns suitable for creating this Catalog.\r
+ *\r
+ *\r
+ * @return array of SystemColumn suitable for making this catalog.\r
+ */\r
+ public SystemColumn[] buildColumnList()\r
+ {\r
+ return new SystemColumn[] {\r
+ SystemColumnImpl.getUUIDColumn("TABLEID", false),\r
+ SystemColumnImpl.getColumn("VIEWDEFINITION", Types.LONGVARCHAR,\r
+ false, TypeId.LONGVARCHAR_MAXWIDTH),\r
+ SystemColumnImpl.getIndicatorColumn("CHECKOPTION"),\r
+ SystemColumnImpl.getUUIDColumn("COMPILATIONSCHEMAID", true),\r
+ \r
+ };\r
+ }\r
+}\r