~ s/default.ddl/ddl/ and automcatically extract the schema if -ddl auto
authorAlexandre Bertails <bertails@w3.org>
Fri, 05 Nov 2010 19:49:19 -0400
changeset 270 00f76f463c09
parent 269 c12793e7e6ba
child 271 18e140f8fd6f
~ s/default.ddl/ddl/ and automcatically extract the schema if -ddl auto
sparql2sqlendpoint/src/main/resources/default-config.json
sparql2sqlendpoint/src/main/scala/Servlet.scala
--- a/sparql2sqlendpoint/src/main/resources/default-config.json	Fri Nov 05 18:35:18 2010 -0400
+++ b/sparql2sqlendpoint/src/main/resources/default-config.json	Fri Nov 05 19:49:19 2010 -0400
@@ -20,8 +20,7 @@
 "
   },
   // the ddl used for the direct mapping (default one)
-  "default": {
-    "ddl": "
+  "ddl": "
 CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
 INSERT INTO Addresses (ID, city, state) VALUES (18, 'Cambridge', 'MA');
 CREATE TABLE Department (ID INT PRIMARY KEY, name STRING, city STRING, manager INT,
@@ -51,5 +50,4 @@
                               FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
 INSERT INTO TaskAssignments (worker, project, deptName, deptCity) VALUES (7, 'pencil survey', 'accounting', 'Cambridge');
 "
-  }
 }
--- a/sparql2sqlendpoint/src/main/scala/Servlet.scala	Fri Nov 05 18:35:18 2010 -0400
+++ b/sparql2sqlendpoint/src/main/scala/Servlet.scala	Fri Nov 05 19:49:19 2010 -0400
@@ -125,7 +125,7 @@
 
   val db:org.w3.sw.rdb.RDB.Database = {
     val DDLParser = new org.w3.sw.sql.SqlParser()
-    val ddl = getConfigurationValue("default.ddl")
+    val ddl = getConfigurationValue("ddl")
     DDLParser.parseAll(DDLParser.ddl, ddl).get
   }
 
@@ -221,8 +221,37 @@
 
   def main(args: Array[String]) {
 
+    def createStatements(connection: => Connection) = {
+      import Control._
+
+      val tables = queryEach(connection, "SHOW TABLES;") { row => row.getNString(1) }
+
+      val create = tables flatMap { tableName =>
+        val showCreateTable = "SHOW CREATE TABLE " + tableName + ";"
+        queryEach(connection, showCreateTable) { row => row.getString(2) }
+      }
+
+      /* remove the stuff the SQL parser does not understand yet */
+      val createStatements = create map { _.replaceAll("\\).*?$", ")")
+                                           .replaceAll(" KEY", "")
+                                           .replaceAll("`", "")
+                                           .replaceAll(" DEFAULT .*?,", ",")
+                                           .replaceAll("`", "")
+                                           .replaceAll(" NOT NULL", "") } mkString "\n"
+      createStatements
+    }
+
     val endpoint = new SPARQL2SQLEndpoint() {
-      override def getConfigurationValue(key:String) = getElementAfter(args.toList, "-"+key) getOrElse super.getConfigurationValue(key)
+      override def getConfigurationValue(key:String) = {
+        val value = getElementAfter(args.toList, "-"+key)
+        if (key == "ddl" && value == Some("auto")) {
+          val statements = createStatements(getConnection)
+          println("Found schema:\n" + statements)
+          statements
+        } else {
+          value getOrElse super.getConfigurationValue(key)
+        }
+      }
     }
 
     val message = endpoint.availableKeys map { key => "* " + key } mkString(start="You can override the following parameters by adding -<parameter> <value> on the command line:\n",