KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > ddl > schemadefinition > tabledefinition


1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
6 import com.daffodilwoods.daffodildb.server.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.*;
8 import com.daffodilwoods.daffodildb.server.sql99.common.*;
9 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
10 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause.*;
12 import com.daffodilwoods.daffodildb.server.sql99.token.*;
13 import com.daffodilwoods.daffodildb.utils.parser.*;
14 import com.daffodilwoods.database.general.*;
15 import com.daffodilwoods.database.resource.*;
16
17 /**
18  * Table Definition can exist in two ways
19  * 1. as a separate single statement
20  * 2. as a schema element in schema definition
21  * 3. from materialised view
22  */

23
24 /** @todo Droping of table in case of power failure */
25 public class tabledefinition implements SQLschemadefinitionstatement, schemaelement {
26
27    public SNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode _OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0;
28    public SNONRESERVEDWORD136444255 _OptSNONRESERVEDWORD1364442551;
29    public dummyrule _Optdummyrule2;
30    public tablecontentssource _tablecontentssource3;
31    public tablename _tablename4;
32    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439225;
33    public tablescope _Opttablescope6;
34    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439227;
35
36    private SchemaDescriptor schemaDescriptor;
37    private QualifiedIdentifier tableName;
38    private ArrayList indexesList; /** @todo ASK What is the info stored */
39    private String JavaDoc defaultIndexName;
40
41    /**
42     * if table definition is part of schema definition then schema descriptor is
43     * set through schema element
44     */

45
46    public void setSchemaDescriptor(_Descriptor schemaDes) throws DException {
47       schemaDescriptor = (SchemaDescriptor) schemaDes;
48    }
49
50    /** algo for table definition
51     * initialize -- currentSession, globalSession, TableDescriptor;
52     * commit the current transaction if not nested statement;
53     * setTableName
54     * a. set table Name
55     * b. setSchemaAndCatalog()
56     * c. ensureSchema()
57     * validateUserPrivileges()
58     * setTableProperties()
59     * a. setTableType()
60     * b. setCommitType()
61     * c. addDateSpanColumns() if date span type of table;
62     * save table descriptor
63     * setColumnsAndConstraints()
64     * a. run table contents
65     * b. validateCheckConstraints()
66     * c. setForeignConstraints();
67     * d. addSystemColumns()
68     * e. save columns and constraints;
69     * createUserPrivileges()
70     * commit data in system tables;globalSession.commit();
71     * createTableAndIndexes() -- for physical creation
72     */

73    public Object JavaDoc run(Object JavaDoc object) throws DException {
74       _ServerSession currentSession = (_ServerSession) object;
75       _ServerSession systemSession = currentSession.getSystemServerSession();
76
77       boolean isIndependentStt = schemaDescriptor == null;
78       checkUnsupportedFeatures();
79       TableDescriptor tableDescriptor = null;
80       try {
81          tableName = getQualifiedTableName(currentSession);
82          if (isIndependentStt) {
83             validateUserPrivilege(currentSession);
84          }
85          tableDescriptor = new TableDescriptor(tableName.catalog,
86                                                tableName.schema, tableName.getName());
87          setTableProperties(tableDescriptor);
88          tableDescriptor.save(currentSession);
89          checkAndSetDateSpan(tableDescriptor, currentSession);
90          executeTableContent(tableDescriptor, currentSession);
91          addSystemColumns(tableDescriptor, currentSession);
92          createPrivileges(tableDescriptor, currentSession);
93
94          /** @todo adding default column code should be in table element list */
95          if (!tableDescriptor.primarKeyCreated) {
96             createDefaultUniqueKey(currentSession);
97          }
98          if (isIndependentStt) {
99             validateCheckConstraintSemantic(object);
100             setReferentialConstraints(object);
101             /** @todo work pending for foreign key column */
102          }
103          createDefaultIndex(currentSession);
104       } catch (DException ex) {
105          if (tableName != null) {
106             systemSession.getDataDictionary().removeTable(tableName);
107          }
108          throw ex;
109       }
110       if (isIndependentStt) {
111          /** @todo why indexes are created after saving the table */
112          createTableAndIndexes(tableDescriptor.indexesList, object);
113       } else {
114          indexesList = tableDescriptor.indexesList;
115       }
116       return null;
117    }
118
119    public void validateCheckConstraintSemantic(Object JavaDoc object) throws DException {
120       _tablecontentssource3.validateCheckConstraintSemantic(object);
121    }
122
123    private void checkUnsupportedFeatures() throws DException {
124       if (! (_tablecontentssource3 instanceof parentableelementlist)) {
125          throw new DException("DSE22", new Object JavaDoc[] {"User-Defined Type"});
126       }
127       if (_Opttablescope6 != null) {
128          throw new DException("DSE22", new Object JavaDoc[] {"Table Scope"});
129       }
130       if (_Optdummyrule2 != null) {
131          throw new DException("DSE22", new Object JavaDoc[] {"Commit Action"});
132       }
133    }
134
135    private QualifiedIdentifier getQualifiedTableName(_ServerSession currentSession) throws DException {
136       String JavaDoc catalogName = _tablename4.getCatalogName();
137       String JavaDoc schemaName = _tablename4.getSchemaName();
138       String JavaDoc tableName = _tablename4.getTableName();
139
140       /**
141        * if table definition is part of schema definition
142        */

143       if (schemaDescriptor != null) {
144          if (schemaName == null) {
145             schemaName = schemaDescriptor.schema_name;
146          } else {
147             if (! (schemaName.equalsIgnoreCase(schemaDescriptor.schema_name))) {
148                throw new DException("DSE902", new Object JavaDoc[] {tableName});
149             }
150          }
151          if (catalogName == null) {
152             catalogName = schemaDescriptor.catalog_name;
153          } else {
154             if (! (catalogName.equalsIgnoreCase(schemaDescriptor.catalog_name))) {
155                throw new DException("DSE226", new Object JavaDoc[] {tableName});
156             }
157          }
158       } else {
159          if (catalogName == null) {
160             catalogName = currentSession.getCurrentCatalog();
161          }
162          if (schemaName == null) {
163             schemaName = currentSession.getCurrentSchema();
164          }
165          schemaDescriptor = new SchemaDescriptor();
166          schemaDescriptor.catalog_name = catalogName;
167          schemaDescriptor.schema_name = schemaName;
168          schemaDescriptor.load(currentSession);
169       }
170       QualifiedIdentifier temp = new QualifiedIdentifier(catalogName, schemaName, tableName);
171       return temp;
172    }
173
174    private void validateUserPrivilege(_ServerSession currentSession) throws DException {
175       String JavaDoc schemaOwner = schemaDescriptor.schema_owner;
176       if (!currentSession.isEnabledAuthorizationIdentifier(schemaOwner, true)) {
177          ;//// Removed By Program ** System.out.println("authorizationIdentifier " + schemaOwner);
178
throw new DException("DSE12", null);
179       }
180    }
181
182    public void createTableAndIndexes(Object JavaDoc object) throws DException {
183       createTableAndIndexes(indexesList, object);
184    }
185
186    private void createTableAndIndexes(ArrayList indexesList, Object JavaDoc object) throws DException {
187       _ServerSession currentSession = (_ServerSession) object;
188       if (!SystemTables.isSystemTable(tableName.getIdentifier())) {
189          currentSession.getSystemServerSession().createTable(tableName);
190          currentSession.createIndex(tableName, defaultIndexName, false);
191       }
192       if (indexesList != null) {
193          if (SystemTables.isSystemTable(tableName.getIdentifier())) {
194             for (int i = 0, size = indexesList.size(); i < size; i++) {
195                currentSession.createIndexForSystemTable(tableName,
196                    (String JavaDoc) indexesList.get(i));
197             }
198          } else {
199             for (int i = 0, size = indexesList.size(); i < size; i++) {
200                currentSession.createIndex(tableName, (String JavaDoc) indexesList.get(i), true);
201             }
202          }
203       }
204    }
205
206    private void createDefaultIndex(_ServerSession currentSession) throws DException {
207       defaultIndexName = currentSession.getDataDictionary().generateIndexName();
208
209       IndexDescriptor indexDesc = new IndexDescriptor();
210       indexDesc.table_catalog = tableName.catalog;
211       indexDesc.table_schema = tableName.schema;
212       indexDesc.table_name = tableName.getName();
213       indexDesc.indexname = defaultIndexName;
214       indexDesc.indextablename = defaultIndexName + "_Index";
215       indexDesc.fixedVariable = Boolean.FALSE;
216       indexDesc.is_system_generated = Boolean.TRUE;
217
218       setIndexColumnDescriptor(indexDesc, SystemFields.systemFields[SystemFields.rowId], 1);
219       if(!((ServerSession)currentSession).getSessionVersionHandler().hasOnlyRowidAsColumnIndex()){
220         setIndexColumnDescriptor(indexDesc,
221                                  SystemFields.systemFields[SystemFields.
222                                  transactionId], 2);
223         setIndexColumnDescriptor(indexDesc,
224                                  SystemFields.systemFields[SystemFields.sessionId],
225                                  3);
226       }
227       indexDesc.save(currentSession);
228
229    }
230
231    private void setIndexColumnDescriptor(IndexDescriptor indexDescriptor,
232                                          String JavaDoc columnName, int ordinalPosition) throws
233        DException {
234       IndexColumnDescriptor indexColDes = new IndexColumnDescriptor(SystemTables.INDEXCOLUMNS);
235       indexColDes.table_catalog = indexDescriptor.table_catalog;
236       indexColDes.table_schema = indexDescriptor.table_schema;
237       indexColDes.table_name = indexDescriptor.table_name;
238       indexColDes.indexname = indexDescriptor.indexname;
239       indexColDes.columnName = columnName;
240       indexColDes.orderType = Boolean.TRUE;
241       indexColDes.ordinalPosition = new Integer JavaDoc(ordinalPosition);
242       indexDescriptor.addIndexColumnDescriptor(indexColDes);
243    }
244
245    private void createDefaultUniqueKey(_ServerSession currentSession) throws DException {
246       TableConstraintDescriptor constDes = new TableConstraintDescriptor();
247       constDes.table_catalog = tableName.catalog;
248       constDes.table_schema = tableName.schema;
249       constDes.table_name = tableName.getName();
250       constDes.constraint_catalog = tableName.catalog;
251       constDes.constraint_schema = tableName.schema;
252       constDes.setGeneratedConstraintName(currentSession, "UKC_");
253       constDes.constraint_type = SqlKeywords.UNIQUE;
254
255       KeyColumnUsageDescriptor keyColumnUsageDes = new KeyColumnUsageDescriptor();
256       keyColumnUsageDes.constraint_catalog = constDes.constraint_catalog;
257       keyColumnUsageDes.constraint_schema = constDes.constraint_schema;
258       keyColumnUsageDes.constraint_name = constDes.constraint_name;
259       keyColumnUsageDes.table_catalog = constDes.table_catalog;
260       keyColumnUsageDes.table_schema = constDes.table_schema;
261       keyColumnUsageDes.table_name = constDes.table_name;
262       keyColumnUsageDes.column_name = SystemFields.systemFields[SystemFields.rowId];
263       keyColumnUsageDes.ordinal_position = 1;
264       keyColumnUsageDes.constraint_catalog = constDes.constraint_catalog;
265       keyColumnUsageDes.constraint_catalog = constDes.constraint_catalog;
266
267       ArrayList list = new ArrayList();
268       list.add(keyColumnUsageDes);
269       constDes.setConstraintColumnDescriptors(list);
270       constDes.save(currentSession);
271    }
272
273    /** @todo
274     * 1. we should add rowid at first ordinal position
275     * */

276    private void addSystemColumns(TableDescriptor tableDescriptor,
277                                  _ServerSession serverSession) throws
278        DException {
279
280       ColumnDescriptor rowIdColumnDes = new ColumnDescriptor();
281       rowIdColumnDes.table_catalog = tableDescriptor.table_catalog;
282       rowIdColumnDes.table_schema = tableDescriptor.table_schema;
283       rowIdColumnDes.table_name = tableDescriptor.table_name;
284       rowIdColumnDes.column_name = SystemFields.systemFields[SystemFields.rowId];
285       rowIdColumnDes.dtd_identifier = tableDescriptor.table_name + "." + SystemFields.systemFields[SystemFields.rowId];
286       rowIdColumnDes.isNullable = SqlKeywords.NO;
287       tableDescriptor.addColumnDescriptor(rowIdColumnDes);
288
289       DataTypeDescriptor dtdDes = new DataTypeDescriptor();
290       dtdDes.object_catalog = rowIdColumnDes.table_catalog;
291       dtdDes.object_schema = rowIdColumnDes.table_schema;
292       dtdDes.object_name = rowIdColumnDes.table_name;
293       dtdDes.object_type = SqlKeywords.TABLE;
294       dtdDes.dtd_identifier = rowIdColumnDes.dtd_identifier;
295       dtdDes.data_Type = SqlKeywords.LONG;
296       dtdDes.numeric_precision = new Integer JavaDoc(Datatypes.LONG_PRECISION);
297       dtdDes.numeric_precision_radix = new Integer JavaDoc(DataTypeDescriptor.implicit_numeric_precision_radix);
298       dtdDes.numeric_scal = new Integer JavaDoc(0);
299       rowIdColumnDes.dataTypeDescriptor = dtdDes;
300       rowIdColumnDes.save(serverSession);
301    }
302
303    private void setTableProperties(TableDescriptor tableDescriptor) throws DException {
304       tableDescriptor.table_type = SqlKeywords.TABLE;
305       if (_OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0 != null) {
306          tableDescriptor.country_code = _OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0.getCountryCode();
307          tableDescriptor.language_code = _OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0.getLanguageCode();
308       }
309    }
310
311    private void executeTableContent(TableDescriptor tableDescriptor,
312                                     _ServerSession serverSystem) throws
313        DException {
314       _tablecontentssource3.setTableDescriptor(tableDescriptor);
315       _tablecontentssource3.run(serverSystem);
316    }
317
318    private void checkAndSetDateSpan(TableDescriptor tableDes,
319                                     _ServerSession serverSystem) throws DException {
320       if (_OptSNONRESERVEDWORD1364442551 != null) {
321          ColumnDescriptor columnDes = new ColumnDescriptor();
322          columnDes.table_catalog = tableDes.table_catalog;
323          columnDes.table_schema = tableDes.table_schema;
324          columnDes.table_name = tableDes.table_name;
325          columnDes.column_name = SqlSchemaConstants.dateSpanFrom;
326          columnDes.isNullable = SqlKeywords.NO;
327          columnDes.dataTypeDescriptor = getDateSpanDataTypeDescriptor(columnDes);
328          columnDes.tableDescriptor = tableDes;
329          tableDes.addColumnDescriptor(columnDes);
330          columnDes.save(serverSystem);
331
332          columnDes = new ColumnDescriptor();
333          columnDes.table_catalog = tableDes.table_catalog;
334          columnDes.table_schema = tableDes.table_schema;
335          columnDes.table_name = tableDes.table_name;
336          columnDes.column_name = SqlSchemaConstants.dateSpanTo;
337          columnDes.isNullable = SqlKeywords.NO;
338          columnDes.dataTypeDescriptor = getDateSpanDataTypeDescriptor(columnDes);
339          columnDes.tableDescriptor = tableDes;
340          tableDes.addColumnDescriptor(columnDes);
341          columnDes.save(serverSystem);
342       }
343    }
344
345    private DataTypeDescriptor getDateSpanDataTypeDescriptor(ColumnDescriptor columnDes) throws DException {
346       DataTypeDescriptor columnTypeDescriptor = new DataTypeDescriptor();
347       columnTypeDescriptor.object_catalog = columnDes.table_catalog;
348       columnTypeDescriptor.object_schema = columnDes.table_schema;
349       columnTypeDescriptor.object_name = columnDes.table_name;
350       columnTypeDescriptor.object_type = SqlKeywords.TABLE;
351       columnTypeDescriptor.data_Type = SqlKeywords.DATE;
352       columnTypeDescriptor.dtd_identifier = columnDes.table_name + "." + columnDes.column_name;
353       columnTypeDescriptor.datatime_precision = new Integer JavaDoc(Datatypes.DATE_PRECISION);
354       return columnTypeDescriptor;
355    }
356
357    public void setReferentialConstraints(Object JavaDoc object) throws DException {
358       _tablecontentssource3.setReferentialConstraints(object);
359    }
360
361    private void createPrivileges(TableDescriptor tableDes,
362                                  _ServerSession currentSession) throws DException {
363       String JavaDoc query = QueryMaker.getTableDefinitionGrantQuery(tableDes.
364           getQualifiedTableName(), schemaDescriptor.schema_owner);
365       grantprivilegestatement grantstt = (grantprivilegestatement)
366       Parser.parseQuery(query);
367       grantstt.setObjectDescriptor(tableDes);
368       grantstt.run(currentSession.getSystemServerSession());
369    }
370
371    /**
372     * @param indexName
373     * Used in case of Materialized view
374     * @throws DException
375     */

376    public void addToIndexList(String JavaDoc indexName) throws DException {
377       if (indexesList == null) {
378          indexesList = new ArrayList();
379       }
380       indexesList.add(indexName);
381    }
382
383    public String JavaDoc toString() {
384       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
385       sb.append(" ");
386       sb.append(_SRESERVEDWORD12065439227);
387       sb.append(" ");
388       if (_Opttablescope6 != null) {
389          sb.append(_Opttablescope6);
390       }
391       sb.append(" ");
392       sb.append(_SRESERVEDWORD12065439225);
393       sb.append(" ");
394       sb.append(_tablename4);
395       sb.append(" ");
396       sb.append(_tablecontentssource3);
397       sb.append(" ");
398       if (_Optdummyrule2 != null) {
399          sb.append(_Optdummyrule2);
400       }
401       sb.append(" ");
402       if (_OptSNONRESERVEDWORD1364442551 != null) {
403          sb.append(_OptSNONRESERVEDWORD1364442551);
404       }
405       if (_OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0 != null) {
406          sb.append(_OptSNONRESERVEDWORD136444255countrycodeOptSRESERVEDWORD1206543922languagecode0);
407       }
408       return sb.toString();
409    }
410
411    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
412       return this;
413    }
414
415 }
416
Popular Tags