KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > CreateAliasNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.CreateAliasNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.loader.ClassInspector;
25
26 import org.apache.derby.iapi.services.context.ContextManager;
27
28 import org.apache.derby.iapi.sql.compile.CompilerContext;
29 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
30
31 import org.apache.derby.iapi.reference.SQLState;
32 import org.apache.derby.iapi.reference.Limits;
33
34 import org.apache.derby.iapi.sql.ResultSet;
35 import org.apache.derby.iapi.sql.execute.ConstantAction;
36
37 import org.apache.derby.iapi.types.TypeId;
38
39 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
40 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
41 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import org.apache.derby.iapi.services.monitor.Monitor;
46 import org.apache.derby.iapi.services.sanity.SanityManager;
47
48 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
49 import org.apache.derby.impl.sql.execute.BaseActivation;
50
51 import org.apache.derby.catalog.AliasInfo;
52 import org.apache.derby.catalog.TypeDescriptor;
53 import org.apache.derby.catalog.types.RoutineAliasInfo;
54 import org.apache.derby.catalog.types.SynonymAliasInfo;
55
56 import java.lang.reflect.Member JavaDoc;
57 import java.util.Vector JavaDoc;
58
59 /**
60  * A CreateAliasNode represents a CREATE ALIAS statement.
61  *
62  * @author Jerry Brenner
63  */

