KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > Column


1 //$Id: Column.java,v 1.18 2005/07/19 23:26:18 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.HibernateException;
7 import org.hibernate.MappingException;
8 import org.hibernate.dialect.Dialect;
9 import org.hibernate.engine.Mapping;
10 import org.hibernate.util.StringHelper;
11
12 /**
13  * A column of a relational database table
14  * @author Gavin King
15  */

16 public class Column implements Selectable, Serializable JavaDoc {
17
18     public static final int DEFAULT_LENGTH = 255;
19     public static final int DEFAULT_PRECISION = 19;
20     public static final int DEFAULT_SCALE = 2;
21     
22     private int length=DEFAULT_LENGTH;
23     private int precision=DEFAULT_PRECISION;
24     private int scale=DEFAULT_SCALE;
25     private Value value;
26     private int typeIndex = 0;
27     private String JavaDoc name;
28     private boolean nullable=true;
29     private boolean unique=false;
30     private String JavaDoc sqlType;
31     private Integer JavaDoc sqlTypeCode;
32     private boolean quoted=false;
33     int uniqueInteger;
34     private String JavaDoc checkConstraint;
35     private String JavaDoc comment;
36
37     public Column() { };
38     
39     public Column(String JavaDoc columnName) {
40         setName(columnName);
41     }
42     
43     public int getLength() {
44         return length;
45     }
46     public void setLength(int length) {
47         this.length = length;
48     }
49     public Value getValue() {
50         return value;
51     }
52     public void setValue(Value value) {
53         this.value= value;
54     }
55     public String JavaDoc getName() {
56         return name;
57     }
58     public void setName(String JavaDoc name) {
59         if (
60             name.charAt(0)=='`' |
61             Dialect.QUOTE.indexOf( name.charAt(0) ) > -1 //TODO: deprecated, remove eventually
62
) {
63             quoted=true;
64             this.name=name.substring( 1, name.length()-1 );
65         }
66         else {
67             this.name = name;
68         }
69     }
70     public String JavaDoc getQuotedName(Dialect d) {
71         return quoted ?
72             d.openQuote() + name + d.closeQuote() :
73             name;
74     }
75     
76     /**
77      * For any column name, generate an alias that is unique
78      * to that column name, and also 10 characters or less
79      * in length.
80      */

81     public String JavaDoc getAlias(Dialect dialect) {
82         String JavaDoc alias = name;
83         String JavaDoc unique = Integer.toString(uniqueInteger) + '_';
84         int lastLetter = StringHelper.lastIndexOfLetter(name);
85         if ( lastLetter == -1 ) {
86             alias = "column";
87         }
88         else if ( lastLetter < name.length()-1 ) {
89             alias = name.substring(0, lastLetter+1);
90         }
91         if ( alias.length() > dialect.getMaxAliasLength() ) {
92             alias = alias.substring( 0, dialect.getMaxAliasLength() - unique.length() );
93         }
94         boolean useRawName = name.equals(alias) &&
95             !quoted &&
96             !name.toLowerCase().equals("rowid");
97         if ( useRawName ) {
98             return alias;
99         }
100         else {
101             return alias + unique;
102         }
103     }
104     
105     /**
106      * Generate a column alias that is unique across multiple tables
107      */

108     public String JavaDoc getAlias(Dialect dialect, Table table) {
109         return getAlias(dialect) + table.getUniqueInteger() + '_';
110     }
111
112     public boolean isNullable() {
113         return nullable;
114     }
115
116     public void setNullable(boolean nullable) {
117         this.nullable=nullable;
118     }
119
120     public int getTypeIndex() {
121         return typeIndex;
122     }
123     public void setTypeIndex(int typeIndex) {
124         this.typeIndex = typeIndex;
125     }
126
127     public int getSqlTypeCode(Mapping mapping) throws MappingException {
128         org.hibernate.type.Type type = getValue().getType();
129         try {
130             int sqlTypeCode = type.sqlTypes(mapping)[ getTypeIndex() ];
131             if(getSqlTypeCode()!=null && getSqlTypeCode().intValue()!=sqlTypeCode) {
132                 throw new MappingException("SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() );
133             }
134             return sqlTypeCode;
135         }
136         catch (Exception JavaDoc e) {
137             throw new MappingException(
138                     "Could not determine type for column " +
139                     name +
140                     " of type " +
141                     type.getClass().getName() +
142                     ": " +
143                     e.getClass().getName(),
144                     e
145                 );
146         }
147     }
148
149     /**
150      * Returns the underlying columns sqltypecode.
151      * If null, it is because the sqltype code is unknown.
152      *
153      * Use #getSqlTypeCode(Mapping) to retreive the sqltypecode used
154      * for the columns associated Value/Type.
155      *
156      * @return sqltypecode if it is set, otherwise null.
157      */

158     public Integer JavaDoc getSqlTypeCode() {
159         return sqlTypeCode;
160     }
161     
162     public void setSqlTypeCode(Integer JavaDoc typecode) {
163         sqlTypeCode=typecode;
164     }
165     
166     public boolean isUnique() {
167         return unique;
168     }
169
170
171     public String JavaDoc getSqlType(Dialect dialect, Mapping mapping) throws HibernateException {
172         return sqlType==null ?
173             dialect.getTypeName( getSqlTypeCode(mapping), getLength(), getPrecision(), getScale() ) :
174             sqlType;
175     }
176
177     public boolean equals(Object JavaDoc object) {
178         return object instanceof Column && equals( (Column) object );
179     }
180
181     public boolean equals(Column column) {
182         if (null == column) return false;
183         if (this == column) return true;
184
185         return isQuoted() ?
186             name.equals(column.name) :
187             name.equalsIgnoreCase(column.name);
188     }
189
190     //used also for generation of FK names!
191
public int hashCode() {
192         return isQuoted() ?
193             name.hashCode() :
194             name.toLowerCase().hashCode();
195     }
196
197     public String JavaDoc getSqlType() {
198         return sqlType;
199     }
200
201     public void setSqlType(String JavaDoc sqlType) {
202         this.sqlType = sqlType;
203     }
204
205     public void setUnique(boolean unique) {
206         this.unique = unique;
207     }
208
209     public boolean isQuoted() {
210         return quoted;
211     }
212
213     public String JavaDoc toString() {
214         return getClass().getName() + '(' + getName() + ')';
215     }
216
217     public String JavaDoc getCheckConstraint() {
218         return checkConstraint;
219     }
220
221     public void setCheckConstraint(String JavaDoc checkConstraint) {
222         this.checkConstraint = checkConstraint;
223     }
224
225     public boolean hasCheckConstraint() {
226         return checkConstraint!=null;
227     }
228
229     public String JavaDoc getTemplate(Dialect dialect) {
230         return getQuotedName(dialect);
231     }
232
233     public boolean isFormula() {
234         return false;
235     }
236
237     public String JavaDoc getText(Dialect d) {
238         return getQuotedName(d);
239     }
240     public String JavaDoc getText() {
241         return getName();
242     }
243     
244     public int getPrecision() {
245         return precision;
246     }
247     public void setPrecision(int scale) {
248         this.precision = scale;
249     }
250
251     public int getScale() {
252         return scale;
253     }
254     public void setScale(int scale) {
255         this.scale = scale;
256     }
257
258     public String JavaDoc getComment() {
259         return comment;
260     }
261
262     public void setComment(String JavaDoc comment) {
263         this.comment = comment;
264     }
265     
266 }
267
Popular Tags