001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi;
020:
021: /**
022: * A coloumn reference in a relational database with a table name,
023: * a column name, a data type and possibly an alias for the table name.
024: *
025: * @author Michael Bell
026: * @version $Revision: 1.1 $
027: *
028: */
029: public class ColumnRef extends Object {
030:
031: /**
032: * Constant indicating a date data type
033: */
034: static public int DATE = 0;
035:
036: /**
037: * Constant indicating a text data type
038: */
039: static public int TEXT = 1;
040:
041: /**
042: * Constant indicating a number data type
043: */
044: static public int NUMBER = 2;
045:
046: /**
047: * Constant indicating a long text data type
048: */
049: static public int LONG_TEXT = 3;
050:
051: /**
052: * The name of the table containing the column
053: */
054: private String m_sTable = null;
055:
056: /**
057: * The alias for the table name
058: */
059: private String m_sTableAlias = null;
060:
061: /**
062: * The name of the column
063: */
064: private String m_sColumn = null;
065:
066: /**
067: * The data type of the column
068: */
069: private int m_nDataType = 1;
070:
071: /**
072: * The cached full string representation of this column reference
073: */
074: private String m_sFullRefCache = null;
075:
076: /**
077: * Hashcode for this object
078: */
079: private int m_nHashcode = 0;
080:
081: /**
082: * Constructs a column reference.
083: *
084: * @param sTable the name of table containing the column
085: * @param sColumn the name of the column
086: * @param nDataType the data type
087: * @throws DataStoreException if any of the parameters are invalid
088: */
089: public ColumnRef(String sTable, String sColumn, int nDataType)
090: throws DataStoreException {
091: if ((sTable == null) || (sColumn == null)) {
092: throw new DataStoreException("Invalid Type: table:"
093: + sTable + ",column:" + sColumn);
094: }
095:
096: if ((nDataType < DATE) || (nDataType > LONG_TEXT)) {
097: throw new DataStoreException("Invalid data type");
098: }
099:
100: m_sTable = sTable;
101: m_sColumn = sColumn;
102: m_nDataType = nDataType;
103: }
104:
105: /**
106: * Returns the name of the table containing the column.
107: *
108: * @return the name of the table containing the column
109: */
110: public String getTable() {
111: return m_sTable;
112: }
113:
114: /**
115: * Sets the name of the table containing the column.
116: *
117: * @param sTable the name of the table containing the column
118: */
119: public void setTable(String sTable) {
120: if (this .m_sFullRefCache != null) {
121: this .m_sFullRefCache = null;
122: }
123: m_sTable = sTable;
124: }
125:
126: /**
127: * Returns the alias for the table name.
128: *
129: * @return the alias for the table name
130: */
131: public String getTableAlias() {
132: return m_sTableAlias;
133: }
134:
135: /**
136: * Sets the alias for the table name.
137: *
138: * @param sTableAlias the alias for the table name
139: */
140: public void setTableAlias(String sTableAlias) {
141: if (this .m_sFullRefCache != null) {
142: this .m_sFullRefCache = null;
143: }
144:
145: m_sTableAlias = sTableAlias;
146: }
147:
148: /**
149: * Returns <code>true</code> if the table name has an alias set.
150: *
151: * @return <code>true</code> if the table name has an alias set.
152: */
153: public boolean hasTableAlias() {
154: return (m_sTableAlias != null);
155: }
156:
157: /**
158: * Returns the name of the column.
159: *
160: * @return the name of the column
161: */
162: public String getColumn() {
163: return m_sColumn;
164: }
165:
166: /**
167: * Sets the name of the column.
168: *
169: * @param sColumn the name of the column
170: */
171: public void setColumn(String sColumn) {
172: if (this .m_sFullRefCache != null) {
173: this .m_sFullRefCache = null;
174: }
175: m_sColumn = sColumn;
176: }
177:
178: /**
179: * Returns the full string representation of this column reference.
180: *
181: * @return the full string representation of this column reference
182: */
183: public String getFullRef() {
184: if (this .m_sFullRefCache == null) {
185: String sTable = m_sTable;
186: if (hasTableAlias() == true) {
187: sTable = m_sTableAlias;
188: }
189:
190: m_sFullRefCache = sTable + "." + m_sColumn;
191: }
192: return this .m_sFullRefCache;
193: }
194:
195: /**
196: * Returns the data type of this column.
197: *
198: * @return the data type of this column
199: */
200: public int getDataType() {
201: return m_nDataType;
202: }
203:
204: /* (non-Javadoc)
205: * @see java.lang.Object#equals(java.lang.Object)
206: */
207: public boolean equals(Object obj) {
208: boolean bEq = false;
209:
210: if (this == obj) {
211: bEq = true;
212: } else if (obj instanceof ColumnRef) {
213: ColumnRef colref = (ColumnRef) obj;
214:
215: if (colref.getColumn().equals(getColumn()) == true
216: && colref.getTable().equals(getTable())) {
217: bEq = true;
218: }
219: }
220:
221: return bEq;
222: }
223:
224: /* (non-Javadoc)
225: * @see java.lang.Object#hashCode()
226: */
227: public int hashCode() {
228:
229: if (m_nHashcode == 0) {
230: int nHash = 17;
231:
232: nHash = 37 * nHash + m_nDataType;
233: if (m_sColumn != null) {
234: nHash = 37 * nHash + m_sColumn.hashCode();
235: }
236: if (m_sTable != null) {
237: nHash = 37 * nHash + m_sTable.hashCode();
238: }
239: if (m_sTableAlias != null) {
240: nHash = 37 * nHash + m_sTableAlias.hashCode();
241: }
242:
243: m_nHashcode = nHash;
244: }
245:
246: return m_nHashcode;
247: }
248:
249: }
|