📄 table.java
字号:
//$Id: Table.java 11303 2007-03-19 22:06:14Z steve.ebersole@jboss.com $package org.hibernate.mapping;import java.io.Serializable;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.LinkedHashMap;import org.hibernate.HibernateException;import org.hibernate.MappingException;import org.hibernate.dialect.Dialect;import org.hibernate.engine.Mapping;import org.hibernate.tool.hbm2ddl.ColumnMetadata;import org.hibernate.tool.hbm2ddl.TableMetadata;import org.hibernate.util.CollectionHelper;/** * 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 uniqueKeys = new HashMap(); private final int uniqueInteger; private boolean quoted; private boolean schemaQuoted; 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 = CollectionHelper.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 : catalog; return qualify( usedCatalog, usedSchema, quotedName ); } public static String qualify(String catalog, String schema, String table) { StringBuffer qualifiedName = new StringBuffer(); 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 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() { if ( uniqueKeys.size() > 1 ) { //deduplicate unique constraints sharing the same columns //this is needed by Hibernate Annotations since it creates automagically // unique constraints for the user Iterator it = uniqueKeys.entrySet().iterator(); Map finalUniqueKeys = new HashMap( uniqueKeys.size() ); while ( it.hasNext() ) { Map.Entry entry = (Map.Entry) it.next(); UniqueKey uk = (UniqueKey) entry.getValue(); List columns = uk.getColumns(); int size = finalUniqueKeys.size(); boolean skip = false; Iterator tempUks = finalUniqueKeys.entrySet().iterator(); while ( tempUks.hasNext() ) { final UniqueKey currentUk = (UniqueKey) ( (Map.Entry) tempUks.next() ).getValue(); if ( currentUk.getColumns().containsAll( columns ) && columns .containsAll( currentUk.getColumns() ) ) { skip = true; break; } } if ( !skip ) finalUniqueKeys.put( entry.getKey(), uk ); } return finalUniqueKeys; } else { return uniqueKeys; } } 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 { StringBuffer root = new StringBuffer( "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. StringBuffer alter = new StringBuffer( 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 { StringBuffer buffer = new StringBuffer( 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) { StringBuffer buf = new StringBuffer( hasPrimaryKey() ? dialect.getCreateTableString() : dialect.getCreateMultisetTableString() ) .append( ' ' ) .append( getQualifiedName( dialect, defaultCatalog, defaultSchema ) ) .append( " (" ); boolean identityColumn = idValue != null && idValue.isIdentityColumn( 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( ' ' );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -