KickJava   Java API By Example, From Geeks To Geeks.

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


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.ddl.descriptors.*;
8 import com.daffodilwoods.daffodildb.server.sql99.token.*;
9 import com.daffodilwoods.database.resource.*;
10
11 public class schemadefinition implements SQLschemadefinitionstatement {
12    public schemaelement[] _OptRepschemaelement0;
13    public schemacharactersetorpath _Optschemacharactersetorpath1;
14    public schemanameclause _schemanameclause2;
15    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223;
16    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439224;
17
18    /** @todo algo
19     * initialize currentSession, globalSession and Schema Descriptor
20     * commit the currentSession
21     * setSchemaName
22     * a. set name
23     * b. set catalog
24     * validate user rights
25     * setSchemaProperties()
26     * a. setSchemaPath
27     * b. setCharacterSet
28     * */

29    public Object JavaDoc run(Object JavaDoc object) throws DException {
30       _ServerSession currentSession = (_ServerSession) object;
31       validateUserRights(currentSession);
32       SchemaDescriptor schemaDescriptor = new SchemaDescriptor();
33       String JavaDoc currentCatalog = currentSession.getCurrentCatalog();
34       String JavaDoc currentSchema = currentSession.getCurrentSchema();
35       checkSchemaName(currentSession);
36       try {
37          setSchemaName(schemaDescriptor, currentSession);
38          setDefaultSchemaProperty(schemaDescriptor, currentSession);
39
40          schemaDescriptor.save(currentSession);
41          currentSession.setCurrentCatalog(schemaDescriptor.catalog_name);
42          currentSession.setCurrentSchema(schemaDescriptor.schema_name);
43
44          executeSchemaElements(schemaDescriptor, object);
45       } finally {
46          currentSession.setCurrentCatalog(currentCatalog);
47          currentSession.setCurrentSchema(currentSchema);
48       }
49       createIndexesForSchemaElements(object);
50       return null;
51    }
52
53    private void checkSchemaName(_ServerSession currentSession) throws DException {
54       String JavaDoc schema_Name = (String JavaDoc) _schemanameclause2.run(currentSession);
55       if (schema_Name != null && schema_Name.equalsIgnoreCase(SqlSchemaConstants.defaultSchema)) {
56          throw new DException("DSE8168", new Object JavaDoc[]{schema_Name});
57       }
58    }
59
60    /* User Rights are implementation defined */
61    private void validateUserRights(_ServerSession connection) throws DException {
62    }
63
64    private void setSchemaName(SchemaDescriptor schemaDescriptor,
65                               _ServerSession serverSession) throws DException {
66       _schemanameclause2.setSchemaDescriptor(schemaDescriptor);
67       _schemanameclause2.run(serverSession);
68
69       if (schemaDescriptor.catalog_name == null) {
70          schemaDescriptor.catalog_name = serverSession.getCurrentCatalog(); // temporary
71
} else if (!serverSession.getCurrentUser().equalsIgnoreCase(SystemTables.SYSTEM)) {
72          if ( (!schemaDescriptor.catalog_name.equalsIgnoreCase(SqlSchemaConstants.defaultCatalog)) ||
73              (schemaDescriptor.catalog_name.equalsIgnoreCase(SqlSchemaConstants.systemCatalog))) {
74             throw new DException("DSE8137", new Object JavaDoc[] {schemaDescriptor.catalog_name});
75          }
76       }
77
78       if (schemaDescriptor.schema_owner == null) {
79          schemaDescriptor.schema_owner = serverSession.getAuthorizationIdentifier();
80       }
81
82       if (schemaDescriptor.schema_name == null) {
83          schemaDescriptor.schema_name = schemaDescriptor.schema_owner;
84
85       }
86       if (schemaDescriptor.schema_name == null && schemaDescriptor.schema_owner == null) {
87          throw new DException("DSE715", null);
88       }
89       if (schemaDescriptor.schema_owner == null) {
90          throw new DException("DSE1029", null);
91       }
92       if (schemaDescriptor.schema_name == null) {
93          throw new DException("DSE1030", null);
94       }
95    }
96
97    private void setDefaultSchemaProperty(SchemaDescriptor schemaDescriptor,
98                                          _ServerSession serverSession) throws
99        DException {
100       setDefaultCharacterSet(schemaDescriptor);
101       setDefaultSchemaPath(schemaDescriptor, serverSession);
102    }
103
104    private void setDefaultCharacterSet(SchemaDescriptor schemaDescriptor) throws
105        DException {
106       schemaDescriptor.default_character_set_catalog = "users";
107       schemaDescriptor.default_character_set_schema = "users";
108       schemaDescriptor.default_character_set_name = SqlSchemaConstants.
109           defaultCharacterSet;
110    }
111
112    private void setDefaultSchemaPath(SchemaDescriptor schemaDescriptor,
113                                      _ServerSession serverSession) throws DException {
114       schemaDescriptor.sql_path = serverSession.getCurrentSchema() + "," +
115           schemaDescriptor.schema_name;
116    }
117
118    private void executeSchemaElements(SchemaDescriptor schemaDescriptor,
119                                       Object JavaDoc object) throws DException {
120       if (_OptRepschemaelement0 != null) {
121          ArrayList schemaElements = new ArrayList(Arrays.asList(_OptRepschemaelement0));
122          int[] executionOrderOfSchemaElements = makeExecutionOrder();
123          int[] types = getTypes(schemaElements);
124          for (int i = 0; i < executionOrderOfSchemaElements.length; i++) {
125             executeDefinitionOfType(schemaDescriptor, object, schemaElements, types,
126                                     executionOrderOfSchemaElements[i]);
127          }
128       }
129    }
130
131    private void createIndexesForSchemaElements(Object JavaDoc object) throws DException {
132       if (_OptRepschemaelement0 != null) {
133          for (int i = 0, size = _OptRepschemaelement0.length; i < size; i++) {
134             if (_OptRepschemaelement0[i] instanceof tabledefinition) {
135                ( (tabledefinition) _OptRepschemaelement0[i]).createTableAndIndexes(
136                    object);
137             }
138          }
139       }
140    }
141
142    public String JavaDoc toString() {
143       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144       sb.append(" ");
145       sb.append(_SRESERVEDWORD12065439224);
146       sb.append(" ");
147       sb.append(_SRESERVEDWORD12065439223);
148       sb.append(" ");
149       sb.append(_schemanameclause2);
150       sb.append(" ");
151       if (_Optschemacharactersetorpath1 != null) {
152          sb.append(_Optschemacharactersetorpath1);
153       }
154       sb.append(" ");
155       if (_OptRepschemaelement0 != null) {
156          for (int i = 0; i < _OptRepschemaelement0.length; i++) {
157             sb.append("").append(_OptRepschemaelement0[i]);
158          }
159       }
160       return sb.toString();
161    }
162
163    private static int[] makeExecutionOrder() throws DException {
164       return new int[] {
165           Domain_Type, Table_Type, View_Type, Trigger_Type, Role_Type,
166           SchemaRoutine_Type, GrantRole_Type, Grant_Type};
167    }
168
169    private void executeDefinitionOfType(SchemaDescriptor schemaDescriptor, Object JavaDoc object, ArrayList schemaElements,
170                                         int[] elementType, int type) throws
171        DException {
172       boolean saveForeignConstraints = type == Table_Type;
173       for (int i = 0, elementCount = schemaElements.size(); i < elementCount; i++) {
174          if (elementType[i] == type) {
175             schemaelement schemaElement = (schemaelement) schemaElements.get(i);
176             schemaElement.setSchemaDescriptor(schemaDescriptor);
177             schemaElement.run(object);
178          }
179       }
180
181       if (saveForeignConstraints) {
182          for (int i = 0, elementCount = schemaElements.size(); i < elementCount; i++) {
183             if (elementType[i] == type) {
184                tabledefinition tableDefintion = (tabledefinition) schemaElements.get(
185                    i);
186                tableDefintion.validateCheckConstraintSemantic(object);
187                tableDefintion.setReferentialConstraints(object);
188             }
189          }
190       }
191    }
192
193    private int[] getTypes(ArrayList schemaElements) throws DException {
194       int elementCount = schemaElements.size();
195       int[] types = new int[elementCount];
196       for (int i = 0; i < elementCount; i++) {
197          schemaelement schemaElement = (schemaelement) schemaElements.get(i);
198          if (schemaElement instanceof schemaroutine) {
199             types[i] = SchemaRoutine_Type;
200          } else if (schemaElement instanceof domaindefinition) {
201             types[i] = Domain_Type;
202          } else if (schemaElement instanceof tabledefinition) {
203             types[i] = Table_Type;
204          } else if (schemaElement instanceof viewdefinition) {
205             types[i] = View_Type;
206          } else if (schemaElement instanceof triggerdefinition) {
207             types[i] = Trigger_Type;
208          } else if (schemaElement instanceof roledefinition) {
209             types[i] = Role_Type;
210          } else if (schemaElement instanceof grantrolestatement) {
211             types[i] = GrantRole_Type;
212          } else if (schemaElement instanceof grantstatement) {
213             types[i] = Grant_Type;
214          }
215       }
216       return types;
217    }
218
219    private static final int Domain_Type = 2;
220    private static final int Table_Type = 3;
221    private static final int View_Type = 4;
222    private static final int Trigger_Type = 5;
223    private static final int SchemaRoutine_Type = 9;
224
225    private static final int Role_Type = 10;
226    private static final int GrantRole_Type = 11;
227    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
228       return this;
229    }
230
231    private static final int Grant_Type = 12;
232 }
233
Popular Tags