[jbpm-commits] JBoss JBPM SVN: r5853 - in jbpm4/trunk/qa: cleandb and 4 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Oct 30 05:29:12 EDT 2009


Author: tom.baeyens at jboss.com
Date: 2009-10-30 05:29:12 -0400 (Fri, 30 Oct 2009)
New Revision: 5853

Added:
   jbpm4/trunk/qa/cleandb/
   jbpm4/trunk/qa/cleandb/build.xml
   jbpm4/trunk/qa/cleandb/pom.xml
   jbpm4/trunk/qa/cleandb/src/
   jbpm4/trunk/qa/cleandb/src/org/
   jbpm4/trunk/qa/cleandb/src/org/jboss/
   jbpm4/trunk/qa/cleandb/src/org/jboss/qa/
   jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java
Log:
adding cleandb tool

Added: jbpm4/trunk/qa/cleandb/build.xml
===================================================================
--- jbpm4/trunk/qa/cleandb/build.xml	                        (rev 0)
+++ jbpm4/trunk/qa/cleandb/build.xml	2009-10-30 09:29:12 UTC (rev 5853)
@@ -0,0 +1,41 @@
+<project default="clean">
+
+	<!-- checking database property -->
+	<fail message="property database must be specified" unless="database"/>
+	
+  <!-- loading jdbc properties -->
+	<echo message="loading properties from ${user.home}/.jbpm4/jdbc/${database}.properties" />
+	<property file="../jdbc/${database}.properties" />
+	
+	<!-- default value for jdbc.schema -->
+  <property name="jdbc.schema" value="" />
+	
+	<target name="compile">
+		<mkdir dir="bin"/>
+		<javac srcdir="src" destdir="bin"/>
+	</target>
+	
+	<target name="clean" depends="compile">
+    <exec executable="mvn.bat" os="Windows Vista, Windows XP,Windows 2000,Windows 98">
+      <arg line="dependency:copy" />
+    </exec>
+    <path id="classpath.cleandb">
+      <fileset dir="lib" includes="*jar"/>
+      <pathelement path="bin"/>
+    </path>
+		<taskdef name="cleandb" classname="org.jboss.qa.CleanDatabase" classpathref="classpath.cleandb" />
+		<echo>
+			url="${jdbc.url}" 
+			username="${jdbc.username}" 
+			password="${jdbc.password}" 
+			schema="${jdbc.schema}" 
+			driver="${jdbc.driver}" 
+		</echo>
+		<cleandb 
+			url="${jdbc.url}" 
+			username="${jdbc.username}" 
+			password="${jdbc.password}" 
+			schema="${jdbc.schema}" 
+			driver="${jdbc.driver}" />
+	</target>
+</project>


Property changes on: jbpm4/trunk/qa/cleandb/build.xml
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: jbpm4/trunk/qa/cleandb/pom.xml
===================================================================
--- jbpm4/trunk/qa/cleandb/pom.xml	                        (rev 0)
+++ jbpm4/trunk/qa/cleandb/pom.xml	2009-10-30 09:29:12 UTC (rev 5853)
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- ====================================================================== -->
+<!--                                                                        -->
+<!--  JBoss, the OpenSource J2EE webOS                                      -->
+<!--                                                                        -->
+<!--  Distributable under LGPL license.                                     -->
+<!--  See terms of license at http://www.gnu.org.                           -->
+<!--                                                                        -->
+<!-- ====================================================================== -->
+
+<!-- $Id: pom.xml 5246 2009-07-06 11:07:48Z tom.baeyens at jboss.com $ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <name>jBPM 4 - Fetch old distro</name>
+  <groupId>org.jbpm.jbpm4</groupId>
+  <artifactId>jbpm-fetch-old-distro</artifactId>
+  <packaging>jar</packaging>
+  <version>4.3-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <configuration>
+          <artifactItems>
+            <artifactItem>
+              <groupId>hsqldb</groupId>
+              <artifactId>hsqldb</artifactId>
+              <version>1.8.0.7</version>
+              <outputDirectory>lib</outputDirectory>
+              <destFileName>hsqldb.jar</destFileName>
+            </artifactItem>
+            <artifactItem>
+              <groupId>com.oracle</groupId>
+              <artifactId>ojdbc14</artifactId>
+              <version>10.2.0.4</version>
+              <outputDirectory>lib</outputDirectory>
+              <destFileName>ojdbc14.jar</destFileName>
+            </artifactItem>
+            <artifactItem>
+		          <groupId>mysql</groupId>
+		          <artifactId>mysql-connector-java</artifactId>
+		          <version>5.1.6</version>
+              <outputDirectory>lib</outputDirectory>
+              <destFileName>mysql-connector-java.jar</destFileName>
+            </artifactItem>
+            <artifactItem>
+		          <groupId>postgresql</groupId>
+		          <artifactId>postgresql</artifactId>
+              <version>8.3-603.jdbc3</version>
+              <outputDirectory>lib</outputDirectory>
+              <destFileName>mysql-connector-java.jar</destFileName>
+            </artifactItem>
+          </artifactItems>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  
+  <!-- Repositories -->
+  <repositories>
+    <repository>
+      <id>repository.jboss.org</id>
+      <url>http://repository.jboss.com/maven2</url>
+      <snapshots>
+        <enabled>false</enabled>
+      </snapshots>
+    </repository>
+    <repository>
+      <id>qa.jboss.com</id>
+      <url>http://www.qa.jboss.com/jdbc-drivers/maven2</url>
+    </repository>
+  </repositories>
+
+</project>