64
65 public class CreateAliasNode extends DDLStatementNode
66 {
67     private String JavaDoc javaClassName;
68     private String JavaDoc methodName;
69     private char aliasType;
70     private boolean delimitedIdentifier;
71
72     private AliasInfo aliasInfo;
73
74
75     /**
76      * Initializer for a CreateAliasNode
77      *
78      * @param aliasName The name of the alias
79      * @param targetObject Target name
80      * @param methodName The method name
81      * @param aliasType The alias type
82      * @param delimitedIdentifier Whether or not to treat the class name
83      * as a delimited identifier if trying to
84      * resolve it as a class alias
85      *
86      * @exception StandardException Thrown on error
87      */

88     public void init(
89                         Object JavaDoc aliasName,
90                         Object JavaDoc targetObject,
91                         Object JavaDoc methodName,
92                         Object JavaDoc aliasSpecificInfo,
93                         Object JavaDoc aliasType,
94                         Object JavaDoc delimitedIdentifier)
95         throws StandardException
96     {
97         TableName qn = (TableName) aliasName;
98         this.aliasType = ((Character JavaDoc) aliasType).charValue();
99
100         initAndCheck(qn);
101
102         switch (this.aliasType)
103         {
104             case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
105             case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
106             {
107                 this.javaClassName = (String JavaDoc) targetObject;
108                 this.methodName = (String JavaDoc) methodName;
109                 this.delimitedIdentifier =
110                                 ((Boolean JavaDoc) delimitedIdentifier).booleanValue();
111
112                 //routineElements contains the description of the procedure.
113
//
114
// 0 - Object[] 3 element array for parameters
115
// 1 - TableName - specific name
116
// 2 - Integer - dynamic result set count
117
// 3 - String language (always java) - ignore
118
// 4 - String external name (also passed directly to create alias node - ignore
119
// 5 - Integer parameter style
120
// 6 - Short - SQL control
121
// 7 - Boolean - CALLED ON NULL INPUT (always TRUE for procedures)
122
// 8 - TypeDescriptor - return type (always NULL for procedures)
123

124                 Object JavaDoc[] routineElements = (Object JavaDoc[]) aliasSpecificInfo;
125                 Object JavaDoc[] parameters = (Object JavaDoc[]) routineElements[0];
126                 int paramCount = ((Vector JavaDoc) parameters[0]).size();
127                 
128                 // Support for Java signatures in Derby was added in 10.1
129
// Check to see the catalogs have been upgraded to 10.1 before
130
// accepting such a method name for a routine. Otherwise
131
// a routine that works in 10.1 soft upgrade mode would
132
// exist when running 10.0 but not resolve to anything.
133
if (this.methodName.indexOf('(') != -1)
134                 {
135                     getDataDictionary().checkVersion(
136                             DataDictionary.DD_VERSION_DERBY_10_1,
137                             "EXTERNAL NAME 'class.method(<signature>)'");
138                     
139                 }
140
141                 String JavaDoc[] names = null;
142                 TypeDescriptor[] types = null;
143                 int[] modes = null;
144                 
145                 if (paramCount > Limits.DB2_MAX_PARAMS_IN_STORED_PROCEDURE)
146                     throw StandardException.newException(SQLState.LANG_TOO_MANY_PARAMETERS_FOR_STORED_PROC,
147                             String.valueOf(Limits.DB2_MAX_PARAMS_IN_STORED_PROCEDURE), aliasName, String.valueOf(paramCount));
148
149                 if (paramCount != 0) {
150
151                     names = new String JavaDoc[paramCount];
152                     ((Vector JavaDoc) parameters[0]).copyInto(names);
153
154                     types = new TypeDescriptor[paramCount];
155                     ((Vector JavaDoc) parameters[1]).copyInto(types);
156
157                     modes = new int[paramCount];
158                     for (int i = 0; i < paramCount; i++) {
159                         modes[i] = ((Integer JavaDoc) (((Vector JavaDoc) parameters[2]).elementAt(i))).intValue();
160
161                         if (TypeId.getBuiltInTypeId(types[i].getJDBCTypeId()).isLongConcatableTypeId())
162                             throw StandardException.newException(SQLState.LANG_LONG_DATA_TYPE_NOT_ALLOWED, names[i]);
163
164                     }
165
166                     if (paramCount > 1) {
167                         String JavaDoc[] dupNameCheck = new String JavaDoc[paramCount];
168                         System.arraycopy(names, 0, dupNameCheck, 0, paramCount);
169                         java.util.Arrays.sort(dupNameCheck);
170                         for (int dnc = 1; dnc < dupNameCheck.length; dnc++) {
171                             if (dupNameCheck[dnc].equals(dupNameCheck[dnc - 1]))
172                                 throw StandardException.newException(SQLState.LANG_DB2_DUPLICATE_NAMES, dupNameCheck[dnc], getFullName());
173                         }
174                     }
175                 }
176
177                 Integer JavaDoc drso = (Integer JavaDoc) routineElements[2];
178                 int drs = drso == null ? 0 : drso.intValue();
179
180                 short sqlAllowed;
181                 Short JavaDoc sqlAllowedObject = (Short JavaDoc) routineElements[6];
182                 if (sqlAllowedObject != null)
183                     sqlAllowed = sqlAllowedObject.shortValue();
184                 else
185                     sqlAllowed = (this.aliasType == AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR ?
186                     RoutineAliasInfo.MODIFIES_SQL_DATA : RoutineAliasInfo.READS_SQL_DATA);
187
188                 Boolean JavaDoc calledOnNullInputO = (Boolean JavaDoc) routineElements[7];
189                 boolean calledOnNullInput;
190                 if (calledOnNullInputO == null)
191                     calledOnNullInput = true;
192                 else
193                     calledOnNullInput = calledOnNullInputO.booleanValue();
194
195                 aliasInfo = new RoutineAliasInfo(this.methodName, paramCount, names, types, modes, drs,
196                         ((Short JavaDoc) routineElements[5]).shortValue(), // parameter style
197
sqlAllowed, calledOnNullInput, (TypeDescriptor) routineElements[8]);
198
199                 implicitCreateSchema = true;
200                 }
201                 break;
202
203             case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
204                 String JavaDoc targetSchema;
205                 implicitCreateSchema = true;
206                 TableName t = (TableName) targetObject;
207                 if (t.getSchemaName() != null)
208                     targetSchema = t.getSchemaName();
209                 else targetSchema = getSchemaDescriptor().getSchemaName();
210                 aliasInfo = new SynonymAliasInfo(targetSchema, t.getTableName());
211                 break;
212
213             default:
214                 if (SanityManager.DEBUG)
215                 {
216                     SanityManager.THROWASSERT(
217                         "Unexpected value for aliasType (" + aliasType + ")");
218                 }
219         }
220     }
221
222     public String JavaDoc statementToString()
223     {
224         switch (this.aliasType)
225         {
226         case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
227             return "CREATE PROCEDURE";
228         case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
229             return "CREATE SYNONYM";
230         default:
231             return "CREATE FUNCTION";
232         }
233     }
234
235     // We inherit the generate() method from DDLStatementNode.
236

237     /**
238      * Bind this CreateAliasNode. This means doing any static error
239      * checking that can be done before actually creating the table.
240      * For example, verifying that the column name list does not
241      * contain any duplicate column names.
242      *
243      * @return The bound query tree
244      *
245      * @exception StandardException Thrown on error
246      */

247
248     public QueryTreeNode bind() throws StandardException
249     {
250         // Procedures and functions do not check class or method validity until
251
// runtime execution. Synonyms do need some validity checks.
252
if (aliasType != AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR)
253             return this;
254
255         // Don't allow creating synonyms in SESSION schema. Causes confusion if
256
// a temporary table is created later with same name.
257
if (isSessionSchema(getSchemaDescriptor().getSchemaName()))
258             throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
259
260         String JavaDoc targetSchema = ((SynonymAliasInfo)aliasInfo).getSynonymSchema();
261         String JavaDoc targetTable = ((SynonymAliasInfo)aliasInfo).getSynonymTable();
262         if (this.getObjectName().equals(targetSchema, targetTable))
263             throw StandardException.newException(SQLState.LANG_SYNONYM_CIRCULAR,
264                         this.getFullName(),
265                         targetSchema+"."+targetTable);
266
267         SchemaDescriptor targetSD = getSchemaDescriptor(targetSchema, false);
268         if ((targetSD != null) && isSessionSchema(targetSD))
269             throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
270
271         return this;
272     }
273
274     /**
275      * Create the Constant information that will drive the guts of Execution.
276      *
277      * @exception StandardException Thrown on failure
278      */

279     public ConstantAction makeConstantAction() throws StandardException
280     {
281         String JavaDoc schemaName;
282         switch (aliasType) {
283         case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
284         case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
285             schemaName = getSchemaDescriptor().getSchemaName();
286             break;
287         case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
288             schemaName = getSchemaDescriptor().getSchemaName();
289             break;
290         default:
291             schemaName = null;
292         }
293
294         return getGenericConstantActionFactory().getCreateAliasConstantAction(
295                                               getRelativeName(),
296                                               schemaName,
297                                               javaClassName,
298                                               aliasInfo,
299                                               aliasType);
300     }
301 }
302
Popular Tags