empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject empire-db git commit: EMPIREDB-221
Date Wed, 05 Aug 2015 10:44:08 GMT
Repository: empire-db
Updated Branches:
  refs/heads/master fd54d22df -> ba1d8264c


EMPIREDB-221

Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo
Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/ba1d8264
Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/ba1d8264
Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/ba1d8264

Branch: refs/heads/master
Commit: ba1d8264ceb0bd5d8c7c069f8330302c8d1f390f
Parents: fd54d22
Author: Rainer Döbele <doebele@apache.org>
Authored: Wed Aug 5 12:44:03 2015 +0200
Committer: Rainer Döbele <doebele@apache.org>
Committed: Wed Aug 5 12:44:03 2015 +0200

----------------------------------------------------------------------
 .../samples/db/advanced/SampleAdvApp.java       |  45 ++-
 .../org/apache/empire/db/DBDatabaseDriver.java  |  85 ++++-
 .../java/org/apache/empire/db/DBSQLScript.java  | 357 ++++++++++++++++---
 .../db/sqlite/DBDatabaseDriverSQLite.java       |  45 ---
 4 files changed, 408 insertions(+), 124 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/empire-db/blob/ba1d8264/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/SampleAdvApp.java
----------------------------------------------------------------------
diff --git a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/SampleAdvApp.java
b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/SampleAdvApp.java
index b59e74b..1100e23 100644
--- a/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/SampleAdvApp.java
+++ b/empire-db-examples/empire-db-example-advanced/src/main/java/org/apache/empire/samples/db/advanced/SampleAdvApp.java
@@ -130,16 +130,19 @@ public class SampleAdvApp
             int idEmp2 = insertEmployee(conn, "Fred", "Bloggs", "M");
             int idEmp3 = insertEmployee(conn, "Emma", "White", "F");
             
-            insertEmpDepHistory(conn, idEmp1,  idDevDep,  DateUtils.getDate(2007, 11,  1));
           
-            insertEmpDepHistory(conn, idEmp1,  idProdDep, DateUtils.getDate(2008,  8,  1));
          
-            insertEmpDepHistory(conn, idEmp1,  idSalDep,  DateUtils.getDate(2009,  4, 15));
          
-
-            insertEmpDepHistory(conn, idEmp2,  idSalDep,  DateUtils.getDate(2006,  2,  1));
           
-            insertEmpDepHistory(conn, idEmp2,  idDevDep,  DateUtils.getDate(2008, 10, 15));
          
-
-            insertEmpDepHistory(conn, idEmp3,  idDevDep,  DateUtils.getDate(2006,  8, 15));
           
-            insertEmpDepHistory(conn, idEmp3,  idSalDep,  DateUtils.getDate(2007,  7,  1));
          
-            insertEmpDepHistory(conn, idEmp3,  idProdDep, DateUtils.getDate(2008,  6, 15));
          
+            // Insert History as batch
+            DBSQLScript batch = new DBSQLScript();
+            insertEmpDepHistory(batch, idEmp1,  idDevDep,  DateUtils.getDate(2007, 11,  1));
           
+            insertEmpDepHistory(batch, idEmp1,  idProdDep, DateUtils.getDate(2008,  8,  1));
          
+            insertEmpDepHistory(batch, idEmp1,  idSalDep,  DateUtils.getDate(2009,  4, 15));
          
+
+            insertEmpDepHistory(batch, idEmp2,  idSalDep,  DateUtils.getDate(2006,  2,  1));
           
+            insertEmpDepHistory(batch, idEmp2,  idDevDep,  DateUtils.getDate(2008, 10, 15));
+            
+            insertEmpDepHistory(batch, idEmp3,  idDevDep,  DateUtils.getDate(2006,  8, 15));
           
+            insertEmpDepHistory(batch, idEmp3,  idSalDep,  DateUtils.getDate(2007,  7,  1));
          
+            insertEmpDepHistory(batch, idEmp3,  idProdDep, DateUtils.getDate(2008,  6, 15));
+            batch.executeBatch(db.getDriver(), conn);
             
             // commit
             db.commit(conn);
