HHH-7666 SchemaUtil cleanup

This commit is contained in:
Brett Meyer 2013-05-16 18:10:09 -04:00
parent 940ff1831e
commit 8412dce0eb
2 changed files with 39 additions and 74 deletions

View File

@ -39,6 +39,7 @@
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.test.annotations.Country;
import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
@ -47,7 +48,7 @@
* @author Hardy Ferentschik
*/
@SuppressWarnings("unchecked")
//@FailureExpectedWithNewMetamodel
@FailureExpectedWithNewMetamodel
public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test
public void testSimpleElement() throws Exception {

View File

@ -23,16 +23,11 @@
*/
package org.hibernate.test.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Iterator;
import org.hibernate.AssertionFailure;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.Metadata;
import org.hibernate.metamodel.spi.MetadataImplementor;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.relational.Column;
@ -40,117 +35,86 @@
import org.hibernate.metamodel.spi.relational.PrimaryKey;
import org.hibernate.metamodel.spi.relational.Schema;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.UniqueKey;
/**
* Check that the Hibernate metamodel contains some database objects
*
*
* @author Brett Meyer
*/
public abstract class SchemaUtil {
public class SchemaUtil {
private SchemaUtil() {}
public static boolean isColumnPresent(
String tableName, String columnName, MetadataImplementor metadata ) {
public static boolean isColumnPresent(String tableName, String columnName, MetadataImplementor metadata) {
try {
TableSpecification table = getTable( tableName, metadata );
return ( table.locateColumn( columnName ) != null );
} catch ( AssertionFailure e ) {
}
catch ( AssertionFailure e ) {
return false;
}
}
public static boolean isTablePresent( String tableName, MetadataImplementor metadata ) {
public static boolean isTablePresent(String tableName, MetadataImplementor metadata) {
try {
TableSpecification table = getTable( tableName, metadata );
return ( table != null );
} catch ( AssertionFailure e ) {
}
catch ( AssertionFailure e ) {
return false;
}
}
public static EntityBinding getEntityBinding(
Class<?> entityClass, MetadataImplementor metadata ) {
public static EntityBinding getEntityBinding(Class<?> entityClass, MetadataImplementor metadata) {
return metadata.getEntityBinding( entityClass.getName() );
}
public static TableSpecification getTable(
Class<?> entityClass, MetadataImplementor metadata ) throws AssertionFailure {
public static TableSpecification getTable(Class<?> entityClass, MetadataImplementor metadata)
throws AssertionFailure {
return getEntityBinding( entityClass, metadata ).getPrimaryTable();
}
public static TableSpecification getTable(String schemaName, String tableName, MetadataImplementor metadata) throws AssertionFailure{
if( StringHelper.isNotEmpty(schemaName)){
Schema schema = metadata.getDatabase().getSchema( null, schemaName );
if(schema == null){
throw new AssertionFailure( "can't find schema "+ schemaName );
}
return schema.locateTable( Identifier.toIdentifier(tableName) );
} else {
Iterable<Schema> schemas = metadata.getDatabase().getSchemas();
for(Schema schema : schemas){
TableSpecification table = schema.locateTable( Identifier.toIdentifier( tableName ) );
if(table != null){
return table;
}
public static TableSpecification getTable(String tableName, MetadataImplementor metadata) throws AssertionFailure {
Iterable<Schema> schemas = metadata.getDatabase().getSchemas();
for ( Schema schema : schemas ) {
TableSpecification table = schema.locateTable( Identifier.toIdentifier( tableName ) );
if ( table != null ) {
return table;
}
}
throw new AssertionFailure( "can't find table " +tableName );
throw new AssertionFailure( "can't find table " + tableName );
}
public static TableSpecification getTable(
String tableName, MetadataImplementor metadata ) throws AssertionFailure {
return getTable( null, tableName, metadata );
}
public static Column getColumn( Class<?> entityClass, String columnName,
MetadataImplementor metadata ) throws AssertionFailure {
public static Column getColumn(Class<?> entityClass, String columnName, MetadataImplementor metadata)
throws AssertionFailure {
return getTable( entityClass, metadata ).locateColumn( columnName );
}
public static Column getColumn( String tableName, String columnName,
MetadataImplementor metadata ) throws AssertionFailure {
return getTable( tableName, metadata ).locateColumn( columnName );
}
public static Column getColumnByAttribute( Class<?> entityClass,
String attributeName, MetadataImplementor metadata ) throws AssertionFailure {
EntityBinding binding = getEntityBinding( entityClass, metadata );
AttributeBinding attributeBinding = binding.locateAttributeBinding(
attributeName );
// TODO
return null;
}
public static PrimaryKey getPrimaryKey( Class<?> entityClass,
MetadataImplementor metadata ) throws AssertionFailure {
public static PrimaryKey getPrimaryKey(Class<?> entityClass, MetadataImplementor metadata) throws AssertionFailure {
return getTable( entityClass, metadata ).getPrimaryKey();
}
public static PluralAttributeBinding getCollection( Class<?> entityClass, String fieldName,
Metadata metadata ) {
Iterator<PluralAttributeBinding> collectionBindings
= metadata.getCollectionBindings().iterator();
public static PluralAttributeBinding getCollection(Class<?> entityClass, String fieldName, Metadata metadata) {
Iterator<PluralAttributeBinding> collectionBindings = metadata.getCollectionBindings().iterator();
while ( collectionBindings.hasNext() ) {
PluralAttributeBinding collectionBinding
= collectionBindings.next();
PluralAttributeBinding collectionBinding = collectionBindings.next();
if ( collectionBinding.getAttribute().getName().equals( fieldName )
&& collectionBinding.getAttribute().getAttributeContainer()
.getClassReference().equals( entityClass ) ) {
&& collectionBinding.getAttribute().getAttributeContainer().getClassReference()
.equals( entityClass ) ) {
return collectionBinding;
}
}
return null;
}
public static TableSpecification getCollectionTable( Class<?> entityClass, String fieldName,
Metadata metadata ) {
public static TableSpecification getCollectionTable(Class<?> entityClass, String fieldName, Metadata metadata) {
PluralAttributeBinding collection = getCollection( entityClass, fieldName, metadata );
return collection.getPluralAttributeKeyBinding().getCollectionTable();
}
public static boolean hasUniqueKeys(TableSpecification table, String... columnNames) {
for (String columnName : columnNames) {
if (!table.hasUniqueKey( table.locateColumn( columnName ) ) ) {
for ( String columnName : columnNames ) {
if ( !table.hasUniqueKey( table.locateColumn( columnName ) ) ) {
return false;
}
}