Property changes on: jbpm4/trunk/qa/cleandb/pom.xml
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java
===================================================================
--- jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java	                        (rev 0)
+++ jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java	2009-10-30 09:29:12 UTC (rev 5853)
@@ -0,0 +1,199 @@
+package org.jboss.qa;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Matcher;
+
+import org.apache.tools.ant.BuildException;
+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);
+
+		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());
+
+			adjustStringsForDatabase(con.getMetaData().getIdentifierQuoteString());
+
+			ResultSet tables = con.getMetaData().getTables(null, getSchema(), null, types);
+			
+			// DROP CONSTRAINTS
+			if (isDropConstraintsFirst()) {
+				
+				while (tables.next()) {
+					String table = tables.getString("TABLE_NAME");
+
+					String schema = getSchema();
+					if (null == schema && tables.getString("TABLE_SCHEM") != null)
+						schema = tables.getString("TABLE_SCHEM");
+					
+					ResultSet fksrs = con.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> 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);
+							}
+						}
+					}
+				}
+			}
+
+			// 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);
+				}
+			}
+
+		} catch (Exception e) {
+			throw new BuildException(e);
+		} finally {
+			if (con != null) {
+				try {
+					if (!con.isClosed()) con.close();
+				} catch (SQLException e) {}
+			}
+		}
+	}
+
+	private void adjustStringsForDatabase(String quoteChar) {
+		setDropTableString("DROP TABLE "+quoteChar+"@TABLE@"+quoteChar);
+		setDropForeignKeyString("ALTER TABLE "+quoteChar+"@TABLE@"+quoteChar+" DROP CONSTRAINT "+quoteChar+"@FK@"+quoteChar);
+
+		if (getUrl().startsWith("jdbc:mysql")) {
+			setDropForeignKeyString("ALTER TABLE "+quoteChar+"@TABLE@"+quoteChar+" DROP FOREIGN KEY "+quoteChar+"@FK@"+quoteChar);
+			setDropConstraintsFirst(true);
+		}
+
+		if (getUrl().startsWith("jdbc:postgresql")) {
+			setDropTableString(getDropTableString() + " CASCADE");
+			setDropConstraintsFirst(false);
+		}
+
+		if (getUrl().startsWith("jdbc:oracle")) {
+			setDropTableString(getDropTableString() + " CASCADE CONSTRAINTS");
+			setDropConstraintsFirst(false);
+		}
+
+		if (getUrl().startsWith("jdbc:db2")) {
+			// db2 drops the constraints by default, when dropping a table
+			setDropConstraintsFirst(false);
+		}
+	}
+
+	public String getUrl() {
+		return url;
+	}
+
+	public void setUrl(String url) {
+		this.url = url;
+	}
+
+	public String getDriver() {
+		return driver;
+	}
+
+	public void setDriver(String driver) {
+		this.driver = driver;
+	}
+
+	public String getUsername() {
+		return username;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+
+	public String getSchema() {
+		return schema;
+	}
+
+	public void setSchema(String schema) {
+		this.schema = schema;
+	}
+
+	public String getDropTableString() {
+		return dropTableString;
+	}
+
+	public void setDropTableString(String dropTableString) {
+		this.dropTableString = dropTableString;
+	}
+
+	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


Property changes on: jbpm4/trunk/qa/cleandb/src/org/jboss/qa/CleanDatabase.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain



More information about the jbpm-commits mailing list