KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TableName
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.sql.dictionary.DataDictionary;
25 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
27
28 import org.apache.derby.iapi.error.StandardException;
29
30 import org.apache.derby.iapi.services.sanity.SanityManager;
31
32 import org.apache.derby.iapi.reference.Property;
33 import org.apache.derby.iapi.reference.SQLState;
34
35 /**
36  * A TableName represents a qualified name, externally represented as a schema name
37  * and an object name separated by a dot. This class is mis-named: it is used to
38  * represent the names of other object types in addition to tables.
39  *
40  * @author Jerry Brenner
41  */

42
43 public class TableName extends QueryTreeNode
44 {
45     /* Both schemaName and tableName can be null, however, if
46     ** tableName is null then schemaName must also be null.
47     */

48     String JavaDoc tableName;
49     String JavaDoc schemaName;
50     private boolean hasSchema;
51
52     /**
53      * Initializer for when you have both the table and schema names.
54      *
55      * @param schemaName The name of the schema being referenced
56      * @param tableName The name of the table being referenced
57      */

58
59     public void init(Object JavaDoc schemaName, Object JavaDoc tableName)
60     {
61         hasSchema = schemaName != null;
62         this.schemaName = (String JavaDoc) schemaName;
63         this.tableName = (String JavaDoc) tableName;
64     }
65
66     /**
67      * Initializer for when you have both the table and schema names.
68      *
69      * @param schemaName The name of the schema being referenced
70      * @param tableName The name of the table being referenced
71      * @param tokBeginOffset begin position of token for the table name
72      * identifier from parser. pass in -1 if unknown
73      * @param tokEndOffset end position of token for the table name
74      * identifier from parser. pass in -1 if unknown
75      */

76     public void init
77     (
78         Object JavaDoc schemaName,
79         Object JavaDoc tableName,
80         Object JavaDoc tokBeginOffset,
81         Object JavaDoc tokEndOffset
82     )
83     {
84         init(schemaName, tableName);
85         this.setBeginOffset(((Integer JavaDoc) tokBeginOffset).intValue());
86         this.setEndOffset(((Integer JavaDoc) tokEndOffset).intValue());
87     }
88
89     /**
90      * Get the table name (without the schema name).
91      *
92      * @return Table name as a String
93      */

94
95     public String JavaDoc getTableName()
96     {
97         return tableName;
98     }
99
100     /**
101      * Return true if this instance was initialized with not null schemaName.
102      *
103      * @return true if this instance was initialized with not null schemaName
104      */

105     
106     public boolean hasSchema(){
107         return hasSchema;
108     }
109
110     /**
111      * Get the schema name.
112      *
113      * @return Schema name as a String
114      */

115
116     public String JavaDoc getSchemaName()
117     {
118         return schemaName;
119     }
120
121     /**
122      * Set the schema name.
123      *
124      * @param schemaName Schema name as a String
125      */

126
127     public void setSchemaName(String JavaDoc schemaName)
128     {
129         this.schemaName = schemaName;
130     }
131
132     /**
133      * Get the full table name (with the schema name, if explicitly
134      * specified).
135      *
136      * @return Full table name as a String
137      */

138
139     public String JavaDoc getFullTableName()
140     {
141         if (schemaName != null)
142             return schemaName + "." + tableName;
143         else
144             return tableName;
145     }
146
147     /**
148      * Convert this object to a String. See comments in QueryTreeNode.java
149      * for how this should be done for tree printing.
150      *
151      * @return This object as a String
152      */

153
154     public String JavaDoc toString()
155     {
156         if (hasSchema)
157             return getFullTableName();
158         else
159             return tableName;
160     }
161
162     /**
163      * 2 TableNames are equal if their both their schemaNames and tableNames are
164      * equal, or if this node's full table name is null (which happens when a
165      * SELECT * is expanded). Also, only check table names if the schema
166      * name(s) are null.
167      *
168      * @param otherTableName The other TableName.
169      *
170      * @return boolean Whether or not the 2 TableNames are equal.
171      */

172     public boolean equals(TableName otherTableName)
173     {
174         if( otherTableName == null)
175             return false;
176         
177         String JavaDoc fullTableName = getFullTableName();
178         if (fullTableName == null)
179         {
180             return true;
181         }
182         else if ((schemaName == null) ||
183                  (otherTableName.getSchemaName() == null))
184         {
185             return tableName.equals(otherTableName.getTableName());
186         }
187         else
188         {
189             return fullTableName.equals(otherTableName.getFullTableName());
190         }
191     }
192
193     /**
194      * 2 TableNames are equal if their both their schemaNames and tableNames are
195      * equal, or if this node's full table name is null (which happens when a
196      * SELECT * is expanded). Also, only check table names if the schema
197      * name(s) are null.
198      *
199      * @param otherSchemaName The other TableName.
200      * @param otherTableName The other TableName.
201      *
202      * @return boolean Whether or not the 2 TableNames are equal.
203      */

204     public boolean equals(String JavaDoc otherSchemaName, String JavaDoc otherTableName)
205     {
206         String JavaDoc fullTableName = getFullTableName();
207         if (fullTableName == null)
208         {
209             return true;
210         }
211         else if ((schemaName == null) ||
212                  (otherSchemaName == null))
213         {
214             return tableName.equals(otherTableName);
215         }
216         else
217         {
218             return fullTableName.equals(otherSchemaName+"."+otherTableName);
219         }
220     }
221
222     ///////////////////////////////////////////////////////////////////////
223
//
224
// BIND METHODS
225
//
226
///////////////////////////////////////////////////////////////////////
227

228     /**
229       * Bind this TableName. This means filling in the schema name if it
230       * wasn't specified.
231       *
232       * @param dataDictionary Data dictionary to bind against.
233       *
234       * @exception StandardException Thrown on error
235       */

236     public void bind( DataDictionary dataDictionary )
237                                throws StandardException
238     {
239         schemaName = getSchemaDescriptor(schemaName).getSchemaName();
240     }
241
242     ///////////////////////////////////////////////////////////////////////
243
//
244
// OBJECT INTERFACE
245
//
246
///////////////////////////////////////////////////////////////////////
247

248     /**
249       * Returns a hashcode for this tableName. This allows us to use TableNames
250       * as keys in hash lists.
251       *
252       * @return hashcode for this tablename
253       */

254     public int hashCode()
255     {
256         return getFullTableName().hashCode();
257     }
258
259     /**
260       * Compares two TableNames. Needed for hashing logic to work.
261       *
262       * @param other other tableName
263       */

264     public boolean equals( Object JavaDoc other )
265     {
266         if ( !( other instanceof TableName ) ) { return false; }
267
268         TableName that = (TableName) other;
269
270         return this.getFullTableName().equals( that.getFullTableName() );
271     }
272
273 }
274
Popular Tags