@@ -183,7 +186,7 @@ public class SampleAdvApp
             	System.out.println("*** drop EMPLOYEE_INFO_VIEW ***");
             	DBSQLScript script = new DBSQLScript();
             	db.getDriver().getDDLScript(DBCmdType.DROP, db.V_EMPLOYEE_INFO, script);
-            	script.run(db.getDriver(), conn, false);
+            	script.executeAll(db.getDriver(), conn);
             }
             ddlSample(conn, idEmp2);
             if (db.getDriver() instanceof DBDatabaseDriverH2) {
@@ -191,7 +194,7 @@ public class SampleAdvApp
             	System.out.println("*** create EMPLOYEE_INFO_VIEW ***");
             	DBSQLScript script = new DBSQLScript();
             	db.getDriver().getDDLScript(DBCmdType.CREATE, db.V_EMPLOYEE_INFO, script);
-            	script.run(db.getDriver(), conn, false);
+            	script.executeAll(db.getDriver(), conn);
             }
 
             // STEP 13: delete records
@@ -303,7 +306,7 @@ public class SampleAdvApp
         // Show DLL Statement
         System.out.println(script.toString());
         // Execute Script
-        script.run(driver, conn, false);
+        script.executeAll(driver, conn);
         // Commit
         db.commit(conn);
     }
@@ -364,15 +367,23 @@ public class SampleAdvApp
      * Inserts an Employee into the Employees table.
      * </PRE>
      */
-    private static void insertEmpDepHistory(Connection conn, int employeeId, int departmentId,
Date dateFrom)
+    private static void insertEmpDepHistory(DBSQLScript script, int employeeId, int departmentId,
Date dateFrom)
     {
         // Insert an Employee
+    	/*
         DBRecord rec = new DBRecord();
         rec.create(T_EDH);
         rec.setValue(T_EDH.C_EMPLOYEE_ID, employeeId);
         rec.setValue(T_EDH.C_DEPARTMENT_ID, departmentId);
         rec.setValue(T_EDH.C_DATE_FROM, dateFrom);
         rec.update(conn);
+        */
+        DBCommand cmd = db.createCommand();
+    	cmd.set(T_EDH.C_EMPLOYEE_ID.to(employeeId));
+    	cmd.set(T_EDH.C_DEPARTMENT_ID.to(departmentId));
+    	cmd.set(T_EDH.C_DATE_FROM.to(dateFrom));
+    	// Add to script for batch execution
+    	script.addInsert(cmd);
     }
 
     /* This procedure demonstrates the use of command parameter for prepared statements */
