empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1041629 - in /incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBCombinedCmd.java DBCommand.java DBCommandExpr.java DBDatabase.java DBDatabaseDriver.java DBReader.java
Date Thu, 02 Dec 2010 23:06:49 GMT
Author: doebele
Date: Thu Dec  2 23:06:48 2010
New Revision: 1041629

URL: http://svn.apache.org/viewvc?rev=1041629&view=rev
Log:
EMPIREDB-91
Allow command parameters (which enforce prepared statements) to be used with DBReader.
ATTENTION: inner class DBCmdParameter has been renamed to DBCommandParam

Modified:
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
    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
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBReader.java

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java?rev=1041629&r1=1041628&r2=1041629&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCombinedCmd.java
Thu Dec  2 23:06:48 2010
@@ -91,6 +91,30 @@ public class DBCombinedCmd extends DBCom
       // DebugMsg(2, "Check: getSelectExprList() for DBCombinedCmd");
       return left.getSelectExprList();
    }
+   
+   /**
+    * Returns the list of parameter values for a prepared statement.
+    * @return the list of parameter values for a prepared statement 
+    */
+   @Override
+   public Object[] getCmdParams()
+   {
+       Object[] leftParams  = left.getCmdParams();
+       Object[] rightParams = right.getCmdParams();
+       // Check
+       if (leftParams==null)
+           return rightParams;
+       if (rightParams==null)
+           return leftParams;
+       // Put them all together
+       Object[] allParams = new Object[leftParams.length+rightParams.length];
+       for (int i=0; i<leftParams.length; i++)
+           allParams[i]=leftParams[i];
+       for (int i=0; i<rightParams.length; i++)
+           allParams[leftParams.length+i]=rightParams[i];
+       // return Params
+       return allParams;
+   }
 
   /**
    * Creates the SQL-Command.

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java?rev=1041629&r1=1041628&r2=1041629&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommand.java
Thu Dec  2 23:06:48 2010
@@ -43,12 +43,12 @@ import org.apache.empire.db.expr.set.DBS
 public abstract class DBCommand extends DBCommandExpr
     implements Cloneable
 {
-    public static final class DBCmdParameter extends DBExpr
+    public static final class DBCommandParam extends DBExpr
     {
         protected DBCommand cmd;
         protected int index; 
         
-        protected DBCmdParameter(DBCommand cmd, int index)
+        protected DBCommandParam(DBCommand cmd, int index)
         {
             this.cmd   = cmd;
             this.index = index;
@@ -95,7 +95,7 @@ public abstract class DBCommand extends 
     protected List<DBCompareExpr>    where          = null;
     protected List<DBCompareExpr>    having         = null;
     protected List<DBColumnExpr>     groupBy        = null;
-    // Params for prepared Statements
+    // Parameters for prepared Statements
     protected Vector<Object>         cmdParams      = null;
     // Database
     private DBDatabase               db;
@@ -223,6 +223,9 @@ public abstract class DBCommand extends 
     
     protected boolean useCmdParam(DBColumn col)
     {
+        if (db.isPreparedStatementsEnabled())
+            return true;
+        // Only use a command param if column is of type BLOB or CLOB
         DataType dt = col.getDataType();
         return ( dt==DataType.BLOB || dt==DataType.CLOB );
     }
@@ -267,9 +270,9 @@ public abstract class DBCommand extends 
             if (chk.column.equals(expr.column))
             { // Overwrite existing value
                 if (useCmdParam(expr.column) && (expr.value instanceof DBExpr) ==
false
-                    && chk.value instanceof DBCmdParameter)
+                    && chk.value instanceof DBCommandParam)
                 { // replace parameter
-                    int index = ((DBCmdParameter) chk.value).index;
+                    int index = ((DBCommandParam) chk.value).index;
                     this.setCmdParam(index, getCmdParamValue(expr.column, expr.value));
                 } 
                 else
@@ -328,25 +331,36 @@ public abstract class DBCommand extends 
     }
 
     /**
-     * Adds an object to in the list of Parameters
-     * and returns a parameter object.
+     * Adds an command parameter which will be used in a prepared statement.
+     * The command parameter returned may be used to alter the value.
      * 
-     * @param item the parameter value 
+     * @param value the initial parameter value 
      * 
-     * @return the parameter object 
+     * @return the command parameter object 
      */
