~ synched with FeDeRate
authorEric Prud'hommeaux <eric@w3.org>
Fri, 29 Oct 2010 14:59:33 -0400
changeset 39 a0d50a09566b
parent 38 871a0feead53
child 40 eb720a2e4895
~ synched with FeDeRate
src/main/scala/Main.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Fri Oct 15 10:24:49 2010 -0400
+++ b/src/main/scala/Main.scala	Fri Oct 29 14:59:33 2010 -0400
@@ -9,15 +9,21 @@
     def apply (rn:RelName) = m(rn)
     def keySet () = m.keySet
   }
+  object Database {
+    def apply (l:(Relation)*):Database =
+      Database(l.map{r => (r.name -> r)}.toMap)
+  }
 
   case class Relation (name:RelName, header:Header, body:List[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
-  case class Header (m:Map[AttrName, SQLDatatype]) {
+
+  case class Header (m:Map[AttrName, Datatype]) {
     def apply (a:AttrName) = m(a)
     def keySet () = m.keySet
-    def sqlDatatype (a:AttrName) : SQLDatatype = m(a)
+    def sqlDatatype (a:AttrName) : Datatype = m(a)
+    def contains (a:AttrName) : Boolean = m.contains(a)
   }
   object Header {
-    def apply (s:(String, SQLDatatype)*):Header =
+    def apply (s:(String, Datatype)*):Header =
       Header(s.map{p => (AttrName(p._1), p._2)}.toMap)
   }
   case class CandidateKey (attrs:List[AttrName])
@@ -39,15 +45,21 @@
 
   case class Target (rel:RelName, key:CandidateKey)
 
-  sealed abstract class SQLDatatype
-  case class SQLInt () extends SQLDatatype
-  case class SQLFloat () extends SQLDatatype
-  case class SQLDate () extends SQLDatatype
-  case class SQLTime () extends SQLDatatype
-  case class SQLTimestamp () extends SQLDatatype
-  case class SQLChar () extends SQLDatatype
-  case class SQLVarchar () extends SQLDatatype
-  case class SQLString () extends SQLDatatype
+  case class Datatype(name:String) {
+    override def toString = "/* " + name + " */"
+  }
+  object Datatype {
+    val CHAR = Datatype("Char")
+    val VARCHAR = Datatype("Varchar")
+    val STRING = Datatype("String")
+    val INTEGER = Datatype("Int")
+    val FLOAT = Datatype("Float")
+    val DOUBLE = Datatype("Double")
+    val DATE = Datatype("Date")
+    val TIME = Datatype("Time")
+    val TIMESTAMP = Datatype("Timestamp")
+    val DATETIME = Datatype("Datetime")
+  }
 
   case class Tuple (m:Map[AttrName, CellValue]) {
     def apply (a:AttrName) = m(a)
@@ -302,19 +314,19 @@
   def predicatemap (u:StemIRI, rn:RelName, as:List[AttrName]) : IRI =
     u + ("/" + UE(rn) + "#" + as.mkString("_"))
 
-  def XSD (d:SQLDatatype) : IRI =
+  def XSD (d:Datatype) : IRI =
     d match {
-      case SQLInt() => IRI("http://www.w3.org/2001/XMLSchema#int")
-      case SQLFloat() => IRI("http://www.w3.org/2001/XMLSchema#float")
-      case SQLDate() => IRI("http://www.w3.org/2001/XMLSchema#date")
-      case SQLTime() => IRI("http://www.w3.org/2001/XMLSchema#time")
-      case SQLTimestamp() => IRI("http://www.w3.org/2001/XMLSchema#timestamp")
-      case SQLChar() => IRI("http://www.w3.org/2001/XMLSchema#char")
-      case SQLVarchar() => IRI("http://www.w3.org/2001/XMLSchema#varchar")
-      case SQLString() => IRI("http://www.w3.org/2001/XMLSchema#string")
+      case RDB.Datatype.INTEGER => IRI("http://www.w3.org/2001/XMLSchema#int")
+      case RDB.Datatype.FLOAT => IRI("http://www.w3.org/2001/XMLSchema#float")
+      case RDB.Datatype.DATE => IRI("http://www.w3.org/2001/XMLSchema#date")
+      case RDB.Datatype.TIME => IRI("http://www.w3.org/2001/XMLSchema#time")
+      case RDB.Datatype.TIMESTAMP => IRI("http://www.w3.org/2001/XMLSchema#timestamp")
+      case RDB.Datatype.CHAR => IRI("http://www.w3.org/2001/XMLSchema#char")
+      case RDB.Datatype.VARCHAR => IRI("http://www.w3.org/2001/XMLSchema#varchar")
+      case RDB.Datatype.STRING => IRI("http://www.w3.org/2001/XMLSchema#string")
     }
 
-  def literalmap (l:LexicalValue, d:SQLDatatype) : TypedLiteral =
+  def literalmap (l:LexicalValue, d:Datatype) : TypedLiteral =
     TypedLiteral(l.s, XSD(d))
 
   def UE (s:String) : String = s.replaceAll(" ", "+")
--- a/src/test/scala/Test.scala	Fri Oct 15 10:24:49 2010 -0400
+++ b/src/test/scala/Test.scala	Fri Oct 29 14:59:33 2010 -0400
@@ -16,9 +16,9 @@
   test("2 People 1 Addresses") {
 
     val addrs = Relation("Addresses",
-			 Header("ID" -> SQLInt(),
-				"city" -> SQLString(),
-				"state" -> SQLString()),
+			 Header("ID" -> RDB.Datatype.INTEGER,
+				"city" -> RDB.Datatype.STRING,
+				"state" -> RDB.Datatype.STRING),
 			 List(Tuple("ID" -> LexicalValue("18"),
 				    "city" -> LexicalValue("Cambridge"),
 				    "state" -> LexicalValue("MA"))),
@@ -27,9 +27,9 @@
 			 ForeignKeys())
 
     val people = Relation("People",
-			  Header("ID" -> SQLInt(),
-				 "fname" -> SQLString(),
-				 "addr" -> SQLInt()),
+			  Header("ID" -> RDB.Datatype.INTEGER,
+				 "fname" -> RDB.Datatype.STRING,
+				 "addr" -> RDB.Datatype.INTEGER),
 			  List(Tuple("ID" -> LexicalValue("7"),
 				     "fname" -> LexicalValue("Bob"),
 				     "addr" -> LexicalValue("18")),
@@ -62,9 +62,9 @@
   test("2 People 1 Addresses 1 Department") {
 
     val addrs = Relation("Addresses",
-			 Header("ID" -> SQLInt(),
-				"city" -> SQLString(),
-				"state" -> SQLString()),
+			 Header("ID" -> RDB.Datatype.INTEGER,
+				"city" -> RDB.Datatype.STRING,
+				"state" -> RDB.Datatype.STRING),
 			 List(Tuple("ID" -> LexicalValue("18"),
 				    "city" -> LexicalValue("Cambridge"),
 				    "state" -> LexicalValue("MA"))),
@@ -73,11 +73,11 @@
 			 ForeignKeys())
 
     val people = Relation("People",
-			  Header("ID" -> SQLInt(),
-				 "fname" -> SQLString(),
-				 "addr" -> SQLInt(),
-				 "deptName" -> SQLString(),
-				 "deptCity" -> SQLString()),
+			  Header("ID" -> RDB.Datatype.INTEGER,
+				 "fname" -> RDB.Datatype.STRING,
+				 "addr" -> RDB.Datatype.INTEGER,
+				 "deptName" -> RDB.Datatype.STRING,
+				 "deptCity" -> RDB.Datatype.STRING),
 			  List(Tuple("ID" -> LexicalValue("7"),
 				     "fname" -> LexicalValue("Bob"),
 				     "addr" -> LexicalValue("18"),
@@ -94,10 +94,10 @@
 				      List("deptName", "deptCity") -> Target("Department", CandidateKey("name", "city"))))
 
     val department = Relation("Department",
-			      Header("ID" -> SQLInt(),
-				     "name" -> SQLString(),
-				     "city" -> SQLString(),
-				     "manager" -> SQLInt()),
+			      Header("ID" -> RDB.Datatype.INTEGER,
+				     "name" -> RDB.Datatype.STRING,
+				     "city" -> RDB.Datatype.STRING,
+				     "manager" -> RDB.Datatype.INTEGER),
 			      List(Tuple("ID" -> LexicalValue("23"),
 					 "name" -> LexicalValue("accounting"),
 					 "city" -> LexicalValue("Cambridge"),
@@ -138,9 +138,9 @@
   test("2 People 1 Addresses 1 Department 2 Projects 1 Task") {
 
     val addrs = Relation("Addresses",
-			 Header("ID" -> SQLInt(),
-				"city" -> SQLString(),
-				"state" -> SQLString()),
+			 Header("ID" -> RDB.Datatype.INTEGER,
+				"city" -> RDB.Datatype.STRING,
+				"state" -> RDB.Datatype.STRING),
 			 List(Tuple("ID" -> LexicalValue("18"),
 				    "city" -> LexicalValue("Cambridge"),
 				    "state" -> LexicalValue("MA"))),
@@ -149,11 +149,11 @@
 			 ForeignKeys())
 
     val people = Relation("People",
-			  Header("ID" -> SQLInt(),
-				 "fname" -> SQLString(),
-				 "addr" -> SQLInt(),
-				 "deptName" -> SQLString(),
-				 "deptCity" -> SQLString()),
+			  Header("ID" -> RDB.Datatype.INTEGER,
+				 "fname" -> RDB.Datatype.STRING,
+				 "addr" -> RDB.Datatype.INTEGER,
+				 "deptName" -> RDB.Datatype.STRING,
+				 "deptCity" -> RDB.Datatype.STRING),
 			  List(Tuple("ID" -> LexicalValue("7"),
 				     "fname" -> LexicalValue("Bob"),
 				     "addr" -> LexicalValue("18"),
@@ -170,10 +170,10 @@
 				      List("deptName", "deptCity") -> Target("Department", CandidateKey("name", "city"))))
 
     val department = Relation("Department",
-			      Header("ID" -> SQLInt(),
-				     "name" -> SQLString(),
-				     "city" -> SQLString(),
-				     "manager" -> SQLInt()),
+			      Header("ID" -> RDB.Datatype.INTEGER,
+				     "name" -> RDB.Datatype.STRING,
+				     "city" -> RDB.Datatype.STRING,
+				     "manager" -> RDB.Datatype.INTEGER),
 			      List(Tuple("ID" -> LexicalValue("23"),
 					 "name" -> LexicalValue("accounting"),
 					 "city" -> LexicalValue("Cambridge"),
@@ -184,10 +184,10 @@
 			      ForeignKeys(List("manager") -> Target("People", CandidateKey("ID"))))
 
     val projects = Relation("Projects",
-			    Header("lead" -> SQLInt(),
-				   "name" -> SQLString(),
-				   "deptName" -> SQLString(),
-				   "deptCity" -> SQLString()),
+			    Header("lead" -> RDB.Datatype.INTEGER,
+				   "name" -> RDB.Datatype.STRING,
+				   "deptName" -> RDB.Datatype.STRING,
+				   "deptCity" -> RDB.Datatype.STRING),
 			    List(Tuple("lead" -> LexicalValue("8"),
 				       "name" -> LexicalValue("pencil survey"),
 				       "deptName" -> LexicalValue("accounting"),
@@ -205,10 +205,10 @@
 					List("deptName", "deptCity") -> Target("Department", CandidateKey("name", "city"))))
 
     val tasks = Relation("TaskAssignments",
-			 Header("worker" -> SQLInt(),
-				"project" -> SQLString(),
-				"deptName" -> SQLString(),
-				"deptCity" -> SQLString()),
+			 Header("worker" -> RDB.Datatype.INTEGER,
+				"project" -> RDB.Datatype.STRING,
+				"deptName" -> RDB.Datatype.STRING,
+				"deptCity" -> RDB.Datatype.STRING),
 			 List(Tuple("worker" -> LexicalValue("7"),
 				    "project" -> LexicalValue("pencil survey"),
 				    "deptName" -> LexicalValue("accounting"),
@@ -269,9 +269,9 @@
   test("1 People 1 Addresses 1 Offices") {
 
     val addrs = Relation("Addresses",
-			 Header("ID" -> SQLInt(),
-				"city" -> SQLString(),
-				"state" -> SQLString()),
+			 Header("ID" -> RDB.Datatype.INTEGER,
+				"city" -> RDB.Datatype.STRING,
+				"state" -> RDB.Datatype.STRING),
 			 List(Tuple("ID" -> LexicalValue("18"),
 				    "city" -> LexicalValue("Cambridge"),
 				    "state" -> LexicalValue("MA"))),
@@ -280,9 +280,9 @@
 			 ForeignKeys())
 
     val people = Relation("People",
-			  Header("ID" -> SQLInt(),
-				 "fname" -> SQLString(),
-				 "addr" -> SQLInt()),
+			  Header("ID" -> RDB.Datatype.INTEGER,
+				 "fname" -> RDB.Datatype.STRING,
+				 "addr" -> RDB.Datatype.INTEGER),
 			  List(Tuple("ID" -> LexicalValue("7"),
 				     "fname" -> LexicalValue("Bob"),
 				     "addr" -> LexicalValue("18"))),
@@ -291,9 +291,9 @@
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID"))))
 
     val offices = Relation("Offices",
-			   Header("ID" -> SQLInt(),
-				  "building" -> SQLInt(),
-				  "ofcNumber" -> SQLString()),
+			   Header("ID" -> RDB.Datatype.INTEGER,
+				  "building" -> RDB.Datatype.INTEGER,
+				  "ofcNumber" -> RDB.Datatype.STRING),
 			   List(Tuple("ID" -> LexicalValue("18"),
 				      "building" -> LexicalValue("32"),
 				      "ofcNumber" -> LexicalValue("G528"))),
@@ -325,9 +325,9 @@
   test("1 People 1 Addresses 1 Offices 1 ExectutiveOffices") {
 
     val addrs = Relation("Addresses",
-			 Header("ID" -> SQLInt(),
-				"city" -> SQLString(),
-				"state" -> SQLString()),
+			 Header("ID" -> RDB.Datatype.INTEGER,
+				"city" -> RDB.Datatype.STRING,
+				"state" -> RDB.Datatype.STRING),
 			 List(Tuple("ID" -> LexicalValue("18"),
 				    "city" -> LexicalValue("Cambridge"),
 				    "state" -> LexicalValue("MA"))),
@@ -336,9 +336,9 @@
 			 ForeignKeys())
 
     val people = Relation("People",
-			  Header("ID" -> SQLInt(),
-				 "fname" -> SQLString(),
-				 "addr" -> SQLInt()),
+			  Header("ID" -> RDB.Datatype.INTEGER,
+				 "fname" -> RDB.Datatype.STRING,
+				 "addr" -> RDB.Datatype.INTEGER),
 			  List(Tuple("ID" -> LexicalValue("7"),
 				     "fname" -> LexicalValue("Bob"),
 				     "addr" -> LexicalValue("18"))),
@@ -347,9 +347,9 @@
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID"))))
 
     val offices = Relation("Offices",
-			   Header("ID" -> SQLInt(),
-				  "building" -> SQLInt(),
-				  "ofcNumber" -> SQLString()),
+			   Header("ID" -> RDB.Datatype.INTEGER,
+				  "building" -> RDB.Datatype.INTEGER,
+				  "ofcNumber" -> RDB.Datatype.STRING),
 			   List(Tuple("ID" -> LexicalValue("18"),
 				      "building" -> LexicalValue("32"),
 				      "ofcNumber" -> LexicalValue("G528"))),
@@ -358,8 +358,8 @@
 			     ForeignKeys(List("ID") -> Target("Addresses", CandidateKey("ID"))))
 
     val execoffices = Relation("ExecutiveOffices",
-			       Header("ID" -> SQLInt(),
-				      "desk" -> SQLString()),
+			       Header("ID" -> RDB.Datatype.INTEGER,
+				      "desk" -> RDB.Datatype.STRING),
 			       List(Tuple("ID" -> LexicalValue("18"),
 					  "desk" -> LexicalValue("oak"))),
 			       List(CandidateKey("ID")),
@@ -417,9 +417,9 @@
 
   test("2 Employees") {
     val employees = Relation("Employees",
-			     Header("ID" -> SQLInt(),
-				    "fname" -> SQLString(),
-				    "boss" -> SQLInt()),
+			     Header("ID" -> RDB.Datatype.INTEGER,
+				    "fname" -> RDB.Datatype.STRING,
+				    "boss" -> RDB.Datatype.INTEGER),
 			     List(Tuple("ID" -> LexicalValue("1"),
 					"fname" -> LexicalValue("Alice"),
 					"boss" -> ␀()),