From b382032f071af3e28725381c7672a33348fb6637 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Thu, 15 Jan 2015 14:47:10 +0100
Subject: [PATCH] Foreign key constraints, SQL syntax, CHAR --> INTEGER

---
 Sda1/P/catalog2rdbms/Schema/catalog.xsd       |  6 +++-
 Sda1/P/catalog2rdbms/Schema/schema.sql        |  2 +-
 Sda1/P/catalog2rdbms/products.xml             |  4 +--
 Sda1/P/catalog2rdbms/schema.sql               | 28 +++++++++++++++++++
 .../mi/sda1/catalog2sql/DataInsert.java       |  4 +--
 Sda1/P/rdbms2catalog/Schema/catalog.xsd       |  6 +++-
 Sda1/P/rdbms2catalog/Schema/schema.sql        |  9 +++---
 Sda1/P/rdbms2catalog/products.xml             |  4 +--
 .../mi/sda1/sql2catalog/RdbmsAccess.java      |  8 +++---
 9 files changed, 54 insertions(+), 17 deletions(-)
 create mode 100644 Sda1/P/catalog2rdbms/schema.sql

diff --git a/Sda1/P/catalog2rdbms/Schema/catalog.xsd b/Sda1/P/catalog2rdbms/Schema/catalog.xsd
index 959f9f663..9df8b8626 100644
--- a/Sda1/P/catalog2rdbms/Schema/catalog.xsd
+++ b/Sda1/P/catalog2rdbms/Schema/catalog.xsd
@@ -7,6 +7,10 @@
                 <xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
             </xs:sequence>
         </xs:complexType>
+        <xs:key name="uniqueProductId">
+            <xs:selector xpath="product"/>
+            <xs:field xpath="@id"/>
+        </xs:key>
     </xs:element>
     
     <xs:element name="product">
@@ -16,7 +20,7 @@
                 <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                 <xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
             </xs:sequence>
-            <xs:attribute name="id" type="xs:ID" use="required"/>
+            <xs:attribute name="id" type="xs:int" use="required"/>
         </xs:complexType>
     </xs:element>
  
diff --git a/Sda1/P/catalog2rdbms/Schema/schema.sql b/Sda1/P/catalog2rdbms/Schema/schema.sql
index c526fef87..5e6f83099 100644
--- a/Sda1/P/catalog2rdbms/Schema/schema.sql
+++ b/Sda1/P/catalog2rdbms/Schema/schema.sql
@@ -2,7 +2,7 @@ DROP TABLE IF EXISTS Description;
 DROP TABLE IF EXISTS Product;
 
 CREATE TABLE Product (
-   id CHAR(20) NOT NULL PRIMARY KEY
+   id INTEGER NOT NULL PRIMARY KEY
   ,name VARCHAR(255) NOT NULL
   ,age SMALLINT
 );
diff --git a/Sda1/P/catalog2rdbms/products.xml b/Sda1/P/catalog2rdbms/products.xml
index 191ea6b4f..15000ab5d 100644
--- a/Sda1/P/catalog2rdbms/products.xml
+++ b/Sda1/P/catalog2rdbms/products.xml
@@ -2,12 +2,12 @@
 
 <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
-   <product id="mpt">
+   <product id="17">
        <name>Monkey Picked Tea</name>
        <description>Rare wild Chinese tea</description>
        <description>Picked only by specially trained monkeys</description>
    </product>
-    <product id="instantTent">
+    <product id="42">
         <name>4-Person Instant Tent</name>
         <description>4-person, 1-room tent</description>
         <description>Pre-attached tent poles</description>
