empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From franci...@apache.org
Subject svn commit: r1070244 - in /incubator/empire-db/trunk: ./ empire-db/ empire-db/src/main/java/org/apache/empire/db/ empire-db/src/main/java/org/apache/empire/db/derby/ empire-db/src/main/java/org/apache/empire/db/h2/ empire-db/src/main/java/org/apache/em...
Date Sun, 13 Feb 2011 14:58:36 GMT
Author: francisdb
Date: Sun Feb 13 14:58:36 2011
New Revision: 1070244

URL: http://svn.apache.org/viewvc?rev=1070244&view=rev
Log:
EMPIREDB-100
Support of long integer fields
Test code by Eric Cournarie

Added:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/DBResource.java
      - copied, changed from r1070234, incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/IntegerTest.java
Removed:
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
Modified:
    incubator/empire-db/trunk/empire-db/pom.xml
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/PreparedStatementTest.java
    incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
    incubator/empire-db/trunk/pom.xml

Modified: incubator/empire-db/trunk/empire-db/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/pom.xml?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/pom.xml (original)
+++ incubator/empire-db/trunk/empire-db/pom.xml Sun Feb 13 14:58:36 2011
@@ -37,15 +37,45 @@
 		</dependency>
 		<!-- TEST -->
 		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>log4j</groupId>
+			<artifactId>log4j</artifactId>
+			<version>1.2.16</version>
+			<scope>test</scope>
+		</dependency>
+		
+		<dependency>
 		    <groupId>hsqldb</groupId>
 		    <artifactId>hsqldb</artifactId>
 		    <scope>test</scope>
 		</dependency> 
 		<dependency>
+		    <groupId>com.h2database</groupId>
+		    <artifactId>h2</artifactId>
+		    <scope>test</scope>
+		</dependency>
+		<dependency>
+		    <groupId>org.apache.derby</groupId>
+		    <artifactId>derby</artifactId>
+		    <scope>test</scope>
+		</dependency>
+		<!-- <dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+			<version>5.1.14</version>
+			<scope>test</scope>
+		</dependency> -->
+		<dependency>
 		    <groupId>commons-io</groupId>
 		    <artifactId>commons-io</artifactId>
 		    <version>1.4</version>
 		    <scope>test</scope>
 		</dependency>
+		
+
 	</dependencies>
 </project>

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
Sun Feb 13 14:58:36 2011
@@ -23,9 +23,9 @@ import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Iterator;
 
+import org.apache.empire.commons.ErrorObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.apache.empire.commons.ErrorObject;
 
 
 /**
@@ -117,7 +117,7 @@ public class DBSQLScript extends ErrorOb
                 // SQLException
                 log.error(e.toString(), e);
                 if (ignoreErrors==false)
-                    return error(DBErrors.SQLException, e);
+                    return error(DBErrors.SQLException, e.getClass().getSimpleName() + ":
" + e.getMessage());
                 // continue
                 log.debug("Ignoring error. Continuing with script...");
             }

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/derby/DBDatabaseDriverDerby.java
Sun Feb 13 14:58:36 2011
@@ -18,6 +18,10 @@
  */
 package org.apache.empire.db.derby;
 
+import java.sql.Connection;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+
 import org.apache.empire.commons.Errors;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
@@ -38,10 +42,6 @@ import org.apache.empire.db.DBTableColum
 import org.apache.empire.db.DBView;
 import org.apache.empire.db.oracle.DBDatabaseDriverOracle.BooleanType;
 
