--- a/src/main/scala/SQL.scala Thu Oct 14 08:31:17 2010 -0400
+++ b/src/main/scala/SQL.scala Fri Oct 15 10:11:15 2010 -0400
@@ -13,7 +13,6 @@
def keySet () = m.keySet
}
- case class Relation (name:RelName, header:Header, body:Set[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
case class Header (m:Map[AttrName, SQLDatatype]) {
def apply (a:AttrName) = m(a)
def keySet () = m.keySet
@@ -84,6 +83,8 @@
override def toString = n
}
+ case class Relation (name:RelName, header:Header, body:Set[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
+
}
object SQLParsers extends RegexParsers {
@@ -259,7 +260,12 @@
case class ForeignKey(rel:RDB.RelName, attr:RDB.AttrName) extends ValueDescription
case class DatabaseDesc(relationdescs:Map[RDB.RelName,RelationDesc])
-case class RelationDesc(primarykey:Option[RDB.AttrName], attributes:Map[RDB.AttrName, ValueDescription])
+object DatabaseDesc {
+ def apply (rs:(RelationDesc)*):DatabaseDesc =
+ DatabaseDesc(rs.map{r => (r.name -> r)}.toMap)
+}
+// case class Relation (name:RelName, header:Header, body:Set[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
+case class RelationDesc(name:RDB.RelName, primarykey:Option[RDB.AttrName], attributes:Map[RDB.AttrName, ValueDescription])
sealed abstract class FieldDescOrKeyDeclaration
case class FieldDesc(attr:RDB.AttrName, value:Value, pkness:Boolean) extends FieldDescOrKeyDeclaration
sealed abstract class KeyDeclaration extends FieldDescOrKeyDeclaration
@@ -319,7 +325,7 @@
if (fks.contains(attr)) (attr -> fks(attr))
else (attr -> value)
})
- val rd = RelationDesc(pk, attrs2)
+ val rd = RelationDesc(relation, pk, attrs2)
(relation -> rd)
}
}
--- a/src/test/scala/SQLTest.scala Thu Oct 14 08:31:17 2010 -0400
+++ b/src/test/scala/SQLTest.scala Fri Oct 15 10:11:15 2010 -0400
@@ -6,6 +6,8 @@
class SQLTest extends FunSuite {
+ implicit def string2relName (n:String) = RDB.RelName(n)
+
test("parse ANDexpression") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
val a = Sql()
@@ -429,23 +431,24 @@
val e = """
CREATE TABLE Sex_DE (ID INT PRIMARY KEY)
"""
- val expected =
- (RDB.RelName("Sex_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER))))
- assert(expected === (a.parseAll(a.createtable, e).get))
+ val expected = DatabaseDesc(
+ RelationDesc("Sex_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER))))
+ assert(expected === (a.parseAll(a.ddl, e).get))
}
test("integrated PK") {
+
val a = Sql()
val e = """
CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EnterpriseEntryID INT)
"""
val expected = DatabaseDesc(
- Map(RDB.RelName("Sex_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("EnterpriseEntryID") -> Value(Datatype.INTEGER)))))
+ RelationDesc("Sex_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("EnterpriseEntryID") -> Value(Datatype.INTEGER))))
assert(expected === (a.parseAll(a.ddl, e).get))
}
@@ -456,10 +459,10 @@
CREATE TABLE Sex_DE (ID INT, EnterpriseEntryID INT, PRIMARY KEY (ID))
"""
val expected = DatabaseDesc(
- Map(RDB.RelName("Sex_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("EnterpriseEntryID") -> Value(Datatype.INTEGER)))))
+ RelationDesc("Sex_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("EnterpriseEntryID") -> Value(Datatype.INTEGER))))
assert(expected === (a.parseAll(a.ddl, e).get))
}
@@ -467,21 +470,21 @@
test("foreign key") {
val a = Sql()
val e = """
-CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, PRIMARY KEY (ID), FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
-CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING)
-"""
- val expected:DatabaseDesc = DatabaseDesc(
- Map(RDB.RelName("Person") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("MiddleName") -> Value(Datatype.STRING),
- RDB.AttrName("DateOfBirth") -> Value(Datatype.DATE),
- RDB.AttrName("SexDE") -> ForeignKey(RDB.RelName("Sex_DE"), RDB.AttrName("ID")))),
- RDB.RelName("Sex_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("EntryName") -> Value(Datatype.STRING)))
- ))
+ CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, PRIMARY KEY (ID), FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
+ CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING)
+ """
+ val expected:DatabaseDesc = DatabaseDesc(
+ RelationDesc("Person",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("MiddleName") -> Value(Datatype.STRING),
+ RDB.AttrName("DateOfBirth") -> Value(Datatype.DATE),
+ RDB.AttrName("SexDE") -> ForeignKey(RDB.RelName("Sex_DE"), RDB.AttrName("ID")))),
+ RelationDesc("Sex_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("EntryName") -> Value(Datatype.STRING)))
+ )
assert(expected === (a.parseAll(a.ddl, e).get))
}
@@ -496,37 +499,37 @@
CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
"""
val expected:DatabaseDesc = DatabaseDesc(
- Map(RDB.RelName("Person") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("MiddleName") -> Value(Datatype.STRING),
- RDB.AttrName("DateOfBirth") -> Value(Datatype.DATE),
- RDB.AttrName("SexDE") -> ForeignKey(RDB.RelName("Sex_DE"), RDB.AttrName("ID")))),
- RDB.RelName("Sex_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("EntryName") -> Value(Datatype.STRING))),
- RDB.RelName("Item_Medication") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("PatientID") -> ForeignKey(RDB.RelName("Person"), RDB.AttrName("ID")),
- RDB.AttrName("PerformedDTTM") -> Value(Datatype.DATE),
- RDB.AttrName("EntryName") -> Value(Datatype.STRING))),
- RDB.RelName("Medication") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("ItemID") -> ForeignKey(RDB.RelName("Item_Medication"), RDB.AttrName("ID")),
- RDB.AttrName("MedDictDE") -> ForeignKey(RDB.RelName("Medication_DE"), RDB.AttrName("ID")))),
- RDB.RelName("Medication_DE") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("NDC") -> Value(Datatype.INTEGER))),
- RDB.RelName("NDCcodes") ->
- RelationDesc(Option(RDB.AttrName("ID")),
- Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
- RDB.AttrName("NDC") -> Value(Datatype.INTEGER),
- RDB.AttrName("ingredient") -> Value(Datatype.INTEGER)))
- ))
+ RelationDesc("Person",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("MiddleName") -> Value(Datatype.STRING),
+ RDB.AttrName("DateOfBirth") -> Value(Datatype.DATE),
+ RDB.AttrName("SexDE") -> ForeignKey(RDB.RelName("Sex_DE"), RDB.AttrName("ID")))),
+ RelationDesc("Sex_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("EntryName") -> Value(Datatype.STRING))),
+ RelationDesc("Item_Medication",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("PatientID") -> ForeignKey(RDB.RelName("Person"), RDB.AttrName("ID")),
+ RDB.AttrName("PerformedDTTM") -> Value(Datatype.DATE),
+ RDB.AttrName("EntryName") -> Value(Datatype.STRING))),
+ RelationDesc("Medication",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("ItemID") -> ForeignKey(RDB.RelName("Item_Medication"), RDB.AttrName("ID")),
+ RDB.AttrName("MedDictDE") -> ForeignKey(RDB.RelName("Medication_DE"), RDB.AttrName("ID")))),
+ RelationDesc("Medication_DE",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("NDC") -> Value(Datatype.INTEGER))),
+ RelationDesc("NDCcodes",
+ Option(RDB.AttrName("ID")),
+ Map(RDB.AttrName("ID") -> Value(Datatype.INTEGER),
+ RDB.AttrName("NDC") -> Value(Datatype.INTEGER),
+ RDB.AttrName("ingredient") -> Value(Datatype.INTEGER)))
+ )
assert(expected === (a.parseAll(a.ddl, e).get))
}