--- a/src/main/scala/Main.scala Wed Sep 29 00:47:43 2010 -0400
+++ b/src/main/scala/Main.scala Wed Sep 29 08:05:11 2010 -0400
@@ -5,11 +5,11 @@
// Relational structure
object SQL {
- case class Database(m:Map[RelName, Relation]) {
+ case class Database (m:Map[RelName, Relation]) {
def apply (rn:RelName) = m(rn)
def keySet () = m.keySet
- def idxables():Set[RelName] = m.keySet filter { rn => !m(rn).candidates.isEmpty }
}
+
case class Relation (header:Header, body:Body, candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
case class Header (m:Map[AttrName, SQLDatatype]) {
def apply (a:AttrName) = m(a)
@@ -25,7 +25,17 @@
}
implicit def map2fks (m:Map[List[String],Target]) = ForeignKeys(m)
- case class Target(rel:RelName, attrs:CandidateKey)
+ case class Target (rel:RelName, attrs: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
type Body = Set[Tuple]
@@ -51,16 +61,6 @@
case class LexicalValue (s:String) extends CellValue
case class ␀ () extends CellValue
- 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
-
type RelName = String
type AttrName = String
@@ -154,6 +154,8 @@
m(rn)(k)(vs)
}
}
+ implicit def list2map (l:Set[(RelName, KeyMap)]):Map[RelName,KeyMap] = l.toMap
+ implicit def list2Nmap (l:Set[(RelName, KeyMap)]):NodeMap = NodeMap(l)
/** The direct mapping requires one parameter: the StemIRI */
case class StemIRI(stem:String) {
@@ -190,12 +192,13 @@
}
/** The triples-generating functions start with databasemap: */
- def databasemap (u:StemIRI, db:Database) : RDFGraph = {
- val nodes = NodeMap(db.idxables.map(rn => rn -> relation2KeyMap(u, rn, db(rn))).toMap)
- db.keySet.flatMap(rn => relationmap(u, rn, db(rn), nodes, db))
+ def directDB (u:StemIRI, db:Database) : RDFGraph = {
+ val idxables = db.keySet filter { rn => !db(rn).candidates.isEmpty }
+ val nodeMap = idxables map {rn => rn -> relation2KeyMap(u, rn, db(rn))}
+ db.keySet.flatMap(rn => directR(u, rn, db(rn), nodeMap, db))
}
- def relationmap (u:StemIRI, rn:RelName, r:Relation, nodes:NodeMap, db:Database) : RDFGraph =
+ def directR (u:StemIRI, rn:RelName, r:Relation, nodes:NodeMap, db:Database) : RDFGraph =
body(r).flatMap(t => tuplemap(u, rn, t, r, nodes, db))
def tuplemap (u:StemIRI, rn:RelName, t:Tuple, r:Relation, nodes:NodeMap, db:Database) : Set[Triple] = {
--- a/src/test/scala/Test.scala Wed Sep 29 00:47:43 2010 -0400
+++ b/src/test/scala/Test.scala Wed Sep 29 08:05:11 2010 -0400
@@ -35,7 +35,7 @@
val db = Database(Map("Addresses" -> addresses,
"People" -> people))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(
@@ -100,7 +100,7 @@
val db = Database(Map("Addresses" -> addresses,
"People" -> people,
"Department" -> department))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(
@@ -209,7 +209,7 @@
"Department" -> department,
"Projects" -> projects,
"TaskAssignments" -> tasks))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(
@@ -290,7 +290,7 @@
val db = Database(Map("Addresses" -> addresses,
"People" -> people,
"Offices" -> offices))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(
@@ -354,7 +354,7 @@
"People" -> people,
"Offices" -> offices,
"ExecutiveOffices" -> execoffices))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(
@@ -417,7 +417,7 @@
Some(List("ID")),
Map(List("boss") -> Target("Employees", List("ID"))))
val db = Database(Map("Employees" -> employees))
- val g = databasemap(StemIRI("http://foo.example/DB"), db)
+ val g = directDB(StemIRI("http://foo.example/DB"), db)
val expected:RDFGraph =
Set(