下面列出了org.hibernate.tool.hbm2ddl.DatabaseMetadata#org.hibernate.tool.hbm2ddl.TableMetadata 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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 )
.startsWith( columnInfo.getTypeName().toLowerCase() )
|| columnInfo.getTypeCode() == col.getSqlTypeCode( mapping );
if ( !typesMatch ) {
throw new HibernateException(
"Wrong column type: " + col.getName() +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
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(Locale.ROOT)
.startsWith( columnInfo.getTypeName().toLowerCase(Locale.ROOT) )
|| 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(Locale.ROOT) +
", expected: " + col.getSqlType( dialect, mapping )
);
}
}
}
}
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 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 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 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 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 void validateSchema(Dialect dialect, DatabaseMetadata databaseMetadata)
throws HibernateException {
secondPassCompile();
String defaultCatalog = properties.getProperty( Environment.DEFAULT_CATALOG );
String defaultSchema = properties.getProperty( Environment.DEFAULT_SCHEMA );
Iterator iter = getTableMappings();
while ( iter.hasNext() ) {
Table table = (Table) iter.next();
if ( table.isPhysicalTable() ) {
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
( table.getSchema() == null ) ? defaultSchema : table.getSchema(),
( table.getCatalog() == null ) ? defaultCatalog : table.getCatalog(),
table.isQuoted());
if ( tableInfo == null ) {
throw new HibernateException( "Missing table: " + table.getName() );
}
else {
table.validateColumns( dialect, mapping, tableInfo );
}
}
}
iter = iterateGenerators( dialect );
while ( iter.hasNext() ) {
PersistentIdentifierGenerator generator = (PersistentIdentifierGenerator) iter.next();
Object key = generator.generatorKey();
if ( !databaseMetadata.isSequence( key ) && !databaseMetadata.isTable( key ) ) {
throw new HibernateException( "Missing sequence or table: " + key );
}
}
}
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 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 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 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 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 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();
}