@@ -537,7 +548,7 @@ public class SampleAdvApp
         System.out.println("Creating new column named FOO as varchar(20) for the EMPLOYEES
table:");
         DBSQLScript script = new DBSQLScript();
         db.getDriver().getDDLScript(DBCmdType.CREATE, C_FOO, script);
-        script.run(db.getDriver(), conn, false);
+        script.executeAll(db.getDriver(), conn);
         
         // Now load a record from that table and set the value for foo
         System.out.println("Changing the value for the FOO field of a particular employee:");
@@ -551,7 +562,7 @@ public class SampleAdvApp
         C_FOO.setSize(40); 
         script.clear();
         db.getDriver().getDDLScript(DBCmdType.ALTER, C_FOO, script);
-        script.run(db.getDriver(), conn, false);
+        script.executeAll(db.getDriver(), conn);
 
         // Now set a longer value for the record
         System.out.println("Changing the value for the FOO field for the above employee to
a longer string:");
@@ -562,7 +573,7 @@ public class SampleAdvApp
         System.out.println("Dropping the FOO column from the employee table:");
         script.clear();
         db.getDriver().getDDLScript(DBCmdType.DROP, C_FOO, script);
-        script.run(db.getDriver(), conn, false);
+        script.executeAll(db.getDriver(), conn);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/empire-db/blob/ba1d8264/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java b/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
index 6d5e4d1..0995c55 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
@@ -397,14 +397,14 @@ public abstract class DBDatabaseDriver implements Serializable
      * @param pstmt the prepared statement
      * @param sqlParams list of objects
      */
-    protected void prepareStatement(PreparedStatement pstmt, Object[] sqlParams, Connection
conn)
+    protected void prepareStatement(PreparedStatement pstmt, Object[] sqlParams) 
     	throws SQLException
 	{
         for (int i=0; i<sqlParams.length; i++)
         {
             Object value = sqlParams[i];
             try {
-                addStatementParam(pstmt, i+1, value, conn);
+                addStatementParam(pstmt, i+1, value); // , conn
             } catch(SQLException e) {
                 log.error("SQLException: Unable to set prepared statement parameter {} to
'{}'", i+1, StringUtils.toString(value));
                 throw e;
@@ -418,9 +418,8 @@ public abstract class DBDatabaseDriver implements Serializable
      * @param pstmt the prepared statement
      * @param paramIndex the parameter index
      * @param value the parameter value
-     * @param conn the connection
      */
-    protected void addStatementParam(PreparedStatement pstmt, int paramIndex, Object value,
Connection conn)
+    protected void addStatementParam(PreparedStatement pstmt, int paramIndex, Object value)
 		throws SQLException
 	{
         if (value instanceof DBBlobData)
@@ -544,7 +543,7 @@ public abstract class DBDatabaseDriver implements Serializable
                     ? conn.prepareStatement(sqlCmd, Statement.RETURN_GENERATED_KEYS)
                     : conn.prepareStatement(sqlCmd);
     	        stmt = pstmt;
-	            prepareStatement(pstmt, sqlParams, conn);
+	            prepareStatement(pstmt, sqlParams); 
 	            count = pstmt.executeUpdate(); 
             }
             else
@@ -575,6 +574,80 @@ public abstract class DBDatabaseDriver implements Serializable
         }
     }
 
+    /**
+     * Executes a list of sql statements as batch
+     * @param sqlCmd
+     * @param sqlCmdParams
+     * @param conn
+     * @return
+     * @throws SQLException
+     */
+    public int[] executeBatch(String[] sqlCmd, Object[][] sqlCmdParams, Connection conn)
+        throws SQLException
+    {   // Execute the Statement
+        if (sqlCmdParams!=null)
+        {   // Use a prepared statement
+        	PreparedStatement pstmt = null;
+	        try
+	        {
+            	int pos=0;
+            	String lastCmd = null;
+            	int[] result = new int[sqlCmd.length];
+            	for (int i=0; i<=sqlCmd.length; i++)
+            	{	// get cmd
+            		String cmd = (i<sqlCmd.length ? sqlCmd[i] : null);
+            		if (StringUtils.compareEqual(cmd, lastCmd, true)==false)
+            		{	// close last statement
+            			if (pstmt!=null)
+            			{	// execute and close
+			        		log.debug("Executing batch containing {} statements", i-pos);
+            				int[] res = pstmt.executeBatch();
+            				for (int j=0; j<res.length; j++)
+            					result[pos+j]=res[j];
+            				pos+=res.length;
+            				close(pstmt);
+            				pstmt = null;
+            			}
+            			// has next?
+	        			if (cmd==null)
+	        				break;
+	        			// new statement
+	        			log.debug("Creating prepared statement for batch: "+cmd);
+            			pstmt = conn.prepareStatement(cmd);
+            			lastCmd = cmd;
+            		}
+            		// add batch
+            		if (sqlCmdParams[i]!=null)
+            		{	
+            			prepareStatement(pstmt, sqlCmdParams[i]); 
+            		}	
+        			log.debug("Adding batch with {} params.", (sqlCmdParams[i]!=null ? sqlCmdParams[i].length
: 0));
+            		pstmt.addBatch();
+            	}
+	            return result; 
+            } finally {
+	            close(pstmt);
+	        }
+        }
+        else
+        {   // Execute a simple statement
+            Statement stmt = conn.createStatement();
+            try {
+            	for (int i=0; i<sqlCmd.length; i++)
+            	{
+            		String cmd = sqlCmd[i];
+            		log.debug("Adding statement to batch: "+cmd);
+            		stmt.addBatch(cmd);
+            	}
+        		log.debug("Executing batch containing {} statements", sqlCmd.length);
+	            int result[] = stmt.executeBatch();
+	            return result;
+            } finally {
+	            close(stmt);
+	        }
+        }
+    }
+    
     // executeQuery
     public ResultSet executeQuery(String sqlCmd, Object[] sqlParams, boolean scrollable,
Connection conn)
         throws SQLException
@@ -589,7 +662,7 @@ public abstract class DBDatabaseDriver implements Serializable
 	        {	// Use prepared statement
 	            PreparedStatement pstmt = conn.prepareStatement(sqlCmd, type, ResultSet.CONCUR_READ_ONLY);
 	            stmt = pstmt;
-	            prepareStatement(pstmt, sqlParams, conn);
+	            prepareStatement(pstmt, sqlParams); 
 	            return pstmt.executeQuery();
 	        } else
 	        {	// Use simple statement

http://git-wip-us.apache.org/repos/asf/empire-db/blob/ba1d8264/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java b/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
index 1083066..2565a6b 100644
--- a/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
+++ b/empire-db/src/main/java/org/apache/empire/db/DBSQLScript.java
@@ -28,12 +28,11 @@ import org.apache.empire.exceptions.InvalidArgumentException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 /**
  * DBSQLScript<br>
  * This class is a collection of sql command strings.<br>
- * The class is used for obtaining and executing DDL commands supplied
- * by the database driver (@see {@link DBDatabaseDriver#getDDLScript(DBCmdType, DBObject,
DBSQLScript)}) 
+ * The class is used for obtaining and executing DDL commands supplied by the
+ * database driver (@see {@link DBDatabaseDriver#getDDLScript(DBCmdType, DBObject, DBSQLScript)})
  */
 public class DBSQLScript implements Iterable<String>
 {
@@ -41,33 +40,111 @@ public class DBSQLScript implements Iterable<String>
     private static final Logger log = LoggerFactory.getLogger(DBSQLScript.class);
     private static final String DEFAULT_COMMAND_SEPARATOR = ";\r\n\r\n";
 
+    /**
+     * SQLCmd
+     * @author doebele
+     */
+    protected static class SQLStmt
+    {
+        private String   cmd;
+        private Object[] params;
+
+        public SQLStmt(String cmd, Object[] params)
+        {
+            this.cmd = cmd;
+            this.params = params;
+        }
+
+        public String getCmd()
+        {
+            return cmd;
+        }
+
+        public void setCmd(String cmd)
+        {
+            this.cmd = cmd;
+        }
+
+        public Object[] getParams()
+        {
+            return params;
+        }
+
+        public void setParams(Object[] params)
+        {
+            this.params = params;
+        }
+    }
+
+    /**
+     * SQLCmdIterator
+     * @author doebele
+     */
+    private static class SQLStmtIterator implements Iterator<String>
+    {
+        private final Iterator<SQLStmt> iterator;
+
+        private SQLStmtIterator(Iterator<SQLStmt> iterator)
+        {
+            this.iterator = iterator;
+        }
+
+        public boolean hasNext()
+        {
+            return iterator.hasNext();
+        }
+
+        public String next()
+        {
+            return iterator.next().getCmd();
+        }
+
+        public void remove()
+        {
+            iterator.remove();
+        }
+    }
+
     // Properties
-    protected String commandSeparator = DEFAULT_COMMAND_SEPARATOR; 
+    protected String commandSeparator = DEFAULT_COMMAND_SEPARATOR;
+
+    protected ArrayList<SQLStmt> sqlStmtList      = new ArrayList<SQLStmt>();
 
-    protected ArrayList<String> sqlCmdList = new ArrayList<String>();
-    
     public DBSQLScript()
     {
         // nothing
     }
-    
+
     public DBSQLScript(String commandSeparator)
     {
         this.commandSeparator = commandSeparator;
     }
-    
+
     /**
      * Adds a statement to the script.
+     * 
      * @param sql the statement
      */
     public void addStmt(String sql)
     {
-        sqlCmdList.add(sql);
+        sqlStmtList.add(new SQLStmt(sql, null));
     }
-    
+
+    /**
+     * Adds a statement to the script.
+     * 
+     * @param sql the statement
+     * @param params the statement parameters
+     */
+    public void addStmt(String sql, Object[] params)
+    {
+        sqlStmtList.add(new SQLStmt(sql, params));
+    }
+
     /**
      * Adds a statement to the script.<br>
      * The supplied StringBuilder will be reset to a length of 0
+     * 
      * @param sql the statement
      */
     public final void addStmt(StringBuilder sql)
@@ -76,115 +153,283 @@ public class DBSQLScript implements Iterable<String>
         // Clear Builder
         sql.setLength(0);
     }
-    
+
+    /**
+     * Adds an insert statement 
+     * @param cmd the insert command
+     */
+    public void addInsert(DBCommand cmd)
+    {
+        if (cmd == null)
+            throw new InvalidArgumentException("cmd", cmd);
+        addStmt(cmd.getInsert(), cmd.getParamValues());
+    }
+
+    /**
+     * Adds an update statement 
+     * @param cmd the insert command
+     */
+    public void addUpdate(DBCommand cmd)
+    {
+        if (cmd == null)
+            throw new InvalidArgumentException("cmd", cmd);
+        addStmt(cmd.getUpdate(), cmd.getParamValues());
+    }
+
+    /**
+     * Adds an delete statement 
+     * @param cmd the insert command
+     */
+    public void addDelete(DBCommand cmd, DBTable table)
+    {
+        if (cmd == null)
+            throw new InvalidArgumentException("cmd", cmd);
+        addStmt(cmd.getDelete(table), cmd.getParamValues());
+    }
+
     /**
      * Returns the number of statements in this script
+     * 
      * @return number of statements in this script
      */
     public int getCount()
     {
-        return sqlCmdList.size();
+        return sqlStmtList.size();
     }
-    
+
     /**
-     * Returns the statement at the given index
+     * Returns the statement command at the given index
      * @param i index of the statement to retrieve
-     * @return the sql statement
+     * @return the sql statement command
      */
     public String getStmt(int i)
     {
-        if (i<0 || i>=sqlCmdList.size())
+        if (i < 0 || i >= sqlStmtList.size())
             throw new InvalidArgumentException("index", i);
-        // return statement
-        return sqlCmdList.get(i);
+        // return statement command
+        return sqlStmtList.get(i).getCmd();
     }
-    
+
     /**
-     * Replaces an entry in the list
-     * @param i index of the statement to replace
-     * @param stmt the new statement for this index, or NULL to remove the statement
+     * Returns the statement command at the given index
+     * @param i index of the statement to retrieve
+     * @return the sql statement params
      */
-    public void setStmt(int i, String stmt)
+    public Object[] getStmtParams(int i)
     {
-        if (i<0 || i>=sqlCmdList.size())
+        if (i < 0 || i >= sqlStmtList.size())
             throw new InvalidArgumentException("index", i);
-        // replace or remove statement
-        if (stmt==null)
-            sqlCmdList.remove(i);
-        else
-            sqlCmdList.set(i, stmt);
+        // return statement params
+        return sqlStmtList.get(i).getParams();
     }
-    
+
     /**
      * Inserts an entry in the list
+     * 
      * @param i index of the statement to replace
      * @param stmt the new statement for this index, or NULL to remove the statement
      */
-    public void insertStmt(int i, String stmt)
+    public void insertStmt(int i, String stmt, Object[] params)
     {
-        if (stmt==null)
+        if (stmt == null)
             throw new InvalidArgumentException("stmt", stmt);
-        if (i<0 || i>sqlCmdList.size())
+        if (i < 0 || i > sqlStmtList.size())
             throw new InvalidArgumentException("index", i);
+        // insert statement
+        sqlStmtList.add(i, new SQLStmt(stmt, params));
+    }
+
+    /**
+     * Inserts an entry in the list
+     * 
+     * @param i index of the statement to replace
+     * @param stmt the new statement for this index, or NULL to remove the statement
+     */
+    public final void insertStmt(int i, String stmt)
+    {
         // replace or remove statement
-        sqlCmdList.add(i, stmt);
+        insertStmt(i, stmt, null);
     }
-    
+
+    /**
+     * Replaces an entry in the list
+     * 
+     * @param i index of the statement to replace
+     * @param cmd the new statement for this index, or NULL to remove the statement
+     * @param params the command params (optional)
+     */
+    public void replaceStmt(int i, String cmd, Object[] params)
+    {
+        if (cmd == null)
+            throw new InvalidArgumentException("cmd", cmd);
+        if (i < 0 || i >= sqlStmtList.size())
+            throw new InvalidArgumentException("index", i);
+        // replace statement
+        SQLStmt stmt = sqlStmtList.get(i);
+        stmt.setCmd(cmd);
+        stmt.setParams(params);
+    }
+
+    /**
+     * Replaces an entry in the list
+     * 
+     * @param i index of the statement to replace
+     * @param cmd the new statement for this index, or NULL to remove the statement
+     */
+    public final void replaceStmt(int i, String cmd)
+    {
+        // replace
+        replaceStmt(i, cmd, null);
+    }
+
+    /**
+     * Removes a statement from the list
+     * @param i index of the statement to replace
+     */
+    public void removeStmt(int i)
+    {
+        // check index
+        if (i < 0 || i >= sqlStmtList.size())
+            throw new InvalidArgumentException("index", i);
+        // remove statement
+        sqlStmtList.remove(i);
+    }
+
     /**
      * Clears the script by removing all statements
      */
     public void clear()
     {
-        sqlCmdList.clear();
+        sqlStmtList.clear();
     }
-    
+
     /**
-     * Runs all SQL Statements using the supplied driver and connection.
+     * Executes all SQL Statements one by one using the supplied driver and connection.
+     * 
      * @param driver the driver used for statement execution
      * @param conn the connection
      * @param ignoreErrors true if errors should be ignored
+     * @return number of records affected
      */
-    public void run(DBDatabaseDriver driver, Connection conn, boolean ignoreErrors)
+    public int executeAll(DBDatabaseDriver driver, Connection conn, boolean ignoreErrors)
     {
         log.debug("Running script containing " + String.valueOf(getCount()) + " statements.");
-        for(String stmt : sqlCmdList)
+        int result = 0;
+        for (SQLStmt stmt : sqlStmtList)
         {
-            try {
+            try
+            {
                 // Execute Statement
-                log.debug("Executing: " + stmt);
-                driver.executeSQL(stmt, null, conn, null);
-            } catch(SQLException e) {
+                log.debug("Executing: {}", stmt.getCmd());
+                int count = driver.executeSQL(stmt.getCmd(), stmt.getParams(), conn, null);
+                result += (count >= 0 ? count : 0);
+            }
+            catch (SQLException e)
+            {
                 // SQLException
                 log.error(e.toString(), e);
-                if (ignoreErrors==false)
-                {   // forward exception
+                if (ignoreErrors == false)
+                { // forward exception
                     throw new EmpireSQLException(driver, e);
-                }    
+                }
                 // continue
                 log.debug("Ignoring error. Continuing with script...");
             }
         }
-        log.debug("Script completed.");
+        log.debug("Script completed. {} records affected.", result);
+        return result;
+    }
+
+    /**
+     * Executes all SQL Statements one by one using the supplied driver and connection.
+     * 
+     * @param driver the driver used for statement execution
+     * @param conn the connection
+     * @return number of records affected
+     */
+    public final int executeAll(DBDatabaseDriver driver, Connection conn)
+    {
+        return executeAll(driver, conn, false);
+    }
+
+    /**
+     * Executes all SQL Statements as a JDBC Batch Job.
+     * 
+     * @param driver the driver used for statement execution
+     * @param conn the connection
+     * @param ignoreErrors true if errors should be ignored
+     */
+    public int executeBatch(DBDatabaseDriver driver, Connection conn)
+    {
+        log.debug("Running batch containing " + String.valueOf(getCount()) + " statements.");
+        try
+        {
+            // Execute Statement
+            int count = sqlStmtList.size();
+            String[] cmdList = new String[count];
+            Object[][] paramList = null;
+            int i = 0;
+            for (SQLStmt stmt : sqlStmtList)
+            {
+                cmdList[i] = stmt.getCmd();
+                // set params
+                if (stmt.getParams() != null)
+                {
+                    if (paramList == null)
+                        paramList = new Object[count][];
+                    paramList[i] = stmt.getParams();
+                }
+                i++;
+            }
+            // Execute batch
+            int[] res = driver.executeBatch(cmdList, paramList, conn);
+            for (count = 0, i = 0; i < (res != null ? res.length : 0); i++)
+                count += (res[i] >= 0 ? res[i] : 0);
+            log.debug("Script completed. {} records affected.", count);
+            return count;
+        }
+        catch (SQLException e)
+        {
+            // SQLException
+            log.error(e.toString(), e);
+            throw new EmpireSQLException(driver, e);
+        }
     }
-    
+
+    /**
+     * Executes all statements one by one. Replaced by executeAll()
+     * 
+     * @param driver the driver used for statement execution
+     * @param conn the connection
+     * @param ignoreErrors true if errors should be ignored
+     * @return number of records affected
+     */
+    @Deprecated
+    public final int run(DBDatabaseDriver driver, Connection conn, boolean ignoreErrors)
+    {
+        return executeAll(driver, conn, ignoreErrors);
+    }
+
     /**
      * Runs all SQL Statements using the supplied driver and connection.
+     * 
      * @param driver the driver used for statement execution
      * @param conn the connection
      */
-    public void run(DBDatabaseDriver driver, Connection conn)
+    @Deprecated
+    public final void run(DBDatabaseDriver driver, Connection conn)
     {
-        run(driver, conn, false);
+        executeAll(driver, conn, false);
     }
-    
+
     /**
      * Returns an iterator
      */
     public Iterator<String> iterator()
     {
-        return sqlCmdList.iterator();
+        return new SQLStmtIterator(sqlStmtList.iterator());
     }
-    
+
     /**
      * Returns the sql script as a string
      */
@@ -192,9 +437,9 @@ public class DBSQLScript implements Iterable<String>
     public String toString()
     {
         StringBuilder script = new StringBuilder();
-        for(String stmt : sqlCmdList)
+        for (SQLStmt stmt : sqlStmtList)
         {
-            script.append(stmt);
+            script.append(stmt.getCmd());
             script.append(commandSeparator);
         }
         return script.toString();

http://git-wip-us.apache.org/repos/asf/empire-db/blob/ba1d8264/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java
----------------------------------------------------------------------
diff --git a/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java
b/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java
index 066e89d..842942a 100644
--- a/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java
+++ b/empire-db/src/main/java/org/apache/empire/db/sqlite/DBDatabaseDriverSQLite.java
@@ -19,10 +19,8 @@
 package org.apache.empire.db.sqlite;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -122,49 +120,6 @@ public class DBDatabaseDriverSQLite extends DBDatabaseDriver
         }
     }
     
-    @Override
-    public int executeSQL(String sqlCmd, Object[] sqlParams, Connection conn, DBSetGenKeys
genKeys) throws SQLException
-    {
-        Statement stmt = null;
-        int count = 0;
-        try
-        {
-            if (sqlParams != null)
-            { // Use a prepared statement
-                PreparedStatement pstmt = conn.prepareStatement(sqlCmd);
-                stmt = pstmt;
-                prepareStatement(pstmt, sqlParams, conn);
-                count = pstmt.executeUpdate();
-            }
-            else
-            { // Execute a simple statement
-                stmt = conn.createStatement();
-                count = stmt.executeUpdate(sqlCmd);
-            }
-            // Retrieve any auto-generated keys
-            if (genKeys != null && count > 0)
-            { // Return Keys
-                ResultSet rs = stmt.getGeneratedKeys();
-                try
-                {
-                    while (rs.next())
-                    {
-                        genKeys.set(rs.getObject(1));
-                    }
-                }
-                finally
-                {
-                    rs.close();
-                }
-            }
-        }
-        finally
-        {
-            close(stmt);
-        }
-        return count;
-    }
-    
     private void setReservedKeywords()
     {
         // list of reserved keywords


Mime
View raw message