You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2012/11/29 21:33:53 UTC

svn commit: r1415362 - in /db/torque/torque4/trunk: torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java

Author: tfischer
Date: Thu Nov 29 20:33:52 2012
New Revision: 1415362

URL: http://svn.apache.org/viewvc?rev=1415362&view=rev
Log:
TORQUE-244 use prepared statements in executeStatement, add possibility to set replacement values

Modified:
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
    db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java?rev=1415362&r1=1415361&r2=1415362&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeer.java Thu Nov 29 20:33:52 2012
@@ -689,7 +689,10 @@ public abstract class BasePeer implement
     public static int executeStatement(String statementString, String dbName)
             throws TorqueException
     {
-        return getBasePeerImpl().executeStatement(statementString, dbName);
+        return getBasePeerImpl().executeStatement(
+                statementString,
+                dbName,
+                null);
     }
 
     /**
@@ -706,7 +709,7 @@ public abstract class BasePeer implement
     public static int executeStatement(String statementString, Connection con)
             throws TorqueException
     {
-        return getBasePeerImpl().executeStatement(statementString, con);
+        return getBasePeerImpl().executeStatement(statementString, con, null);
     }
 
     /**

Modified: db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java?rev=1415362&r1=1415361&r2=1415362&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java (original)
+++ db/torque/torque4/trunk/torque-runtime/src/main/java/org/apache/torque/util/BasePeerImpl.java Thu Nov 29 20:33:52 2012
@@ -2081,42 +2081,80 @@ public class BasePeerImpl<T> implements 
     }
 
     /**
-     * Utility method which executes a given sql statement.  This
-     * method should be used for update, insert, and delete
-     * statements.  Use executeQuery() for selects.
+     * Utility method which executes a given sql statement
+     * as prepared statement.
+     * This method should be used for update, insert, and delete statements.
+     * Use executeQuery() for selects.
      *
      * @param statementString A String with the sql statement to execute.
+     *
      * @return The number of rows affected.
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     *
+     * @throws TorqueException if executing the statement fails
+     *         or no database connection can be established.
      */
     public int executeStatement(String statementString) throws TorqueException
     {
-        return executeStatement(statementString, Torque.getDefaultDB());
+        return executeStatement(statementString, Torque.getDefaultDB(), null);
+    }
+
+    /**
+     * Utility method which executes a given sql statement
+     * as prepared statement.
+     * This method should be used for update, insert, and delete statements.
+     * Use executeQuery() for selects.
+     *
+     * @param statementString A String with the sql statement to execute.
+     * @param replacementValues values to use as placeholders in the query.
+     *        or null or empty if no placeholders need to be filled.
+     *
+     * @return The number of rows affected.
+     *
+     * @throws TorqueException if executing the statement fails
+     *         or no database connection can be established.
+     */
+    public int executeStatement(
+                String statementString,
+                List<JdbcTypedValue> replacementValues)
+            throws TorqueException
+    {
+        return executeStatement(
+                statementString,
+                Torque.getDefaultDB(),
+                replacementValues);
     }
 
     /**
-     * Utility method which executes a given sql statement.  This
-     * method should be used for update, insert, and delete
-     * statements.  Use executeQuery() for selects.
+     * Utility method which executes a given sql statement
+     * as prepared statement.
+     * This method should be used for update, insert, and delete statements.
+     * Use executeQuery() for selects.
      *
      * @param statementString A String with the sql statement to execute.
      * @param dbName The name of the database to execute the statement against,
      *        or null for the default DB.
+     * @param replacementValues values to use as placeholders in the query.
+     *        or null or empty if no placeholders need to be filled.
      *
      * @return The number of rows affected.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws TorqueException if executing the statement fails
+     *         or no database connection can be established.
      */
