Author: max.andersen(a)jboss.com
Date: 2006-11-03 09:05:56 -0500 (Fri, 03 Nov 2006)
New Revision: 10706
Added:
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.java
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java
Modified:
branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
Log:
HHH-1629 SchemaUpdate/validator doesn't listen to quoting
Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03
13:38:40 UTC (rev 10705)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03
14:05:56 UTC (rev 10706)
@@ -879,7 +879,8 @@
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
( table.getSchema() == null ) ? settings.getDefaultSchemaName() :
table.getSchema(),
- ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() :
table.getCatalog()
+ ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() :
table.getCatalog(),
+ table.isQuoted()
);
if ( tableInfo == null ) {
@@ -921,7 +922,8 @@
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
table.getSchema(),
- table.getCatalog()
+ table.getCatalog(),
+ table.isQuoted()
);
if ( dialect.hasAlterTable() ) {
@@ -1000,7 +1002,8 @@
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
( table.getSchema() == null ) ? settings.getDefaultSchemaName() :
table.getSchema(),
- ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() :
table.getCatalog());
+ ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() :
table.getCatalog(),
+ table.isQuoted());
if ( tableInfo == null ) {
throw new HibernateException( "Missing table: " + table.getName() );
}
Modified:
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java
===================================================================
---
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java 2006-11-03
13:38:40 UTC (rev 10705)
+++
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java 2006-11-03
14:05:56 UTC (rev 10706)
@@ -48,7 +48,7 @@
private static final String[] TYPES = {"TABLE"};
- public TableMetadata getTableMetadata(String name, String schema, String catalog) throws
HibernateException {
+ public TableMetadata getTableMetadata(String name, String schema, String catalog,
boolean isQuoted) throws HibernateException {
Object identifier = identifier(catalog, schema, name);
TableMetadata table = (TableMetadata) tables.get(identifier);
@@ -61,7 +61,8 @@
ResultSet rs = null;
try {
- if ( meta.storesUpperCaseIdentifiers() ) {
+ if ( (isQuoted && meta.storesUpperCaseQuotedIdentifiers())
+ || (!isQuoted && meta.storesUpperCaseIdentifiers() )) {
rs = meta.getTables(
StringHelper.toUpperCase(catalog),
StringHelper.toUpperCase(schema),
@@ -69,7 +70,8 @@
TYPES
);
}
- else if ( meta.storesLowerCaseIdentifiers() ) {
+ else if ( (isQuoted && meta.storesLowerCaseQuotedIdentifiers())
+ || (!isQuoted && meta.storesLowerCaseIdentifiers() )) {
rs = meta.getTables(
StringHelper.toLowerCase(catalog),
StringHelper.toLowerCase(schema),
@@ -140,27 +142,33 @@
public boolean isSequence(Object key) {
if (key instanceof String){
String[] strings = StringHelper.split(".", (String) key);
- return sequences.contains( ( (String) strings[strings.length-1] ).toLowerCase());
+ return sequences.contains( strings[strings.length-1].toLowerCase());
}
return false;
}
- public boolean isTable(Object key) throws HibernateException {
- if(key instanceof String) {
- if ( getTableMetadata( (String) key, null, null ) != null ) {
- return true;
- } else {
- String[] strings = StringHelper.split(".", (String) key);
- if(strings.length==3) {
- return getTableMetadata(strings[2], strings[1], strings[0]) != null;
- } else if (strings.length==2) {
- return getTableMetadata(strings[1], strings[0], null) != null;
- }
- }
- }
- return false;
- }
-
+ public boolean isTable(Object key) throws HibernateException {
+ if(key instanceof String) {
+ Table tbl = new Table((String)key);
+ if ( getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(),
tbl.isQuoted() ) != null ) {
+ return true;
+ } else {
+ String[] strings = StringHelper.split(".", (String) key);
+ if(strings.length==3) {
+ tbl = new Table(strings[2]);
+ tbl.setCatalog(strings[0]);
+ tbl.setSchema(strings[1]);
+ return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(),
tbl.isQuoted() ) != null;
+ } else if (strings.length==2) {
+ tbl = new Table(strings[1]);
+ tbl.setSchema(strings[0]);
+ return getTableMetadata( tbl.getName(), tbl.getSchema(), tbl.getCatalog(),
tbl.isQuoted() ) != null;
+ }
+ }
+ }
+ return false;
+ }
+
public String toString() {
return "DatabaseMetadata" + tables.keySet().toString() +
sequences.toString();
}
Modified:
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
===================================================================
---
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2006-11-03
13:38:40 UTC (rev 10705)
+++
branches/Branch_3_2/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2006-11-03
14:05:56 UTC (rev 10706)
@@ -9,11 +9,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.hibernate.util.StringHelper;
/**
* JDBC table metadata
- * @author Christoph Sturm
+ * @author Christoph Sturm, Max Rydahl Andersen
*/
public class TableMetadata {
@@ -108,24 +107,7 @@
ResultSet rs = null;
try {
- if ( meta.storesUpperCaseIdentifiers() ) {
- rs = meta.getImportedKeys(
- StringHelper.toUpperCase(catalog),
- StringHelper.toUpperCase(schema),
- StringHelper.toUpperCase(name)
- );
- }
- else if ( meta.storesLowerCaseIdentifiers() ) {
- rs = meta.getImportedKeys(
- StringHelper.toLowerCase(catalog),
- StringHelper.toLowerCase(schema),
- StringHelper.toLowerCase(name)
- );
- }
- else {
- rs = meta.getImportedKeys(catalog, schema, name);
- }
-
+ rs = meta.getImportedKeys(catalog, schema, name);
while ( rs.next() ) addForeignKey(rs);
}
finally {
@@ -137,28 +119,8 @@
ResultSet rs = null;
try {
- if ( meta.storesUpperCaseIdentifiers() ) {
- rs = meta.getIndexInfo(
- StringHelper.toUpperCase(catalog),
- StringHelper.toUpperCase(schema),
- StringHelper.toUpperCase(name),
- false,
- true
- );
- }
- else if ( meta.storesLowerCaseIdentifiers() ) {
- rs = meta.getIndexInfo(
- StringHelper.toLowerCase(catalog),
- StringHelper.toLowerCase(schema),
- StringHelper.toLowerCase(name),
- false,
- true
- );
- }
- else {
- rs = meta.getIndexInfo(catalog, schema, name, false, true);
- }
-
+ rs = meta.getIndexInfo(catalog, schema, name, false, true);
+
while ( rs.next() ) {
if ( rs.getShort("TYPE") == DatabaseMetaData.tableIndexStatistic )
continue;
addIndex(rs);
@@ -173,26 +135,7 @@
ResultSet rs = null;
try {
- if ( meta.storesUpperCaseIdentifiers() ) {
- rs = meta.getColumns(
- StringHelper.toUpperCase(catalog),
- StringHelper.toUpperCase(schema),
- StringHelper.toUpperCase(name),
- "%"
- );
- }
- else if ( meta.storesLowerCaseIdentifiers() ) {
- rs = meta.getColumns(
- StringHelper.toLowerCase(catalog),
- StringHelper.toLowerCase(schema),
- StringHelper.toLowerCase(name),
- "%"
- );
- }
- else {
- rs = meta.getColumns(catalog, schema, name, "%");
- }
-
+ rs = meta.getColumns(catalog, schema, name, "%");
while ( rs.next() ) addColumn(rs);
}
finally {
Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml 2006-11-03
13:38:40 UTC (rev 10705)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml 2006-11-03
14:05:56 UTC (rev 10706)
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping SYSTEM
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.tool">
+ <class name="Team" table="`Team`">
+ <id name="id" column="`iD`">
+ <generator class="native">
+ <param name="sequence">TEAM_SEQ</param>
+ </generator>
+ </id>
+ <property name="name"/>
+ </class>
+
+ <class entity-name="OtherTeam" name="Team"
table="TEAM">
+ <id name="id" column="id">
+ <generator class="native">
+ <param name="sequence">TEAM_SEQ</param>
+ </generator>
+ </id>
+ <property name="name" column="xname"/>
+ </class>
+
+</hibernate-mapping>
Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.java
===================================================================
--- branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.java 2006-11-03
13:38:40 UTC (rev 10705)
+++ branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/Team.java 2006-11-03
14:05:56 UTC (rev 10706)
@@ -0,0 +1,20 @@
+package org.hibernate.test.tool;
+
+
+public class Team {
+ private Long id;
+ private String name;
+ public Long getId() {
+ return id;
+ }
+ public void setId(Long id) {
+ this.id = id;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
Added: branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java
===================================================================
---
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java 2006-11-03
13:38:40 UTC (rev 10705)
+++
branches/Branch_3_2/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java 2006-11-03
14:05:56 UTC (rev 10706)
@@ -0,0 +1,209 @@
+package org.hibernate.test.tool;
+
+import java.sql.Connection;
+import java.sql.Statement;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.test.TestCase;
+import org.hibernate.tool.hbm2ddl.SchemaExport;
+import org.hibernate.tool.hbm2ddl.SchemaUpdate;
+import org.hibernate.tool.hbm2ddl.SchemaValidator;
+
+/**
+ * @author Anthony
+ *
+ * Basic smoke test for schemaupdate/validator.
+ * Dependent on schemas, and probably also HQLDB - Not possible to have in the global
test suite at the moment.
+ *
+ * WARNING, if you want this test to work, you need to define a default schema = SB
+ * in hibernate global configuration.
+ * This schema should not be the same at the default db user schema and should come after
the users schema alphabetically.
+ *
+ */
+public class TestSchemaTools extends TestCase{
+
+ public void testSchemaTools() throws Exception{
+ // database schema have been created thanks to the setUp method
+ // we have 2 schemas SA et SB, SB must be set as the default schema
+ // used by hibernate hibernate.default_schema SB
+ SchemaExport se = new SchemaExport(getCfg());
+ se.create(true,true);
+
+ // here we modify the generated table in order to test SchemaUpdate
+ Session session = openSession();
+ Connection conn = session.connection();
+ Statement stat = conn.createStatement();
+ stat.execute("ALTER TABLE \"SB\".\"Team\" DROP COLUMN name
");
+
+ // update schema
+ SchemaUpdate su = new SchemaUpdate(getCfg());
+ su.execute(true,true);
+
+ // we can run schema validation. Note that in the setUp method a *wrong* table
+ // has been created with different column names
+ // if schema validator chooses the bad db schema, then the testcase will fail
(exception)
+ SchemaValidator sv = new SchemaValidator(getCfg());
+ sv.validate();
+
+ // it's time to clean our database
+ se.drop(true,true);
+
+ // then the schemas and false table.
+
+ stat.execute("DROP TABLE \"SA\".\"Team\" ");
+ stat.execute(" DROP SCHEMA sa ");
+ stat.execute("DROP SCHEMA sb ");
+ stat.close();
+ session.close();
+ }
+
+ public void testSchemaToolsNonQuote() throws Exception{
+ // database schema have been created thanks to the setUp method
+ // we have 2 schemas SA et SB, SB must be set as the default schema
+ // used by hibernate hibernate.default_schema SB
+ SchemaExport se = new SchemaExport(getCfg());
+ se.create(true,true);
+
+ // here we modify the generated table in order to test SchemaUpdate
+ Session session = openSession();
+ Connection conn = session.connection();
+ Statement stat = conn.createStatement();
+ stat.execute("ALTER TABLE \"SB\".\"TEAM\" DROP COLUMN xname
");
+
+ // update schema
+ SchemaUpdate su = new SchemaUpdate(getCfg());
+ su.execute(true,true);
+
+ // we can run schema validation. Note that in the setUp method a *wrong* table
+ // has been created with different column names
+ // if schema validator chooses the bad db schema, then the testcase will fail
(exception)
+ SchemaValidator sv = new SchemaValidator(getCfg());
+ sv.validate();
+
+ // it's time to clean our database
+ se.drop(true,true);
+
+ // then the schemas and false table.
+
+ stat.execute("DROP TABLE \"SA\".\"Team\" ");
+ stat.execute(" DROP SCHEMA sa ");
+ stat.execute("DROP SCHEMA sb ");
+ stat.close();
+ session.close();
+ }
+ public void testFailingQuoteValidation() throws Exception{
+ // database schema have been created thanks to the setUp method
+ // we have 2 schemas SA et SB, SB must be set as the default schema
+ // used by hibernate hibernate.default_schema SB
+ SchemaExport se = new SchemaExport(getCfg());
+ se.create(true,true);
+
+ // here we modify the generated table in order to test SchemaUpdate
+ Session session = openSession();
+ Connection conn = session.connection();
+ Statement stat = conn.createStatement();
+ stat.execute("ALTER TABLE \"SB\".\"Team\" DROP COLUMN name
");
+
+ // update schema
+ //SchemaUpdate su = new SchemaUpdate(getCfg());
+ //su.execute(true,true);
+
+ try {
+ SchemaValidator sv = new SchemaValidator(getCfg());
+ sv.validate();
+ fail("should fail since we mutated the current schema.");
+ } catch(HibernateException he) {
+
+ }
+
+ // it's time to clean our database
+ se.drop(true,true);
+
+ // then the schemas and false table.
+
+ stat.execute("DROP TABLE \"SA\".\"Team\" ");
+ stat.execute(" DROP SCHEMA sa ");
+ stat.execute("DROP SCHEMA sb ");
+ stat.close();
+ session.close();
+ }
+
+ public void testFailingNonQuoteValidation() throws Exception{
+ // database schema have been created thanks to the setUp method
+ // we have 2 schemas SA et SB, SB must be set as the default schema
+ // used by hibernate hibernate.default_schema SB
+ SchemaExport se = new SchemaExport(getCfg());
+ se.create(true,true);
+
+ // here we modify the generated table in order to test SchemaUpdate
+ Session session = openSession();
+ Connection conn = session.connection();
+ Statement stat = conn.createStatement();
+ stat.execute("ALTER TABLE \"SB\".\"TEAM\" DROP COLUMN xname
");
+
+ // update schema
+ //SchemaUpdate su = new SchemaUpdate(getCfg());
+ //su.execute(true,true);
+
+ try {
+ SchemaValidator sv = new SchemaValidator(getCfg());
+ sv.validate();
+ fail("should fail since we mutated the current schema.");
+ } catch(HibernateException he) {
+
+ }
+
+ // it's time to clean our database
+ se.drop(true,true);
+
+ // then the schemas and false table.
+
+ stat.execute("DROP TABLE \"SA\".\"Team\" ");
+ stat.execute(" DROP SCHEMA sa ");
+ stat.execute("DROP SCHEMA sb ");
+ stat.close();
+ session.close();
+ }
+
+ public TestSchemaTools(String arg0) {
+ super(arg0);
+ }
+
+ public String[] getMappings() {
+ return new String[] {"tool/Team.hbm.xml"};
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestSchemaTools.class);
+ }
+
+ public static void main(String[] args) throws Exception {
+ TestRunner.run( suite() );
+ }
+
+ protected boolean recreateSchema() {
+ return false;
+ }
+
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ Session session = openSession();
+ Connection conn = session.connection();
+ Statement stat = conn.createStatement();
+ stat.execute("CREATE SCHEMA sb AUTHORIZATION DBA ");
+ stat.execute(" CREATE SCHEMA sa AUTHORIZATION DBA ");
+ stat.execute(" CREATE TABLE \"SA\".\"Team\" (test INTEGER)
");
+ stat.close();
+ conn.close();
+
+ }
+
+
+
+}