empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1241546 - in /incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBDatabase.java DBDatabaseDriver.java
Date Tue, 07 Feb 2012 18:25:12 GMT
Author: doebele
Date: Tue Feb  7 18:25:12 2012
New Revision: 1241546

URL: http://svn.apache.org/viewvc?rev=1241546&view=rev
Log:
EMPIREDB-126
new overloads for preparedStatements

Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java?rev=1241546&r1=1241545&r2=1241546&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java
Tue Feb  7 18:25:12 2012
@@ -668,13 +668,13 @@ public abstract class DBDatabase extends
      * Returns the value of the first row/column of a sql-query as an object.
      * 
      * @param sqlCmd the SQL-Command
+     * @param sqlParams list of query parameter values
      * @param conn a valid connection to the database.
      * 
      * @return the first column in the current row as a Java object 
      *         or <code>null</code> if there was no value 
      */
-
-    public Object querySingleValue(String sqlCmd, Connection conn)
+    public Object querySingleValue(String sqlCmd, Object[] sqlParams, Connection conn)
     {
         checkOpen(); 
         ResultSet rs = null;
@@ -684,7 +684,7 @@ public abstract class DBDatabase extends
             if (log.isDebugEnabled())
                 log.debug("executing: " + sqlCmd);
             // Get the next Value
-            rs = driver.executeQuery(sqlCmd, null, false, conn);
+            rs = driver.executeQuery(sqlCmd, sqlParams, false, conn);
             if (rs == null)
                 throw new UnexpectedReturnValueException(rs, "driver.executeQuery()");
             // Check Result
@@ -708,17 +708,48 @@ public abstract class DBDatabase extends
     }
 
     /**
+     * Returns the value of the first row/column of a sql-query as an object.
+     * 
+     * @param sqlCmd the SQL-Command
+     * @param conn a valid connection to the database.
+     * 
+     * @return the first column in the current row as a Java object 
+     *         or <code>null</code> if there was no value 
+     */
+    public Object querySingleValue(String sqlCmd, Connection conn)
+    {
+        return querySingleValue(sqlCmd, (Object[])null, conn);  
+    }
+    
+    /**
+     * Returns the value of the first row/column of a sql-query as an int.
+     * 
+     * @param sqlCmd the SQL statement
+     * @param sqlParams list of query parameter values
+     * @param defVal the default value if no value was returned by the database
+     * @param conn a valid connection to the database.
+     *
+     * @return the result as a int value, if no result the int value 0
+     */
+    public int querySingleInt(String sqlCmd, Object[] sqlParams, int defVal, Connection conn)
+    { 
+        Object value = querySingleValue(sqlCmd, sqlParams, conn);
+        return ObjectUtils.getInteger(value, defVal);
+    }
+
+    /**
      * Returns the value of the first row/column of a sql-query as an int.
      * 
      * @param sqlCmd the SQL statement
      * @param defVal the default value if no value was returned by the database
      * @param conn a valid connection to the database.
+     *
      * @return the result as a int value, if no result the int value 0
      */
     public int querySingleInt(String sqlCmd, int defVal, Connection conn)
     { 
         Object value = querySingleValue(sqlCmd, conn);
-        return ((value != null) ? Integer.parseInt(value.toString()) : defVal);
+        return ObjectUtils.getInteger(value, defVal);
     }
 
     /**
@@ -726,66 +757,72 @@ public abstract class DBDatabase extends
      * 
      * @param sqlCmd the SQL statement
      * @param conn a valid connection to the database.
+     *
      * @return the result as a int value, if no result the int value 0
      */
     public final int querySingleInt(String sqlCmd, Connection conn)
     { 
         return querySingleInt(sqlCmd, 0, conn);
     }
-    
+
     /**
      * Returns the value of the first row/column of a sql-query as a long.
      * 
      * @param sqlCmd the SQL statement
+     * @param sqlParams list of query parameter values
      * @param defVal the default value
      * @param conn a valid connection to the database.
      * 
      * @return the result as a long value, if no result the long value 0
      */
-    public long querySingleLong(String sqlCmd, long defVal, Connection conn)
+    public long querySingleLong(String sqlCmd, Object[] sqlParams, long defVal, Connection
conn)
     { 
-        Object value = querySingleValue(sqlCmd, conn);
+        Object value = querySingleValue(sqlCmd, sqlParams, conn);
         return ((value != null) ? Long.parseLong(value.toString()) : defVal);
     }
-
+    
     /**
      * Returns the value of the first row/column of a sql-query as a long.
      * 
      * @param sqlCmd the SQL statement
+     * @param defVal the default value
      * @param conn a valid connection to the database.
+     * 
      * @return the result as a long value, if no result the long value 0
      */
-    public final long querySingleLong(String sqlCmd, Connection conn)
+    public long querySingleLong(String sqlCmd, long defVal, Connection conn)
     { 
-        return querySingleLong(sqlCmd, 0, conn);
+        Object value = querySingleValue(sqlCmd, conn);
+        return ((value != null) ? Long.parseLong(value.toString()) : defVal);
     }
