--- a/sparql2sql/src/test/scala/SparqlToSqlTest.scala Wed Nov 03 20:30:26 2010 -0400
+++ b/sparql2sql/src/test/scala/SparqlToSqlTest.scala Wed Nov 03 20:31:34 2010 -0400
@@ -1067,4 +1067,64 @@
"""
}
+ // !! read from /sparql2sqlendpoint/src/main/resources/ddl.sql
+ val EmpAddrDdl = """
+CREATE TABLE Addresses (ID INT PRIMARY KEY, city VARCHAR(50), state VARCHAR(50));
+INSERT INTO Addresses (ID, city, state) VALUES (18, 'Cambridge', 'MA');
+CREATE TABLE Department (ID INT PRIMARY KEY, name VARCHAR(50), city VARCHAR(50), manager INT,
+ FOREIGN KEY (manager) REFERENCES People(ID),
+ UNIQUE (name, city));
+INSERT INTO Department (ID, name, city, manager) VALUES (23, 'accounting', 'Cambridge', 8);
+CREATE TABLE People (ID INT PRIMARY KEY, fname VARCHAR(50), addr INT,
+ FOREIGN KEY (addr) REFERENCES Addresses(ID),
+ deptName VARCHAR(50), deptCity VARCHAR(50),
+ FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
+INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (7, 'Bob', 18, 'accounting', 'Cambridge');
+INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (8, 'Sue', NULL, NULL, NULL);
+CREATE TABLE Projects (lead INT,
+ FOREIGN KEY (lead) REFERENCES People(ID),
+ name VARCHAR(50), UNIQUE (lead, name),
+ deptName VARCHAR(50), deptCity VARCHAR(50),
+ UNIQUE (name, deptName, deptCity),
+ FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
+INSERT INTO Projects (lead, name, deptName, deptCity) VALUES (8, 'pencil survey', 'accounting', 'Cambridge');
+INSERT INTO Projects (lead, name, deptName, deptCity) VALUES (8, 'eraser survey', 'accounting', 'Cambridge');
+CREATE TABLE TaskAssignments (worker INT,
+ FOREIGN KEY (worker) REFERENCES People(ID),
+ project VARCHAR(50), PRIMARY KEY (worker, project),
+ deptName VARCHAR(50), deptCity VARCHAR(50),
+ FOREIGN KEY (worker) REFERENCES People(ID),
+ FOREIGN KEY (project, deptName, deptCity) REFERENCES Projects(name, deptName, deptCity),
+ FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
+INSERT INTO TaskAssignments (worker, project, deptName, deptCity) VALUES (7, 'pencil survey', 'accounting', 'Cambridge');
+"""
+ val EmpAddr:Database = DDLParser.parseAll(DDLParser.ddl, EmpAddrDdl).get
+
+ test("EmpAddr1") {
+ val sparqlParser = Sparql()
+ val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX Addresses: <http://foo.example/DB/Addresses#>
+PREFIX People: <http://foo.example/DB/People#>
+SELECT ?name ?city
+ WHERE {
+ ?who People:fname ?name .
+ ?who People:addr ?addr .
+ ?addr Addresses:city ?city .
+ }
+""").get
+ val sqlParser = SqlParser()
+ val parsed = sqlParser.parseAll(sqlParser.select, """
+SELECT R_addr.city AS city, R_who.fname AS name
+ FROM People AS R_who
+ INNER JOIN Addresses AS R_addr
+ WHERE R_addr.ID=R_who.addr
+ AND R_addr.city IS NOT NULL
+ AND R_who.fname IS NOT NULL
+""").get
+ val generated = SparqlToSql(EmpAddr, sparqlSelect, StemURI("http://foo.example/DB/"), false, false)._1
+ assert(generated === parsed)
+ val output = """
+"""
+ }
+
}