[hibernate-commits] Hibernate SVN: r10713 - in trunk/Hibernate3: src/org/hibernate/cfg src/org/hibernate/tool/hbm2ddl test/org/hibernate/test test/org/hibernate/test/tool
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Fri Nov 3 11:21:02 EST 2006
Author: max.andersen at jboss.com
Date: 2006-11-03 11:20:54 -0500 (Fri, 03 Nov 2006)
New Revision: 10713
Added:
trunk/Hibernate3/test/org/hibernate/test/tool/
trunk/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/tool/Team.java
trunk/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java
Modified:
trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java
trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
Log:
merged HHH-1629 and HHH-2208 from 3.2
Modified: trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/src/org/hibernate/cfg/Configuration.java 2006-11-03 16:20:54 UTC (rev 10713)
@@ -916,11 +916,13 @@
while ( iter.hasNext() ) {
Table table = (Table) iter.next();
if ( table.isPhysicalTable() ) {
-
+ Settings settings = buildSettings();
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
- table.getSchema(),
- table.getCatalog()
+ ( table.getSchema() == null ) ? settings.getDefaultSchemaName() : table.getSchema(),
+ ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() : table.getCatalog(),
+ table.isQuoted()
+
);
if ( tableInfo == null ) {
script.add(
@@ -961,7 +963,8 @@
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
table.getSchema(),
- table.getCatalog()
+ table.getCatalog(),
+ table.isQuoted()
);
if ( dialect.hasAlterTable() ) {
@@ -1035,13 +1038,13 @@
while ( iter.hasNext() ) {
Table table = (Table) iter.next();
if ( table.isPhysicalTable() ) {
+ Settings settings = buildSettings();
TableMetadata tableInfo = databaseMetadata.getTableMetadata(
table.getName(),
- table.getSchema(),
- table.getCatalog()
- );
-
+ ( table.getSchema() == null ) ? settings.getDefaultSchemaName() : table.getSchema(),
+ ( table.getCatalog() == null ) ? settings.getDefaultCatalogName() : table.getCatalog(),
+ table.isQuoted());
if ( tableInfo == null ) {
throw new HibernateException( "Missing table: " + table.getName() );
}
Modified: trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/DatabaseMetadata.java 2006-11-03 16:20:54 UTC (rev 10713)
@@ -16,6 +16,7 @@
import org.hibernate.HibernateException;
import org.hibernate.exception.JDBCExceptionHelper;
import org.hibernate.exception.SQLExceptionConverter;
+import org.hibernate.mapping.Table;
import org.hibernate.dialect.Dialect;
import org.hibernate.util.StringHelper;
@@ -47,9 +48,10 @@
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 {
- TableMetadata table = (TableMetadata) tables.get(name);
+ Object identifier = identifier(catalog, schema, name);
+ TableMetadata table = (TableMetadata) tables.get(identifier);
if (table!=null) {
return table;
}
@@ -59,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),
@@ -67,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),
@@ -83,7 +87,7 @@
String tableName = rs.getString("TABLE_NAME");
if ( name.equalsIgnoreCase(tableName) ) {
table = new TableMetadata(rs, meta, extras);
- tables.put(name, table);
+ tables.put(identifier, table);
return table;
}
}
@@ -107,6 +111,10 @@
}
+ private Object identifier(String catalog, String schema, String name) {
+ return Table.qualify(catalog,schema,name);
+ }
+
private void initSequences(Connection connection, Dialect dialect) throws SQLException {
if ( dialect.supportsSequences() ) {
String sql = dialect.getQuerySequencesString();
@@ -132,25 +140,35 @@
}
public boolean isSequence(Object key) {
- return key instanceof String && sequences.contains( ( (String) key ).toLowerCase() );
- }
-
- 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;
- }
- }
+ if (key instanceof String){
+ String[] strings = StringHelper.split(".", (String) key);
+ return sequences.contains( strings[strings.length-1].toLowerCase());
}
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: trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/src/org/hibernate/tool/hbm2ddl/TableMetadata.java 2006-11-03 16:20:54 UTC (rev 10713)
@@ -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: trunk/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/test/org/hibernate/test/tool/Team.hbm.xml 2006-11-03 16:20:54 UTC (rev 10713)
@@ -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: trunk/Hibernate3/test/org/hibernate/test/tool/Team.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/tool/Team.java 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/test/org/hibernate/test/tool/Team.java 2006-11-03 16:20:54 UTC (rev 10713)
@@ -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: trunk/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java 2006-11-03 15:38:01 UTC (rev 10712)
+++ trunk/Hibernate3/test/org/hibernate/test/tool/TestSchemaTools.java 2006-11-03 16:20:54 UTC (rev 10713)
@@ -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();
+
+ }
+
+
+
+}
More information about the hibernate-commits
mailing list