HHH-6389 : Add TableSpecification.getQualifiedName(Dialect) and implement in subclasses

This commit is contained in:
Gail Badner 2011-07-05 21:01:11 -07:00
parent 3b22a9fd29
commit 7c84b08391
7 changed files with 107 additions and 10 deletions

View File

@ -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

View File

@ -25,6 +25,8 @@ package org.hibernate.metamodel.relational;
import java.util.Collections;
import org.hibernate.dialect.Dialect;
/**
* A <tt>data container</tt> defined by a <tt>SELECT</tt> statement. This translates into an inline view in the
* SQL statements: <code>select ... from (select ... from logical_table_table ...) ...</code>
@ -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}";

View File

@ -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 ) {

View File

@ -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 <tt>TABLE</tt> (or <tt>VIEW</tt>).
@ -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<String,Index> 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 + '}';

View File

@ -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<String> getComments();
public void addComment(String comment);
public String getQualifiedName(Dialect dialect);
}

View File

@ -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 ) );
}
}

View File

@ -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 ) );
}
}