diff --git a/Sda1/P/catalog2rdbms/schema.sql b/Sda1/P/catalog2rdbms/schema.sql
new file mode 100644
index 000000000..f2dd345da
--- /dev/null
+++ b/Sda1/P/catalog2rdbms/schema.sql
@@ -0,0 +1,28 @@
+DROP TABLE IF EXISTS Description;
+DROP TABLE IF EXISTS Product;
+
+CREATE TABLE Product (
+   id INTEGER NOT NULL PRIMARY KEY
+  ,name VARCHAR(255) NOT NULL
+  ,age SMALLINT
+) ENGINE=InnoDB;
+
+CREATE TABLE Description (
+   product INTEGER NOT NULL
+  ,orderIndex int NOT NULL   -- preserving the order of descriptions belonging to a given product
+  ,text VARCHAR(255) NOT NULL
+  ,UNIQUE(product, orderIndex)
+  ,FOREIGN KEY(product) REFERENCES Product(id)
+) ENGINE=InnoDB;
+
+-- example data corresponding to products.xml --
+
+-- Product lacking age property --
+INSERT INTO Product (id, name) VALUES ('mpt', 'Monkey Picked Tea');
+INSERT INTO Description VALUES('mpt', 0, 'Picked only by specially trained monkeys');
+INSERT INTO Description VALUES('mpt', 1, 'Rare wild Chinese tea');
+
+INSERT INTO Product VALUES ('instantTent', '4-Person Instant Tent', 15);
+INSERT INTO Description VALUES('instantTent', 0, 'Exclusive WeatherTec system.');
+INSERT INTO Description VALUES('instantTent', 1, '4-person, 1-room tent');
+INSERT INTO Description VALUES('instantTent', 2, 'Pre-attached tent poles');
diff --git a/Sda1/P/catalog2rdbms/src/main/java/de/hdm_stuttgart/mi/sda1/catalog2sql/DataInsert.java b/Sda1/P/catalog2rdbms/src/main/java/de/hdm_stuttgart/mi/sda1/catalog2sql/DataInsert.java
index ac50830cf..41c4a28db 100644
--- a/Sda1/P/catalog2rdbms/src/main/java/de/hdm_stuttgart/mi/sda1/catalog2sql/DataInsert.java
+++ b/Sda1/P/catalog2rdbms/src/main/java/de/hdm_stuttgart/mi/sda1/catalog2sql/DataInsert.java
@@ -29,7 +29,7 @@ public class DataInsert {
 	 * @param productName See {@link #insertproduct(String, String, int)}
 	 */
 	public void insertproduct(final String productId, final String productName) {
-      final String sqlInsertStmt = "INSERT INTO Product (id, name) VALUES ('" + productId + "', '" + productName +  "');";
+      final String sqlInsertStmt = "INSERT INTO Product (id, name) VALUES (" + productId + ", '" + productName +  "');";
       try {
          stmt.executeUpdate(sqlInsertStmt);
       } catch (SQLException e) {
@@ -48,7 +48,7 @@ public class DataInsert {
 	   
 	   // A PreparedStatement is preferable but not yet introduced in lecture
 	   //
-		final String sqlInsertStmt = "INSERT INTO Product VALUES ('" + productId + "', '" + productName +  "', " + age + ");";
+		final String sqlInsertStmt = "INSERT INTO Product VALUES (" + productId + ", '" + productName +  "', " + age + ");";
       try {
          stmt.executeUpdate(sqlInsertStmt);
       } catch (SQLException e) {
diff --git a/Sda1/P/rdbms2catalog/Schema/catalog.xsd b/Sda1/P/rdbms2catalog/Schema/catalog.xsd
index 959f9f663..9df8b8626 100644
--- a/Sda1/P/rdbms2catalog/Schema/catalog.xsd
+++ b/Sda1/P/rdbms2catalog/Schema/catalog.xsd
@@ -7,6 +7,10 @@
                 <xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
             </xs:sequence>
         </xs:complexType>
+        <xs:key name="uniqueProductId">
+            <xs:selector xpath="product"/>
+            <xs:field xpath="@id"/>
+        </xs:key>
     </xs:element>
     
     <xs:element name="product">
@@ -16,7 +20,7 @@
                 <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
                 <xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
             </xs:sequence>
-            <xs:attribute name="id" type="xs:ID" use="required"/>
+            <xs:attribute name="id" type="xs:int" use="required"/>
         </xs:complexType>
     </xs:element>
  
diff --git a/Sda1/P/rdbms2catalog/Schema/schema.sql b/Sda1/P/rdbms2catalog/Schema/schema.sql
index c526fef87..f2dd345da 100644
--- a/Sda1/P/rdbms2catalog/Schema/schema.sql
+++ b/Sda1/P/rdbms2catalog/Schema/schema.sql
@@ -2,17 +2,18 @@ DROP TABLE IF EXISTS Description;
 DROP TABLE IF EXISTS Product;
 
 CREATE TABLE Product (
-   id CHAR(20) NOT NULL PRIMARY KEY
+   id INTEGER NOT NULL PRIMARY KEY
   ,name VARCHAR(255) NOT NULL
   ,age SMALLINT
-);
+) ENGINE=InnoDB;
 
 CREATE TABLE Description (
-   product CHAR(20) NOT NULL REFERENCES Product
+   product INTEGER NOT NULL
   ,orderIndex int NOT NULL   -- preserving the order of descriptions belonging to a given product
   ,text VARCHAR(255) NOT NULL
   ,UNIQUE(product, orderIndex)
-);
+  ,FOREIGN KEY(product) REFERENCES Product(id)
+) ENGINE=InnoDB;
 
 -- example data corresponding to products.xml --
 
diff --git a/Sda1/P/rdbms2catalog/products.xml b/Sda1/P/rdbms2catalog/products.xml
index 191ea6b4f..15000ab5d 100644
--- a/Sda1/P/rdbms2catalog/products.xml
+++ b/Sda1/P/rdbms2catalog/products.xml
@@ -2,12 +2,12 @@
 
 <catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
-   <product id="mpt">
+   <product id="17">
        <name>Monkey Picked Tea</name>
        <description>Rare wild Chinese tea</description>
        <description>Picked only by specially trained monkeys</description>
    </product>
-    <product id="instantTent">
+    <product id="42">
         <name>4-Person Instant Tent</name>
         <description>4-person, 1-room tent</description>
         <description>Pre-attached tent poles</description>
diff --git a/Sda1/P/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java b/Sda1/P/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java
index a3b149c29..d13fc1070 100644
--- a/Sda1/P/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java
+++ b/Sda1/P/rdbms2catalog/src/main/java/de/hdm_stuttgart/mi/sda1/sql2catalog/RdbmsAccess.java
@@ -29,10 +29,10 @@ public class RdbmsAccess {
 	   final ResultSet products = selectProduct.executeQuery();
 	   
 	   while (products.next()) {
-	      final String productId = products.getString("id");
+	      final int productId = products.getInt("id");
 	      final Element productElement = new Element("product");
 	      root.addContent(productElement);
-	      productElement.setAttribute("id", productId);
+	      productElement.setAttribute("id", "" + productId);
 	      
 	      final Element nameElement = new Element("name");
 	      productElement.addContent(nameElement);
@@ -50,8 +50,8 @@ public class RdbmsAccess {
       products.close();
 	}
 	
-	private void addDescriptions(final String productId, final Element productElement) throws SQLException {
-	   selectDescription.setString(1, productId);
+	private void addDescriptions(final int productId, final Element productElement) throws SQLException {
+	   selectDescription.setInt(1, productId);
 	   final ResultSet descriptions = selectDescription.executeQuery();
 	   while (descriptions.next()) {
 	      final Element descriptionElement = new Element("description");
-- 
GitLab