001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.TableName
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
025: import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
026: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
027:
028: import org.apache.derby.iapi.error.StandardException;
029:
030: import org.apache.derby.iapi.services.sanity.SanityManager;
031:
032: import org.apache.derby.iapi.reference.Property;
033: import org.apache.derby.iapi.reference.SQLState;
034:
035: /**
036: * A TableName represents a qualified name, externally represented as a schema name
037: * and an object name separated by a dot. This class is mis-named: it is used to
038: * represent the names of other object types in addition to tables.
039: *
040: * @author Jerry Brenner
041: */
042:
043: public class TableName extends QueryTreeNode {
044: /* Both schemaName and tableName can be null, however, if
045: ** tableName is null then schemaName must also be null.
046: */
047: String tableName;
048: String schemaName;
049: private boolean hasSchema;
050:
051: /**
052: * Initializer for when you have both the table and schema names.
053: *
054: * @param schemaName The name of the schema being referenced
055: * @param tableName The name of the table being referenced
056: */
057:
058: public void init(Object schemaName, Object tableName) {
059: hasSchema = schemaName != null;
060: this .schemaName = (String) schemaName;
061: this .tableName = (String) tableName;
062: }
063:
064: /**
065: * Initializer for when you have both the table and schema names.
066: *
067: * @param schemaName The name of the schema being referenced
068: * @param tableName The name of the table being referenced
069: * @param tokBeginOffset begin position of token for the table name
070: * identifier from parser. pass in -1 if unknown
071: * @param tokEndOffset end position of token for the table name
072: * identifier from parser. pass in -1 if unknown
073: */
074: public void init(Object schemaName, Object tableName,
075: Object tokBeginOffset, Object tokEndOffset) {
076: init(schemaName, tableName);
077: this .setBeginOffset(((Integer) tokBeginOffset).intValue());
078: this .setEndOffset(((Integer) tokEndOffset).intValue());
079: }
080:
081: /**
082: * Get the table name (without the schema name).
083: *
084: * @return Table name as a String
085: */
086:
087: public String getTableName() {
088: return tableName;
089: }
090:
091: /**
092: * Return true if this instance was initialized with not null schemaName.
093: *
094: * @return true if this instance was initialized with not null schemaName
095: */
096:
097: public boolean hasSchema() {
098: return hasSchema;
099: }
100:
101: /**
102: * Get the schema name.
103: *
104: * @return Schema name as a String
105: */
106:
107: public String getSchemaName() {
108: return schemaName;
109: }
110:
111: /**
112: * Set the schema name.
113: *
114: * @param schemaName Schema name as a String
115: */
116:
117: public void setSchemaName(String schemaName) {
118: this .schemaName = schemaName;
119: }
120:
121: /**
122: * Get the full table name (with the schema name, if explicitly
123: * specified).
124: *
125: * @return Full table name as a String
126: */
127:
128: public String getFullTableName() {
129: if (schemaName != null)
130: return schemaName + "." + tableName;
131: else
132: return tableName;
133: }
134:
135: /**
136: * Convert this object to a String. See comments in QueryTreeNode.java
137: * for how this should be done for tree printing.
138: *
139: * @return This object as a String
140: */
141:
142: public String toString() {
143: if (hasSchema)
144: return getFullTableName();
145: else
146: return tableName;
147: }
148:
149: /**
150: * 2 TableNames are equal if their both their schemaNames and tableNames are
151: * equal, or if this node's full table name is null (which happens when a
152: * SELECT * is expanded). Also, only check table names if the schema
153: * name(s) are null.
154: *
155: * @param otherTableName The other TableName.
156: *
157: * @return boolean Whether or not the 2 TableNames are equal.
158: */
159: public boolean equals(TableName otherTableName) {
160: if (otherTableName == null)
161: return false;
162:
163: String fullTableName = getFullTableName();
164: if (fullTableName == null) {
165: return true;
166: } else if ((schemaName == null)
167: || (otherTableName.getSchemaName() == null)) {
168: return tableName.equals(otherTableName.getTableName());
169: } else {
170: return fullTableName.equals(otherTableName
171: .getFullTableName());
172: }
173: }
174:
175: /**
176: * 2 TableNames are equal if their both their schemaNames and tableNames are
177: * equal, or if this node's full table name is null (which happens when a
178: * SELECT * is expanded). Also, only check table names if the schema
179: * name(s) are null.
180: *
181: * @param otherSchemaName The other TableName.
182: * @param otherTableName The other TableName.
183: *
184: * @return boolean Whether or not the 2 TableNames are equal.
185: */
186: public boolean equals(String otherSchemaName, String otherTableName) {
187: String fullTableName = getFullTableName();
188: if (fullTableName == null) {
189: return true;
190: } else if ((schemaName == null) || (otherSchemaName == null)) {
191: return tableName.equals(otherTableName);
192: } else {
193: return fullTableName.equals(otherSchemaName + "."
194: + otherTableName);
195: }
196: }
197:
198: ///////////////////////////////////////////////////////////////////////
199: //
200: // BIND METHODS
201: //
202: ///////////////////////////////////////////////////////////////////////
203:
204: /**
205: * Bind this TableName. This means filling in the schema name if it
206: * wasn't specified.
207: *
208: * @param dataDictionary Data dictionary to bind against.
209: *
210: * @exception StandardException Thrown on error
211: */
212: public void bind(DataDictionary dataDictionary)
213: throws StandardException {
214: schemaName = getSchemaDescriptor(schemaName).getSchemaName();
215: }
216:
217: ///////////////////////////////////////////////////////////////////////
218: //
219: // OBJECT INTERFACE
220: //
221: ///////////////////////////////////////////////////////////////////////
222:
223: /**
224: * Returns a hashcode for this tableName. This allows us to use TableNames
225: * as keys in hash lists.
226: *
227: * @return hashcode for this tablename
228: */
229: public int hashCode() {
230: return getFullTableName().hashCode();
231: }
232:
233: /**
234: * Compares two TableNames. Needed for hashing logic to work.
235: *
236: * @param other other tableName
237: */
238: public boolean equals(Object other) {
239: if (!(other instanceof TableName)) {
240: return false;
241: }
242:
243: TableName that = (TableName) other;
244:
245: return this.getFullTableName().equals(that.getFullTableName());
246: }
247:
248: }
|