-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Column.java
381 lines (319 loc) · 8.59 KB
/
Column.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.mapping;
import java.io.Serializable;
import java.util.Locale;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.SQLFunctionRegistry;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.sql.Template;
/**
* A column of a relational database table
*
* @author Gavin King
*/
public class Column implements Selectable, Serializable, Cloneable {
public static final int DEFAULT_LENGTH = 255;
public static final int DEFAULT_PRECISION = 19;
public static final int DEFAULT_SCALE = 2;
private int length = DEFAULT_LENGTH;
private int precision = DEFAULT_PRECISION;
private int scale = DEFAULT_SCALE;
private Value value;
private int typeIndex;
private String name;
private boolean nullable = true;
private boolean unique;
private String sqlType;
private Integer sqlTypeCode;
private boolean quoted;
int uniqueInteger;
private String checkConstraint;
private String comment;
private String defaultValue;
private String customWrite;
private String customRead;
public Column() {
}
public Column(String columnName) {
setName( columnName );
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public Value getValue() {
return value;
}
public void setValue(Value value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
if (
StringHelper.isNotEmpty( name ) &&
Dialect.QUOTE.indexOf( name.charAt( 0 ) ) > -1 //TODO: deprecated, remove eventually
) {
quoted = true;
this.name = name.substring( 1, name.length() - 1 );
}
else {
this.name = name;
}
}
/**
* returns quoted name as it would be in the mapping file.
*/
public String getQuotedName() {
return quoted ?
"`" + name + "`" :
name;
}
public String getQuotedName(Dialect d) {
return quoted ?
d.openQuote() + name + d.closeQuote() :
name;
}
@Override
public String getAlias(Dialect dialect) {
final int lastLetter = StringHelper.lastIndexOfLetter( name );
final String suffix = Integer.toString( uniqueInteger ) + '_';
String alias = name;
if ( lastLetter == -1 ) {
alias = "column";
}
else if ( name.length() > lastLetter + 1 ) {
alias = name.substring( 0, lastLetter + 1 );
}
boolean useRawName = name.length() + suffix.length() <= dialect.getMaxAliasLength()
&& !quoted && !name.toLowerCase( Locale.ROOT ).equals( "rowid" );
if ( !useRawName ) {
if ( suffix.length() >= dialect.getMaxAliasLength() ) {
throw new MappingException(
String.format(
"Unique suffix [%s] length must be less than maximum [%d]",
suffix, dialect.getMaxAliasLength()
)
);
}
if ( alias.length() + suffix.length() > dialect.getMaxAliasLength() ) {
alias = alias.substring( 0, dialect.getMaxAliasLength() - suffix.length() );
}
}
return alias + suffix;
}
/**
* Generate a column alias that is unique across multiple tables
*/
@Override
public String getAlias(Dialect dialect, Table table) {
return getAlias( dialect ) + table.getUniqueInteger() + '_';
}
public boolean isNullable() {
return nullable;
}
public void setNullable(boolean nullable) {
this.nullable = nullable;
}
public int getTypeIndex() {
return typeIndex;
}
public void setTypeIndex(int typeIndex) {
this.typeIndex = typeIndex;
}
public boolean isUnique() {
return unique;
}
@Override
public int hashCode() {
//used also for generation of FK names!
return isQuoted() ?
name.hashCode() :
name.toLowerCase( Locale.ROOT ).hashCode();
}
@Override
public boolean equals(Object object) {
return object instanceof Column && equals( (Column) object );
}
@SuppressWarnings("SimplifiableIfStatement")
public boolean equals(Column column) {
if ( null == column ) {
return false;
}
if ( this == column ) {
return true;
}
return isQuoted() ?
name.equals( column.name ) :
name.equalsIgnoreCase( column.name );
}
public int getSqlTypeCode(Mapping mapping) throws MappingException {
org.hibernate.type.Type type = getValue().getType();
try {
int sqlTypeCode = type.sqlTypes( mapping )[getTypeIndex()];
if ( getSqlTypeCode() != null && getSqlTypeCode() != sqlTypeCode ) {
throw new MappingException( "SQLType code's does not match. mapped as " + sqlTypeCode + " but is " + getSqlTypeCode() );
}
return sqlTypeCode;
}
catch (Exception e) {
throw new MappingException(
"Could not determine type for column " +
name +
" of type " +
type.getClass().getName() +
": " +
e.getClass().getName(),
e
);
}
}
/**
* Returns the underlying columns sqltypecode.
* If null, it is because the sqltype code is unknown.
* <p/>
* Use #getSqlTypeCode(Mapping) to retrieve the sqltypecode used
* for the columns associated Value/Type.
*
* @return sqlTypeCode if it is set, otherwise null.
*/
public Integer getSqlTypeCode() {
return sqlTypeCode;
}
public void setSqlTypeCode(Integer typeCode) {
sqlTypeCode = typeCode;
}
public String getSqlType(Dialect dialect, Mapping mapping) throws HibernateException {
if ( sqlType == null ) {
sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), getLength(), getPrecision(), getScale() );
}
return sqlType;
}
public String getSqlType() {
return sqlType;
}
public void setSqlType(String sqlType) {
this.sqlType = sqlType;
}
public void setUnique(boolean unique) {
this.unique = unique;
}
public boolean isQuoted() {
return quoted;
}
@Override
public String toString() {
return getClass().getName() + '(' + getName() + ')';
}
public String getCheckConstraint() {
return checkConstraint;
}
public void setCheckConstraint(String checkConstraint) {
this.checkConstraint = checkConstraint;
}
public boolean hasCheckConstraint() {
return checkConstraint != null;
}
@Override
public String getTemplate(Dialect dialect, SQLFunctionRegistry functionRegistry) {
return hasCustomRead()
// see note in renderTransformerReadFragment wrt access to SessionFactory
? Template.renderTransformerReadFragment( customRead, getQuotedName( dialect ) )
: Template.TEMPLATE + '.' + getQuotedName( dialect );
}
public boolean hasCustomRead() {
return customRead != null;
}
public String getReadExpr(Dialect dialect) {
return hasCustomRead() ? customRead : getQuotedName( dialect );
}
public String getWriteExpr() {
return ( customWrite != null && customWrite.length() > 0 ) ? customWrite : "?";
}
@Override
public boolean isFormula() {
return false;
}
@Override
public String getText(Dialect d) {
return getQuotedName( d );
}
@Override
public String getText() {
return getName();
}
public int getPrecision() {
return precision;
}
public void setPrecision(int scale) {
this.precision = scale;
}
public int getScale() {
return scale;
}
public void setScale(int scale) {
this.scale = scale;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getCustomWrite() {
return customWrite;
}
public void setCustomWrite(String customWrite) {
this.customWrite = customWrite;
}
public String getCustomRead() {
return customRead;
}
public void setCustomRead(String customRead) {
this.customRead = StringHelper.nullIfEmpty( customRead );
}
public String getCanonicalName() {
return quoted ? name : name.toLowerCase( Locale.ROOT );
}
/**
* Shallow copy, the value is not copied
*/
@Override
public Column clone() {
Column copy = new Column();
copy.setLength( length );
copy.setScale( scale );
copy.setValue( value );
copy.setTypeIndex( typeIndex );
copy.setName( getQuotedName() );
copy.setNullable( nullable );
copy.setPrecision( precision );
copy.setUnique( unique );
copy.setSqlType( sqlType );
copy.setSqlTypeCode( sqlTypeCode );
copy.uniqueInteger = uniqueInteger; //usually useless
copy.setCheckConstraint( checkConstraint );
copy.setComment( comment );
copy.setDefaultValue( defaultValue );
copy.setCustomRead( customRead );
copy.setCustomWrite( customWrite );
return copy;
}
}