[jbpm-commits] JBoss JBPM SVN: r5855 - in jbpm4/trunk/qa/cleandb: src/org/jboss/qa and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Fri Oct 30 12:05:54 EDT 2009
Author: tom.baeyens at jboss.com
Date: 2009-10-30 12:05:54 -0400 (Fri, 30 Oct 2009)
New Revision: 5855
Added:
jbpm4/trunk/qa/cleandb/.classpath
jbpm4/trunk/qa/cleandb/.project
Modified:
jbpm4/trunk/qa/cleandb/build.xml
jbpm4/trunk/qa/cleandb/pom.xml
jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java
Log:
cleandb improvements
Added: jbpm4/trunk/qa/cleandb/.classpath
===================================================================
--- jbpm4/trunk/qa/cleandb/.classpath (rev 0)
+++ jbpm4/trunk/qa/cleandb/.classpath 2009-10-30 16:05:54 UTC (rev 5855)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" output="bin" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="lib/ant.jar"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
Property changes on: jbpm4/trunk/qa/cleandb/.classpath
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: jbpm4/trunk/qa/cleandb/.project
===================================================================
--- jbpm4/trunk/qa/cleandb/.project (rev 0)
+++ jbpm4/trunk/qa/cleandb/.project 2009-10-30 16:05:54 UTC (rev 5855)
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>jbpm-qa-cleandb</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
Property changes on: jbpm4/trunk/qa/cleandb/.project
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: jbpm4/trunk/qa/cleandb/build.xml
===================================================================
--- jbpm4/trunk/qa/cleandb/build.xml 2009-10-30 09:30:01 UTC (rev 5854)
+++ jbpm4/trunk/qa/cleandb/build.xml 2009-10-30 16:05:54 UTC (rev 5855)
@@ -20,7 +20,7 @@
<arg line="dependency:copy" />
</exec>
<path id="classpath.cleandb">
- <fileset dir="lib" includes="*jar"/>
+ <fileset dir="lib" includes="*jar" excludes="ant.jar" />
<pathelement path="bin"/>
</path>
<taskdef name="cleandb" classname="org.jboss.qa.CleanDatabase" classpathref="classpath.cleandb" />
Modified: jbpm4/trunk/qa/cleandb/pom.xml
===================================================================
--- jbpm4/trunk/qa/cleandb/pom.xml 2009-10-30 09:30:01 UTC (rev 5854)
+++ jbpm4/trunk/qa/cleandb/pom.xml 2009-10-30 16:05:54 UTC (rev 5855)
@@ -54,6 +54,13 @@
<outputDirectory>lib</outputDirectory>
<destFileName>mysql-connector-java.jar</destFileName>
</artifactItem>
+ <artifactItem>
+ <groupId>org.apache.ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.7.0</version>
+ <outputDirectory>lib</outputDirectory>
+ <destFileName>ant.jar</destFileName>
+ </artifactItem>
</artifactItems>
</configuration>
</plugin>
Modified: jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java
===================================================================
--- jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java 2009-10-30 09:30:01 UTC (rev 5854)
+++ jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java 2009-10-30 16:05:54 UTC (rev 5855)
@@ -1,9 +1,12 @@
package org.jboss.qa;
+
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
@@ -11,189 +14,216 @@
import org.apache.tools.ant.Task;
public class CleanDatabase extends Task {
-
- private String url;
- private String driver;
- private String username;
- private String password;
- private String schema;
- private String dropTableString = null;
- private String dropForeignKeyString = null;
- private boolean dropConstraintsFirst = true;
-
- public void execute() throws BuildException {
- Connection con = null;
- // schema is optional, but let's make it null if it is empty
- if ("".equals(getSchema()))
- setSchema(null);
+
+ static String ORACLE = "oracle";
+ static String MYSQL = "mysql";
+ static String POSTGRESQL = "postgtresql";
+ static String DB2 = "db2";
+ static String HSQLDB = "hsqldb";
+ static String OTHER = "other";
- try {
- Class.forName(getDriver());
- } catch (java.lang.ClassNotFoundException e) {
- throw new BuildException("Driver not found: " + e.getMessage(), e);
- }
-
- try {
- String[] types = {"TABLE"};
- con = DriverManager.getConnection(getUrl(), getUsername(), getPassword());
+ private String url;
+ private String driver;
+ private String username;
+ private String password;
+ private String schema;
+
+ private String dbType;
+ private String quote;
+ private String dropTableString = null;
+ private String dropForeignKeyString = null;
+ private boolean dropConstraintsFirst = true;
- adjustStringsForDatabase(con.getMetaData().getIdentifierQuoteString());
+ public void execute() throws BuildException {
- ResultSet tables = con.getMetaData().getTables(null, getSchema(), null, types);
-
- // DROP CONSTRAINTS
- if (isDropConstraintsFirst()) {
-
- while (tables.next()) {
- String table = tables.getString("TABLE_NAME");
+ Connection connection = null;
+ try {
+ connection = getConnection();
+ initialize(connection);
- String schema = getSchema();
- if (null == schema && tables.getString("TABLE_SCHEM") != null)
- schema = tables.getString("TABLE_SCHEM");
-
- ResultSet fksrs = con.getMetaData().getImportedKeys(null, schema, table);
+ List<String> tableNames = getTableNames(connection);
- // we are interested in the FK's only, not in the column they represent...
- // the above method returns one record for each column which is part of a FK
- // eg:
- // FK1 represents CUST_ID and ORDR_NUM from EMBD_KM2O_ORDR table
- // it returns two rows, but we want to drop the constraint (one record).
- // So, we use another list, to get rid of "duplicates"
- Set<String> fks = new HashSet<String>();
- while (fksrs.next()) {
- fks.add(fksrs.getString("FK_NAME"));
- }
-
- for (String fkname : fks) {
- if (fkname != null) {
- try {
- String stmt = getDropForeignKeyString()
- .replaceAll("@SCHEMA at .", schema == null ? "" : Matcher.quoteReplacement(schema)+".")
- .replaceAll("@TABLE@", Matcher.quoteReplacement(table))
- .replaceAll("@FK@", Matcher.quoteReplacement(fkname));
- log("Issuing drop foreign key command:" + stmt);
- con.createStatement().executeUpdate(stmt);
- } catch(Exception e) {
- throw new BuildException("Something went wrong on dropping the FK "+fkname+" from table " + table + ": " + e.getMessage(), e);
- }
- }
- }
- }
- }
+ if (dbType==MYSQL) {
+ for (String tableName : tableNames) {
+ Set<String> foreignKeyNames = getForeignKeyNames(connection, tableName);
+ for (String foreignKeyName : foreignKeyNames) {
+ String stmt = dropForeignKeyString
+ .replaceAll("@TABLE@", Matcher.quoteReplacement(tableName))
+ .replaceAll("@FK@", Matcher.quoteReplacement(foreignKeyName));
+ log("dropping foreign key: " + stmt);
+ connection.createStatement().executeUpdate(stmt);
+ }
+ }
+ }
- // DROP TABLE -- get a fresh result, as some drivers seems to get crazy on .first or .beforeFirst...
- tables = con.getMetaData().getTables(null, getSchema(), null, types);
- while (tables.next()) {
- String table = tables.getString("TABLE_NAME");
- try {
- String stmt = getDropTableString()
- .replaceAll("@SCHEMA at .", schema == null ? "" : Matcher.quoteReplacement(schema)+".")
- .replaceAll("@TABLE@", Matcher.quoteReplacement(table));
- log("Issuing drop table command:" + stmt);
- con.createStatement().executeUpdate(stmt);
- } catch(Exception e) {
- throw new BuildException("Something went wrong on dropping table " + table + ": " + e.getMessage(), e);
- }
- }
+ for (String tableName: tableNames) {
+ String stmt = dropTableString
+ .replaceAll("@TABLE@", Matcher.quoteReplacement(tableName));
+ log("dropping table: " + stmt);
+ connection.createStatement().executeUpdate(stmt);
+ }
- } catch (Exception e) {
- throw new BuildException(e);
- } finally {
- if (con != null) {
- try {
- if (!con.isClosed()) con.close();
- } catch (SQLException e) {}
- }
- }
- }
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new BuildException(e);
- private void adjustStringsForDatabase(String quoteChar) {
- setDropTableString("DROP TABLE "+quoteChar+"@TABLE@"+quoteChar);
- setDropForeignKeyString("ALTER TABLE "+quoteChar+"@TABLE@"+quoteChar+" DROP CONSTRAINT "+quoteChar+"@FK@"+quoteChar);
+ } finally {
+ if (connection != null) {
+ try {
+ if (!connection.isClosed()) {
+ connection.close();
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
- if (getUrl().startsWith("jdbc:mysql")) {
- setDropForeignKeyString("ALTER TABLE "+quoteChar+"@TABLE@"+quoteChar+" DROP FOREIGN KEY "+quoteChar+"@FK@"+quoteChar);
- setDropConstraintsFirst(true);
- }
+ private List<String> getTableNames(Connection connection) throws Exception {
+ List<String> tableNames = new ArrayList<String>();
+
+ String[] types = { "TABLE" };
+ ResultSet resultSet = connection.getMetaData().getTables(null, schema, null, types);
+ while (resultSet.next()) {
+ String tableName = resultSet.getString("TABLE_NAME");
+ if ( tableName.startsWith("JBPM")
+ || tableName.startsWith("jbpm")
+ ) {
+ log("adding table "+tableName);
+ tableNames.add(tableName);
+ } else {
+ log("--- skipping table "+tableName+" ---");
+ }
+ }
+
+ return tableNames;
+ }
- if (getUrl().startsWith("jdbc:postgresql")) {
- setDropTableString(getDropTableString() + " CASCADE");
- setDropConstraintsFirst(false);
- }
+ private Set<String> getForeignKeyNames(Connection connection, String table) throws Exception {
+ ResultSet resultSet = connection.getMetaData().getImportedKeys(null, schema, table);
+ // we are interested in the FK's only, not in the column they
+ // represent...
+ // the above method returns one record for each column which is part
+ // of a FK
+ // eg:
+ // FK1 represents CUST_ID and ORDR_NUM from EMBD_KM2O_ORDR table
+ // it returns two rows, but we want to drop the constraint (one
+ // record).
+ // So, we use another list, to get rid of "duplicates"
+ Set<String> foreignKeyNames = new HashSet<String>();
+ while (resultSet.next()) {
+ String foreignKeyName = resultSet.getString("FK_NAME");
+ if (foreignKeyName != null) {
+ foreignKeyNames.add(foreignKeyName);
+ }
+ }
+ return foreignKeyNames;
+ }
- if (getUrl().startsWith("jdbc:oracle")) {
- setDropTableString(getDropTableString() + " CASCADE CONSTRAINTS");
- setDropConstraintsFirst(false);
- }
+ private Connection getConnection() throws Exception {
+ // initialize the JDBC driver
+ log("initializing JDBC driver "+getDriver());
+ Class.forName(getDriver());
+
+ // create the connection
+ log("creating JDBC connection "+getUrl());
+ return DriverManager.getConnection(getUrl(), getUsername(), getPassword());
+ }
- if (getUrl().startsWith("jdbc:db2")) {
- // db2 drops the constraints by default, when dropping a table
- setDropConstraintsFirst(false);
- }
- }
+ private void initialize(Connection connection) throws Exception {
+ quote = connection.getMetaData().getIdentifierQuoteString();
- public String getUrl() {
- return url;
- }
+ // schema is optional, but let's make it null if it is empty
+ if ("".equals(schema)) {
+ schema = null;
+ }
- public void setUrl(String url) {
- this.url = url;
- }
+ if (getUrl().startsWith("jdbc:mysql")) {
+ dbType = MYSQL;
+ dropForeignKeyString = "ALTER TABLE " + quote + "@TABLE@" + quote + " DROP FOREIGN KEY " + quote + "@FK@" + quote;
+
+ } else if (getUrl().startsWith("jdbc:postgresql")) {
+ dbType = POSTGRESQL;
+ dropTableString = getDropTableString() + " CASCADE";
+
+ } else if (getUrl().startsWith("jdbc:oracle")) {
+ dbType = ORACLE;
+ dropTableString = "DROP TABLE "+(schema!=null ? quote+schema+quote+"." : "")+quote+"@TABLE@"+quote+" CASCADE CONSTRAINTS";
+
+ } else if (getUrl().startsWith("jdbc:db2")) {
+ dbType = DB2;
+ // db2 drops the constraints by default, when dropping a table
+
+ } else {
+ dbType = OTHER;
+ dropTableString = "DROP TABLE " + (schema!=null ? quote+schema+"." : "") + quote + "@TABLE@" + quote;
+ dropForeignKeyString = "ALTER TABLE " + quote + "@TABLE@" + quote + " DROP CONSTRAINT " + quote + "@FK@" + quote;
+ }
+ }
- public String getDriver() {
- return driver;
- }
+ public String getUrl() {
+ return url;
+ }
- public void setDriver(String driver) {
- this.driver = driver;
- }
+ public void setUrl(String url) {
+ this.url = url;
+ }
- public String getUsername() {
- return username;
- }
+ public String getDriver() {
+ return driver;
+ }
- public void setUsername(String username) {
- this.username = username;
- }
+ public void setDriver(String driver) {
+ this.driver = driver;
+ }
- public String getPassword() {
- return password;
- }
+ public String getUsername() {
+ return username;
+ }
- public void setPassword(String password) {
- this.password = password;
- }
+ public void setUsername(String username) {
+ this.username = username;
+ }
- public String getSchema() {
- return schema;
- }
+ public String getPassword() {
+ return password;
+ }
- public void setSchema(String schema) {
- this.schema = schema;
- }
+ public void setPassword(String password) {
+ this.password = password;
+ }
- public String getDropTableString() {
- return dropTableString;
- }
+ public String getSchema() {
+ return schema;
+ }
- public void setDropTableString(String dropTableString) {
- this.dropTableString = dropTableString;
- }
+ public void setSchema(String schema) {
+ this.schema = schema;
+ }
- public String getDropForeignKeyString() {
- return dropForeignKeyString;
- }
+ public String getDropTableString() {
+ return dropTableString;
+ }
- public void setDropForeignKeyString(String dropForeignKeyString) {
- this.dropForeignKeyString = dropForeignKeyString;
- }
-
- public boolean isDropConstraintsFirst() {
- return dropConstraintsFirst;
- }
+ public void setDropTableString(String dropTableString) {
+ this.dropTableString = dropTableString;
+ }
- public void setDropConstraintsFirst(boolean dropConstraintsFirst) {
- this.dropConstraintsFirst = dropConstraintsFirst;
- }
+ public String getDropForeignKeyString() {
+ return dropForeignKeyString;
+ }
+ public void setDropForeignKeyString(String dropForeignKeyString) {
+ this.dropForeignKeyString = dropForeignKeyString;
+ }
+
+ public boolean isDropConstraintsFirst() {
+ return dropConstraintsFirst;
+ }
+
+ public void setDropConstraintsFirst(boolean dropConstraintsFirst) {
+ this.dropConstraintsFirst = dropConstraintsFirst;
+ }
+
}
\ No newline at end of file
More information about the jbpm-commits
mailing list