diff --git a/Sda1/P/catalog2rdbms/Schema/catalog.xsd b/Sda1/P/catalog2rdbms/Schema/catalog.xsd index 959f9f663e401b7ee6e1eeb04d4b1f8a2f2840ca..9df8b8626a18dc60c23ccb69556e20c18c2da0cc 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 c526fef8754be6d183ce7213995ed1415c7d8bd4..5e6f8309913357183d3e378b080494f244c91954 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 191ea6b4f3194f2bbbcae5582a0a611c2612eb67..15000ab5d08d5160a6b034cc6a2bcfacf66b9b9e 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 0000000000000000000000000000000000000000..f2dd345da36b4d32e9660d979ec1343528698bda --- /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 ac50830cfe4897f2255493c3c540c762b2d816ba..41c4a28db8cf94179fe62b09f6a6b99b3439c222 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 959f9f663e401b7ee6e1eeb04d4b1f8a2f2840ca..9df8b8626a18dc60c23ccb69556e20c18c2da0cc 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 c526fef8754be6d183ce7213995ed1415c7d8bd4..f2dd345da36b4d32e9660d979ec1343528698bda 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 191ea6b4f3194f2bbbcae5582a0a611c2612eb67..15000ab5d08d5160a6b034cc6a2bcfacf66b9b9e 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 a3b149c298a030dc9fa56c655edc4cf830018878..d13fc1070fc39906b9af984eb3be977bd7248bfd 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");