--- a/project/build/RDB2RDF.scala Sun Oct 31 16:10:19 2010 -0400
+++ b/project/build/RDB2RDF.scala Sun Oct 31 17:10:02 2010 -0400
@@ -17,7 +17,8 @@
lazy val sparql2sql = project("sparql2sql", "sparql2sql", new SPARQL2SQL(_), sparql, sql)
lazy val sparql2sparql = project("sparql2sparql", "sparql2sparql", new SPARQL2SPARQL(_), sparql)
lazy val sql2sql = project("sql2sql", "sql2sql", new SQL2SQL(_), sql)
- lazy val sparqlendpoint = project("sparqlendpoint", "sparqlendpoint", new SPARQLEndPoint(_), sparql2sql)
+ lazy val sparql2sqlendpoint = project("sparql2sqlendpoint", "sparql2sqlendpoint", new SPARQL2SQLEndPoint(_), sparql2sql)
+ lazy val sparql2sparql2sql = project("sparql2sparql2sql", "sparql2sparql2sql", new SPARQL2SPARQL2SQL(_), sparql2sql)
class RDB(info: ProjectInfo) extends DefaultProject(info) with Common
class SQL(info: ProjectInfo) extends DefaultProject(info) with Common
@@ -27,13 +28,13 @@
class SPARQL2SQL(info: ProjectInfo) extends DefaultProject(info) with Common
class SPARQL2SPARQL(info: ProjectInfo) extends DefaultProject(info) with Common
class SQL2SQL(info: ProjectInfo) extends DefaultProject(info) with Common
- class SPARQLEndPoint(info: ProjectInfo) extends DefaultWebProject(info) with Common with BSBMPlugin {
+ class SPARQL2SQLEndPoint(info: ProjectInfo) extends DefaultWebProject(info) with Common with BSBMPlugin {
val jetty6 = "org.mortbay.jetty" % "jetty" % "6.1.14" % "test"
val servlet = "javax.servlet" % "servlet-api" % "2.5" % "provided"
val mysql = "mysql" % "mysql-connector-java" % "5.1.12"
override def webappUnmanaged = super.webappUnmanaged +++ ("src" / "main" / "resources" / "database.properties")
}
-
+ class SPARQL2SPARQL2SQL(info: ProjectInfo) extends DefaultProject(info) with Common
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/resources/database.properties Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,3 @@
+host = localhost
+user = root
+password =
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/resources/ddl.txt Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,3 @@
+CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
+CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
+CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/resources/default-sparql-query.txt Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,17 @@
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX task : <http://hr.example/DB/Tasks#>
+PREFIX tass : <http://hr.example/DB/TaskAssignments#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?name ?bday
+ WHERE { { ?above tass:employee ?who .
+ ?above tass:task ?atask .
+ ?atask task:lead ?taskLead .
+ ?taskLead empP:lastName ?name }
+ UNION
+ { ?below tass:task ?btask .
+ ?btask task:lead ?who .
+ ?below tass:employee ?managed .
+ ?managed empP:lastName ?name .
+ ?managed empP:birthday ?bday }
+ ?who empP:lastName "Smith"^^xsd:string .
+ ?who empP:birthday ?bday }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/resources/rdb2rdf.properties Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,1 @@
+default-stemuri = http://hr.example/DB/
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/scala/Config.scala Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,43 @@
+package org.w3.sw.sparql2sqlendpoint
+
+import java.util.Properties
+import scala.io.{Source, Codec}
+
+import org.w3.sw.sql.SqlParser
+import org.w3.sw.rdb.RDB.Database
+
+trait ConfigHelper {
+
+ class P(p:Properties) {
+ def getProperty(key:String):Option[String] =
+ try { Some(p.getProperty(key)) } catch { case _ => None }
+ }
+
+ def load(filename:String) = {
+ val prop = new Properties
+ prop.load(this.getClass.getResourceAsStream("/"+filename))
+ new P(prop)
+ }
+
+ def getContent(filename:String):String = {
+ val is = this.getClass.getResourceAsStream("/"+filename)
+ Source.fromInputStream(is)(Codec.UTF8).getLines().mkString("\n")
+ }
+
+}
+
+object Config extends ConfigHelper {
+
+ val rdb2rdfProp = load("rdb2rdf.properties")
+
+ val DDLParser = SqlParser()
+
+ val dbDdl = getContent("ddl.txt")
+
+ val db:org.w3.sw.rdb.RDB.Database = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+
+ val defaultSparqlQuery = getContent("default-sparql-query.txt")
+
+ val defaultStemURI = rdb2rdfProp.getProperty("default-stemuri").get
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/scala/Servlet.scala Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,166 @@
+package org.w3.sw.sparql2sqlendpoint
+
+import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
+import scala.xml.XML
+
+import java.sql.{DriverManager, Connection, Statement, ResultSet, ResultSetMetaData}
+
+import java.sql.{Types => T}
+
+import java.net.URI
+import org.w3.sw.rdb.RDB.{RelName,AttrName}
+import org.w3.sw.sql.SqlParser
+import org.w3.sw.sparql
+import org.w3.sw.sparql.Sparql
+import org.w3.sw.sql
+import org.w3.sw.sparql2sql.{SparqlToSql,StemURI,SqlToXMLRes}
+
+object Control {
+
+ private def using[Closeable <: {def close(): Unit}, B](closeable: Closeable)(getB: Closeable => B): B =
+ try {
+ getB(closeable)
+ } finally {
+ closeable.close()
+ }
+
+ private def bmap[T](test: => Boolean)(block: => T): List[T] = {
+ val ret = new scala.collection.mutable.ListBuffer[T]
+ while(test) ret += block
+ ret.toList
+ }
+
+ /** Executes the SQL and processes the result set using the specified function. */
+ private def query[B](connection: Connection, sql: String)(process: ResultSet => B): B =
+ using (connection) { connection =>
+ using (connection.createStatement) { statement =>
+ using (statement.executeQuery(sql)) { results =>
+ process(results)
+ }
+ }
+ }
+
+ /** Executes the SQL and uses the process function to convert each row into a T. */
+ def queryEach[T](connection: Connection, sql: String)(process: ResultSet => T): List[T] =
+ query(connection, sql) { results =>
+ bmap(results.next) {
+ process(results)
+ }
+ }
+
+ def process(connection: Connection, sql: String,
+ head: List[String] => String,
+ startres: String,
+ binding: (String, String /*, Map[sparql.Assignable, SparqlToSql.SQL2RDFValueMapper], StemURI*/) => String,
+ endres: String, foot: String): String = {
+
+ query(connection, sql) { results =>
+
+ val metadata = results.getMetaData
+
+ val vars:List[String] =
+ (1 to metadata.getColumnCount) map { column => metadata.getColumnLabel(column) } toList
+
+ def processCell(rs:ResultSet, column:Int):Option[String] =
+ try {
+ val name:String = metadata.getColumnLabel(column)
+ val value:String =
+ metadata.getColumnType(column) match {
+ case T.VARCHAR => rs.getString(column)
+ case T.INTEGER => rs.getInt(column).toString
+ case T.DATE => rs.getDate(column).toString
+ case _ => throw new java.lang.Exception("you have to map this type!!!")
+ }
+ Some(binding(name, value))
+ } catch {
+ case _ => None
+ }
+
+ def processRow(rs:ResultSet):String =
+ (1 to metadata.getColumnCount) flatMap { column => processCell(rs, column) } mkString ""
+
+ val rows:List[String] = bmap(results.next) {
+ processRow(results)
+ }
+
+ val body = rows map { startres + _ + endres } mkString ""
+
+ head(vars) + body + foot
+
+ }
+
+ }
+
+}
+
+
+class SparqlEndpoint extends HttpServlet {
+
+ val encoding = "utf-8"
+
+ override def doGet(request:HttpServletRequest, response:HttpServletResponse) {
+
+ request.getParameter("query") match {
+ case null | "" => processIndex(request, response)
+ case query => processSparql(request, response, query)
+ }
+
+ }
+
+ def processSparql(request:HttpServletRequest, response:HttpServletResponse, query:String) {
+
+ val stemURI:StemURI = StemURI(Some(request.getParameter("stemuri")) getOrElse Config.defaultStemURI)
+
+ val sparqlParser = Sparql()
+
+ val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, query).get
+
+ val (generated, rdfmap) = SparqlToSql(Config.db, sparqlSelect, stemURI, true, false)
+
+ Class.forName("com.mysql.jdbc.Driver").newInstance
+ val connection:Connection = DriverManager.getConnection("jdbc:mysql://localhost/rdb2rdf", "root", "")
+
+// if (! connection.isClosed) println("Successfully connected")
+
+ val res = Control.process(connection=connection,
+ sql=generated.toString,
+ head=SqlToXMLRes.head(_),
+ startres=SqlToXMLRes.startresult,
+ binding=SqlToXMLRes.binding(_, _, rdfmap, stemURI),
+ endres=SqlToXMLRes.endresult,
+ foot=SqlToXMLRes.foot)
+
+ response.setContentType("text/plain; charset='" + encoding + "'")
+
+ response.getWriter.write(res)
+
+ }
+
+ def processIndex(request:HttpServletRequest, response:HttpServletResponse) {
+
+ val index =
+ <html xmlns="http://www.w3.org/1999/xhtml">
+ <head><title>RDB2RDF Sparql endpoint</title></head>
+ <body>
+ <h1>RDB2RDF Sparql endpoint</h1>
+ <form action="sparql">
+ <p>
+ StemURI: <input cols="80" name="stemuri" id="stemuri" value={ Config.defaultStemURI } /><br />
+ <textarea rows="10" cols="80" name="query" id="query">{ Config.defaultSparqlQuery }</textarea>
+ <input type="submit" />
+ </p>
+ </form>
+ <hr />
+ <address>
+ <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Jun 2010
+ </address>
+ </body>
+ </html>
+
+ response.setContentType("application/xml; charset='" + encoding + "'")
+
+ XML.write(response.getWriter, index, encoding, false, null)
+
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sqlendpoint/src/main/webapp/WEB-INF/web.xml Sun Oct 31 17:10:02 2010 -0400
@@ -0,0 +1,10 @@
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
+ <servlet>
+ <servlet-name>sparqlendpoint</servlet-name>
+ <servlet-class>org.w3.sparql2sql.servlet.SparqlEndpoint</servlet-class>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>sparqlendpoint</servlet-name>
+ <url-pattern>/sparql/*</url-pattern>
+ </servlet-mapping>
+</web-app>
--- a/sparqlendpoint/src/main/resources/database.properties Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-host = localhost
-user = root
-password =
--- a/sparqlendpoint/src/main/resources/ddl.txt Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,3 +0,0 @@
-CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
-CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
-CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
--- a/sparqlendpoint/src/main/resources/default-sparql-query.txt Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Tasks#>
-PREFIX tass : <http://hr.example/DB/TaskAssignments#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?name ?bday
- WHERE { { ?above tass:employee ?who .
- ?above tass:task ?atask .
- ?atask task:lead ?taskLead .
- ?taskLead empP:lastName ?name }
- UNION
- { ?below tass:task ?btask .
- ?btask task:lead ?who .
- ?below tass:employee ?managed .
- ?managed empP:lastName ?name .
- ?managed empP:birthday ?bday }
- ?who empP:lastName "Smith"^^xsd:string .
- ?who empP:birthday ?bday }
--- a/sparqlendpoint/src/main/resources/rdb2rdf.properties Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-default-stemuri = http://hr.example/DB/
\ No newline at end of file
--- a/sparqlendpoint/src/main/scala/Config.scala Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-package org.w3.sw.sparqlendpoint
-
-import java.util.Properties
-import scala.io.{Source, Codec}
-
-import org.w3.sw.sql.SqlParser
-import org.w3.sw.rdb.RDB.Database
-
-trait ConfigHelper {
-
- class P(p:Properties) {
- def getProperty(key:String):Option[String] =
- try { Some(p.getProperty(key)) } catch { case _ => None }
- }
-
- def load(filename:String) = {
- val prop = new Properties
- prop.load(this.getClass.getResourceAsStream("/"+filename))
- new P(prop)
- }
-
- def getContent(filename:String):String = {
- val is = this.getClass.getResourceAsStream("/"+filename)
- Source.fromInputStream(is)(Codec.UTF8).getLines().mkString("\n")
- }
-
-}
-
-object Config extends ConfigHelper {
-
- val rdb2rdfProp = load("rdb2rdf.properties")
-
- val DDLParser = SqlParser()
-
- val dbDdl = getContent("ddl.txt")
-
- val db:org.w3.sw.rdb.RDB.Database = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
-
- val defaultSparqlQuery = getContent("default-sparql-query.txt")
-
- val defaultStemURI = rdb2rdfProp.getProperty("default-stemuri").get
-
-}
--- a/sparqlendpoint/src/main/scala/Servlet.scala Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,166 +0,0 @@
-package org.w3.sw.sparqlendpoint
-
-import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
-import scala.xml.XML
-
-import java.sql.{DriverManager, Connection, Statement, ResultSet, ResultSetMetaData}
-
-import java.sql.{Types => T}
-
-import java.net.URI
-import org.w3.sw.rdb.RDB.{RelName,AttrName}
-import org.w3.sw.sql.SqlParser
-import org.w3.sw.sparql
-import org.w3.sw.sparql.Sparql
-import org.w3.sw.sql
-import org.w3.sw.sparql2sql.{SparqlToSql,StemURI,SqlToXMLRes}
-
-object Control {
-
- private def using[Closeable <: {def close(): Unit}, B](closeable: Closeable)(getB: Closeable => B): B =
- try {
- getB(closeable)
- } finally {
- closeable.close()
- }
-
- private def bmap[T](test: => Boolean)(block: => T): List[T] = {
- val ret = new scala.collection.mutable.ListBuffer[T]
- while(test) ret += block
- ret.toList
- }
-
- /** Executes the SQL and processes the result set using the specified function. */
- private def query[B](connection: Connection, sql: String)(process: ResultSet => B): B =
- using (connection) { connection =>
- using (connection.createStatement) { statement =>
- using (statement.executeQuery(sql)) { results =>
- process(results)
- }
- }
- }
-
- /** Executes the SQL and uses the process function to convert each row into a T. */
- def queryEach[T](connection: Connection, sql: String)(process: ResultSet => T): List[T] =
- query(connection, sql) { results =>
- bmap(results.next) {
- process(results)
- }
- }
-
- def process(connection: Connection, sql: String,
- head: List[String] => String,
- startres: String,
- binding: (String, String /*, Map[sparql.Assignable, SparqlToSql.SQL2RDFValueMapper], StemURI*/) => String,
- endres: String, foot: String): String = {
-
- query(connection, sql) { results =>
-
- val metadata = results.getMetaData
-
- val vars:List[String] =
- (1 to metadata.getColumnCount) map { column => metadata.getColumnLabel(column) } toList
-
- def processCell(rs:ResultSet, column:Int):Option[String] =
- try {
- val name:String = metadata.getColumnLabel(column)
- val value:String =
- metadata.getColumnType(column) match {
- case T.VARCHAR => rs.getString(column)
- case T.INTEGER => rs.getInt(column).toString
- case T.DATE => rs.getDate(column).toString
- case _ => throw new java.lang.Exception("you have to map this type!!!")
- }
- Some(binding(name, value))
- } catch {
- case _ => None
- }
-
- def processRow(rs:ResultSet):String =
- (1 to metadata.getColumnCount) flatMap { column => processCell(rs, column) } mkString ""
-
- val rows:List[String] = bmap(results.next) {
- processRow(results)
- }
-
- val body = rows map { startres + _ + endres } mkString ""
-
- head(vars) + body + foot
-
- }
-
- }
-
-}
-
-
-class SparqlEndpoint extends HttpServlet {
-
- val encoding = "utf-8"
-
- override def doGet(request:HttpServletRequest, response:HttpServletResponse) {
-
- request.getParameter("query") match {
- case null | "" => processIndex(request, response)
- case query => processSparql(request, response, query)
- }
-
- }
-
- def processSparql(request:HttpServletRequest, response:HttpServletResponse, query:String) {
-
- val stemURI:StemURI = StemURI(Some(request.getParameter("stemuri")) getOrElse Config.defaultStemURI)
-
- val sparqlParser = Sparql()
-
- val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, query).get
-
- val (generated, rdfmap) = SparqlToSql(Config.db, sparqlSelect, stemURI, true, false)
-
- Class.forName("com.mysql.jdbc.Driver").newInstance
- val connection:Connection = DriverManager.getConnection("jdbc:mysql://localhost/rdb2rdf", "root", "")
-
-// if (! connection.isClosed) println("Successfully connected")
-
- val res = Control.process(connection=connection,
- sql=generated.toString,
- head=SqlToXMLRes.head(_),
- startres=SqlToXMLRes.startresult,
- binding=SqlToXMLRes.binding(_, _, rdfmap, stemURI),
- endres=SqlToXMLRes.endresult,
- foot=SqlToXMLRes.foot)
-
- response.setContentType("text/plain; charset='" + encoding + "'")
-
- response.getWriter.write(res)
-
- }
-
- def processIndex(request:HttpServletRequest, response:HttpServletResponse) {
-
- val index =
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head><title>RDB2RDF Sparql endpoint</title></head>
- <body>
- <h1>RDB2RDF Sparql endpoint</h1>
- <form action="sparql">
- <p>
- StemURI: <input cols="80" name="stemuri" id="stemuri" value={ Config.defaultStemURI } /><br />
- <textarea rows="10" cols="80" name="query" id="query">{ Config.defaultSparqlQuery }</textarea>
- <input type="submit" />
- </p>
- </form>
- <hr />
- <address>
- <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Jun 2010
- </address>
- </body>
- </html>
-
- response.setContentType("application/xml; charset='" + encoding + "'")
-
- XML.write(response.getWriter, index, encoding, false, null)
-
- }
-
-}
--- a/sparqlendpoint/src/main/webapp/WEB-INF/web.xml Sun Oct 31 16:10:19 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
- <servlet>
- <servlet-name>sparqlendpoint</servlet-name>
- <servlet-class>org.w3.sparql2sql.servlet.SparqlEndpoint</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>sparqlendpoint</servlet-name>
- <url-pattern>/sparql/*</url-pattern>
- </servlet-mapping>
-</web-app>