KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > SetSchemaConstantAction


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.SetSchemaConstantAction
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.execute;
23
24
25 import org.apache.derby.iapi.sql.execute.ConstantAction;
26
27 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
28 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
29 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
30 import org.apache.derby.iapi.sql.ParameterValueSet;
31 import org.apache.derby.iapi.sql.StatementType;
32
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34
35 import org.apache.derby.iapi.reference.Limits;
36
37 import org.apache.derby.iapi.error.StandardException;
38
39 import org.apache.derby.iapi.sql.Activation;
40
41 import org.apache.derby.iapi.services.sanity.SanityManager;
42
43 import org.apache.derby.iapi.reference.SQLState;
44
45 /**
46  * This class describes actions that are ALWAYS performed for a
47  * SET SCHEMA Statement at Execution time.
48  *
49  * @author jamie
50  */

51
52 class SetSchemaConstantAction extends GenericConstantAction
53 {
54
55     private final String JavaDoc schemaName;
56     private final int type;
57     
58     // CONSTRUCTORS
59

60     /**
61      * Make the ConstantAction for a SET SCHEMA statement.
62      *
63      * @param schemaName Name of schema.
64      * @param type type of set schema (e.g. SET_SCHEMA_DYNAMIC, SET_SCHEMA_USER)
65      */

66     SetSchemaConstantAction(String JavaDoc schemaName, int type)
67     {
68         this.schemaName = schemaName;
69         this.type = type;
70     }
71
72     ///////////////////////////////////////////////
73
//
74
// OBJECT SHADOWS
75
//
76
///////////////////////////////////////////////
77

78     public String JavaDoc toString()
79     {
80         // Do not put this under SanityManager.DEBUG - it is needed for
81
// error reporting.
82
// if the error happens after we have figured out the schema name for
83
// dynamic we want to use it rather than ?
84
return "SET SCHEMA " + ((type == StatementType.SET_SCHEMA_USER) ? "USER" :
85                 ((type == StatementType.SET_SCHEMA_DYNAMIC && schemaName == null) ? "?" : schemaName));
86     }
87
88     // INTERFACE METHODS
89

90
91     /**
92      * This is the guts of the Execution-time logic for CREATE SCHEMA.
93      *
94      * @see ConstantAction#executeConstantAction
95      *
96      * @exception StandardException Thrown on failure
97      */

98     public void executeConstantAction( Activation activation )
99                         throws StandardException
100     {
101         LanguageConnectionContext lcc;
102         DataDictionary dd;
103
104         // find the language context.
105
lcc = activation.getLanguageConnectionContext();
106
107         dd = lcc.getDataDictionary();
108         String JavaDoc thisSchemaName = schemaName;
109         if (type == StatementType.SET_SCHEMA_DYNAMIC)
110         {
111             ParameterValueSet pvs = activation.getParameterValueSet();
112             DataValueDescriptor dvs = pvs.getParameter(0);
113             thisSchemaName = dvs.getString();
114             //null parameter is not allowed
115
if (thisSchemaName == null || thisSchemaName.length() > Limits.MAX_IDENTIFIER_LENGTH)
116                 throw StandardException.newException(SQLState.LANG_DB2_REPLACEMENT_ERROR, "CURRENT SCHEMA");
117         }
118         else if (type == StatementType.SET_SCHEMA_USER)
119         {
120             thisSchemaName = lcc.getAuthorizationId();
121         }
122         // if schemaName is null, sd will be null and default schema will be used
123
SchemaDescriptor sd = dd.getSchemaDescriptor(thisSchemaName, null, true);
124         lcc.setDefaultSchema(sd);
125     }
126 }
127
Popular Tags