diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java
index 9274ce36d1..f677e3adb1 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Identifier.java
@@ -24,6 +24,7 @@
package org.hibernate.metamodel.relational;
+import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.StringHelper;
/**
@@ -95,6 +96,25 @@ public class Identifier {
return isQuoted;
}
+ /**
+ * If this is a quoted identifier, then return the identifier name
+ * enclosed in dialect-specific open- and end-quotes; otherwise,
+ * simply return the identifier name.
+ *
+ * @param dialect
+ * @return if quoted, identifier name enclosed in dialect-specific
+ * open- and end-quotes; otherwise, the identifier name.
+ */
+ public String encloseInQuotesIfQuoted(Dialect dialect) {
+ return isQuoted ?
+ new StringBuilder( name.length() + 2 )
+ .append( dialect.openQuote() )
+ .append( name )
+ .append( dialect.closeQuote() )
+ .toString() :
+ name;
+ }
+
@Override
public String toString() {
return isQuoted
diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java
index b37a72e8ca..23651e8d82 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/InLineView.java
@@ -25,6 +25,8 @@ package org.hibernate.metamodel.relational;
import java.util.Collections;
+import org.hibernate.dialect.Dialect;
+
/**
* A data container defined by a SELECT statement. This translates into an inline view in the
* SQL statements: select ... from (select ... from logical_table_table ...) ...
@@ -96,6 +98,15 @@ public class InLineView extends AbstractTableSpecification {
throw new UnsupportedOperationException( "Cannot comment on inline view" );
}
+ @Override
+ public String getQualifiedName(Dialect dialect) {
+ return new StringBuilder( select.length() + 4 )
+ .append( "( " )
+ .append( select )
+ .append( " )" )
+ .toString();
+ }
+
@Override
public String toLoggableString() {
return "{inline-view}";
diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java
index 237fb96745..b27b73ac27 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ObjectName.java
@@ -24,6 +24,7 @@
package org.hibernate.metamodel.relational;
import org.hibernate.HibernateException;
+import org.hibernate.dialect.Dialect;
/**
* Models the qualified name of a database object.
@@ -94,14 +95,11 @@ public class ObjectName {
this.schema = schema;
this.catalog = catalog;
- StringBuilder buff = new StringBuilder( name.toString() );
- if ( catalog != null ) {
- buff.insert( 0, catalog.toString() + '.' );
- }
- if ( schema != null ) {
- buff.insert( 0, schema.toString() + '.' );
- }
- this.identifier = buff.toString();
+ this.identifier = qualify(
+ schema == null ? null : schema.toString(),
+ catalog == null ? null : catalog.toString(),
+ name.toString()
+ );
int tmpHashCode = schema != null ? schema.hashCode() : 0;
tmpHashCode = 31 * tmpHashCode + ( catalog != null ? catalog.hashCode() : 0 );
@@ -125,6 +123,34 @@ public class ObjectName {
return identifier;
}
+ public String toText(Dialect dialect) {
+ if ( dialect == null ) {
+ throw new IllegalArgumentException( "dialect must be non-null." );
+ }
+ return qualify(
+ encloseInQuotesIfQuoted( schema, dialect ),
+ encloseInQuotesIfQuoted( catalog, dialect ),
+ encloseInQuotesIfQuoted( name, dialect )
+ );
+ }
+
+ private static String encloseInQuotesIfQuoted(Identifier identifier, Dialect dialect) {
+ return identifier == null ?
+ null :
+ identifier.encloseInQuotesIfQuoted( dialect );
+ }
+
+ private static String qualify(String schema, String catalog, String name) {
+ StringBuilder buff = new StringBuilder( name );
+ if ( catalog != null ) {
+ buff.insert( 0, catalog + '.' );
+ }
+ if ( schema != null ) {
+ buff.insert( 0, schema + '.' );
+ }
+ return buff.toString();
+ }
+
@Override
public boolean equals(Object o) {
if ( this == o ) {
diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java
index 512aa85f03..7a413b244f 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Table.java
@@ -29,7 +29,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
-import org.hibernate.internal.util.StringHelper;
+import org.hibernate.dialect.Dialect;
/**
* Models the concept of a relational TABLE (or VIEW).
@@ -40,6 +40,7 @@ import org.hibernate.internal.util.StringHelper;
public class Table extends AbstractTableSpecification implements Exportable {
private final Schema database;
private final Identifier tableName;
+ private final ObjectName objectName;
private final String qualifiedName;
private LinkedHashMap indexes;
@@ -54,7 +55,7 @@ public class Table extends AbstractTableSpecification implements Exportable {
public Table(Schema database, Identifier tableName) {
this.database = database;
this.tableName = tableName;
- ObjectName objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), tableName );
+ objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), tableName );
this.qualifiedName = objectName.toText();
}
@@ -144,6 +145,11 @@ public class Table extends AbstractTableSpecification implements Exportable {
comments.add( comment );
}
+ @Override
+ public String getQualifiedName(Dialect dialect) {
+ return objectName.toText( dialect );
+ }
+
@Override
public String toString() {
return "Table{name=" + qualifiedName + '}';
diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java
index 39dc2ff564..86752c3552 100644
--- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java
+++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/TableSpecification.java
@@ -23,6 +23,8 @@
*/
package org.hibernate.metamodel.relational;
+import org.hibernate.dialect.Dialect;
+
/**
* Models what ANSI SQL terms a table specification which is a table or a view or an inline view.
*
@@ -96,4 +98,6 @@ public interface TableSpecification extends ValueContainer, Loggable {
public Iterable getComments();
public void addComment(String comment);
+
+ public String getQualifiedName(Dialect dialect);
}
diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/relational/ObjectNameTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/relational/ObjectNameTests.java
index d006197cec..30b2f2d10d 100644
--- a/hibernate-core/src/test/java/org/hibernate/metamodel/relational/ObjectNameTests.java
+++ b/hibernate-core/src/test/java/org/hibernate/metamodel/relational/ObjectNameTests.java
@@ -25,6 +25,8 @@ package org.hibernate.metamodel.relational;
import org.junit.Test;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
@@ -53,10 +55,18 @@ public class ObjectNameTests extends BaseUnitTestCase {
@Test
public void testIdentifierBuilding() {
+ Dialect dialect = new H2Dialect();
ObjectName on = new ObjectName( "schema", "catalog", "name" );
assertEquals( "schema.catalog.name", on.toText() );
on = new ObjectName( "schema", null, "name" );
assertEquals( "schema.name", on.toText() );
+ assertEquals( "schema.name", on.toText( dialect ) );
+ on = new ObjectName( "`schema`", "`catalog`", "`name`" );
+ assertEquals( "`schema`.`catalog`.`name`", on.toText() );
+ assertEquals( "\"schema\".\"catalog\".\"name\"", on.toText( dialect ) );
+ on = new ObjectName( "`schema`", null, "`name`" );
+ assertEquals( "`schema`.`name`", on.toText() );
+ assertEquals( "\"schema\".\"name\"", on.toText( dialect ) );
}
}
diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/relational/TableManipulationTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/relational/TableManipulationTests.java
index 89424d4426..4e0ac45c93 100644
--- a/hibernate-core/src/test/java/org/hibernate/metamodel/relational/TableManipulationTests.java
+++ b/hibernate-core/src/test/java/org/hibernate/metamodel/relational/TableManipulationTests.java
@@ -27,6 +27,8 @@ import java.sql.Types;
import org.junit.Test;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
@@ -128,4 +130,22 @@ public class TableManipulationTests extends BaseUnitTestCase {
assertEquals( page, pageBookFk.getSourceTable() );
assertEquals( book, pageBookFk.getTargetTable() );
}
+
+ @Test
+ public void testQualifiedName() {
+ Dialect dialect = new H2Dialect();
+ Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
+ Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) );
+ assertEquals( "my_table", table.getTableName().getName() );
+ assertEquals( "my_table", table.getTableName().toString() );
+ assertEquals( "schema.\"catalog\".my_table", table.getQualifiedName( dialect ) );
+
+ table = schema.createTable( Identifier.toIdentifier( "`my_table`" ) );
+ assertEquals( "my_table", table.getTableName().getName() );
+ assertEquals( "`my_table`", table.getTableName().toString() );
+ assertEquals( "schema.\"catalog\".\"my_table\"", table.getQualifiedName( dialect ) );
+
+ InLineView inLineView = schema.createInLineView( "my_inlineview", "select ..." );
+ assertEquals( "( select ... )", inLineView.getQualifiedName( dialect ) );
+ }
}