-import java.sql.Connection;
-import java.util.GregorianCalendar;
-import java.util.Iterator;
-
 
 /**
  * This class provides support for the Derby database system.
@@ -250,7 +250,7 @@ public class DBDatabaseDriverDerby exten
            case DECIMAL:   return "CAST(? AS DECIMAL)";
            case DOUBLE:    return "CAST(? AS DECIMAL)";
            case DATE:      return "CAST(? AS DATE)";
-           case DATETIME:  return "CAST(? AS DATETIME)";
+           case DATETIME:  return "CAST(? AS TIMESTAMP)";
            // Convert to text
            case TEXT:
                 return "CAST(? AS CHAR)";
@@ -515,12 +515,6 @@ public class DBDatabaseDriverDerby exten
                 } else {
                     sql.append("INT");
                 }
-//                if (size>0)
-//                {   // Set Integer length
-//                    sql.append("(");
-//                    sql.append(String.valueOf(size));
-//                    sql.append(")");
-//                }
                 break;
             }
             case AUTOINC:

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/h2/DBDatabaseDriverH2.java
Sun Feb 13 14:58:36 2011
@@ -503,14 +503,12 @@ public class DBDatabaseDriverH2 extends 
         switch (c.getDataType())
         {
             case INTEGER:
-            { // Integer type
-                sql.append("INT");
-                int size = (int)c.getSize();
-                if (size>0)
-                {   // Set Integer length
-                    sql.append("(");
-                    sql.append(String.valueOf(size));
-                    sql.append(")");
+            {
+                int size = (int) c.getSize();
+                if (size >= 8) {
+                    sql.append("BIGINT");
+                } else {
+                    sql.append("INT");
                 }
                 break;
             }    

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/mysql/DBDatabaseDriverMySQL.java
Sun Feb 13 14:58:36 2011
@@ -600,14 +600,20 @@ public class DBDatabaseDriverMySQL exten
         switch (c.getDataType())
         {
             case INTEGER:
-            { // Integer type
-                sql.append("INT");
-                int size = (int)c.getSize();
-                if (size>0)
-                {   // Set Integer length
-                    sql.append("(");
-                    sql.append(String.valueOf(size));
-                    sql.append(")");
+            {
+                int size = (int) c.getSize();
+                if (size >= 8) {
+                    sql.append("BIGINT");
+                } 
+                else 
+                {
+                    sql.append("INT");
+                    if (size > 0)
+                    {   // Set Integer length
+                        sql.append("(");
+                        sql.append(String.valueOf(size));
+                        sql.append(")");
+                    }
                 }
                 break;
             }    

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/postgresql/DBDatabaseDriverPostgreSQL.java
Sun Feb 13 14:58:36 2011
@@ -18,8 +18,11 @@
  */
 package org.apache.empire.db.postgresql;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+
 import org.apache.empire.commons.Errors;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.data.DataType;
@@ -38,11 +41,8 @@ import org.apache.empire.db.DBSQLScript;
 import org.apache.empire.db.DBTable;
 import org.apache.empire.db.DBTableColumn;
 import org.apache.empire.db.DBView;
-
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.GregorianCalendar;
-import java.util.Iterator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 /**
@@ -656,7 +656,7 @@ public class DBDatabaseDriverPostgreSQL 
         switch (c.getDataType())
         {
             case INTEGER:
-            { // Integer type
+            {
                 int size = (int) c.getSize();
                 if (size >= 8) {
                     sql.append("BIGINT");

Copied: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/DBResource.java
(from r1070234, incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java)
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/DBResource.java?p2=incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/DBResource.java&p1=incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java&r1=1070234&r2=1070244&rev=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/HsqldbResource.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/DBResource.java Sun
Feb 13 14:58:36 2011
@@ -18,24 +18,44 @@
  */
 package org.apache.empire;
 
-import java.io.File;
-import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.sql.Statement;
 
-import org.apache.commons.io.FileUtils;
+import org.apache.empire.db.DBDatabaseDriver;
 import org.apache.empire.db.DBTools;
+import org.apache.empire.db.derby.DBDatabaseDriverDerby;
+import org.apache.empire.db.h2.DBDatabaseDriverH2;
+import org.apache.empire.db.hsql.DBDatabaseDriverHSql;
 import org.junit.rules.ExternalResource;
 
