[hibernate-commits] Hibernate SVN: r14503 - in branches/Branch_3_2/HibernateExt/tools/src: java/org/hibernate/tool/hbm2x and 2 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Fri Apr 11 07:07:15 EDT 2008
Author: max.andersen at jboss.com
Date: 2008-04-11 07:07:14 -0400 (Fri, 11 Apr 2008)
New Revision: 14503
Added:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java
Modified:
branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/Hbm2DDLExporterTask.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java
branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/CachedMetaDataTest.java
Log:
The core tools part of JBIDE-1617
Modified: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/Hbm2DDLExporterTask.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/Hbm2DDLExporterTask.java 2008-04-11 04:47:54 UTC (rev 14502)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/ant/Hbm2DDLExporterTask.java 2008-04-11 11:07:14 UTC (rev 14503)
@@ -4,16 +4,8 @@
*/
package org.hibernate.tool.ant;
-import java.io.File;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.Iterator;
-
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.hibernate.tool.hbm2ddl.SchemaExport;
-import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2x.Exporter;
+import org.hibernate.tool.hbm2x.Hbm2DDLExporter;
/**
* @author max
@@ -29,52 +21,37 @@
boolean create = true;
boolean format = false;
- String outputfileName = null;
+ String outputFileName = null;
private boolean haltOnError = false;
public Hbm2DDLExporterTask(HibernateToolTask parent) {
super(parent);
}
- public void execute() {
- if(schemaUpdate) {
- SchemaUpdate update = new SchemaUpdate(parent.getConfiguration() );
- update.execute(scriptToConsole, exportToDatabase);
- }
- else {
- SchemaExport export = new SchemaExport(parent.getConfiguration() );
- if(outputfileName!=null) {
- export.setOutputFile(new File(getDestdir(),outputfileName).toString() );
- }
- if(delimiter!=null) {
- export.setDelimiter(delimiter);
- }
- export.setHaltOnError(haltOnError);
- export.setFormat(format);
- if(drop && create) {
- export.create(scriptToConsole, exportToDatabase);
- } else {
- export.execute(scriptToConsole, exportToDatabase, drop, create);
- }
-
- if(export.getExceptions().size()>0) {
- Iterator iterator = export.getExceptions().iterator();
- int cnt=1;
- parent.log(export.getExceptions().size() + " errors occurred while performing <hbm2ddl>.", Project.MSG_WARN);
- while ( iterator.hasNext() ) {
- Throwable throwable = (Throwable) iterator.next();
- parent.log("Error #" + cnt + ": " + throwable.toString(), Project.MSG_WARN);
- StringWriter sw = new StringWriter();
- throwable.printStackTrace(new PrintWriter(sw));
- parent.log(sw.getBuffer().toString(), Project.MSG_VERBOSE);
-
- }
- if(haltOnError) {
- throw new BuildException("Errors while performing <hbm2ddl>");
- }
- }
- }
+ public String getName() {
+ return "hbm2ddl (Generates database schema)";
}
+
+ protected Exporter configureExporter(Exporter exp) {
+ Hbm2DDLExporter exporter = (Hbm2DDLExporter) exp;
+ super.configureExporter( exp );
+ exporter.setExport(exportToDatabase);
+ exporter.setConsole(scriptToConsole);
+ exporter.setUpdate(schemaUpdate);
+ exporter.setDelimiter(delimiter);
+ exporter.setDrop(drop);
+ exporter.setCreate(create);
+ exporter.setFormat(format);
+ exporter.setOutputFileName(outputFileName);
+ exporter.setHaltonerror(haltOnError);
+ return exporter;
+ }
+
+ protected Exporter createExporter() {
+ Hbm2DDLExporter exporter = new Hbm2DDLExporter(parent.getConfiguration(), parent.getDestDir());
+ return exporter;
+ }
+
public void setExport(boolean export) {
exportToDatabase = export;
@@ -105,7 +82,7 @@
* File out put name (default: empty)
*/
public void setOutputFileName(String fileName) {
- outputfileName = fileName;
+ outputFileName = fileName;
}
public void setDrop(boolean drop) {
@@ -116,10 +93,6 @@
this.create = create;
}
- public String getName() {
- return "hbm2ddl (Generates database schema)";
- }
-
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
@@ -131,8 +104,4 @@
public void setHaltonerror(boolean haltOnError) {
this.haltOnError = haltOnError;
}
-
- protected Exporter createExporter() {
- throw new IllegalStateException("Should not call create exporter on hbm2ddl");
- }
}
Added: branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java (rev 0)
+++ branches/Branch_3_2/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Hbm2DDLExporter.java 2008-04-11 11:07:14 UTC (rev 14503)
@@ -0,0 +1,163 @@
+/*******************************************************************************
+ * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.tool.hbm2x;
+
+import java.io.File;
+import java.util.Iterator;
+
+import org.apache.tools.ant.BuildException;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.tool.hbm2ddl.SchemaExport;
+import org.hibernate.tool.hbm2ddl.SchemaUpdate;
+
+/**
+ * Schema Export (.ddl) code generation.
+ *
+ * @author Vitali
+ *
+ */
+public class Hbm2DDLExporter extends AbstractExporter {
+
+ protected boolean exportToDatabase = true;
+ protected boolean scriptToConsole = true;
+ protected boolean schemaUpdate = false;
+ protected String delimiter = ";";
+ protected boolean drop = false;
+ protected boolean create = true;
+ protected boolean format = false;
+
+ protected String outputFileName = null;
+ protected boolean haltOnError = false;
+
+ public Hbm2DDLExporter() {
+ }
+
+ public Hbm2DDLExporter(Configuration cfg, File outputdir) {
+ super(cfg, outputdir);
+ }
+
+ protected boolean setupBoolProperty(String property, boolean defaultVal) {
+ if (!getProperties().containsKey(property)) {
+ return defaultVal;
+ }
+ return Boolean.parseBoolean(getProperties().getProperty(property));
+ }
+
+ protected void setupContext() {
+
+ exportToDatabase = setupBoolProperty("exportToDatabase", exportToDatabase);
+ scriptToConsole = setupBoolProperty("scriptToConsole", scriptToConsole);
+ schemaUpdate = setupBoolProperty("schemaUpdate", schemaUpdate);
+ delimiter = getProperties().getProperty("delimiter", delimiter);
+ drop = setupBoolProperty("drop", drop);
+ create = setupBoolProperty("create", create);
+ format = setupBoolProperty("format", format);
+ outputFileName = getProperties().getProperty("outputFileName", outputFileName);
+ haltOnError = setupBoolProperty("haltOnError", haltOnError);
+ super.setupContext();
+ }
+
+ protected void cleanUpContext() {
+ super.cleanUpContext();
+ }
+
+ protected void doStart() {
+
+ final Configuration configuration = getConfiguration();
+ if (schemaUpdate) {
+ SchemaUpdate update = new SchemaUpdate(configuration);
+ update.execute(scriptToConsole, exportToDatabase);
+ } else {
+ SchemaExport export = new SchemaExport(configuration);
+ if (null != outputFileName) {
+ export.setOutputFile(new File(getOutputDirectory(),
+ outputFileName).toString());
+ }
+ if (null != delimiter) {
+ export.setDelimiter(delimiter);
+ }
+ export.setHaltOnError(haltOnError);
+ export.setFormat(format);
+ if (drop && create) {
+ export.create(scriptToConsole, exportToDatabase);
+ } else {
+ export.execute(scriptToConsole, exportToDatabase, drop, create);
+ }
+
+ if (!export.getExceptions().isEmpty()) {
+ int i = 1;
+ for (Iterator iterator = export.getExceptions().iterator(); iterator
+ .hasNext(); i++) {
+ Throwable element = (Throwable) iterator.next();
+ log.warn("Error #" + i + ": ", element);
+
+ }
+ log.error(i - 1 + " occurred while performing Hbm2DDLExporter.");
+ if (haltOnError) {
+ throw new BuildException(
+ "Errors while performing Hbm2DDLExporter");
+ }
+ }
+ }
+ }
+
+ public void setExport(boolean export) {
+ exportToDatabase = export;
+ }
+
+ /**
+ * Run SchemaUpdate instead of SchemaExport
+ */
+ public void setUpdate(boolean update) {
+ this.schemaUpdate = update;
+ }
+
+ /**
+ * Output sql to console ? (default true)
+ */
+ public void setConsole(boolean console) {
+ this.scriptToConsole = console;
+ }
+
+ /**
+ * Format the generated sql
+ */
+ public void setFormat(boolean format) {
+ this.format = format;
+ }
+
+ /**
+ * File out put name (default: empty)
+ */
+ public void setOutputFileName(String fileName) {
+ outputFileName = fileName;
+ }
+
+ public void setDrop(boolean drop) {
+ this.drop = drop;
+ }
+
+ public void setCreate(boolean create) {
+ this.create = create;
+ }
+
+ public void setDelimiter(String delimiter) {
+ this.delimiter = delimiter;
+ }
+
+ public String getDelimiter() {
+ return delimiter;
+ }
+
+ public void setHaltonerror(boolean haltOnError) {
+ this.haltOnError = haltOnError;
+ }
+}
Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java 2008-04-11 04:47:54 UTC (rev 14502)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/ant/AntHibernateToolTest.java 2008-04-11 11:07:14 UTC (rev 14503)
@@ -34,12 +34,17 @@
}
public void testHbm2DDLLogic() {
- executeTarget("testantcfg");
File baseDir = new File(project.getProperty("build.dir"), "topdown");
File onlyCreate = new File(baseDir, "onlycreate.sql");
File onlyDrop = new File(baseDir, "onlydrop.sql");
File dropAndCreate = new File(baseDir, "dropandcreate.sql");
+ assertFalse(onlyCreate.exists());
+ assertFalse(onlyDrop.exists());
+ assertFalse(dropAndCreate.exists());
+
+ executeTarget("testantcfg");
+
assertTrue(onlyCreate.exists());
assertTrue(onlyDrop.exists());
assertTrue(dropAndCreate.exists());
@@ -51,8 +56,12 @@
assertNotNull(TestHelper.findFirstString("drop", onlyDrop));
assertEquals(null, TestHelper.findFirstString("drop", onlyCreate));
- assertNotNull(TestHelper.findFirstString("create", onlyCreate));
+ assertNotNull(TestHelper.findFirstString("create", onlyCreate));
+ onlyCreate.delete();
+ onlyDrop.delete();
+ dropAndCreate.delete();
+
}
public void testJDBCConfiguration() {
Modified: branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/CachedMetaDataTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/CachedMetaDataTest.java 2008-04-11 04:47:54 UTC (rev 14502)
+++ branches/Branch_3_2/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/CachedMetaDataTest.java 2008-04-11 11:07:14 UTC (rev 14503)
@@ -1,179 +1,178 @@
-/*
- * Created on 2004-12-01
- *
- */
-package org.hibernate.tool.hbm2x;
-
-import java.util.Iterator;
-
-import org.hibernate.cfg.JDBCMetaDataConfiguration;
-import org.hibernate.cfg.JDBCReaderFactory;
-import org.hibernate.cfg.Settings;
-import org.hibernate.cfg.reveng.DatabaseCollector;
-import org.hibernate.cfg.reveng.DefaultDatabaseCollector;
-import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
-import org.hibernate.cfg.reveng.JDBCReader;
-import org.hibernate.cfg.reveng.ReverseEngineeringRuntimeInfo;
-import org.hibernate.cfg.reveng.dialect.CachedMetaDataDialect;
-import org.hibernate.cfg.reveng.dialect.MetaDataDialect;
-import org.hibernate.connection.ConnectionProvider;
-import org.hibernate.exception.SQLExceptionConverter;
-import org.hibernate.mapping.Table;
-import org.hibernate.tool.JDBCMetaDataBinderTestCase;
-
-
-
-/**
- * @author max
- *
- */
-public class CachedMetaDataTest extends JDBCMetaDataBinderTestCase {
-
- protected void configure(JDBCMetaDataConfiguration configuration) {
- super.configure( configuration );
- }
-
- public class MockedMetaDataDialect implements MetaDataDialect {
-
- MetaDataDialect delegate;
- private boolean failOnDelegateAccess;
-
- public MockedMetaDataDialect(MetaDataDialect realMetaData) {
- delegate = realMetaData;
- }
-
- public void close() {
- delegate.close();
- }
-
- public void close(Iterator iterator) {
- delegate.close( iterator );
- }
-
- public void configure(ReverseEngineeringRuntimeInfo info) {
- delegate.configure(info);
- }
-
- public Iterator getColumns(String catalog, String schema, String table, String column) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getColumns( catalog, schema, table, column );
- }
- }
-
- public Iterator getExportedKeys(String catalog, String schema, String table) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getExportedKeys( catalog, schema, table );
- }
- }
-
- public Iterator getIndexInfo(String catalog, String schema, String table) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getIndexInfo( catalog, schema, table );
- }
- }
-
- public Iterator getPrimaryKeys(String catalog, String schema, String name) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getPrimaryKeys( catalog, schema, name );
- }
- }
-
- public Iterator getTables(String catalog, String schema, String table) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getTables( catalog, schema, table );
- }
- }
-
- public boolean needQuote(String name) {
- return delegate.needQuote( name );
- }
-
- public void setDelegate(Object object) {
- this.delegate = null;
- }
-
- public void setFailOnDelegateAccess(boolean b) {
- failOnDelegateAccess = b;
- }
-
- public Iterator getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String name) {
- if(failOnDelegateAccess) {
- throw new IllegalStateException("delegate not accessible");
- } else {
- return delegate.getSuggestedPrimaryKeyStrategyName(catalog, schema, name);
- }
- }
-
- }
-
- public void testCachedDialect() {
- Settings buildSettings = cfg.buildSettings();
-
- MetaDataDialect realMetaData = JDBCReaderFactory.newMetaDataDialect( buildSettings.getDialect(), cfg.getProperties() );
-
- MockedMetaDataDialect mock = new MockedMetaDataDialect(realMetaData);
- CachedMetaDataDialect dialect = new CachedMetaDataDialect(mock);
-
- JDBCReader reader = JDBCReaderFactory.newJDBCReader( buildSettings, new DefaultReverseEngineeringStrategy(), dialect );
-
- DatabaseCollector dc = new DefaultDatabaseCollector();
- reader.readDatabaseSchema( dc, null, null );
-
- validate( dc );
-
- mock.setFailOnDelegateAccess(true);
-
- reader = JDBCReaderFactory.newJDBCReader( buildSettings, new DefaultReverseEngineeringStrategy(), dialect );
-
- dc = new DefaultDatabaseCollector();
- reader.readDatabaseSchema( dc, null, null );
-
- validate(dc);
-
-
-
-
- }
-
- private void validate(DatabaseCollector dc) {
- Iterator iterator = dc.iterateTables();
- Table firstChild = (Table) iterator.next();
- assertEquals(firstChild.getName(), "CHILD");
- assertTrue(iterator.hasNext());
-
- iterator = dc.iterateTables();
- assertNotNull(iterator.next());
- assertNotNull(iterator.next());
- assertFalse(iterator.hasNext());
-
-
- assertHasNext("should have recorded one foreignkey to child table", 1, firstChild.getForeignKeyIterator() );
- }
-
- protected String[] getCreateSQL() {
-
- return new String[] {
- "create table master ( id char not null, name varchar(20), primary key (id) )",
- "create table child ( childid char not null, masterref char, primary key (childid), foreign key (masterref) references master(id) )",
- };
- }
-
- protected String[] getDropSQL() {
-
- return new String[] {
- "drop table child",
- "drop table master",
- };
- }
-
-}
+/*
+ * Created on 2004-12-01
+ *
+ */
+package org.hibernate.tool.hbm2x;
+
+import java.util.Iterator;
+
+import org.hibernate.cfg.JDBCMetaDataConfiguration;
+import org.hibernate.cfg.JDBCReaderFactory;
+import org.hibernate.cfg.Settings;
+import org.hibernate.cfg.reveng.DatabaseCollector;
+import org.hibernate.cfg.reveng.DefaultDatabaseCollector;
+import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
+import org.hibernate.cfg.reveng.JDBCReader;
+import org.hibernate.cfg.reveng.ReverseEngineeringRuntimeInfo;
+import org.hibernate.cfg.reveng.dialect.CachedMetaDataDialect;
+import org.hibernate.cfg.reveng.dialect.MetaDataDialect;
+import org.hibernate.mapping.Table;
+import org.hibernate.tool.JDBCMetaDataBinderTestCase;
+
+
+
+/**
+ * @author max
+ *
+ */
+public class CachedMetaDataTest extends JDBCMetaDataBinderTestCase {
+
+ protected void configure(JDBCMetaDataConfiguration configuration) {
+ super.configure( configuration );
+ }
+
+ public class MockedMetaDataDialect implements MetaDataDialect {
+
+ MetaDataDialect delegate;
+ private boolean failOnDelegateAccess;
+
+ public MockedMetaDataDialect(MetaDataDialect realMetaData) {
+ delegate = realMetaData;
+ }
+
+ public void close() {
+ delegate.close();
+ }
+
+ public void close(Iterator iterator) {
+ delegate.close( iterator );
+ }
+
+ public void configure(ReverseEngineeringRuntimeInfo info) {
+ delegate.configure(info);
+ }
+
+ public Iterator getColumns(String catalog, String schema, String table, String column) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getColumns( catalog, schema, table, column );
+ }
+ }
+
+ public Iterator getExportedKeys(String catalog, String schema, String table) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getExportedKeys( catalog, schema, table );
+ }
+ }
+
+ public Iterator getIndexInfo(String catalog, String schema, String table) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getIndexInfo( catalog, schema, table );
+ }
+ }
+
+ public Iterator getPrimaryKeys(String catalog, String schema, String name) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getPrimaryKeys( catalog, schema, name );
+ }
+ }
+
+ public Iterator getTables(String catalog, String schema, String table) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getTables( catalog, schema, table );
+ }
+ }
+
+ public boolean needQuote(String name) {
+ return delegate.needQuote( name );
+ }
+
+ public void setDelegate(Object object) {
+ this.delegate = null;
+ }
+
+ public void setFailOnDelegateAccess(boolean b) {
+ failOnDelegateAccess = b;
+ }
+
+ public Iterator getSuggestedPrimaryKeyStrategyName(String catalog, String schema, String name) {
+ if(failOnDelegateAccess) {
+ throw new IllegalStateException("delegate not accessible");
+ } else {
+ return delegate.getSuggestedPrimaryKeyStrategyName(catalog, schema, name);
+ }
+ }
+
+ }
+
+ public void testCachedDialect() {
+ Settings buildSettings = cfg.buildSettings();
+
+ MetaDataDialect realMetaData = JDBCReaderFactory.newMetaDataDialect( buildSettings.getDialect(), cfg.getProperties() );
+
+ MockedMetaDataDialect mock = new MockedMetaDataDialect(realMetaData);
+ CachedMetaDataDialect dialect = new CachedMetaDataDialect(mock);
+
+ JDBCReader reader = JDBCReaderFactory.newJDBCReader( buildSettings, new DefaultReverseEngineeringStrategy(), dialect );
+
+ DatabaseCollector dc = new DefaultDatabaseCollector();
+ reader.readDatabaseSchema( dc, null, null );
+
+ validate( dc );
+
+ mock.setFailOnDelegateAccess(true);
+
+ reader = JDBCReaderFactory.newJDBCReader( buildSettings, new DefaultReverseEngineeringStrategy(), dialect );
+
+ dc = new DefaultDatabaseCollector();
+ reader.readDatabaseSchema( dc, null, null );
+
+ validate(dc);
+
+
+
+
+ }
+
+ private void validate(DatabaseCollector dc) {
+ Iterator iterator = dc.iterateTables();
+ Table firstChild = (Table) iterator.next();
+ assertTrue("CHILD".equals(firstChild.getName()) ||
+ "MASTER".equals(firstChild.getName()));
+ assertTrue(iterator.hasNext());
+
+ iterator = dc.iterateTables();
+ assertNotNull(iterator.next());
+ assertNotNull(iterator.next());
+ assertFalse(iterator.hasNext());
+
+
+ assertHasNext("should have recorded one foreignkey to child table", 1, firstChild.getForeignKeyIterator() );
+ }
+
+ protected String[] getCreateSQL() {
+
+ return new String[] {
+ "create table master ( id char not null, name varchar(20), primary key (id) )",
+ "create table child ( childid char not null, masterref char, primary key (childid), foreign key (masterref) references master(id) )",
+ };
+ }
+
+ protected String[] getDropSQL() {
+
+ return new String[] {
+ "drop table child",
+ "drop table master",
+ };
+ }
+
+}
More information about the hibernate-commits
mailing list