~ disabled primary keys
authorEric Prud'hommeaux <eric@w3.org>
Fri, 04 Jun 2010 18:21:49 -0400
changeset 14 fdc36e0b0a87
parent 13 78cb04cb4b2f
child 15 edc6c17a664d
child 16 add26642a710
~ disabled primary keys
src/main/scala/Main.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Fri Jun 04 15:04:23 2010 -0400
+++ b/src/main/scala/Main.scala	Fri Jun 04 18:21:49 2010 -0400
@@ -37,13 +37,11 @@
 object SQL {
 
   case class Database( m:Map[RelName, Relation] )
-  case class Relation ( header:Header, body:Body, pk:PrimaryKey, fks:ForeignKeys )
+  case class Relation ( header:Header, body:Body )
   case class Header (types:Map[AttrName, SQLDatatype], pk:PrimaryKey, fks:ForeignKeys) {
-    def find (p:((AttrName, SQLDatatype)) ⇒ Boolean) = types.find(p)
     def keySet () = types.keySet
-    def apply (key:AttrName) = types(key)
   }
-  type Header999 = Map[AttrName, SQLDatatype]
+
   type Body = Set[Tuple]
   type PrimaryKey = List[AttrName]
   type ForeignKeys = Map[AttrName, Target]
@@ -55,11 +53,6 @@
 
   type Tuple = Map[AttrName, CellValue]
 
-  sealed abstract class LinkType
-  case class Pk () extends LinkType
-  case class Fk( rn:RelName, a:AttrName ) extends LinkType
-  case class NotLinked () extends LinkType
-
   sealed abstract class SQLDatatype
   case class SQLString () extends SQLDatatype
   case class SQLInt () extends SQLDatatype
@@ -73,8 +66,7 @@
   def body (r:Relation) : Body = r.body
 
   def lexvalue (h:Header, t:Tuple, a:AttrName) : CellValue = t(a)
-  def sqlDatatype (h:Header, a:AttrName) : SQLDatatype = h(a)
-  def linktype (h:Header, a:AttrName) : LinkType = error("not implemented") // h(a)._1
+  def sqlDatatype (h:Header, a:AttrName) : SQLDatatype = h.types(a)
 
 }
 
@@ -100,34 +92,25 @@
     val k = pk(h)
     val s = nodemap(u, rn, k, lexvalue(h, t, k).asInstanceOf[LexicalValue]) // Assume: no NULLs in primary key
 
-    val nulllist = nulls(h, t)
-    val scalarlist = h.keySet -- h.pk -- h.fks.keySet -- nulllist
-    val referencelist = h.fks.keySet -- nulllist
-
-    // scalarlist.map(a => scalartriples2(u, rn, s, a, h, t)) ++
-    // referencelist.map(a => referencetriples2(u, rn, s, a, h, t))
+    // val nulllist = nulls(h, t)
+    // val scalarlist = h.keySet -- h.fks.keySet -- nulllist /* -- h.pk*/
+    // val referencelist = h.fks.keySet -- nulllist
 
-    // scalartriples(u, rn, s, scalars(h, nonnulls(h, t))) ++
-    // referencetriples(u, rn, s, references(h, nonnulls(h, t)))
-
-    // scalars(h, nonnulls(h, t)).map{case(a, l, dt) => scalarmap(u, rn, a, l, dt, s)} ++
-    // references(h, nonnulls(h, t)).map{case(a, l, fk) => referencemap(u, rn, a, l, fk, s)}
-
-    // nonnullscalars(h, t).map{case(a, l, dt) => scalarmap(u, rn, a, l, dt, s)} ++
-    // nonnullreferences(h, t).map{case(a, l, fk) => referencemap(u, rn, a, l, fk, s)}
+    // scalarlist.map(a => scalartriples(u, rn, s, a, h, t)) ++
+    // referencelist.map(a => referencetriples(u, rn, s, a, h, t))
 
     h.keySet.flatMap(a => cellmap(u, rn, h, a, s, t))
   }
 
 /* BEGIN different approaches { */
 
