empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1041860 - in /incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db: DBCommand.java DBDatabaseDriver.java DBQuery.java DBRowSet.java DBTable.java expr/compare/DBCompareColExpr.java
Date Fri, 03 Dec 2010 15:13:44 GMT
Author: doebele
Date: Fri Dec  3 15:13:44 2010
New Revision: 1041860

URL: http://svn.apache.org/viewvc?rev=1041860&view=rev
Log:
EMPIREDB-91
few more enhancements and fixes

Modified:
    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/DBDatabaseDriver.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
    incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java

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=1041860&r1=1041859&r2=1041860&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
Fri Dec  3 15:13:44 2010
@@ -26,6 +26,8 @@ import java.util.List;
 import java.util.Set;
 import java.util.Vector;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.empire.EmpireException;
 import org.apache.empire.commons.Errors;
 import org.apache.empire.data.DataType;
@@ -105,6 +107,11 @@ public abstract class DBCommand extends 
             return cmd.getDatabase();
         }
         
+        public DataType getDataType()
+        {
+        	return type;
+        }
+        
         public Object getValue()
         {
             return value;
@@ -115,7 +122,10 @@ public abstract class DBCommand extends 
             this.value = getCmdParamValue(value);
         }
     }
-    
+
+    // Logger
+    @SuppressWarnings("hiding")
+    protected static final Log log = LogFactory.getLog(DBCommand.class);
     // Distinct Select
     protected boolean                selectDistinct = false;
     // Lists
@@ -141,11 +151,18 @@ public abstract class DBCommand extends 
         this.db = db;
     }
 
+    /**
+     * internally used to reset the command param usage count.
+     * Note: Only one thread my generate an SQL statement 
+     */
     private void resetParamUsage()
     {
         paramUsageCount = 0;
     }
     
+    /**
+     * internally used to reorder the command params to match their order of occurance
+     */
     private synchronized void notifyParamUsage(DBCommandParam param)
     {
         int index = cmdParams.indexOf(param);
@@ -162,6 +179,32 @@ public abstract class DBCommand extends 
         }
         paramUsageCount++;
     }
+
+    /**
+     * internally used to remove the command param used in a constraint
+     */
+   	private void removeCommandParam(DBCompareColExpr cmp) 
+   	{
+        if (cmdParams!=null && (cmp.getValue() instanceof DBCommandParam))
+   			cmdParams.remove(cmp.getValue());
+   	}
+
+    /**
+     * internally used to remove all command params used in a list of constraints
+     */
+   	private void removeAllCommandParams(List<DBCompareExpr> list)
+    {
+        if (cmdParams == null)
+        	return;
+        for(DBCompareExpr cmp : list)
+        {	// Check whether it is a compare column expr.
+            if (!(cmp instanceof DBCompareColExpr))
+            	continue;
+            // Check the value is a DBCommandParam
+        	removeCommandParam((DBCompareColExpr)cmp);
+        }
+    }
+   	
     
     /**
      * Creates a clone of this class.
@@ -376,26 +419,11 @@ public abstract class DBCommand extends 
      * 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.
      *  
-     * @param colExpr the column expression for which to create the parameter
-     * 
-     * @return the command parameter object 
-     */
-    public final DBCommandParam addCmdParam(DBColumnExpr colExpr)
-    {
-        return addCmdParam(colExpr.getDataType(), null);
-    }
-    
-    /**
-     * 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.
-     *  
-     * @param type the data type of the parameter
-     * 
-     * @return the command parameter object 
+     * @return the command parameter object
      */
-    public final DBCommandParam addCmdParam(DataType type)
+    public final DBCommandParam addCmdParam(Object value)
     {
-        return addCmdParam(type, null);
+        return addCmdParam(DataType.UNKNOWN, value);
     }
 
     /**
@@ -746,6 +774,7 @@ public abstract class DBCommand extends 
      */
     public void clearWhere()
     {
+    	removeAllCommandParams(where);
         where = null;
     }
 
@@ -754,6 +783,7 @@ public abstract class DBCommand extends 
      */
     public void clearHaving()
     {
+    	removeAllCommandParams(having);
         having = null;
     }
 
