HHH-3532 - schema update task should look for foreign key signature

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16659 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-06-01 21:50:17 +00:00
parent 9c26388abf
commit baa2efd863
3 changed files with 87 additions and 21 deletions

View File

@ -1048,7 +1048,7 @@ public class Configuration implements Serializable {
ForeignKey fk = (ForeignKey) subIter.next();
if ( fk.isPhysicalConstraint() ) {
boolean create = tableInfo == null || (
tableInfo.getForeignKeyMetadata( fk.getName() ) == null && (
tableInfo.getForeignKeyMetadata( fk ) == null && (
//Icky workaround for MySQL bug:
!( dialect instanceof MySQLDialect ) ||
tableInfo.getIndexMetadata( fk.getName() ) == null

View File

@ -20,39 +20,73 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.tool.hbm2ddl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
/**
* JDBC foreign key metadata
*
* @author Christoph Sturm
*/
public class ForeignKeyMetadata {
private final String name;
private final List columns = new ArrayList();
private final String refTable;
private final Map references = new HashMap();
ForeignKeyMetadata(ResultSet rs) throws SQLException {
name = rs.getString("FK_NAME");
refTable = rs.getString("PKTABLE_NAME");
}
public String getName() {
return name;
}
void addColumn(ColumnMetadata column) {
if (column != null) columns.add(column);
public String getReferencedTableName() {
return refTable;
}
public ColumnMetadata[] getColumns() {
return (ColumnMetadata[]) columns.toArray(new ColumnMetadata[0]);
void addReference(ResultSet rs) throws SQLException {
references.put( rs.getString("FKCOLUMN_NAME").toLowerCase(), rs.getString("PKCOLUMN_NAME") );
}
private boolean hasReference(Column column, Column ref) {
String refName = (String) references.get(column.getName().toLowerCase());
return ref.getName().equalsIgnoreCase(refName);
}
public boolean matches(ForeignKey fk) {
if ( refTable.equalsIgnoreCase( fk.getReferencedTable().getName() ) ) {
if ( fk.getColumnSpan() == references.size() ) {
List fkRefs;
if ( fk.isReferenceToPrimaryKey() ) {
fkRefs = fk.getReferencedTable().getPrimaryKey().getColumns();
}
else {
fkRefs = fk.getReferencedColumns();
}
for ( int i = 0; i < fk.getColumnSpan(); i++ ) {
Column column = fk.getColumn( i );
Column ref = ( Column ) fkRefs.get( i );
if ( !hasReference( column, ref ) ) {
return false;
}
}
return true;
}
}
return false;
}
public String toString() {
return "ForeignKeyMetadata(" + name + ')';
}

View File

@ -20,7 +20,6 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.tool.hbm2ddl;
@ -29,16 +28,20 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.mapping.ForeignKey;
/**
* JDBC table metadata
* @author Christoph Sturm, Max Rydahl Andersen
*
* @author Christoph Sturm
* @author Max Rydahl Andersen
*/
public class TableMetadata {
private static final Logger log = LoggerFactory.getLogger(TableMetadata.class);
private final String catalog;
@ -91,6 +94,17 @@ public class TableMetadata {
return (ForeignKeyMetadata) foreignKeys.get( keyName.toLowerCase() );
}
public ForeignKeyMetadata getForeignKeyMetadata(ForeignKey fk) {
Iterator it = foreignKeys.values().iterator();
while ( it.hasNext() ) {
ForeignKeyMetadata existingFk = ( ForeignKeyMetadata ) it.next();
if ( existingFk.matches( fk ) ) {
return existingFk;
}
}
return null;
}
public IndexMetadata getIndexMetadata(String indexName) {
return (IndexMetadata) indexes.get( indexName.toLowerCase() );
}
@ -98,7 +112,9 @@ public class TableMetadata {
private void addForeignKey(ResultSet rs) throws SQLException {
String fk = rs.getString("FK_NAME");
if (fk == null) return;
if (fk == null) {
return;
}
ForeignKeyMetadata info = getForeignKeyMetadata(fk);
if (info == null) {
@ -106,13 +122,15 @@ public class TableMetadata {
foreignKeys.put( info.getName().toLowerCase(), info );
}
info.addColumn( getColumnMetadata( rs.getString("FKCOLUMN_NAME") ) );
info.addReference( rs );
}
private void addIndex(ResultSet rs) throws SQLException {
String index = rs.getString("INDEX_NAME");
if (index == null) return;
if (index == null) {
return;
}
IndexMetadata info = getIndexMetadata(index);
if (info == null) {
@ -126,7 +144,9 @@ public class TableMetadata {
public void addColumn(ResultSet rs) throws SQLException {
String column = rs.getString("COLUMN_NAME");
if (column==null) return;
if (column==null) {
return;
}
if ( getColumnMetadata(column) == null ) {
ColumnMetadata info = new ColumnMetadata(rs);
@ -139,10 +159,14 @@ public class TableMetadata {
try {
rs = meta.getImportedKeys(catalog, schema, name);
while ( rs.next() ) addForeignKey(rs);
while ( rs.next() ) {
addForeignKey(rs);
}
}
finally {
if (rs != null) rs.close();
if (rs != null) {
rs.close();
}
}
}
@ -153,12 +177,16 @@ public class TableMetadata {
rs = meta.getIndexInfo(catalog, schema, name, false, true);
while ( rs.next() ) {
if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic ) continue;
if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic ) {
continue;
}
addIndex(rs);
}
}
finally {
if (rs != null) rs.close();
if (rs != null) {
rs.close();
}
}
}
@ -167,10 +195,14 @@ public class TableMetadata {
try {
rs = meta.getColumns(catalog, schema, name, "%");
while ( rs.next() ) addColumn(rs);
while ( rs.next() ) {
addColumn(rs);
}
}
finally {
if (rs != null) rs.close();
if (rs != null) {
rs.close();
}
}
}