-  def scalartriples2 (u:StemIRI, rn:RelName, s:IRI, a:AttrName, h:Header, t:Tuple) : Triple = {
+  def scalartriples (u:StemIRI, rn:RelName, s:IRI, a:AttrName, h:Header, t:Tuple) : Triple = {
     val p = predicatemap (u, rn, a)
     val l = t(a).asInstanceOf[LexicalValue]
     val o = ObjectLiteral(literalmap(l, sqlDatatype(h, a)))
     Triple(s, p, o)
   }
-  def referencetriples2 (u:StemIRI, rn:RelName, s:IRI, a:AttrName, h:Header, t:Tuple) : Triple = {
+  def referencetriples (u:StemIRI, rn:RelName, s:IRI, a:AttrName, h:Header, t:Tuple) : Triple = {
     val p = predicatemap (u, rn, a)
     val l = t(a).asInstanceOf[LexicalValue]
     val target = h.fks(a)
@@ -143,102 +126,21 @@
       })
   }
 
-  def nonnulls (h:Header, t:Tuple) : Set[(AttrName, LexicalValue)] = {
-    h.keySet.flatMap(a =>
-      lexvalue(h, t, a) match {
-	case ☹() => None
-	case l:LexicalValue => Some((a, l))
-      })
-  }
-
-  def scalars (h:Header, s:Set[(AttrName, LexicalValue)]) : Set[(AttrName, LexicalValue, SQLDatatype)] = {
-    s.flatMap{case(a, l) =>
-      linktype(h, a) match {
-	case Fk(rn2, a2) => None
-	case _ => Some((a, l, sqlDatatype(h, a)))
-      }}
-  }
-
-  def references (h:Header, s:Set[(AttrName, LexicalValue)]) : Set[(AttrName, LexicalValue, Fk)] = {
-    s.flatMap{case(a, l) =>
-      linktype(h, a) match {
-  	case fk:Fk => Some((a, l, fk))
-	case _ => None
-      }}
-  }
-
-  def scalartriples (u:StemIRI, rn:RelName, s:IRI, objs:Set[(AttrName, LexicalValue, SQLDatatype)]) : Set[Triple] = {
-    objs.map{
-      case(a, l, dt) => {
-	val p = predicatemap (u, rn, a)
-	val o = ObjectLiteral(literalmap(l, dt))
-	Triple(s, p, o)
-      }
-    }
-  }
-  def referencetriples (u:StemIRI, rn:RelName, s:IRI, objs:Set[(AttrName, LexicalValue, Fk)]) : Set[Triple] = {
-    objs.map{
-      case(a, l, fk) => {
-	val p = predicatemap (u, rn, a)
-	val Fk(rn2, a2) = fk
-	val o = ObjectIRI(nodemap(u, rn2, a2, l))
-	Triple(s, p, o)
-      }
-    }
-  }
-
-  def scalarmap (u:StemIRI, rn:RelName, a:AttrName, l:LexicalValue, dt:SQLDatatype, s:IRI) : Triple = {
-    val p = predicatemap (u, rn, a)
-    val o = ObjectLiteral(literalmap(l, dt))
-    Triple(s, p, o)
-  }
-  def referencemap (u:StemIRI, rn:RelName, a:AttrName, l:LexicalValue, fk:Fk, s:IRI) : Triple = {
-    val p = predicatemap (u, rn, a)
-    val Fk(rn2, a2) = fk
-    val o = ObjectIRI(nodemap(u, rn2, a2, l))
-    Triple(s, p, o)
-  }
-
-  def nonnullscalars (h:Header, t:Tuple) : Set[(AttrName, LexicalValue, SQLDatatype)] = {
-    h.keySet.flatMap(a =>
+  def cellmap (u:StemIRI, rn:RelName, h:Header, a:AttrName, s:IRI, t:Tuple) : Option[Triple] = {
+    if (h.pk.find(x => x == a) != None)
+      None
+    else
       lexvalue(h, t, a) match {
 	case ☹() => None
 	case l:LexicalValue => {
-	  h.fks.get(a) match {
-  	    case Some(_) => None
-	    case _ => Some((a, l, sqlDatatype(h, a)))
+	  val p = predicatemap (u, rn, a)
+	  val o = h.fks.get(a) match {
+  	    case Some(target:Target) => ObjectIRI(nodemap(u, target.rel, target.attr, l))
+	    case None => ObjectLiteral(literalmap(l, sqlDatatype(h, a)))
 	  }
-	}
-      })
-  }
-
-  def nonnullreferences (h:Header, t:Tuple) : Set[(AttrName, LexicalValue, Fk)] = {
-    h.keySet.flatMap(a =>
-      lexvalue(h, t, a) match {
-      case ☹() => None
-      case l:LexicalValue => {
-	h.fks.get(a) match {
-  	  case Some(target:Target) => Some((a, l, Fk(target.rel, target.attr)))
-	  case _ => None
+	  Some(Triple(s, p, o))
 	}
       }
-    })
-  }
-
-// /* } END different approaches */
-
-  def cellmap (u:StemIRI, rn:RelName, h:Header, a:AttrName, s:IRI, t:Tuple) : Option[Triple] = {
-    lexvalue(h, t, a) match {
-      case ☹() => None
-      case l:LexicalValue => {
-	val p = predicatemap (u, rn, a)
-	val o = h.fks.get(a) match {
-  	  case Some(target:Target) => ObjectIRI(nodemap(u, target.rel, target.attr, l))
-	  case None => ObjectLiteral(literalmap(l, sqlDatatype(h, a)))
-	}
-	Some(Triple(s, p, o))
-      }
-    }
   }
 
   def nodemap (u:StemIRI, rn:RelName, a:AttrName, l:LexicalValue) : IRI =
--- a/src/test/scala/Test.scala	Fri Jun 04 15:04:23 2010 -0400
+++ b/src/test/scala/Test.scala	Fri Jun 04 18:21:49 2010 -0400
@@ -9,16 +9,20 @@
 class Test extends FunSuite {
   test("2 People 1 Addresses") {
 
-    val addresses = Relation(Map("ID" -> (Pk(), SQLInt()),
-				 "city" -> (NotLinked(), SQLString()),
-				 "state" -> (NotLinked(), SQLString())),
+    val addresses = Relation(Header(Map("ID" -> SQLInt(),
+					"city" -> SQLString(),
+					"state" -> SQLString()),
+				    List("ID"),
+				    Map[AttrName, Target]()),
 			     Set(Map("ID" -> LexicalValue("18"),
 				     "city" -> LexicalValue("Cambridge"),
 				     "state" -> LexicalValue("MA"))))
 
-    val people = Relation(Map("ID" -> (Pk(), SQLInt()),
-			      "fname" -> (NotLinked(), SQLString()),
-			      "addr" -> (Fk("Addresses", "ID"), SQLInt())),
+    val people = Relation(Header(Map("ID" -> SQLInt(),
+				     "fname" -> SQLString(),
+				     "addr" -> SQLInt()),
+				 List("ID"),
+				 Map[AttrName, Target]("addr" -> Target("Addresses", "ID"))),
 			  Set(Map("ID" -> LexicalValue("7"),
 				  "fname" -> LexicalValue("Bob"),
 				  "addr" -> LexicalValue("18")),
@@ -32,13 +36,13 @@
     val u = StemIRI("http://foo.example/DB")
     val expected:RDFGraph =
       Set(
-	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#ID"),Literal("7",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+//	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#ID"),Literal("7",IRI("http://www.w3.org/2001/XMLSchema#int"))),
 	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#fname"),Literal("Bob",IRI("http://www.w3.org/2001/XMLSchema#string"))),
 	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#addr"),IRI("http://foo.example/DB/Addresses/ID.18#_")),
-	Triple(IRI("http://foo.example/DB/People/ID.8#_"),IRI("http://foo.example/DB/People#ID"),Literal("8",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+//	Triple(IRI("http://foo.example/DB/People/ID.8#_"),IRI("http://foo.example/DB/People#ID"),Literal("8",IRI("http://www.w3.org/2001/XMLSchema#int"))),
 	Triple(IRI("http://foo.example/DB/People/ID.8#_"),IRI("http://foo.example/DB/People#fname"),Literal("Sue",IRI("http://www.w3.org/2001/XMLSchema#string"))), 
 
-	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#ID"),Literal("18",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+//	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#ID"),Literal("18",IRI("http://www.w3.org/2001/XMLSchema#int"))),
 	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#city"),Literal("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
 	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#state"),Literal("MA",IRI("http://www.w3.org/2001/XMLSchema#string"))))