-    
+
     /**
-     * Returns the value of the first row/column of a sql-query as a double.
+     * Returns the value of the first row/column of a sql-query as a long.
      * 
      * @param sqlCmd the SQL statement
-     * @param defVal the default value
      * @param conn a valid connection to the database.
-     * 
+     *
      * @return the result as a long value, if no result the long value 0
      */
-    public double querySingleDouble(String sqlCmd, double defVal, Connection conn)
+    public final long querySingleLong(String sqlCmd, Connection conn)
     { 
-        Object value = querySingleValue(sqlCmd, conn);
-        return ((value != null) ? Double.parseDouble(value.toString()) : defVal);
+        return querySingleLong(sqlCmd, 0, conn);
     }
-
+    
     /**
-     * Returns the value of the first row/column of a sql-query as a double.
+     * Returns the value of the first row/column of a sql-query as a string.
      * 
      * @param sqlCmd the SQL statement
+     * @param sqlParams list of query parameter values
+     * @param defVal the default value if no value was returned by the database
      * @param conn a valid connection to the database.
-     * 
-     * @return the result as a long value, if no result the long value 0
+     *
+     * @return the result as a String object, if no result a empty String
      */
-    public final double querySingleDouble(String sqlCmd, Connection conn)
+    public String querySingleString(String sqlCmd, Object[] sqlParams, String defVal, Connection
conn)
     { 
-        return querySingleDouble(sqlCmd, 0.0, conn);
+        Object value = querySingleValue(sqlCmd, sqlParams, conn);
+        return ((value != null) ? value.toString() : defVal);
     }
     
     /**
@@ -794,7 +831,8 @@ public abstract class DBDatabase extends
      * @param sqlCmd the SQL statement
      * @param defVal the default value if no value was returned by the database
      * @param conn a valid connection to the database.
-     * @return the result as a String object, if no result a emtpy String
+     *
+     * @return the result as a String object, if no result a empty String
      */
     public String querySingleString(String sqlCmd, String defVal, Connection conn)
     { 
@@ -807,7 +845,8 @@ public abstract class DBDatabase extends
      * 
      * @param sqlCmd the SQL statement
      * @param conn a valid connection to the database.
-     * @return the result as a String object, if no result a emtpy String
+     *
+     * @return the result as a String object, if no result a empty String
      */
     public final String querySingleString(String sqlCmd, Connection conn)
     { 

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java?rev=1241546&r1=1241545&r2=1241546&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabaseDriver.java
Tue Feb  7 18:25:12 2012
@@ -399,13 +399,18 @@ public abstract class DBDatabaseDriver i
      * @param pstmt the prepared statement
      * @param sqlParams list of objects
      */
-    protected void prepareStatement(PreparedStatement pstmt, Object[] sqlParams)
+    protected void prepareStatement(PreparedStatement pstmt, Object[] sqlParams, Connection
conn)
     	throws SQLException
 	{
         for (int i=0; i<sqlParams.length; i++)
         {
             Object value = sqlParams[i];
-            addStatementParam(pstmt, i+1, value);
+            try {
+                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;
+            }
         }
 	}
 
@@ -415,7 +420,7 @@ public abstract class DBDatabaseDriver i
      * @param pstmt the prepared statement
      * @param sqlParams list of objects
      */
-    protected void addStatementParam(PreparedStatement pstmt, int paramIndex, Object value)
+    protected void addStatementParam(PreparedStatement pstmt, int paramIndex, Object value,
Connection conn)
 		throws SQLException
 	{
         if (value instanceof DBBlobData)
@@ -539,7 +544,7 @@ public abstract class DBDatabaseDriver i
                     ? conn.prepareStatement(sqlCmd, Statement.RETURN_GENERATED_KEYS)
                     : conn.prepareStatement(sqlCmd);
     	        stmt = pstmt;
-	            prepareStatement(pstmt, sqlParams);
+	            prepareStatement(pstmt, sqlParams, conn);
 	            count = pstmt.executeUpdate(); 
             }
             else
@@ -584,7 +589,7 @@ public abstract class DBDatabaseDriver i
 	        {	// Use prepared statement
 	            PreparedStatement pstmt = conn.prepareStatement(sqlCmd, type, ResultSet.CONCUR_READ_ONLY);
 	            stmt = pstmt;
-	            prepareStatement(pstmt, sqlParams);
+	            prepareStatement(pstmt, sqlParams, conn);
 	            return pstmt.executeQuery();
 	        } else
 	        {	// Use simple statement
@@ -593,6 +598,7 @@ public abstract class DBDatabaseDriver i
 	        }
         } catch(SQLException e) {
             // close statement (if not null)
+            log.error("Error executing query '"+sqlCmd+"' --> "+e.getMessage(), e);
             close(stmt);
             throw e;
         }



Mime
View raw message