-    public DBCmdParameter addCmdParam(Object item)
+    public DBCommandParam addCmdParam(Object value)
     {
         if (cmdParams==null)
             cmdParams= new Vector<Object>();
         // Adds the parameter 
         int index = cmdParams.size(); 
-        if (cmdParams.add(item)==false)
+        if (cmdParams.add(value)==false)
             return null; // unknown error
         // Creates a Parameter expression
-        return new DBCmdParameter(this, index);
+        return new DBCommandParam(this, index);
     }
-    
+
+    /**
+     * Adds an command parameter which will be used in a prepared statement.
+     * The initial value of the command parameter is null but can be modified using the setValue
method.
+     *  
+     * @return the command parameter object
+     */
+    public final DBCommandParam addCmdParam()
+    {
+        return addCmdParam(null);
+    }
+
     /**
      * Adds a join to the list of join expressions.
      * 
@@ -817,9 +831,9 @@ public abstract class DBCommand extends 
     
     /**
      * Returns the list of parameter values for a prepared statement.
-     * 
      * @return the list of parameter values for a prepared statement 
      */
+    @Override
     public Object[] getCmdParams()
     {
         if (cmdParams==null)

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java?rev=1041629&r1=1041628&r2=1041629&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
Thu Dec  2 23:06:48 2010
@@ -303,6 +303,8 @@ public abstract class DBCommandExpr exte
     public abstract boolean getSelect(StringBuilder buf);
 
     public abstract DBColumnExpr[] getSelectExprList();
+    
+    public abstract Object[] getCmdParams();
 
     public DBColumnExpr getCmdColumn(DBColumnExpr col)
     {

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=1041629&r1=1041628&r2=1041629&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
Thu Dec  2 23:06:48 2010
@@ -80,6 +80,13 @@ public abstract class DBDatabase extends
     protected List<DBRelation> relations = new ArrayList<DBRelation>();
     protected List<DBView>     views     = new ArrayList<DBView>();
     protected DBDatabaseDriver driver    = null;
+    
+    /**   
+     * Property that indicates whether to always use usePreparedStatements (Default is false!)
+     * Note: This will only apply for update and insert commands as well as for read operations
on a DBRecord.
+     * For custom SQL commands parameters must be explicitly declared using cmd.addCmdParam();
  
+     */
+    private boolean preparedStatementsEnabled = false;
 
     /**
      * Constructs a new DBDatabase object set the variable 'schema' = null.
@@ -124,6 +131,29 @@ public abstract class DBDatabase extends
     {
         return driver;
     }
+    
+    /**
+     * return whether prepared statements are preferred over normal statements (Default is
false)
+     * Note: This will only apply for update and insert commands as well as for read operations
on a DBRecord.
+     * For custom SQL commands parameters must be explicitly declared using cmd.addCmdParam();
  
+     * @return true if prepared Statements are enabled or false if not
+     */
+    public boolean isPreparedStatementsEnabled()
+    {
+        return preparedStatementsEnabled;
+    }
+
+    /**
+     * enables or disables the use of prepared statements for update and insert commands
as well as for read operations on a DBRecord.
+     * Note: For custom SQL commands parameters must be explicitly declared using cmd.addCmdParam();
  
+     * @param preparedStatementsEnabled
+     */
+    public void setPreparedStatementsEnabled(boolean preparedStatementsEnabled)
+    {
+        this.preparedStatementsEnabled = preparedStatementsEnabled;
+        // log prepared statement 
+        log.info("PreparedStatementsEnabled is " + String.valueOf(preparedStatementsEnabled));
+    }
 
     /**
      * Sets the database driver for this database. This will

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=1041629&r1=1041628&r2=1041629&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
Thu Dec  2 23:06:48 2010
@@ -43,8 +43,8 @@ import org.apache.empire.data.DataType;
 
 
 /**
- *
- * 
+ * The DBDatabaseDriver class is an abstract base class for all database drivers.
+ * Its purpose is to handle everything that is - or might be - database vendor specific.

  */
 public abstract class DBDatabaseDriver extends ErrorObject
 {
@@ -244,13 +244,13 @@ public abstract class DBDatabaseDriver e
 
     /**
      * Returns whether or not a particular feature is supported by this driver
-     * @param type type of requrested feature. @see DBDriverFeature
+     * @param type type of requested feature. @see DBDriverFeature
      * @return true if the features is supported or false otherwise
      */
     public abstract boolean isSupported(DBDriverFeature type);
 
     /**
-     * Detects wether a table or column name needs to be quoted or not<BR/>
+     * Detects whether a table or column name needs to be quoted or not<BR/>
      * By default all reserved SQL keywords as well as names 
      * containing a "-", "/", "+" or " " require quoting.<BR/>
      * Overrides this function to add database specific keywords like "user" or "count" 

@@ -331,7 +331,7 @@ public abstract class DBDatabaseDriver e
      * 
      * @param db the database
      * @param SeqName the name of the sequence
-     * @param minValue the minmum value of the sequence
+     * @param minValue the minimum value of the sequence
      * @param conn a valid database connection
      * @return a new unique sequence value or null if an error occurred
      */
@@ -388,16 +388,25 @@ public abstract class DBDatabaseDriver e
                 // handling for blobs
                 DBBlobData blobData = (DBBlobData)sqlParams[i];
                 pstmt.setBinaryStream(i + 1, blobData.getInputStream(), blobData.getLength());
+                // log
+                if (log.isDebugEnabled())
+                    log.debug("Setting statement param " + String.valueOf(i) + " to BLOB
data");
             }
             else if(sqlParams[i] instanceof DBClobData)
             {
                 // handling for clobs
                 DBClobData clobData = (DBClobData)sqlParams[i];
                 pstmt.setCharacterStream(i + 1, clobData.getReader(), clobData.getLength());
+                // log
+                if (log.isDebugEnabled())
+                    log.debug("Setting statement param " + String.valueOf(i) + " to CLOB
data");
             }
             else
-            {
+            {   // simple parameter value 
                 pstmt.setObject(i + 1, sqlParams[i]);
+                // log
+                if (log.isDebugEnabled())
+                    log.debug("Setting statement param " + String.valueOf(i) + " to '" +
String.valueOf(sqlParams[i])+ "'");
             }
         }
 	}
@@ -415,10 +424,10 @@ public abstract class DBDatabaseDriver e
     
     /**
      * <P>
-     * Reads a sinlge column value from the given JDBC ResultSet and returns a value object
of desired data type.<BR> 
+     * Reads a single column value from the given JDBC ResultSet and returns a value object
of desired data type.<BR> 
      * See {@link DBExpr#getValueClass(DataType)} for java class type mapping.
      * <P>
-     * This gives the driver the oportunity to change the value
+     * This gives the driver the opportunity to change the value
      * i.e. to simulate missing data types with other types.
      * <P>
      * @param rset the sql Resultset with the current data row

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBReader.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBReader.java?rev=1041629&r1=1041628&r2=1041629&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBReader.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBReader.java Thu
Dec  2 23:06:48 2010
@@ -406,14 +406,14 @@ public class DBReader extends DBRecordDa
     {
         if (isOpen())
             close();
-        // SQL Commanand
+        // SQL Command
         String sqlCmd = cmd.getSelect();
-        // Create Statement and connection
+        // Create Statement
         db = cmd.getDatabase();
-        rset = db.executeQuery(sqlCmd, null, scrollable, conn);
+        rset = db.executeQuery(sqlCmd, cmd.getCmdParams(), scrollable, conn);
         if (rset==null)
             return error(db);
-        // sucessfully opened
+        // successfully opened
         colList = cmd.getSelectExprList();
         addOpenResultSet();
         return success();



Mime
View raw message