Created
December 18, 2013 21:51
-
-
Save gbadner/8030453 to your computer and use it in GitHub Desktop.
org.hibernate.metamodel.spi.relational.Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Table extends AbstractTableSpecification implements Exportable { | |
private final Schema database; | |
private Identifier physicalName; | |
private Identifier logicalName; | |
private ObjectName objectName; | |
private String exportIdentifier; | |
private final Set<Index> indexes = new LinkedHashSet<Index>(); | |
private final Set<UniqueKey> uniqueKeys = new LinkedHashSet<UniqueKey>(); | |
private final List<CheckConstraint> checkConstraints = new ArrayList<CheckConstraint>(); | |
private final List<String> comments = new ArrayList<String>(); | |
/** | |
* Constructs a {@link Table} instance. | |
* | |
* @param database - the schema | |
* @param logicalName - The logical name | |
* @param physicalName - the physical table name. | |
*/ | |
public Table(Schema database, Identifier logicalName, Identifier physicalName) { | |
this.database = database; | |
this.logicalName = logicalName; | |
this.physicalName = physicalName; | |
this.objectName = new ObjectName( database, physicalName ); | |
this.exportIdentifier = objectName.toText(); | |
} | |
@Override | |
public Schema getSchema() { | |
return database; | |
} | |
/** | |
* Gets the logical table name. | |
* | |
* @return the logical table name. | |
*/ | |
@Override | |
public Identifier getLogicalName() { | |
return logicalName; | |
} | |
/** | |
* Gets the table name. | |
* @return the table name. | |
*/ | |
public Identifier getTableName() { | |
return physicalName; | |
} | |
@Override | |
public String getLoggableValueQualifier() { | |
return exportIdentifier; | |
} | |
@Override | |
public String getExportIdentifier() { | |
return exportIdentifier; | |
} | |
@Override | |
public String toLoggableString() { | |
return exportIdentifier; | |
} | |
@Override | |
public Iterable<Index> getIndexes() { | |
return Collections.unmodifiableSet( indexes ); | |
} | |
@Override | |
public Index getOrCreateIndex(String name) { | |
Index result = null; | |
if ( name != null ) { | |
result = locateConstraint( indexes, name ); | |
} | |
if ( result == null ) { | |
result = new Index( this, name ); | |
indexes.add( result ); | |
} | |
return result; | |
} | |
@Override | |
public Set<UniqueKey> getUniqueKeys() { | |
return Collections.unmodifiableSet( uniqueKeys ); | |
} | |
@Override | |
public UniqueKey getOrCreateUniqueKey(String name) { | |
UniqueKey result = null; | |
if ( name != null ) { | |
result = locateConstraint( uniqueKeys, name ); | |
} | |
if ( result == null ) { | |
result = new UniqueKey( this, name ); | |
uniqueKeys.add( result ); | |
} | |
return result; | |
} | |
@Override | |
public Iterable<CheckConstraint> getCheckConstraints() { | |
return checkConstraints; | |
} | |
@Override | |
public void addCheckConstraint(String checkCondition) { | |
//todo ? StringHelper.isEmpty( checkCondition ); | |
//todo default name? | |
checkConstraints.add( new CheckConstraint( this, "", checkCondition ) ); | |
} | |
@Override | |
public Iterable<String> getComments() { | |
return comments; | |
} | |
@Override | |
public void addComment(String comment) { | |
comments.add( comment ); | |
} | |
@Override | |
public String getQualifiedName(Dialect dialect) { | |
return objectName.toText( dialect ); | |
} | |
public String[] sqlCreateStrings(Dialect dialect) { | |
boolean hasPrimaryKey = getPrimaryKey().getColumns().iterator().hasNext(); | |
StringBuilder buf = | |
new StringBuilder( | |
hasPrimaryKey ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() ) | |
.append( ' ' ) | |
.append( objectName.toText( dialect ) ) | |
.append( " (" ); | |
boolean isPrimaryKeyIdentity = | |
hasPrimaryKey && | |
getPrimaryKey().getColumnSpan() == 1 && | |
getPrimaryKey().getColumns().get( 0 ).isIdentity(); | |
// Try to find out the name of the primary key in case the dialect needs it to create an identity | |
String pkColName = null; | |
if ( hasPrimaryKey && isPrimaryKeyIdentity ) { | |
Column pkColumn = getPrimaryKey().getColumns().iterator().next(); | |
pkColName = pkColumn.getColumnName().encloseInQuotesIfQuoted( dialect ); | |
} | |
boolean isFirst = true; | |
for ( Value simpleValue : values() ) { | |
if ( ! Column.class.isInstance( simpleValue ) ) { | |
continue; | |
} | |
if ( isFirst ) { | |
isFirst = false; | |
} | |
else { | |
buf.append( ", " ); | |
} | |
Column col = ( Column ) simpleValue; | |
String colName = col.getColumnName().encloseInQuotesIfQuoted( dialect ); | |
buf.append( colName ).append( ' ' ); | |
if ( isPrimaryKeyIdentity && colName.equals( pkColName ) ) { | |
// to support dialects that have their own identity data type | |
if ( dialect.hasDataTypeInIdentityColumn() ) { | |
buf.append( getTypeString( col, dialect ) ); | |
} | |
buf.append( ' ' ) | |
.append( dialect.getIdentityColumnString( col.getJdbcDataType().getTypeCode() ) ); | |
} | |
else { | |
buf.append( getTypeString( col, dialect ) ); | |
String defaultValue = col.getDefaultValue(); | |
if ( defaultValue != null ) { | |
buf.append( " default " ).append( defaultValue ); | |
} | |
if ( col.isNullable() ) { | |
buf.append( dialect.getNullColumnString() ); | |
} | |
else { | |
buf.append( " not null" ); | |
} | |
} | |
if ( col.isUnique() ) { | |
UniqueKey uk = getOrCreateUniqueKey( col.getColumnName() | |
.encloseInQuotesIfQuoted( dialect ) + '_' ); | |
uk.addColumn( col ); | |
buf.append( dialect.getUniqueDelegate() | |
.getColumnDefinitionUniquenessFragment( col ) ); | |
} | |
if ( col.getCheckCondition() != null && dialect.supportsColumnCheck() ) { | |
buf.append( " check (" ) | |
.append( col.getCheckCondition() ) | |
.append( ")" ); | |
} | |
String columnComment = col.getComment(); | |
if ( columnComment != null ) { | |
buf.append( dialect.getColumnComment( columnComment ) ); | |
} | |
} | |
if ( hasPrimaryKey ) { | |
buf.append( ", " ) | |
.append( getPrimaryKey().sqlConstraintStringInCreateTable( dialect ) ); | |
} | |
<<<<<<< HEAD | |
buf.append( dialect.getUniqueDelegate().getTableCreationUniqueConstraintsFragment( this ) ); | |
======= | |
if ( dialect.supportsUniqueConstraintInCreateAlterTable() ) { | |
for ( UniqueKey uk : uniqueKeys ) { | |
String constraint = uk.sqlConstraintStringInCreateTable( dialect ); | |
if ( constraint != null ) { | |
buf.append( ", " ).append( constraint ); | |
} | |
} | |
} | |
>>>>>>> HHH-7092 : Create default name for foreign and unique key constraints | |
if ( dialect.supportsTableCheck() ) { | |
for ( CheckConstraint checkConstraint : checkConstraints ) { | |
buf.append( ", check (" ) | |
.append( checkConstraint ) | |
.append( ')' ); | |
} | |
} | |
buf.append( ')' ); | |
buf.append( dialect.getTableTypeString() ); | |
String[] sqlStrings = new String[ comments.size() + 1 ]; | |
sqlStrings[ 0 ] = buf.toString(); | |
for ( int i = 0 ; i < comments.size(); i++ ) { | |
sqlStrings[ i + 1 ] = dialect.getTableComment( comments.get( i ) ); | |
} | |
return sqlStrings; | |
} | |
private static String getTypeString(Column col, Dialect dialect) { | |
String typeString; | |
if ( col.getSqlType() != null ) { | |
typeString = col.getSqlType(); | |
} | |
else { | |
Size size = col.getSize() == null ? | |
new Size( ) : | |
col.getSize(); | |
typeString = dialect.getTypeName( | |
col.getJdbcDataType().getTypeCode(), | |
size.getLength(), | |
size.getPrecision(), | |
size.getScale() | |
); | |
} | |
return typeString; | |
} | |
@Override | |
public String[] sqlDropStrings(Dialect dialect) { | |
return new String[] { dialect.getDropTableString( getQualifiedName( dialect ) ) }; | |
} | |
@Override | |
public String toString() { | |
return "Table{name=" + exportIdentifier + '}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment