/
Table.java
864 lines (733 loc) · 23.7 KB
/
Table.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.mapping;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.tool.hbm2ddl.ColumnMetadata;
import org.hibernate.tool.hbm2ddl.TableMetadata;
/**
* A relational table
*
* @author Gavin King
*/
public class Table implements RelationalModel, Serializable {
private String name;
private String schema;
private String catalog;
/**
* contains all columns, including the primary key
*/
private Map columns = new LinkedHashMap();
private KeyValue idValue;
private PrimaryKey primaryKey;
private Map indexes = new HashMap();
private Map foreignKeys = new HashMap();
private Map<String,UniqueKey> uniqueKeys = new HashMap<String,UniqueKey>();
private final int uniqueInteger;
private boolean quoted;
private boolean schemaQuoted;
private boolean catalogQuoted;
private static int tableCounter = 0;
private List checkConstraints = new ArrayList();
private String rowId;
private String subselect;
private boolean isAbstract;
private boolean hasDenormalizedTables = false;
private String comment;
static class ForeignKeyKey implements Serializable {
String referencedClassName;
List columns;
List referencedColumns;
ForeignKeyKey(List columns, String referencedClassName, List referencedColumns) {
this.referencedClassName = referencedClassName;
this.columns = new ArrayList();
this.columns.addAll( columns );
if ( referencedColumns != null ) {
this.referencedColumns = new ArrayList();
this.referencedColumns.addAll( referencedColumns );
}
else {
this.referencedColumns = Collections.EMPTY_LIST;
}
}
public int hashCode() {
return columns.hashCode() + referencedColumns.hashCode();
}
public boolean equals(Object other) {
ForeignKeyKey fkk = (ForeignKeyKey) other;
return fkk.columns.equals( columns ) &&
fkk.referencedClassName.equals( referencedClassName ) && fkk.referencedColumns
.equals( referencedColumns );
}
}
public Table() {
uniqueInteger = tableCounter++;
}
public Table(String name) {
this();
setName( name );
}
public String getQualifiedName(Dialect dialect, String defaultCatalog, String defaultSchema) {
if ( subselect != null ) {
return "( " + subselect + " )";
}
String quotedName = getQuotedName( dialect );
String usedSchema = schema == null ?
defaultSchema :
getQuotedSchema( dialect );
String usedCatalog = catalog == null ?
defaultCatalog :
getQuotedCatalog( dialect );
return qualify( usedCatalog, usedSchema, quotedName );
}
public static String qualify(String catalog, String schema, String table) {
StringBuilder qualifiedName = new StringBuilder();
if ( catalog != null ) {
qualifiedName.append( catalog ).append( '.' );
}
if ( schema != null ) {
qualifiedName.append( schema ).append( '.' );
}
return qualifiedName.append( table ).toString();
}
public String getName() {
return name;
}
/**
* returns quoted name as it would be in the mapping file.
*/
public String getQuotedName() {
return quoted ?
"`" + name + "`" :
name;
}
public String getQuotedName(Dialect dialect) {
return quoted ?
dialect.openQuote() + name + dialect.closeQuote() :
name;
}
/**
* returns quoted name as it is in the mapping file.
*/
public String getQuotedSchema() {
return schemaQuoted ?
"`" + schema + "`" :
schema;
}
public String getQuotedSchema(Dialect dialect) {
return schemaQuoted ?
dialect.openQuote() + schema + dialect.closeQuote() :
schema;
}
public String getQuotedCatalog() {
return catalogQuoted ?
"`" + catalog + "`" :
catalog;
}
public String getQuotedCatalog(Dialect dialect) {
return catalogQuoted ?
dialect.openQuote() + catalog + dialect.closeQuote() :
catalog;
}
public void setName(String name) {
if ( name.charAt( 0 ) == '`' ) {
quoted = true;
this.name = name.substring( 1, name.length() - 1 );
}
else {
this.name = name;
}
}
/**
* Return the column which is identified by column provided as argument.
*
* @param column column with atleast a name.
* @return the underlying column or null if not inside this table. Note: the instance *can* be different than the input parameter, but the name will be the same.
*/
public Column getColumn(Column column) {
if ( column == null ) {
return null;
}
Column myColumn = (Column) columns.get( column.getCanonicalName() );
return column.equals( myColumn ) ?
myColumn :
null;
}
public Column getColumn(int n) {
Iterator iter = columns.values().iterator();
for ( int i = 0; i < n - 1; i++ ) {
iter.next();
}
return (Column) iter.next();
}
public void addColumn(Column column) {
Column old = (Column) getColumn( column );
if ( old == null ) {
columns.put( column.getCanonicalName(), column );
column.uniqueInteger = columns.size();
}
else {
column.uniqueInteger = old.uniqueInteger;
}
}
public int getColumnSpan() {
return columns.size();
}
public Iterator getColumnIterator() {
return columns.values().iterator();
}
public Iterator getIndexIterator() {
return indexes.values().iterator();
}
public Iterator getForeignKeyIterator() {
return foreignKeys.values().iterator();
}
public Iterator getUniqueKeyIterator() {
return getUniqueKeys().values().iterator();
}
Map getUniqueKeys() {
cleanseUniqueKeyMapIfNeeded();
return uniqueKeys;
}
private int sizeOfUniqueKeyMapOnLastCleanse = 0;
private void cleanseUniqueKeyMapIfNeeded() {
if ( uniqueKeys.size() == sizeOfUniqueKeyMapOnLastCleanse ) {
// nothing to do
return;
}
cleanseUniqueKeyMap();
sizeOfUniqueKeyMapOnLastCleanse = uniqueKeys.size();
}
private void cleanseUniqueKeyMap() {
// We need to account for a few conditions here...
// 1) If there are multiple unique keys contained in the uniqueKeys Map, we need to deduplicate
// any sharing the same columns as other defined unique keys; this is needed for the annotation
// processor since it creates unique constraints automagically for the user
// 2) Remove any unique keys that share the same columns as the primary key; again, this is
// needed for the annotation processor to handle @Id @OneToOne cases. In such cases the
// unique key is unnecessary because a primary key is already unique by definition. We handle
// this case specifically because some databases fail if you try to apply a unique key to
// the primary key columns which causes schema export to fail in these cases.
if ( uniqueKeys.isEmpty() ) {
// nothing to do
return;
}
else if ( uniqueKeys.size() == 1 ) {
// we have to worry about condition 2 above, but not condition 1
final Map.Entry<String,UniqueKey> uniqueKeyEntry = uniqueKeys.entrySet().iterator().next();
if ( isSameAsPrimaryKeyColumns( uniqueKeyEntry.getValue() ) ) {
uniqueKeys.remove( uniqueKeyEntry.getKey() );
}
}
else {
// we have to check both conditions 1 and 2
final Iterator<Map.Entry<String,UniqueKey>> uniqueKeyEntries = uniqueKeys.entrySet().iterator();
while ( uniqueKeyEntries.hasNext() ) {
final Map.Entry<String,UniqueKey> uniqueKeyEntry = uniqueKeyEntries.next();
final UniqueKey uniqueKey = uniqueKeyEntry.getValue();
boolean removeIt = false;
// condition 1 : check against other unique keys
for ( UniqueKey otherUniqueKey : uniqueKeys.values() ) {
// make sure its not the same unique key
if ( uniqueKeyEntry.getValue() == otherUniqueKey ) {
continue;
}
if ( otherUniqueKey.getColumns().containsAll( uniqueKey.getColumns() )
&& uniqueKey.getColumns().containsAll( otherUniqueKey.getColumns() ) ) {
removeIt = true;
break;
}
}
// condition 2 : check against pk
if ( isSameAsPrimaryKeyColumns( uniqueKeyEntry.getValue() ) ) {
removeIt = true;
}
if ( removeIt ) {
//uniqueKeys.remove( uniqueKeyEntry.getKey() );
uniqueKeyEntries.remove();
}
}
}
}
private boolean isSameAsPrimaryKeyColumns(UniqueKey uniqueKey) {
if ( primaryKey == null || ! primaryKey.columnIterator().hasNext() ) {
// happens for many-to-many tables
return false;
}
return primaryKey.getColumns().containsAll( uniqueKey.getColumns() )
&& uniqueKey.getColumns().containsAll( primaryKey.getColumns() );
}
public void validateColumns(Dialect dialect, Mapping mapping, TableMetadata tableInfo) {
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column col = (Column) iter.next();
ColumnMetadata columnInfo = tableInfo.getColumnMetadata( col.getName() );
if ( columnInfo == null ) {
throw new HibernateException( "Missing column: " + col.getName() + " in " + Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()));
}
else {
final boolean typesMatch = col.getSqlType( dialect, mapping ).toLowerCase()
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
if ( !typesMatch ) {
throw new HibernateException(
"Wrong column type in " +
Table.qualify( tableInfo.getCatalog(), tableInfo.getSchema(), tableInfo.getName()) +
" for column " + col.getName() +
". Found: " + columnInfo.getTypeName().toLowerCase() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
public Iterator sqlAlterStrings(Dialect dialect, Mapping p, TableMetadata tableInfo, String defaultCatalog,
String defaultSchema)
throws HibernateException {
StringBuilder root = new StringBuilder( "alter table " )
.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( ' ' )
.append( dialect.getAddColumnString() );
Iterator iter = getColumnIterator();
List results = new ArrayList();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
ColumnMetadata columnInfo = tableInfo.getColumnMetadata( column.getName() );
if ( columnInfo == null ) {
// the column doesnt exist at all.
StringBuilder alter = new StringBuilder( root.toString() )
.append( ' ' )
.append( column.getQuotedName( dialect ) )
.append( ' ' )
.append( column.getSqlType( dialect, p ) );
String defaultValue = column.getDefaultValue();
if ( defaultValue != null ) {
alter.append( " default " ).append( defaultValue );
}
if ( column.isNullable() ) {
alter.append( dialect.getNullColumnString() );
}
else {
alter.append( " not null" );
}
boolean useUniqueConstraint = column.isUnique() &&
dialect.supportsUnique() &&
( column.isNullable() || dialect.supportsNotNullUnique() );
if ( useUniqueConstraint ) {
alter.append( " unique" );
}
if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
alter.append( " check(" )
.append( column.getCheckConstraint() )
.append( ")" );
}
String columnComment = column.getComment();
if ( columnComment != null ) {
alter.append( dialect.getColumnComment( columnComment ) );
}
results.add( alter.toString() );
}
}
return results.iterator();
}
public boolean hasPrimaryKey() {
return getPrimaryKey() != null;
}
public String sqlTemporaryTableCreateString(Dialect dialect, Mapping mapping) throws HibernateException {
StringBuilder buffer = new StringBuilder( dialect.getCreateTemporaryTableString() )
.append( ' ' )
.append( name )
.append( " (" );
Iterator itr = getColumnIterator();
while ( itr.hasNext() ) {
final Column column = (Column) itr.next();
buffer.append( column.getQuotedName( dialect ) ).append( ' ' );
buffer.append( column.getSqlType( dialect, mapping ) );
if ( column.isNullable() ) {
buffer.append( dialect.getNullColumnString() );
}
else {
buffer.append( " not null" );
}
if ( itr.hasNext() ) {
buffer.append( ", " );
}
}
buffer.append( ") " );
buffer.append( dialect.getCreateTemporaryTablePostfix() );
return buffer.toString();
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) {
StringBuilder buf = new StringBuilder( hasPrimaryKey() ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() )
.append( ' ' )
.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( " (" );
boolean identityColumn = idValue != null && idValue.isIdentityColumn( p.getIdentifierGeneratorFactory(), dialect );
// Try to find out the name of the primary key to create it as identity if the IdentityGenerator is used
String pkname = null;
if ( hasPrimaryKey() && identityColumn ) {
pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect );
}
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column col = (Column) iter.next();
buf.append( col.getQuotedName( dialect ) )
.append( ' ' );
if ( identityColumn && col.getQuotedName( dialect ).equals( pkname ) ) {
// to support dialects that have their own identity data type
if ( dialect.hasDataTypeInIdentityColumn() ) {
buf.append( col.getSqlType( dialect, p ) );
}
buf.append( ' ' )
.append( dialect.getIdentityColumnString( col.getSqlTypeCode( p ) ) );
}
else {
buf.append( col.getSqlType( dialect, p ) );
String defaultValue = col.getDefaultValue();
if ( defaultValue != null ) {
buf.append( " default " ).append( defaultValue );
}
if ( col.isNullable() ) {
buf.append( dialect.getNullColumnString() );
}
else {
buf.append( " not null" );
}
}
boolean useUniqueConstraint = col.isUnique() &&
( col.isNullable() || dialect.supportsNotNullUnique() );
if ( useUniqueConstraint ) {
if ( dialect.supportsUnique() ) {
buf.append( " unique" );
}
else {
UniqueKey uk = getOrCreateUniqueKey( col.getQuotedName( dialect ) + '_' );
uk.addColumn( col );
}
}
if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) {
buf.append( " check (" )
.append( col.getCheckConstraint() )
.append( ")" );
}
String columnComment = col.getComment();
if ( columnComment != null ) {
buf.append( dialect.getColumnComment( columnComment ) );
}
if ( iter.hasNext() ) {
buf.append( ", " );
}
}
if ( hasPrimaryKey() ) {
buf.append( ", " )
.append( getPrimaryKey().sqlConstraintString( dialect ) );
}
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) {
Iterator ukiter = getUniqueKeyIterator();
while ( ukiter.hasNext() ) {
UniqueKey uk = (UniqueKey) ukiter.next();
String constraint = uk.sqlConstraintString( dialect );
if ( constraint != null ) {
buf.append( ", " ).append( constraint );
}
}
}
/*Iterator idxiter = getIndexIterator();
while ( idxiter.hasNext() ) {
Index idx = (Index) idxiter.next();
buf.append(',').append( idx.sqlConstraintString(dialect) );
}*/
if ( dialect.supportsTableCheck() ) {
Iterator chiter = checkConstraints.iterator();
while ( chiter.hasNext() ) {
buf.append( ", check (" )
.append( chiter.next() )
.append( ')' );
}
}
buf.append( ')' );
if ( comment != null ) {
buf.append( dialect.getTableComment( comment ) );
}
return buf.append( dialect.getTableTypeString() ).toString();
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema) {
StringBuilder buf = new StringBuilder( "drop table " );
if ( dialect.supportsIfExistsBeforeTableName() ) {
buf.append( "if exists " );
}
buf.append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) )
.append( dialect.getCascadeConstraintsString() );
if ( dialect.supportsIfExistsAfterTableName() ) {
buf.append( " if exists" );
}
return buf.toString();
}
public PrimaryKey getPrimaryKey() {
return primaryKey;
}
public void setPrimaryKey(PrimaryKey primaryKey) {
this.primaryKey = primaryKey;
}
public Index getOrCreateIndex(String indexName) {
Index index = (Index) indexes.get( indexName );
if ( index == null ) {
index = new Index();
index.setName( indexName );
index.setTable( this );
indexes.put( indexName, index );
}
return index;
}
public Index getIndex(String indexName) {
return (Index) indexes.get( indexName );
}
public Index addIndex(Index index) {
Index current = (Index) indexes.get( index.getName() );
if ( current != null ) {
throw new MappingException( "Index " + index.getName() + " already exists!" );
}
indexes.put( index.getName(), index );
return index;
}
public UniqueKey addUniqueKey(UniqueKey uniqueKey) {
UniqueKey current = (UniqueKey) uniqueKeys.get( uniqueKey.getName() );
if ( current != null ) {
throw new MappingException( "UniqueKey " + uniqueKey.getName() + " already exists!" );
}
uniqueKeys.put( uniqueKey.getName(), uniqueKey );
return uniqueKey;
}
public UniqueKey createUniqueKey(List keyColumns) {
String keyName = "UK" + uniqueColumnString( keyColumns.iterator() );
UniqueKey uk = getOrCreateUniqueKey( keyName );
uk.addColumns( keyColumns.iterator() );
return uk;
}
public UniqueKey getUniqueKey(String keyName) {
return (UniqueKey) uniqueKeys.get( keyName );
}
public UniqueKey getOrCreateUniqueKey(String keyName) {
UniqueKey uk = (UniqueKey) uniqueKeys.get( keyName );
if ( uk == null ) {
uk = new UniqueKey();
uk.setName( keyName );
uk.setTable( this );
uniqueKeys.put( keyName, uk );
}
return uk;
}
public void createForeignKeys() {
}
public ForeignKey createForeignKey(String keyName, List keyColumns, String referencedEntityName) {
return createForeignKey( keyName, keyColumns, referencedEntityName, null );
}
public ForeignKey createForeignKey(String keyName, List keyColumns, String referencedEntityName,
List referencedColumns) {
Object key = new ForeignKeyKey( keyColumns, referencedEntityName, referencedColumns );
ForeignKey fk = (ForeignKey) foreignKeys.get( key );
if ( fk == null ) {
fk = new ForeignKey();
if ( keyName != null ) {
fk.setName( keyName );
}
else {
fk.setName( "FK" + uniqueColumnString( keyColumns.iterator(), referencedEntityName ) );
//TODO: add referencedClass to disambiguate to FKs on the same
// columns, pointing to different tables
}
fk.setTable( this );
foreignKeys.put( key, fk );
fk.setReferencedEntityName( referencedEntityName );
fk.addColumns( keyColumns.iterator() );
if ( referencedColumns != null ) {
fk.addReferencedColumns( referencedColumns.iterator() );
}
}
if ( keyName != null ) {
fk.setName( keyName );
}
return fk;
}
public String uniqueColumnString(Iterator iterator) {
return uniqueColumnString( iterator, null );
}
public String uniqueColumnString(Iterator iterator, String referencedEntityName) {
int result = 0;
if ( referencedEntityName != null ) {
result += referencedEntityName.hashCode();
}
while ( iterator.hasNext() ) {
result += iterator.next().hashCode();
}
return ( Integer.toHexString( name.hashCode() ) + Integer.toHexString( result ) ).toUpperCase();
}
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
if ( schema != null && schema.charAt( 0 ) == '`' ) {
schemaQuoted = true;
this.schema = schema.substring( 1, schema.length() - 1 );
}
else {
this.schema = schema;
}
}
public String getCatalog() {
return catalog;
}
public void setCatalog(String catalog) {
if ( catalog != null && catalog.charAt( 0 ) == '`' ) {
catalogQuoted = true;
this.catalog = catalog.substring( 1, catalog.length() - 1 );
}
else {
this.catalog = catalog;
}
}
public int getUniqueInteger() {
return uniqueInteger;
}
public void setIdentifierValue(KeyValue idValue) {
this.idValue = idValue;
}
public KeyValue getIdentifierValue() {
return idValue;
}
public boolean isSchemaQuoted() {
return schemaQuoted;
}
public boolean isCatalogQuoted() {
return catalogQuoted;
}
public boolean isQuoted() {
return quoted;
}
public void setQuoted(boolean quoted) {
this.quoted = quoted;
}
public void addCheckConstraint(String constraint) {
checkConstraints.add( constraint );
}
public boolean containsColumn(Column column) {
return columns.containsValue( column );
}
public String getRowId() {
return rowId;
}
public void setRowId(String rowId) {
this.rowId = rowId;
}
public String toString() {
StringBuilder buf = new StringBuilder().append( getClass().getName() )
.append( '(' );
if ( getCatalog() != null ) {
buf.append( getCatalog() + "." );
}
if ( getSchema() != null ) {
buf.append( getSchema() + "." );
}
buf.append( getName() ).append( ')' );
return buf.toString();
}
public String getSubselect() {
return subselect;
}
public void setSubselect(String subselect) {
this.subselect = subselect;
}
public boolean isSubselect() {
return subselect != null;
}
public boolean isAbstractUnionTable() {
return hasDenormalizedTables() && isAbstract;
}
public boolean hasDenormalizedTables() {
return hasDenormalizedTables;
}
void setHasDenormalizedTables() {
hasDenormalizedTables = true;
}
public void setAbstract(boolean isAbstract) {
this.isAbstract = isAbstract;
}
public boolean isAbstract() {
return isAbstract;
}
public boolean isPhysicalTable() {
return !isSubselect() && !isAbstractUnionTable();
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Iterator getCheckConstraintsIterator() {
return checkConstraints.iterator();
}
public Iterator sqlCommentStrings(Dialect dialect, String defaultCatalog, String defaultSchema) {
List comments = new ArrayList();
if ( dialect.supportsCommentOn() ) {
String tableName = getQualifiedName( dialect, defaultCatalog, defaultSchema );
if ( comment != null ) {
StringBuilder buf = new StringBuilder()
.append( "comment on table " )
.append( tableName )
.append( " is '" )
.append( comment )
.append( "'" );
comments.add( buf.toString() );
}
Iterator iter = getColumnIterator();
while ( iter.hasNext() ) {
Column column = (Column) iter.next();
String columnComment = column.getComment();
if ( columnComment != null ) {
StringBuilder buf = new StringBuilder()
.append( "comment on column " )
.append( tableName )
.append( '.' )
.append( column.getQuotedName( dialect ) )
.append( " is '" )
.append( columnComment )
.append( "'" );
comments.add( buf.toString() );
}
}
}
return comments.iterator();
}
}