HHH-6174 Allowing of creation of ObjectName from single string

This commit is contained in:
Hardy Ferentschik 2011-05-05 15:39:39 +02:00
parent 360317eedf
commit f3cf1d2ae5
1 changed files with 82 additions and 11 deletions

View File

@ -23,6 +23,8 @@
*/ */
package org.hibernate.metamodel.relational; package org.hibernate.metamodel.relational;
import org.hibernate.HibernateException;
/** /**
* Models the qualified name of a database object. * Models the qualified name of a database object.
* <p/> * <p/>
@ -33,6 +35,9 @@ package org.hibernate.metamodel.relational;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class ObjectName { public class ObjectName {
// todo - should depend on DatabaseMetaData. For now hard coded (HF)
private static String SEPARATOR = ".";
private final Identifier schema; private final Identifier schema;
private final Identifier catalog; private final Identifier catalog;
private final Identifier name; private final Identifier name;
@ -40,6 +45,19 @@ public class ObjectName {
private final String identifier; private final String identifier;
private final int hashCode; private final int hashCode;
/**
* Tries to create an {@code ObjectName} from a name.
*
* @param objectName simple or qualified name of the database object.
*/
public ObjectName(String objectName) {
this(
extractSchema( objectName ),
extractCatalog( objectName ),
extractName( objectName )
);
}
public ObjectName(Identifier name) { public ObjectName(Identifier name) {
this( null, null, name ); this( null, null, name );
} }
@ -123,12 +141,6 @@ public class ObjectName {
&& areEqual( schema, that.schema ); && areEqual( schema, that.schema );
} }
private boolean areEqual(Identifier one, Identifier other) {
return one == null
? other == null
: one.equals( other );
}
@Override @Override
public int hashCode() { public int hashCode() {
return hashCode; return hashCode;
@ -143,5 +155,64 @@ public class ObjectName {
", catalog='" + catalog + '\'' + ", catalog='" + catalog + '\'' +
'}'; '}';
} }
private boolean areEqual(Identifier one, Identifier other) {
return one == null
? other == null
: one.equals( other );
}
private static String extractSchema(String qualifiedName) {
if ( qualifiedName == null ) {
return null;
}
String[] tokens = qualifiedName.split( SEPARATOR );
if ( tokens.length == 0 || tokens.length == 1 ) {
return null;
}
else if ( tokens.length == 2 ) {
// todo - this case needs to be refined w/ help of DatabaseMetaData (HF)
return null;
}
else if ( tokens.length == 3 ) {
return tokens[0];
}
else {
throw new HibernateException( "Unable to parse object name: " + qualifiedName );
}
}
private static String extractCatalog(String qualifiedName) {
if ( qualifiedName == null ) {
return null;
}
String[] tokens = qualifiedName.split( SEPARATOR );
if ( tokens.length == 0 || tokens.length == 1 ) {
return null;
}
else if ( tokens.length == 2 ) {
// todo - this case needs to be refined w/ help of DatabaseMetaData (HF)
return null;
}
else if ( tokens.length == 3 ) {
return tokens[1];
}
else {
throw new HibernateException( "Unable to parse object name: " + qualifiedName );
}
}
private static String extractName(String qualifiedName) {
if ( qualifiedName == null ) {
return null;
}
String[] tokens = qualifiedName.split( SEPARATOR );
if ( tokens.length == 0 ) {
return qualifiedName;
}
else {
return tokens[tokens.length - 1];
}
}
} }