HHH-6174 Allowing of creation of ObjectName from single string
This commit is contained in:
parent
360317eedf
commit
f3cf1d2ae5
|
@ -23,6 +23,8 @@
|
|||
*/
|
||||
package org.hibernate.metamodel.relational;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
|
||||
/**
|
||||
* Models the qualified name of a database object.
|
||||
* <p/>
|
||||
|
@ -33,6 +35,9 @@ package org.hibernate.metamodel.relational;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ObjectName {
|
||||
// todo - should depend on DatabaseMetaData. For now hard coded (HF)
|
||||
private static String SEPARATOR = ".";
|
||||
|
||||
private final Identifier schema;
|
||||
private final Identifier catalog;
|
||||
private final Identifier name;
|
||||
|
@ -40,6 +45,19 @@ public class ObjectName {
|
|||
private final String identifier;
|
||||
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) {
|
||||
this( null, null, name );
|
||||
}
|
||||
|
@ -123,12 +141,6 @@ public class ObjectName {
|
|||
&& areEqual( schema, that.schema );
|
||||
}
|
||||
|
||||
private boolean areEqual(Identifier one, Identifier other) {
|
||||
return one == null
|
||||
? other == null
|
||||
: one.equals( other );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
|
@ -143,5 +155,64 @@ public class ObjectName {
|
|||
", 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue