KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.LockTableNode
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.context.ContextManager;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.sql.compile.CompilerContext;
33
34 import org.apache.derby.iapi.sql.conn.Authorizer;
35
36 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
37 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
38 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
39 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
40 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
41
42 import org.apache.derby.iapi.reference.SQLState;
43
44 import org.apache.derby.iapi.sql.execute.ConstantAction;
45
46 import org.apache.derby.iapi.sql.Activation;
47 import org.apache.derby.iapi.sql.ResultSet;
48 import org.apache.derby.iapi.reference.ClassName;
49
50 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
51 import org.apache.derby.iapi.services.classfile.VMOpcode;
52
53 /**
54  * A LockTableNode is the root of a QueryTree that represents a LOCK TABLE command:
55  * LOCK TABLE <TableName> IN SHARE/EXCLUSIVE MODE
56  *
57  * @author Jerry Brenner
58  */

59
60 public class LockTableNode extends MiscellaneousStatementNode
61 {
62     private TableName tableName;
63     private boolean exclusiveMode;
64     private long conglomerateNumber;
65     private TableDescriptor lockTableDescriptor;
66
67     /**
68      * Initializer for LockTableNode
69      *
70      * @param tableName The table to lock
71      * @param exclusiveMode boolean, whether or not to get an exclusive lock.
72      */

73     public void init(Object JavaDoc tableName, Object JavaDoc exclusiveMode)
74     {
75         this.tableName = (TableName) tableName;
76         this.exclusiveMode = ((Boolean JavaDoc) exclusiveMode).booleanValue();
77     }
78
79     /**
80      * Convert this object to a String. See comments in QueryTreeNode.java
81      * for how this should be done for tree printing.
82      *
83      * @return This object as a String
84      */

85
86     public String JavaDoc toString()
87     {
88         if (SanityManager.DEBUG)
89         {
90             return "tableName: " + tableName + "\n" +
91                 "exclusiveMode: " + exclusiveMode + "\n" +
92                 "conglomerateNumber: " + conglomerateNumber + "\n" +
93                 super.toString();
94         }
95         else
96         {
97             return "";
98         }
99     }
100
101     public String JavaDoc statementToString()
102     {
103         return "LOCK TABLE";
104     }
105
106     /**
107      * Bind this LockTableNode. This means looking up the table,
108      * verifying it exists and getting the heap conglomerate number.
109      *
110      * @return The bound query tree
111      *
112      * @exception StandardException Thrown on error
113      */

114
115     public QueryTreeNode bind() throws StandardException
116     {
117         CompilerContext cc = getCompilerContext();
118         ConglomerateDescriptor cd;
119         DataDictionary dd = getDataDictionary();
120         SchemaDescriptor sd;
121
122         String JavaDoc schemaName = tableName.getSchemaName();
123         sd = getSchemaDescriptor(schemaName);
124
125         // Users are not allowed to lock system tables
126
if (sd.isSystemSchema())
127         {
128             throw StandardException.newException(SQLState.LANG_NO_USER_DDL_IN_SYSTEM_SCHEMA,
129                             statementToString(), schemaName);
130         }
131
132         lockTableDescriptor = getTableDescriptor(tableName.getTableName(), sd);
133
134         if (lockTableDescriptor == null)
135         {
136             // Check if the reference is for a synonym.
137
TableName synonymTab = resolveTableToSynonym(tableName);
138             if (synonymTab == null)
139                 throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, tableName);
140             tableName = synonymTab;
141             sd = getSchemaDescriptor(tableName.getSchemaName());
142
143             lockTableDescriptor = getTableDescriptor(synonymTab.getTableName(), sd);
144             if (lockTableDescriptor == null)
145                 throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, tableName);
146         }
147
148         //throw an exception if user is attempting to lock a temporary table
149
if (lockTableDescriptor.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE)
150         {
151                 throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE);
152         }
153
154         conglomerateNumber = lockTableDescriptor.getHeapConglomerateId();
155
156         /* Get the base conglomerate descriptor */
157         cd = lockTableDescriptor.getConglomerateDescriptor(conglomerateNumber);
158
159         /* Statement is dependent on the TableDescriptor and ConglomerateDescriptor */
160         cc.createDependency(lockTableDescriptor);
161         cc.createDependency(cd);
162
163         if (isPrivilegeCollectionRequired())
164         {
165             // need SELECT privilege to perform lock table statement.
166
cc.pushCurrentPrivType(Authorizer.SELECT_PRIV);
167             cc.addRequiredTablePriv(lockTableDescriptor);
168             cc.popCurrentPrivType();
169         }
170
171         return this;
172     }
173
174     /**
175      * Return true if the node references SESSION schema tables (temporary or permanent)
176      *
177      * @return true if references SESSION schema tables, else false
178      *
179      * @exception StandardException Thrown on error
180      */

181     public boolean referencesSessionSchema()
182         throws StandardException
183     {
184         //If lock table is on a SESSION schema table, then return true.
185
return isSessionSchema(lockTableDescriptor.getSchemaName());
186     }
187
188     /**
189      * Create the Constant information that will drive the guts of Execution.
190      *
191      * @exception StandardException Thrown on failure
192      */

193     public ConstantAction makeConstantAction() throws StandardException
194     {
195         return getGenericConstantActionFactory().getLockTableConstantAction(
196                         tableName.getFullTableName(),
197                         conglomerateNumber,
198                         exclusiveMode);
199     }
200 }
201
Popular Tags