tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From am...@apache.org
Subject svn commit: r608066 - in /incubator/tuscany/java/das/rdb/src: main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java test/java/org/apache/tuscany/das/rdb/test/AliasTests.java
Date Wed, 02 Jan 2008 09:59:37 GMT
Author: amita
Date: Wed Jan  2 01:59:35 2008
New Revision: 608066

URL: http://svn.apache.org/viewvc?rev=608066&view=rev
Log:
TUSCANY-1944 support update of PK

Modified:
    incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java
    incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/AliasTests.java

Modified: incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java?rev=608066&r1=608065&r2=608066&view=diff
==============================================================================
--- incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java
(original)
+++ incubator/tuscany/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/generator/impl/UpdateGenerator.java
Wed Jan  2 01:59:35 2008
@@ -54,6 +54,18 @@
         super();
     }
 
+    /*Utility to check if the given HashSet of Properties contains a property with certain
name*/
+    private boolean isContained(HashSet changedFields, String propertyName) {
+    	Iterator itr = changedFields.iterator();
+    	while(itr.hasNext()) {
+    		Property changedProperty = (Property)itr.next();
+    		if(changedProperty.getName().equals(propertyName)) {
+    			return true;
+    		}
+    	}
+    	return false;
+    }
+    
     public UpdateCommandImpl getUpdateCommand(MappingWrapper mapping, DataObject changedObject,
Table table) {      
         List parameters = new ArrayList();
         Type type = changedObject.getType();
@@ -98,18 +110,23 @@
         while (pkColumnNames.hasNext() && pkPropertyNames.hasNext()) {
             String columnName = (String) pkColumnNames.next();
             String propertyName = (String) pkPropertyNames.next();
-            statement.append(columnName);
-            statement.append(" = ?");
-            if (pkColumnNames.hasNext() && pkPropertyNames.hasNext()) {
-                statement.append(" and ");
+            
+            if(!isContained(changedFields, propertyName)) {//if PK itself is changed, don't
add here, will get added below as collision parameter
+	            statement.append(columnName);
+	            statement.append(" = ?");
+	            if (pkColumnNames.hasNext() && pkPropertyNames.hasNext()) {
+	                statement.append(" and ");
+	            }
+	            parameters.add(createParameter(tableWrapper, type.getProperty(propertyName),
idx++));
             }
-            parameters.add(createParameter(tableWrapper, type.getProperty(propertyName),
idx++));
         }
 
         if (tableWrapper.getCollisionColumn() == null) {
             Iterator iter = changedFields.iterator();
             while (iter.hasNext()) {
-                statement.append(" and ");
+            	if(statement.lastIndexOf(" where ") != statement.length()-7)
+            		statement.append(" and ");
+            	
                 Property changedProperty = (Property) iter.next();
                 Column column = tableWrapper.getColumnByPropertyName(changedProperty.getName());

                 statement.append(column == null ? changedProperty.getName() : column.getColumnName());
@@ -136,8 +153,10 @@
             }
            
         } else {
-            statement.append(" and ");
-            statement.append(tableWrapper.getCollisionColumn().getColumnName());
+        	if(statement.lastIndexOf(" where ") != statement.length()-7)
+        		statement.append(" and ");
+
+        	statement.append(tableWrapper.getCollisionColumn().getColumnName());
             statement.append(" = ?");
             parameters.add(createParameter(tableWrapper, 
                     type.getProperty(tableWrapper.getCollisionColumnPropertyName()), idx++));
                      
@@ -146,8 +165,9 @@
         UpdateCommandImpl updateCommand = new OptimisticWriteCommandImpl(statement.toString());
         
         Iterator params = parameters.iterator();
-        while (params.hasNext()) {           
-        	updateCommand.addParameter((ParameterExtendedImpl) params.next());
+        while (params.hasNext()) {     
+        	ParameterExtendedImpl ptemp = (ParameterExtendedImpl) params.next();
+        	updateCommand.addParameter(ptemp);
         }
            
         if (this.logger.isDebugEnabled()) {

Modified: incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/AliasTests.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/AliasTests.java?rev=608066&r1=608065&r2=608066&view=diff
==============================================================================
--- incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/AliasTests.java
(original)
+++ incubator/tuscany/java/das/rdb/src/test/java/org/apache/tuscany/das/rdb/test/AliasTests.java
Wed Jan  2 01:59:35 2008
@@ -161,4 +161,26 @@
         assertEquals("Williams", customer.getString("lastname"));
 
     }
+    
+    /**
+     * Test modify PK value
+     */
+    public void testModifyPK() throws Exception {
+
+        DAS das = DAS.FACTORY.createDAS(getConfig("customerMapping.xml"), getConnection());
+        // Read a customer
+        Command select = das.createCommand("SELECT * FROM CUSTOMER WHERE CUSTOMER.ID = 1");
+
+        DataObject root = select.executeQuery();
+        DataObject customer = root.getDataObject("Customer[1]");
+        assertEquals(1, customer.getInt("id"));
+
+        customer.setInt("id", 100);
+        das.applyChanges(root);
+        
+        select = das.createCommand("SELECT * FROM CUSTOMER WHERE CUSTOMER.ID = 100");
+        root = select.executeQuery();
+        customer = root.getDataObject("Customer[1]");
+        assertEquals(100, customer.getInt("id"));
+    }    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message