@@ -770,6 +800,7 @@ public abstract class DBCommand extends 
      */
     public void clear()
     {
+        cmdParams = null;
         clearSelectDistinct();
         clearSelect();
         clearSet();
@@ -779,7 +810,7 @@ public abstract class DBCommand extends 
         clearGroupBy();
         clearOrderBy();
         clearLimit();
-        cmdParams = null;
+        resetParamUsage();
     }
 
     /**
@@ -794,6 +825,9 @@ public abstract class DBCommand extends 
         	DBCompareExpr other = list.get(i);
             if (expr.isMutuallyExclusive(other)==false)
                 continue;
+            // Check if we replace a DBCommandParam
+            if (other instanceof DBCompareColExpr)
+            	removeCommandParam((DBCompareColExpr)other);
             // columns match
             list.set(i, expr);
             return;
@@ -819,13 +853,15 @@ public abstract class DBCommand extends 
             DBColumnExpr c = ((DBCompareColExpr)cmp).getColumnExpr();
             DBColumn udc = c.getUpdateColumn();
             if (c.equals(col) || (udc!=null && udc.equals(col.getUpdateColumn())))
-            {	// found the column
+            {   // Check if we replace a DBCommandParam
+            	removeCommandParam((DBCompareColExpr)cmp);
+            	// remove the constraint
             	list.remove(cmp);
             	return;
             }
         }
     }
-
+    
     /**
      * Gets a list of all tables referenced by the query.
      *  
@@ -887,7 +923,11 @@ public abstract class DBCommand extends 
     {
         if (cmdParams==null || cmdParams.size()==0)
             return null;
-        // return Params
+        // Check whether all parameters have been used
+        if (paramUsageCount>0 && paramUsageCount!=cmdParams.size())
+	        log.warn("DBCommand parameter count ("+String.valueOf(cmdParams.size())
+	        	   + ") does not match parameter use count ("+String.valueOf(paramUsageCount)+")");
+        // Create result array
         Object[] values = new Object[cmdParams.size()];
         for (int i=0; i<values.length; i++)
             values[i]=cmdParams.get(i).getValue();

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=1041860&r1=1041859&r2=1041860&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
Fri Dec  3 15:13:44 2010
@@ -391,7 +391,7 @@ public abstract class DBDatabaseDriver e
                 pstmt.setBinaryStream(i + 1, blobData.getInputStream(), blobData.getLength());
                 // log
                 if (log.isDebugEnabled())
-                    log.debug("Setting statement param " + String.valueOf(i) + " to BLOB
data");
+                    log.debug("Statement param " + String.valueOf(i+1) + " set to BLOB data");
             }
             else if(value instanceof DBClobData)
             {
@@ -400,14 +400,14 @@ public abstract class DBDatabaseDriver e
                 pstmt.setCharacterStream(i + 1, clobData.getReader(), clobData.getLength());
                 // log
                 if (log.isDebugEnabled())
-                    log.debug("Setting statement param " + String.valueOf(i) + " to CLOB
data");
+                    log.debug("Statement param " + String.valueOf(i+1) + " set to CLOB data");
             }
             else
             {   // simple parameter value 
                 pstmt.setObject(i + 1, value);
                 // log
                 if (log.isDebugEnabled())
-                    log.debug("Setting statement param " + String.valueOf(i) + " to '" +
String.valueOf(value)+ "'");
+                    log.debug("Statement param " + String.valueOf(i+1) + " set to '" + String.valueOf(value)+
"'");
             }
         }
 	}

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java?rev=1041860&r1=1041859&r2=1041860&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java Fri
Dec  3 15:13:44 2010
@@ -28,6 +28,7 @@ import org.apache.empire.commons.Errors;
 import org.apache.empire.commons.ObjectUtils;
 import org.apache.empire.commons.Options;
 import org.apache.empire.data.DataType;
+import org.apache.empire.db.DBCommand.DBCommandParam;
 import org.apache.empire.db.expr.compare.DBCompareColExpr;
 import org.apache.empire.db.expr.compare.DBCompareExpr;
 import org.apache.empire.db.expr.join.DBJoinExpr;
@@ -456,14 +457,23 @@ public class DBQuery extends DBRowSet
             {
                 DBCompareExpr cmp = cmd.where.get(i);
                 if (cmp instanceof DBCompareColExpr)
-                { // Check whether
+                { 	// Check whether constraint belongs to update table
                     DBCompareColExpr cmpExpr = (DBCompareColExpr) cmp;
                     DBColumn col = cmpExpr.getColumnExpr().getUpdateColumn();
                     if (col!=null && col.getRowSet() == table)
+                    {	// add the constraint
+                    	if (cmpExpr.getValue() instanceof DBCommandParam)
+                    	{	// Create a new command param
+                    		DBColumnExpr colExpr = cmpExpr.getColumnExpr();
+                    		DBCommandParam param =(DBCommandParam)cmpExpr.getValue(); 
+                    		DBCommandParam value = upd.addCmdParam(colExpr, param.getValue());
+                    		cmp = new DBCompareColExpr(colExpr, cmpExpr.getCmpop(), value);
+                    	}
                         upd.where(cmp);
+                    }    
                 } 
                 else
-                { // other constraints are not supported
+                {	// other constraints are not supported
                     return error(Errors.NotSupported, "updateRecord");
                 }
             }

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java?rev=1041860&r1=1041859&r2=1041860&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBRowSet.java Fri
Dec  3 15:13:44 2010
@@ -493,18 +493,13 @@ public abstract class DBRowSet extends D
     }
     
     /**
-     * Reads the record with the given primary key from the database.
-     * <P>
-     * @param rec the DBRecord object which will hold the record data
-     * @param key the primary key values
-     * @param conn a valid JDBC connection.
-     * @return true if successful
+     * Set the constraints for a single record from a supplied key 
+     * @param cmd the command to which to add the constraints
+     * @param key the record key
+     * @return true if the constraints have been successfully set or false otherwise
      */
