KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > ColumnRef


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi;
20
21 /**
22  * A coloumn reference in a relational database with a table name,
23  * a column name, a data type and possibly an alias for the table name.
24  *
25  * @author Michael Bell
26  * @version $Revision: 1.1 $
27  *
28  */

29 public class ColumnRef extends Object JavaDoc {
30     
31     /**
32      * Constant indicating a date data type
33      */

34     static public int DATE = 0;
35     
36     /**
37      * Constant indicating a text data type
38      */

39     static public int TEXT = 1;
40     
41     /**
42      * Constant indicating a number data type
43      */

44     static public int NUMBER = 2;
45     
46     /**
47      * Constant indicating a long text data type
48      */

49     static public int LONG_TEXT = 3;
50     
51     /**
52      * The name of the table containing the column
53      */

54     private String JavaDoc m_sTable = null;
55     
56     /**
57      * The alias for the table name
58      */

59     private String JavaDoc m_sTableAlias = null;
60     
61     /**
62      * The name of the column
63      */

64     private String JavaDoc m_sColumn = null;
65     
66     /**
67      * The data type of the column
68      */

69     private int m_nDataType = 1;
70     
71     /**
72      * The cached full string representation of this column reference
73      */

74     private String JavaDoc m_sFullRefCache = null;
75     
76     /**
77      * Hashcode for this object
78      */

79     private int m_nHashcode = 0;
80
81     /**
82      * Constructs a column reference.
83      *
84      * @param sTable the name of table containing the column
85      * @param sColumn the name of the column
86      * @param nDataType the data type
87      * @throws DataStoreException if any of the parameters are invalid
88      */

89     public ColumnRef(String JavaDoc sTable, String JavaDoc sColumn, int nDataType)
90               throws DataStoreException {
91         if ((sTable == null) || (sColumn == null)) {
92             throw new DataStoreException("Invalid Type: table:" + sTable +
93                                          ",column:" + sColumn);
94         }
95
96         if ((nDataType < DATE) || (nDataType > LONG_TEXT)) {
97             throw new DataStoreException("Invalid data type");
98         }
99
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getFullRef() {
184         if( this.m_sFullRefCache==null ) {
185             String JavaDoc 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     
205     /* (non-Javadoc)
206      * @see java.lang.Object#equals(java.lang.Object)
207      */

208     public boolean equals(Object JavaDoc obj) {
209         boolean bEq = false;
210         
211         if (this == obj) {
212             bEq = true;
213         } else if(obj instanceof ColumnRef) {
214             ColumnRef colref = (ColumnRef) obj;
215             
216             if(colref.getColumn().equals(getColumn()) == true
217              && colref.getTable().equals(getTable())) {
218                 bEq = true;
219             }
220         }
221         
222         return bEq;
223     }
224     
225     /* (non-Javadoc)
226      * @see java.lang.Object#hashCode()
227      */

228     public int hashCode() {
229
230         if (m_nHashcode == 0) {
231             int nHash = 17;
232
233             nHash = 37 * nHash + m_nDataType;
234             if (m_sColumn != null) {
235                 nHash = 37 * nHash + m_sColumn.hashCode();
236             }
237             if (m_sTable != null) {
238                 nHash = 37 * nHash + m_sTable.hashCode();
239             }
240             if (m_sTableAlias != null) {
241                 nHash = 37 * nHash + m_sTableAlias.hashCode();
242             }
243             
244             m_nHashcode = nHash;
245         }
246
247         return m_nHashcode;
248     }
249
250 }
Popular Tags