-public class HsqldbResource extends ExternalResource
+public class DBResource extends ExternalResource
 {
-
-    private static final String PATH = "target/hsqldb-unit-test/";
+    private final DB db;
     
     public Connection connection;
     
+    public DBResource(final DB db)
+    {
+        this.db = db;
+    }
+    
+    public DBDatabaseDriver newDriver(){
+        try
+        {
+            return db.driver.newInstance();
+        } 
+        catch (InstantiationException e)
+        {
+            throw new RuntimeException(e);
+        } 
+        catch (IllegalAccessException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+    
     public Connection getConnection()
     {
         return connection;
@@ -44,25 +64,24 @@ public class HsqldbResource extends Exte
     @Override
     protected void before()
         throws Throwable
-    {
-        // clean up possible previous test files
-        FileUtils.deleteDirectory(new File(PATH));
-        
-        Class.forName("org.hsqldb.jdbcDriver");
-        connection = DriverManager.getConnection("jdbc:hsqldb:" + PATH, "sa", "");
+    {   
+        Class.forName(db.jdbcClass);
+        connection = DriverManager.getConnection(db.jdbcURL, "sa", "");
     }
     
     @Override
     protected void after()
     {
-        try
-        {
-            Statement st = connection.createStatement();
-            // properly shutdown hsqldb
-            st.execute("SHUTDOWN");
-        } catch (SQLException ex)
-        {
-            ex.printStackTrace();
+        if(db == DB.HSQL){
+            try
+            {
+                Statement st = connection.createStatement();
+                // properly shutdown hsqldb
+                st.execute("SHUTDOWN");
+            } catch (SQLException ex)
+            {
+                ex.printStackTrace();
+            }
         }
         try
         {
@@ -71,12 +90,31 @@ public class HsqldbResource extends Exte
         {
             e.printStackTrace();
         }
-        try
-        {
-            FileUtils.deleteDirectory(new File(PATH));
-        } catch (IOException e)
+    }
+    
+    public enum DB{
+        HSQL(
+             "org.hsqldb.jdbcDriver", 
+             "jdbc:hsqldb:mem:data/derby/test",
+             DBDatabaseDriverHSql.class),
+        DERBY(
+              "org.apache.derby.jdbc.EmbeddedDriver", 
+              "jdbc:derby:memory:data/derby/test;create=true",
+              DBDatabaseDriverDerby.class),
+        H2(
+           "org.h2.Driver", 
+           "jdbc:h2:mem:data/h2/test",
+           DBDatabaseDriverH2.class);
+        
+        private final String jdbcClass;
+        private final String jdbcURL;
+        private final Class<? extends DBDatabaseDriver> driver;
+        
+        private DB(final String jdbcClass, final String jdbcURL, final Class<? extends
DBDatabaseDriver> driver)
         {
-            e.printStackTrace();
+            this.driver = driver;
+            this.jdbcClass = jdbcClass;
+            this.jdbcURL = jdbcURL;
         }
     }
 }

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/EmpireExceptionTest.java
Sun Feb 13 14:58:36 2011
@@ -18,7 +18,8 @@
  */
 package org.apache.empire;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 
 import org.apache.empire.commons.ErrorInfo;
 import org.apache.empire.commons.ErrorObject;
@@ -68,6 +69,7 @@ public class EmpireExceptionTest
 	@Before
 	public void setupException()
 	{
+	    ErrorObject.setExceptionsEnabled(false);
 		this.errorObject = new MockErrorObject();
 		this.exception = new EmpireException(errorObject);
 	}

Added: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/IntegerTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/IntegerTest.java?rev=1070244&view=auto
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/IntegerTest.java
(added)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/IntegerTest.java
Sun Feb 13 14:58:36 2011
@@ -0,0 +1,247 @@
+package org.apache.empire.db;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.empire.commons.ErrorObject;
+import org.apache.empire.data.DataMode;
+import org.apache.empire.data.DataType;
+import org.apache.empire.db.derby.DBDatabaseDriverDerby;
+import org.apache.empire.db.h2.DBDatabaseDriverH2;
+import org.apache.empire.db.hsql.DBDatabaseDriverHSql;
+import org.apache.empire.db.mysql.DBDatabaseDriverMySQL;
+import org.apache.empire.db.oracle.DBDatabaseDriverOracle;
+import org.apache.empire.db.postgresql.DBDatabaseDriverPostgreSQL;
+import org.apache.empire.db.sqlserver.DBDatabaseDriverMSSQL;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Support of long integer fields
+ * https://issues.apache.org/jira/browse/EMPIREDB-100
+ */
+public class IntegerTest {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(IntegerTest.class);
+
+    @Before
+    public void enableExceptions(){
+        ErrorObject.setExceptionsEnabled(true);
+    }
+    
+    @Test
+    public void testHsqldb() {
+        SampleConfig config = new SampleConfig();
+        config.databaseProvider = "hsqldb";
+        config.jdbcClass = "org.hsqldb.jdbcDriver";
+        config.jdbcURL = "jdbc:hsqldb:mem:data/derby/test";
+        testLongInteger(config);
+    }
+    
+    @Test
+    public void testDerby() {
+        SampleConfig config = new SampleConfig();
+        config.databaseProvider = "derby";
+        config.jdbcClass = "org.apache.derby.jdbc.EmbeddedDriver";
+        config.jdbcURL = "jdbc:derby:memory:data/derby/test;create=true";
+        testLongInteger(config);
+    }
+    
+    @Test
+    public void testH2() {
+        SampleConfig config = new SampleConfig();
+        config.databaseProvider = "h2";
+        config.jdbcClass = "org.h2.Driver";
+        config.jdbcURL = "jdbc:h2:mem:data/h2/test";
+        testLongInteger(config);
+    }
+    
+    @Test
+    @Ignore("This can only run when we have a local mydsl server set up")
+    public void testMySql()
+    {
+        SampleConfig config = new SampleConfig();
+        config.databaseProvider = "mysql";
+        config.jdbcClass = "com.mysql.jdbc.Driver";
+        config.jdbcURL = "jdbc:mysql://localhost:3306/test";
+        config.jdbcUser = "test";
+        config.jdbcPwd = "test";
+        config.schemaName = "test";
+        testLongInteger(config);
+    }
+
+    private void testLongInteger(SampleConfig config) {
+        SampleDB database = new SampleDB();
+
+        Connection connection = getJDBCConnection(config);
+
+        DBDatabaseDriver driver = getDatabaseDriver(config, connection);
+
+        database.open(driver, connection);
+
+        createDatabase(database, driver, connection);
+
+        DBRecord maxRec = new DBRecord();
+        maxRec.create(database.SAMPLE);
+        maxRec.setValue(database.SAMPLE.MY_INTEGER, Integer.MAX_VALUE);
+        maxRec.setValue(database.SAMPLE.MY_LONG, Long.MAX_VALUE);
+        maxRec.setValue(database.SAMPLE.MY_SHORT, Short.MAX_VALUE);
+        maxRec.update(connection);
+        
+        DBRecord minRec = new DBRecord();
+        minRec.create(database.SAMPLE);
+        minRec.setValue(database.SAMPLE.MY_INTEGER, Integer.MIN_VALUE);
+        minRec.setValue(database.SAMPLE.MY_LONG, Long.MIN_VALUE);
+        minRec.setValue(database.SAMPLE.MY_SHORT, Short.MIN_VALUE);
+        minRec.update(connection);
+    }
+
+    private static Connection getJDBCConnection(SampleConfig config) {
+        Connection conn = null;
+        LOGGER.info("Connecting to Database'" + config.jdbcURL + "'");
+
+        try{
+            // Connect to the database
+            Class.forName(config.jdbcClass).newInstance();
+            conn = DriverManager.getConnection(config.jdbcURL, config.jdbcUser, config.jdbcPwd);
+            LOGGER.info("Connected successfully");
+            conn.setAutoCommit(false);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException(e);
+        } 
+            
+        return conn;
+    }
+
+    private DBDatabaseDriver getDatabaseDriver(SampleConfig config, Connection conn) {
+        if (config.databaseProvider.equalsIgnoreCase("mysql")) {
+            DBDatabaseDriverMySQL driver = new DBDatabaseDriverMySQL();
+            driver.setDatabaseName(config.schemaName);
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("oracle")) {
+            DBDatabaseDriverOracle driver = new DBDatabaseDriverOracle();
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("sqlserver")) {
+            DBDatabaseDriverMSSQL driver = new DBDatabaseDriverMSSQL();
+            driver.setDatabaseName(config.schemaName);
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("hsqldb")) {
+            DBDatabaseDriverHSql driver = new DBDatabaseDriverHSql();
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("postgresql")) {
+            DBDatabaseDriverPostgreSQL driver = new DBDatabaseDriverPostgreSQL();
+            driver.setDatabaseName(config.schemaName);
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("h2")) {
+            DBDatabaseDriverH2 driver = new DBDatabaseDriverH2();
+            driver.setDatabaseName(config.schemaName);
+            return driver;
+        }
+        if (config.databaseProvider.equalsIgnoreCase("derby")) {
+            DBDatabaseDriverDerby driver = new DBDatabaseDriverDerby();
+            driver.setDatabaseName(config.schemaName);
+            return driver;
+        }
+
+        throw new RuntimeException("Unknown Database Provider " + config.databaseProvider);
+    }
+
+    private void createDatabase(DBDatabase db, DBDatabaseDriver driver, Connection conn)
{
+        // try to remove previously existing tables
+        List<String> tables = getTableNames(db, conn);
+        DBSQLScript script2 = new DBSQLScript();
+        for(DBTable table:db.getTables()){
+            if(tables.contains(table.getName())){
+                db.getDriver().getDDLScript(DBCmdType.DROP, table, script2);
+            }
+        }
+        script2.run(driver, conn, false);
+        // Commit
+        db.commit(conn);
+        
+        // create DDL for Database Definition
+        DBSQLScript script = new DBSQLScript();
+        db.getCreateDDLScript(driver, script);
+        
+        // Show DDL Statement
+        LOGGER.info(script.toString());
+        // Execute Script
+        script.run(driver, conn, false);
+        // Commit
+        db.commit(conn);
+    }
+
+    private List<String> getTableNames(DBDatabase db, Connection conn)
+    {
+        List<String> tables = new ArrayList<String>();
+        ResultSet result = null;
+        try
+        {
+            DatabaseMetaData metaData = conn.getMetaData();
+            result = metaData.getTables(db.getSchema(), null, null, null);
+            while(result.next()) {
+                tables.add(result.getString("TABLE_NAME"));
+            }
+        } 
+        catch (SQLException e)
+        {
+            e.printStackTrace();
+        }finally{
+            if (result != null){
+                try
+                {
+                    result.close();
+                } 
+                catch (SQLException e)
+                {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return tables;
+    }
+
+    public static class SampleDB extends DBDatabase {
+
+        public final SampleTable SAMPLE = new SampleTable(this);
+
+        public class SampleTable extends DBTable {
+
+            public final DBTableColumn MY_INTEGER;
+            public final DBTableColumn MY_LONG;
+            public final DBTableColumn MY_SHORT;
+
+            public SampleTable(DBDatabase db) {
+                super("SAMPLE", db);
+                MY_INTEGER = addColumn("MY_INTEGER", DataType.INTEGER, 0, DataMode.NotNull);
+                MY_LONG = addColumn("MY_LONG", DataType.INTEGER, 8, DataMode.NotNull);
+                MY_SHORT = addColumn("MY_SHORT", DataType.INTEGER, 2, DataMode.NotNull);
+            }
+        }
+    }
+
+    public static class SampleConfig {
+
+        public String databaseProvider;
+        public String jdbcClass;
+        public String jdbcURL;
+        public String jdbcUser;
+        public String jdbcPwd;
+        public String schemaName;
+    }
+}

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/PreparedStatementTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/PreparedStatementTest.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/PreparedStatementTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/PreparedStatementTest.java
Sun Feb 13 14:58:36 2011
@@ -24,9 +24,10 @@ import static org.junit.Assert.assertTru
 
 import java.sql.Connection;
 
-import org.apache.empire.HsqldbResource;
+import org.apache.empire.DBResource;
+import org.apache.empire.DBResource.DB;
+import org.apache.empire.commons.ErrorObject;
 import org.apache.empire.db.DBCommand.DBCommandParam;
-import org.apache.empire.db.hsql.DBDatabaseDriverHSql;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -34,14 +35,16 @@ import org.junit.Test;
 public class PreparedStatementTest{
  
     @Rule
-    public HsqldbResource hsqldb = new HsqldbResource();
+    public DBResource dbResource = new DBResource(DB.HSQL);
     
     @Test
     public void testPreparedStatement()
     {
-        Connection conn = hsqldb.getConnection();
+        ErrorObject.setExceptionsEnabled(true);
         
-        DBDatabaseDriver driver = new DBDatabaseDriverHSql();
+        Connection conn = dbResource.getConnection();
+        
+        DBDatabaseDriver driver = dbResource.newDriver();
         CompanyDB db = new CompanyDB();
         db.open(driver, conn);
         DBSQLScript script = new DBSQLScript();
@@ -51,7 +54,7 @@ public class PreparedStatementTest{
         DBRecord department = new DBRecord();
         department.create(db.DEPARTMENT);
         department.setValue(db.DEPARTMENT.NAME, "junit");
-        department.setValue(db.DEPARTMENT.BUSINESS_UNIT, "testers");
+        department.setValue(db.DEPARTMENT.BUSINESS_UNIT, "test");
         department.update(conn);
 
         long id = department.getInt(db.DEPARTMENT.ID);

Modified: incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/test/java/org/apache/empire/db/hsql/DBDatabaseDriverHSqlTest.java
Sun Feb 13 14:58:36 2011
@@ -25,7 +25,9 @@ import static org.junit.Assert.assertTru
 import java.sql.Connection;
 import java.util.Date;
 
-import org.apache.empire.HsqldbResource;
+import org.apache.empire.DBResource;
+import org.apache.empire.DBResource.DB;
+import org.apache.empire.commons.ErrorObject;
 import org.apache.empire.db.CompanyDB;
 import org.apache.empire.db.DBCmdType;
 import org.apache.empire.db.DBDatabaseDriver;
@@ -38,24 +40,26 @@ import org.junit.Test;
 public class DBDatabaseDriverHSqlTest{
  
     @Rule
-    public HsqldbResource hsqldb = new HsqldbResource();
+    public DBResource dbResource = new DBResource(DB.HSQL);
     
     @Test
     public void test()
     {
-        Connection conn = hsqldb.getConnection();
+        ErrorObject.setExceptionsEnabled(true);
+        
+        Connection conn = dbResource.getConnection();
      
-        DBDatabaseDriver driver = new DBDatabaseDriverHSql();
+        DBDatabaseDriver driver = dbResource.newDriver();
         CompanyDB db = new CompanyDB();
-        db.open(driver, hsqldb.getConnection());
+        db.open(driver, dbResource.getConnection());
         DBSQLScript script = new DBSQLScript();
         db.getCreateDDLScript(db.getDriver(), script);
-        script.run(db.getDriver(), hsqldb.getConnection(), false);
+        script.run(db.getDriver(), dbResource.getConnection(), false);
         
         DBRecord dep = new DBRecord();
         dep.create(db.DEPARTMENT);
         dep.setValue(db.DEPARTMENT.NAME, "junit");
-        dep.setValue(db.DEPARTMENT.BUSINESS_UNIT, "testers");
+        dep.setValue(db.DEPARTMENT.BUSINESS_UNIT, "test");
         dep.update(conn);
         
         Date date = dep.getDateTime(db.DEPARTMENT.UPDATE_TIMESTAMP);

Modified: incubator/empire-db/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/pom.xml?rev=1070244&r1=1070243&r2=1070244&view=diff
==============================================================================
--- incubator/empire-db/trunk/pom.xml (original)
+++ incubator/empire-db/trunk/pom.xml Sun Feb 13 14:58:36 2011
@@ -225,6 +225,11 @@
 				<groupId>org.slf4j</groupId>
 				<artifactId>slf4j-log4j12</artifactId>
 				<version>1.6.1</version>
+			</dependency>			
+			<dependency>
+				<groupId>org.slf4j</groupId>
+				<artifactId>slf4j-simple</artifactId>
+				<version>1.6.1</version>
 			</dependency>
 			<!-- commons -->			
 			<dependency>
@@ -232,11 +237,21 @@
 				<artifactId>commons-beanutils</artifactId>
 				<version>1.7.0</version>
 			</dependency>
-			<!-- hsqldb -->			
+			<!-- databases -->			
+			<dependency>
+			    <groupId>hsqldb</groupId>
+			    <artifactId>hsqldb</artifactId>
+			    <version>1.8.0.10</version>
+			</dependency> 
+			<dependency>
+			    <groupId>com.h2database</groupId>
+			    <artifactId>h2</artifactId>
+			    <version>1.3.148</version>
+			</dependency>
 			<dependency>
-				<groupId>hsqldb</groupId>
-				<artifactId>hsqldb</artifactId>
-				<version>1.8.0.10</version>
+			    <groupId>org.apache.derby</groupId>
+			    <artifactId>derby</artifactId>
+			    <version>10.7.1.1</version>
 			</dependency>
 			<!-- struts2 -->			
 			<dependency>



Mime
View raw message