-    public boolean readRecord(DBRecord rec, Object[] key, Connection conn)
+    protected boolean setKeyConstraints(DBCommand cmd, Object[] key)
     {
-        // Check Arguments
-        if (conn == null || rec == null)
-            return error(Errors.InvalidArg, null, "conn|rec");
         // Check Primary key
         if (primaryKey == null ) 
             return error(DBErrors.NoPrimaryKey, getName()); // Invalid Argument
@@ -512,9 +507,7 @@ public abstract class DBRowSet extends D
         DBColumn[] keyColumns = primaryKey.getColumns();
         if (key == null || key.length != keyColumns.length)
             return error(DBErrors.RecordInvalidKey, key); // Invalid Argument
-        // Select
-        DBCommand cmd = db.createCommand();
-        cmd.select(columns);
+        // Add the key constraints
         for (int i = 0; i < key.length; i++)
         {   // Set key column constraint
             Object value = key[i];
@@ -522,6 +515,28 @@ public abstract class DBRowSet extends D
                 value = cmd.addCmdParam(keyColumns[i], value);
             cmd.where(keyColumns[i].is(value));
         }    
+        return true;
+    }
+    
+    /**
+     * Reads the record with the given primary key from the database.
+     * <P>
+     * @param rec the DBRecord object which will hold the record data
+     * @param key the primary key values
+     * @param conn a valid JDBC connection.
+     * @return true if successful
+     */
+    public boolean readRecord(DBRecord rec, Object[] key, Connection conn)
+    {
+        // Check Arguments
+        if (conn == null || rec == null)
+            return error(Errors.InvalidArg, null, "conn|rec");
+        // Select
+        DBCommand cmd = db.createCommand();
+        cmd.select(columns);
+        // Set key constraints
+        if (!setKeyConstraints(cmd, key))
+        	return false;
         // Read Record
         if (!readRecord(rec, cmd, conn))
         {   // Record not found
@@ -546,18 +561,12 @@ public abstract class DBRowSet extends D
         // Check Arguments
         if (conn == null)
             return error(Errors.InvalidArg, conn, "conn");
-        // Check Primary key
-        if (primaryKey == null ) 
-            return error(DBErrors.NoPrimaryKey, getName()); // Invalid Argument
-        // Check Columns
-        DBColumn[] keyColumns = primaryKey.getColumns();
-        if (key == null || key.length != keyColumns.length)
-            return error(DBErrors.RecordInvalidKey, key); // Invalid Argument
         // Select
         DBCommand cmd = db.createCommand();
         cmd.select(count());
-        for (int i = 0; i < key.length; i++)
-            cmd.where(keyColumns[i].is(key[i]));
+        // Set key constraints
+        if (!setKeyConstraints(cmd, key))
+        	return false;
         // check exits
         return (db.querySingleInt(cmd.getSelect(), conn)==1);
     }

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java?rev=1041860&r1=1041859&r2=1041860&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java (original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTable.java Fri
Dec  3 15:13:44 2010
@@ -445,12 +445,13 @@ public class DBTable extends DBRowSet im
         
         // Build SQL-Statement
         DBCommand cmd = db.createCommand();
-        for (int i = 0; i < key.length; i++)
-            cmd.where(keyColumns[i].is(key[i]));
+        // Set key constraints
+        if (!setKeyConstraints(cmd, key))
+        	return false;
 
         // Perform delete
         String sqlCmd = cmd.getDelete(this);
-        int affected  = db.executeSQL(sqlCmd, null, conn);
+        int affected  = db.executeSQL(sqlCmd, cmd.getCmdParamValues(), conn);
         if (affected < 0)
         { // Delete Failed
             return error(db);

Modified: incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java?rev=1041860&r1=1041859&r2=1041860&view=diff
==============================================================================
--- incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
(original)
+++ incubator/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/expr/compare/DBCompareColExpr.java
Fri Dec  3 15:13:44 2010
@@ -100,7 +100,7 @@ public class DBCompareColExpr extends DB
 
     /**
      * The value to compare the column expression with
-     * @param value the comparision value 
+     * @param value the comparison value 
      */
     public void setValue(Object value)
     {
@@ -238,7 +238,7 @@ public class DBCompareColExpr extends DB
     }
 
     /**
-     * Returns wheter the constraint should replace another one or not.
+     * Returns whether the constraint should replace another one or not.
      * 
      * @return true it the constraints are mutually exclusive or false otherwise
      */



Mime
View raw message