The attached patch adds a "keyRestricted" attribute to the
Relationship element in the DAS Config. This attribute is used to
indicate that that a one to one relationship is  a true one to one
relationship, rather than a one to many relationship in the database
that is treated as a 1-1 in the object model. If keyRestricted is
true, updates to the relationship will not be allowed.

Brent
Index: 
src/test/java/org/apache/tuscany/das/rdb/test/OneToOneRelationshipTests.java
===================================================================
--- 
src/test/java/org/apache/tuscany/das/rdb/test/OneToOneRelationshipTests.java    
    (revision 425267)
+++ 
src/test/java/org/apache/tuscany/das/rdb/test/OneToOneRelationshipTests.java    
    (working copy)
@@ -152,4 +152,31 @@
         assertEquals("Joe Hotshot", employee.getString("NAME"));
         
     }
+    
+    public void testRestrictedOneToOneRelationship() throws Exception {
+               DAS das = 
DAS.FACTORY.createDAS(getConfig("OneToOneRestrictedConfig.xml"),
+                               getConnection());
+
+               Command read = das.getCommand("get named employee with 
company");
+        read.setParameter(1, "Mary Smith");
+        DataObject root = read.executeQuery();
+        DataObject mary = root.getDataObject("EMPLOYEE[1]");
+        DataObject company = mary.getDataObject("company");
+
+        DataObject bob = root.createDataObject("EMPLOYEE");
+        bob.setString("NAME", "bob");
+        bob.setString("SN", "E0005");
+        bob.setInt("MANAGER", 0);
+        
+        bob.setDataObject("company", company);
+        
+        try {
+               das.applyChanges(root);
+               fail("Relationship modification should not be allowed.");
+        } catch (RuntimeException ex) {
+               assertEquals("Can not modify a one to one relationship that is 
key restricted", ex.getMessage());               
+        }
+        assertEquals("ACME Publishing", company.getString("NAME"));            
    
+               
+       }
 }
Index: src/test/resources/OneToOneRestrictedConfig.xml
===================================================================
--- src/test/resources/OneToOneRestrictedConfig.xml     (revision 0)
+++ src/test/resources/OneToOneRestrictedConfig.xml     (revision 0)
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="ASCII"?>
+<!--
+  Copyright (c) 2005 The Apache Software Foundation or its licensors, as 
applicable.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ -->
+<Config 
xsi:noNamespaceSchemaLocation="http:///org.apache.tuscany.das.rdb/config.xsd"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
+               
+       <Command name="get all companies" SQL="select * from COMPANY" kind = 
"Select"/>
+       
+       <Command name="get named employee" SQL="select * from EMPLOYEE where 
NAME= ?" kind = "Select"/>
+       
+       <Command name="get named employee with company" 
+               SQL="select * from EMPLOYEE left outer join COMPANY on 
EMPLOYEE.ID = COMPANY.EOTMID where EMPLOYEE.NAME= ?" kind = "Select"/>
+
+       <Command name="get employee by ID" SQL="select * from EMPLOYEE where 
ID= ?" kind = "Select"/>
+       
+       <Command name="get all employees" SQL="select * from EMPLOYEE" kind = 
"Select"/>
+       
+    <Command name="get companies with employee of the month" 
+               SQL="select * from COMPANY left outer join EMPLOYEE on 
COMPANY.EOTMID = EMPLOYEE.ID" kind="Select"/>
+
+
+       <Table tableName="COMPANY">
+       <Column columnName="ID" primaryKey="true" generated="true"/>
+       </Table>
+  
+       <Table tableName="DEPARTMENT">
+               <Column columnName="ID" primaryKey="true" generated="true"/>
+       </Table>
+               
+       <Table tableName="EMPLOYEE">
+               <Column columnName="ID" primaryKey="true" generated="true"/>
+       </Table>        
+               
+       <Relationship name="company" primaryKeyTable="EMPLOYEE" 
foreignKeyTable="COMPANY" many="false" keyRestricted="true">
+       <KeyPair primaryKeyColumn="ID" foreignKeyColumn="EOTMID" />
+       </Relationship>
+                       
+</Config>
+                       
Index: src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java
===================================================================
--- src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java 
(revision 425267)
+++ src/main/java/org/apache/tuscany/das/rdb/impl/ChangeSummarizer.java 
(working copy)
@@ -153,6 +153,8 @@
                        Relationship rel = mw.getRelationshipByReference(ref);  
                
                        
                        if ( !rel.isMany()) {
+                               if ( rel.isKeyRestricted())
+                                       throw new RuntimeException("Can not 
modify a one to one relationship that is key restricted");
                                // This is a one-one relationship
                                Table t = 
mapping.getTableByTypeName(changedObject.getType().getName());
                                TableWrapper tw = new TableWrapper(t);
Index: src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java
===================================================================
--- src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java    
(revision 425267)
+++ src/main/java/org/apache/tuscany/das/rdb/impl/ChangeFactory.java    
(working copy)
@@ -76,9 +76,6 @@
                return new CreateOperation(getCreateCommand(changedObject), 
changedObject, propagatedID);
        }
        
-       ChangeOperation createInsertOperation(DataObject changedObject) {
-               return createInsertOperation(changedObject, null);
-       }
 
 
        private InsertCommandImpl getCreateCommand(DataObject changedObject) {
Index: src/main/resources/config.xsd
===================================================================
--- src/main/resources/config.xsd       (revision 425267)
+++ src/main/resources/config.xsd       (working copy)
@@ -63,6 +63,7 @@
       <xsd:attribute name="primaryKeyTable" type="xsd:string"/>
       <xsd:attribute name="foreignKeyTable" type="xsd:string"/>
       <xsd:attribute name="many" type="xsd:boolean"/>
+      <xsd:attribute name="keyRestricted" type="xsd:boolean"/>
    </xsd:complexType>
    <xsd:complexType name="Table">
       <xsd:sequence>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to