KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > types > RoutineAliasInfo


1 /*
2
3    Derby - Class org.apache.derby.catalog.types.RoutineAliasInfo
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.catalog.types;
23
24 import org.apache.derby.iapi.services.io.Formatable;
25 import org.apache.derby.iapi.services.io.StoredFormatIds;
26 import org.apache.derby.iapi.services.io.ArrayUtil;
27 import org.apache.derby.iapi.reference.SQLState;
28 import org.apache.derby.iapi.reference.JDBC30Translation;
29 import org.apache.derby.catalog.AliasInfo;
30 import org.apache.derby.catalog.TypeDescriptor;
31 import java.io.IOException JavaDoc;
32 import java.io.ObjectInput JavaDoc;
33 import java.io.ObjectOutput JavaDoc;
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35
36 /**
37  * Describe a r (procedure or function) alias.
38  *
39  * @see AliasInfo
40  */

41 public class RoutineAliasInfo extends MethodAliasInfo
42 {
43
44     private static final String JavaDoc[] SQL_CONTROL = {"MODIFIES SQL DATA", "READS SQL DATA", "CONTAINS SQL", "NO SQL"};
45     public static final short MODIFIES_SQL_DATA = 0;
46     public static final short READS_SQL_DATA = 1;
47     public static final short CONTAINS_SQL = 2;
48     public static final short NO_SQL = 3;
49
50
51
52     /** PARAMETER STYLE JAVA */
53     public static final short PS_JAVA = 0;
54
55     private int parameterCount;
56
57     private TypeDescriptor[] parameterTypes;
58     private String JavaDoc[] parameterNames;
59     /**
60         IN, OUT, INOUT
61     */

62     private int[] parameterModes;
63
64     private int dynamicResultSets;
65
66     /**
67         Return type for functions. Null for procedures.
68     */

69     private TypeDescriptor returnType;
70
71     /**
72         Parameter style - always PS_JAVA at the moment.
73     */

74     private short parameterStyle;
75
76     /**
77         What SQL is allowed by this procedure.
78     */

79     private short sqlAllowed;
80
81     /**
82         SQL Specific name (future)
83     */

84     private String JavaDoc specificName;
85
86     /**
87         True if the routine is called on null input.
88         (always true for procedures).
89     */

90     private boolean calledOnNullInput;
91
92     // What type of alias is this: PROCEDURE or FUNCTION?
93
private transient char aliasType;
94
95     public RoutineAliasInfo() {
96     }
97
98     /**
99         Create a RoutineAliasInfo for an internal PROCEDURE.
100     */

101     public RoutineAliasInfo(String JavaDoc methodName, int parameterCount, String JavaDoc[] parameterNames,
102         TypeDescriptor[] parameterTypes, int[] parameterModes, int dynamicResultSets, short parameterStyle, short sqlAllowed) {
103
104         this(methodName, parameterCount, parameterNames, parameterTypes, parameterModes,
105             dynamicResultSets, parameterStyle, sqlAllowed, true, (TypeDescriptor) null);
106     }
107
108     /**
109         Create a RoutineAliasInfo for a PROCEDURE or FUNCTION
110     */

111     public RoutineAliasInfo(String JavaDoc methodName, int parameterCount, String JavaDoc[] parameterNames,
112         TypeDescriptor[] parameterTypes, int[] parameterModes, int dynamicResultSets, short parameterStyle, short sqlAllowed,
113         boolean calledOnNullInput, TypeDescriptor returnType)
114     {
115
116         super(methodName);
117         this.parameterCount = parameterCount;
118         this.parameterNames = parameterNames;
119         this.parameterTypes = parameterTypes;
120         this.parameterModes = parameterModes;
121         this.dynamicResultSets = dynamicResultSets;
122         this.parameterStyle = parameterStyle;
123         this.sqlAllowed = sqlAllowed;
124         this.calledOnNullInput = calledOnNullInput;
125         this.returnType = returnType;
126
127         if (SanityManager.DEBUG) {
128
129             if (parameterCount != 0 && parameterNames.length != parameterCount) {
130                 SanityManager.THROWASSERT("Invalid parameterNames array " + parameterNames.length + " != " + parameterCount);
131             }
132             else if (parameterCount == 0 && parameterNames != null && parameterNames.length != 0) {
133                 SanityManager.THROWASSERT("Invalid parameterNames array " + " not zero " + " != " + parameterCount);
134             }
135
136             if (parameterCount != 0 && parameterTypes.length != parameterCount) {
137                 SanityManager.THROWASSERT("Invalid parameterTypes array " + parameterTypes.length + " != " + parameterCount);
138             }
139             else if (parameterCount == 0 && parameterTypes != null && parameterTypes.length != 0) {
140                 SanityManager.THROWASSERT("Invalid parameterTypes array " + " not zero " + " != " + parameterCount);
141             }
142
143             if (parameterCount != 0 && parameterModes.length != parameterCount) {
144                 SanityManager.THROWASSERT("Invalid parameterModes array " + parameterModes.length + " != " + parameterCount);
145             }
146             else if (parameterCount == 0 && parameterModes != null && parameterModes.length != 0) {
147                 SanityManager.THROWASSERT("Invalid parameterModes array " + " not zero " + " != " + parameterCount);
148             }
149
150             if (returnType != null) {
151                 if (!((sqlAllowed >= RoutineAliasInfo.READS_SQL_DATA) && (sqlAllowed <= RoutineAliasInfo.NO_SQL))) {
152                     SanityManager.THROWASSERT("Invalid sqlAllowed for FUNCTION " + methodName + " " + sqlAllowed);
153                 }
154             } else {
155                 if (!((sqlAllowed >= RoutineAliasInfo.MODIFIES_SQL_DATA) && (sqlAllowed <= RoutineAliasInfo.NO_SQL))) {
156                     SanityManager.THROWASSERT("Invalid sqlAllowed for PROCEDURE " + methodName + " " + sqlAllowed);
157                 }
158                 
159             }
160         }
161     }
162
163     public int getParameterCount() {
164         return parameterCount;
165     }
166
167     public TypeDescriptor[] getParameterTypes() {
168         return parameterTypes;
169     }
170
171     public int[] getParameterModes() {
172         return parameterModes;
173     }
174     public String JavaDoc[] getParameterNames() {
175         return parameterNames;
176     }
177
178     public int getMaxDynamicResultSets() {
179         return dynamicResultSets;
180     }
181
182     public short getParameterStyle() {
183         return parameterStyle;
184     }
185
186     public short getSQLAllowed() {
187         return sqlAllowed;
188     }
189
190     public boolean calledOnNullInput() {
191         return calledOnNullInput;
192     }
193
194     public TypeDescriptor getReturnType() {
195         return returnType;
196     }
197
198
199     // Formatable methods
200

201     /**
202      * Read this object from a stream of stored objects.
203      *
204      * @param in read this.
205      *
206      * @exception IOException thrown on error
207      * @exception ClassNotFoundException thrown on error
208      */

209     public void readExternal( ObjectInput JavaDoc in )
210          throws IOException JavaDoc, ClassNotFoundException JavaDoc
211     {
212         super.readExternal(in);
213         specificName = (String JavaDoc) in.readObject();
214         dynamicResultSets = in.readInt();
215         parameterCount = in.readInt();
216         parameterStyle = in.readShort();
217         sqlAllowed = in.readShort();
218         returnType = (TypeDescriptor) in.readObject();
219         calledOnNullInput = in.readBoolean();
220         in.readInt(); // future expansion.
221

222         if (parameterCount != 0) {
223             parameterNames = new String JavaDoc[parameterCount];
224             parameterTypes = new TypeDescriptor[parameterCount];
225
226             ArrayUtil.readArrayItems(in, parameterNames);
227             ArrayUtil.readArrayItems(in, parameterTypes);
228             parameterModes = ArrayUtil.readIntArray(in);
229
230         } else {
231             parameterNames = null;
232             parameterTypes = null;
233             parameterModes = null;
234         }
235     }
236
237     /**
238      * Write this object to a stream of stored objects.
239      *
240      * @param out write bytes here.
241      *
242      * @exception IOException thrown on error
243      */

244     public void writeExternal( ObjectOutput JavaDoc out )
245          throws IOException JavaDoc
246     {
247         super.writeExternal(out);
248         out.writeObject(specificName);
249         out.writeInt(dynamicResultSets);
250         out.writeInt(parameterCount);
251         out.writeShort(parameterStyle);
252         out.writeShort(sqlAllowed);
253         out.writeObject(returnType);
254         out.writeBoolean(calledOnNullInput);
255         out.writeInt(0); // future expansion
256
if (parameterCount != 0) {
257             ArrayUtil.writeArrayItems(out, parameterNames);
258             ArrayUtil.writeArrayItems(out, parameterTypes);
259             ArrayUtil.writeIntArray(out, parameterModes);
260         }
261     }
262  
263     /**
264      * Get the formatID which corresponds to this class.
265      *
266      * @return the formatID of this class
267      */

268     public int getTypeFormatId() { return StoredFormatIds.ROUTINE_INFO_V01_ID; }
269
270     /**
271      * Get this alias info as a string. NOTE: The "ALIASINFO" column
272      * in the SYSALIASES table will return the result of this method
273      * on a ResultSet.getString() call. That said, since the dblook
274      * utility uses ResultSet.getString() to retrieve ALIASINFO and
275      * to generate the DDL, THIS METHOD MUST RETURN A STRING THAT
276      * IS SYNTACTICALLY VALID, or else the DDL generated by dblook
277      * will be incorrect.
278      */

279     public String JavaDoc toString() {
280
281         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
282         sb.append(getMethodName());
283         sb.append('(');
284         for (int i = 0; i < parameterCount; i++) {
285             if (i != 0)
286                 sb.append(',');
287
288             if (returnType == null) {
289             // This is a PROCEDURE. We only want to print the
290
// parameter mode (ex. "IN", "OUT", "INOUT") for procedures--
291
// we don't do it for functions since use of the "IN" keyword
292
// is not part of the FUNCTION syntax.
293
sb.append(RoutineAliasInfo.parameterMode(parameterModes[i]));
294                 sb.append(' ');
295             }
296             sb.append(parameterNames[i]);
297             sb.append(' ');
298             sb.append(parameterTypes[i].getSQLstring());
299         }
300         sb.append(')');
301
302         if (returnType != null) {
303         // this a FUNCTION, so syntax requires us to append the return type.
304
sb.append(" RETURNS " + returnType.getSQLstring());
305         }
306
307         sb.append(" LANGUAGE JAVA PARAMETER STYLE JAVA ");
308         sb.append(RoutineAliasInfo.SQL_CONTROL[getSQLAllowed()]);
309         if ((returnType == null) &&
310             (dynamicResultSets != 0))
311         { // Only print dynamic result sets if this is a PROCEDURE
312
// because it's not valid syntax for FUNCTIONs.
313
sb.append(" DYNAMIC RESULT SETS ");
314             sb.append(dynamicResultSets);
315         }
316
317         if (returnType != null) {
318         // this a FUNCTION, so append the syntax telling what to
319
// do with a null parameter.
320
sb.append(calledOnNullInput ? " CALLED " : " RETURNS NULL ");
321             sb.append("ON NULL INPUT");
322         }
323         
324         return sb.toString();
325     }
326
327     public static String JavaDoc parameterMode(int parameterMode) {
328         switch (parameterMode) {
329         case JDBC30Translation.PARAMETER_MODE_IN:
330             return "IN";
331         case JDBC30Translation.PARAMETER_MODE_OUT:
332             return "OUT";
333         case JDBC30Translation.PARAMETER_MODE_IN_OUT:
334             return "INOUT";
335         default:
336             return "UNKNOWN";
337         }
338     }
339 }
340
Popular Tags