-    public int executeStatement(String statementString, String dbName)
+    public int executeStatement(
+            String statementString,
+            String dbName,
+            List<JdbcTypedValue> replacementValues)
         throws TorqueException
     {
         Connection con = null;
         try
         {
             con = Transaction.begin(dbName);
-            int rowCount = executeStatement(statementString, con);
+            int rowCount = executeStatement(
+                    statementString,
+                    con,
+                    replacementValues);
             Transaction.commit(con);
             con = null;
             return rowCount;
@@ -2131,29 +2169,53 @@ public class BasePeerImpl<T> implements 
     }
 
     /**
-     * Utility method which executes a given sql statement.  This
-     * method should be used for update, insert, and delete
-     * statements.  Use executeQuery() for selects.
+     * Utility method which executes a given sql statement
+     * as prepared statement.
+     * This method should be used for update, insert, and delete statements.
+     * Use executeQuery() for selects.
      *
      * @param statementString A String with the sql statement to execute.
      * @param con The database connection to use.
+     * @param replacementValues values to use as placeholders in the query.
+     *        or null or empty if no placeholders need to be filled.
      *
      * @return The number of rows affected.
      *
-     * @throws TorqueException Any exceptions caught during processing will be
-     *         rethrown wrapped into a TorqueException.
+     * @throws TorqueException if executing the statement fails.
      */
     public int executeStatement(
             String statementString,
-            Connection con)
+            Connection con,
+            List<JdbcTypedValue> replacementValues)
         throws TorqueException
     {
         int rowCount = -1;
-        Statement statement = null;
+        PreparedStatement statement = null;
         try
         {
-            statement = con.createStatement();
-            rowCount = statement.executeUpdate(statementString);
+            statement = con.prepareStatement(statementString);
+            if (replacementValues != null)
+            {
+                int position = 1;
+                for (JdbcTypedValue replacementValue : replacementValues)
+                {
+                    if (replacementValue.getValue() == null)
+                    {
+                        statement.setNull(
+                                position,
+                                replacementValue.getJdbcType());
+                    }
+                    else
+                    {
+                        statement.setObject(
+                                position,
+                                replacementValue.getValue(),
+                                replacementValue.getJdbcType());
+                    }
+                    ++position;
+                }
+            }
+            rowCount = statement.executeUpdate();
         }
         catch (SQLException e)
         {

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java?rev=1415362&r1=1415361&r2=1415362&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java (original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/util/BasePeerTest.java Thu Nov 29 20:33:52 2012
@@ -19,8 +19,12 @@ package org.apache.torque.util;
  * under the License.
  */
 
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.torque.BaseDatabaseTestCase;
-import org.apache.torque.test.peer.AuthorPeer;
+import org.apache.torque.test.dbobject.Author;
 import org.apache.torque.test.peer.BookPeer;
 
 /**
@@ -41,8 +45,6 @@ public class BasePeerTest extends BaseDa
         criteria.add(BookPeer.TITLE, "Book 1 - Author 1");
 
         // execute
-        BasePeerImpl basePeerImpl = new BasePeerImpl();
-        basePeerImpl.setTableMap(AuthorPeer.getTableMap());
         int deleted = BasePeer.doDelete(criteria);
 
         // verify
@@ -52,4 +54,35 @@ public class BasePeerTest extends BaseDa
         criteria.add(BookPeer.TITLE, "Book 1 - Author 1");
         assertEquals(0, countHelper.count(criteria));
     }
+
+    public void testExecuteStatement() throws Exception
+    {
+        // prepare
+        cleanBookstore();
+        List<Author> authors = insertBookstoreData();
+        String statement = "DELETE FROM " + BookPeer.TABLE_NAME
+                + " WHERE " + BookPeer.BOOK_ID + "=?"
+                + " AND "+ BookPeer.AUTHOR_ID + "=?";
+        List<JdbcTypedValue> replacements = new ArrayList<JdbcTypedValue>();
+        replacements.add(new JdbcTypedValue(
+                authors.get(2).getBooks().get(4).getBookId(),
+                Types.VARCHAR));
+        replacements.add(
+            new JdbcTypedValue(
+                    authors.get(2).getAuthorId(),
+                    Types.INTEGER));
+
+        // execute
+        BasePeerImpl<?> basePeerImpl = new BasePeerImpl<Object>();
+        int deleted = basePeerImpl.executeStatement(statement, replacements);
+
+        // verify
+        assertEquals(1, deleted);
+        assertEquals(99, countHelper.count(BookPeer.getTableMap()));
+        Criteria criteria = new Criteria();
+        criteria.and(
+                BookPeer.BOOK_ID,
+                authors.get(2).getBooks().get(4).getBookId());
+        assertEquals(0, countHelper.count(criteria));
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org