--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.catalog.SYSTABLESRowFactory\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 org.apache.derby.iapi.types.DataValueDescriptor;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.SystemColumn;\r
+\r
+import org.apache.derby.iapi.types.DataValueFactory;\r
+import org.apache.derby.iapi.types.RowLocation;\r
+import org.apache.derby.iapi.types.SQLChar;\r
+import org.apache.derby.iapi.types.SQLVarchar;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;\r
+import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.TableDescriptor;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.sql.execute.ExecIndexRow;\r
+import org.apache.derby.iapi.sql.execute.ExecutionFactory;\r
+import org.apache.derby.iapi.sql.execute.ExecRow;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.catalog.UUID;\r
+import org.apache.derby.iapi.services.uuid.UUIDFactory;\r
+\r
+/**\r
+ * Factory for creating a SYSTABLES row.\r
+ *\r
+ *\r
+ * @version 0.1\r
+ */\r
+\r
+class SYSTABLESRowFactory extends CatalogRowFactory\r
+{\r
+ private static final String TABLENAME_STRING = "SYSTABLES";\r
+\r
+ protected static final int SYSTABLES_COLUMN_COUNT = 5;\r
+ /* Column #s for systables (1 based) */\r
+ protected static final int SYSTABLES_TABLEID = 1;\r
+ protected static final int SYSTABLES_TABLENAME = 2;\r
+ protected static final int SYSTABLES_TABLETYPE = 3;\r
+ protected static final int SYSTABLES_SCHEMAID = 4;\r
+ protected static final int SYSTABLES_LOCKGRANULARITY = 5;\r
+\r
+ protected static final int SYSTABLES_INDEX1_ID = 0;\r
+ protected static final int SYSTABLES_INDEX1_TABLENAME = 1;\r
+ protected static final int SYSTABLES_INDEX1_SCHEMAID = 2;\r
+\r
+ protected static final int SYSTABLES_INDEX2_ID = 1;\r
+ protected static final int SYSTABLES_INDEX2_TABLEID = 1;\r
+ \r
+ // all indexes are unique.\r
+\r
+ private static final String[] uuids =\r
+ {\r
+ "80000018-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID\r
+ ,"80000028-00d0-fd77-3ed8-000a0a0b1900" // heap UUID\r
+ ,"8000001a-00d0-fd77-3ed8-000a0a0b1900" // SYSTABLES_INDEX1\r
+ ,"8000001c-00d0-fd77-3ed8-000a0a0b1900" // SYSTABLES_INDEX2\r
+ };\r
+\r
+ private static final int[][] indexColumnPositions = \r
+ { \r
+ { SYSTABLES_TABLENAME, SYSTABLES_SCHEMAID},\r
+ { SYSTABLES_TABLEID }\r
+ };\r
+\r
+ /////////////////////////////////////////////////////////////////////////////\r
+ //\r
+ // CONSTRUCTORS\r
+ //\r
+ /////////////////////////////////////////////////////////////////////////////\r
+\r
+ SYSTABLESRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf)\r
+ {\r
+ super(uuidf,ef,dvf);\r
+ initInfo(SYSTABLES_COLUMN_COUNT, TABLENAME_STRING, indexColumnPositions, (boolean[]) null, uuids);\r
+ }\r
+\r
+ /////////////////////////////////////////////////////////////////////////////\r
+ //\r
+ // METHODS\r
+ //\r
+ /////////////////////////////////////////////////////////////////////////////\r
+\r
+ /**\r
+ * Make a SYSTABLES row\r
+ *\r
+ * @return Row suitable for inserting into SYSTABLES.\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+\r
+ public ExecRow makeRow(TupleDescriptor td,\r
+ TupleDescriptor parent)\r
+ throws StandardException\r
+ {\r
+ UUID oid;\r
+ String tabSType = null;\r
+ int tabIType;\r
+ ExecRow row;\r
+ String lockGranularity = null;\r
+ String tableID = null;\r
+ String schemaID = null;\r
+ String tableName = null;\r
+\r
+\r
+ if (td != null)\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
+ TableDescriptor descriptor = (TableDescriptor)td;\r
+ SchemaDescriptor schema = (SchemaDescriptor)parent;\r
+\r
+ oid = descriptor.getUUID();\r
+ if ( oid == null )\r
+ {\r
+ oid = getUUIDFactory().createUUID();\r
+ descriptor.setUUID(oid);\r
+ }\r
+ tableID = oid.toString();\r
+ \r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(schema != null, \r
+ "Schema should not be null unless empty row is true");\r
+ if (schema.getUUID() == null)\r
+ {\r
+ SanityManager.THROWASSERT("schema " + schema + " has a null OID");\r
+ }\r
+ }\r
+ \r
+ schemaID = schema.getUUID().toString();\r
+\r
+ tableName = descriptor.getName();\r
+\r
+ /* RESOLVE - Table Type should really be a char in the descriptor\r
+ * T, S, V, S instead of 0, 1, 2, 3\r
+ */\r
+ tabIType = descriptor.getTableType();\r
+ switch (tabIType)\r
+ {\r
+ case TableDescriptor.BASE_TABLE_TYPE:\r
+ tabSType = "T";\r
+ break;\r
+ case TableDescriptor.SYSTEM_TABLE_TYPE:\r
+ tabSType = "S";\r
+ break;\r
+ case TableDescriptor.VIEW_TYPE:\r
+ tabSType = "V";\r
+ break; \r
+\r
+ case TableDescriptor.SYNONYM_TYPE:\r
+ tabSType = "A";\r
+ break; \r
+\r
+ default:\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("invalid table type");\r
+ }\r
+ char[] lockGChar = new char[1];\r
+ lockGChar[0] = descriptor.getLockGranularity();\r
+ lockGranularity = new String(lockGChar);\r
+ }\r
+\r
+ /* Insert info into systables */\r
+\r
+ /* RESOLVE - It would be nice to require less knowledge about systables\r
+ * and have this be more table driven.\r
+ */\r
+\r
+ /* Build the row to insert */\r
+ row = getExecutionFactory().getValueRow(SYSTABLES_COLUMN_COUNT);\r
+\r
+ /* 1st column is TABLEID (UUID - char(36)) */\r
+ row.setColumn(SYSTABLES_TABLEID, new SQLChar(tableID));\r
+\r
+ /* 2nd column is NAME (varchar(30)) */\r
+ row.setColumn(SYSTABLES_TABLENAME, new SQLVarchar(tableName));\r
+\r
+ /* 3rd column is TABLETYPE (char(1)) */\r
+ row.setColumn(SYSTABLES_TABLETYPE, new SQLChar(tabSType));\r
+\r
+ /* 4th column is SCHEMAID (UUID - char(36)) */\r
+ row.setColumn(SYSTABLES_SCHEMAID, new SQLChar(schemaID));\r
+\r
+ /* 5th column is LOCKGRANULARITY (char(1)) */\r
+ row.setColumn(SYSTABLES_LOCKGRANULARITY, new SQLChar(lockGranularity));\r
+\r
+ return row;\r
+ }\r
+\r
+ /**\r
+ * Builds an empty index row.\r
+ *\r
+ * @param indexNumber Index to build empty row for.\r
+ * @param rowLocation Row location for last column of index row\r
+ *\r
+ * @return corresponding empty index row\r
+ * @exception StandardException thrown on failure\r
+ */\r
+ ExecIndexRow buildEmptyIndexRow( int indexNumber,\r
+ RowLocation rowLocation)\r
+ throws StandardException\r
+ {\r
+ int ncols = getIndexColumnCount(indexNumber);\r
+ ExecIndexRow row = getExecutionFactory().getIndexableRow(ncols + 1);\r
+\r
+ row.setColumn(ncols + 1, rowLocation);\r
+\r
+ switch( indexNumber )\r
+ {\r
+ case SYSTABLES_INDEX1_ID:\r
+ /* 1st column is TABLENAME (varchar(128)) */\r
+ row.setColumn(1, new SQLVarchar());\r
+\r
+ /* 2nd column is SCHEMAID (UUID - char(36)) */\r
+ row.setColumn(2, new SQLChar());\r
+\r
+ break;\r
+\r
+ case SYSTABLES_INDEX2_ID:\r
+ /* 1st column is TABLEID (UUID - char(36)) */\r
+ row.setColumn(1,new SQLChar());\r
+ break;\r
+ } // end switch\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 TableDescriptor out of a SYSTABLES row\r
+ *\r
+ * @param row a SYSTABLES row\r
+ * @param parentTupleDescriptor Null for this kind of descriptor.\r
+ * @param dd dataDictionary\r
+ *\r
+ * @return a table descriptor equivalent to a SYSTABLES row\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+\r
+ public TupleDescriptor buildDescriptor(\r
+ ExecRow row,\r
+ TupleDescriptor parentTupleDescriptor,\r
+ DataDictionary dd )\r
+ throws StandardException\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(row.nColumns() == SYSTABLES_COLUMN_COUNT, "Wrong number of columns for a SYSTABLES row");\r
+\r
+ DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();\r
+\r
+ String tableUUIDString; \r
+ String schemaUUIDString; \r
+ int tableTypeEnum;\r
+ String lockGranularity;\r
+ String tableName, tableType;\r
+ DataValueDescriptor col;\r
+ UUID tableUUID;\r
+ UUID schemaUUID;\r
+ SchemaDescriptor schema;\r
+ TableDescriptor tabDesc;\r
+\r
+ /* 1st column is TABLEID (UUID - char(36)) */\r
+ col = row.getColumn(SYSTABLES_TABLEID);\r
+ tableUUIDString = col.getString();\r
+ tableUUID = getUUIDFactory().recreateUUID(tableUUIDString);\r
+\r
+\r
+ /* 2nd column is TABLENAME (varchar(128)) */\r
+ col = row.getColumn(SYSTABLES_TABLENAME);\r
+ tableName = col.getString();\r
+\r
+ /* 3rd column is TABLETYPE (char(1)) */\r
+ col = row.getColumn(SYSTABLES_TABLETYPE);\r
+ tableType = col.getString();\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(tableType.length() == 1, "Fourth column type incorrect");\r
+ }\r
+ switch (tableType.charAt(0))\r
+ {\r
+ case 'T' : \r
+ tableTypeEnum = TableDescriptor.BASE_TABLE_TYPE;\r
+ break;\r
+ case 'S' :\r
+ tableTypeEnum = TableDescriptor.SYSTEM_TABLE_TYPE;\r
+ break;\r
+ case 'V' :\r
+ tableTypeEnum = TableDescriptor.VIEW_TYPE;\r
+ break;\r
+ case 'A' :\r
+ tableTypeEnum = TableDescriptor.SYNONYM_TYPE;\r
+ break;\r
+ default:\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("Fourth column value invalid");\r
+ tableTypeEnum = -1;\r
+ }\r
+\r
+ /* 4th column is SCHEMAID (UUID - char(36)) */\r
+ col = row.getColumn(SYSTABLES_SCHEMAID);\r
+ schemaUUIDString = col.getString();\r
+ schemaUUID = getUUIDFactory().recreateUUID(schemaUUIDString);\r
+ \r
+ schema = dd.getSchemaDescriptor(schemaUUID, null);\r
+\r
+ /* 5th column is LOCKGRANULARITY (char(1)) */\r
+ col = row.getColumn(SYSTABLES_LOCKGRANULARITY);\r
+ lockGranularity = col.getString();\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(lockGranularity.length() == 1, "Fifth column type incorrect");\r
+ }\r
+\r
+ // RESOLVE - Deal with lock granularity\r
+ tabDesc = ddg.newTableDescriptor(tableName, schema, tableTypeEnum, lockGranularity.charAt(0));\r
+ tabDesc.setUUID(tableUUID);\r
+ return tabDesc;\r
+ }\r
+\r
+ /**\r
+ * Get the table name out of this SYSTABLES row\r
+ *\r
+ * @param row a SYSTABLES row\r
+ *\r
+ * @return string, the table name\r
+ *\r
+ * @exception StandardException thrown on failure\r
+ */\r
+ protected String getTableName(ExecRow row)\r
+ throws StandardException\r
+ {\r
+ DataValueDescriptor col;\r
+\r
+ col = row.getColumn(SYSTABLES_TABLENAME);\r
+ return col.getString();\r
+ }\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.getIdentifierColumn("TABLENAME", false),\r
+ SystemColumnImpl.getIndicatorColumn("TABLETYPE"),\r
+ SystemColumnImpl.getUUIDColumn("SCHEMAID", false),\r
+ SystemColumnImpl.getIndicatorColumn("LOCKGRANULARITY"),\r
+ };\r
+ }\r
+\r
+}\r