--- a/src/main/scala/Config.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/main/scala/Config.scala Fri Oct 15 16:42:41 2010 -0400
@@ -3,7 +3,8 @@
import java.util.Properties
import scala.io.{Source, Codec}
-import w3c.sw.sql.{Sql,DatabaseDesc}
+import w3c.sw.sql.SqlParser
+import w3c.sw.sql.RDB.Database
trait ConfigHelper {
@@ -29,11 +30,11 @@
val rdb2rdfProp = load("rdb2rdf.properties")
- val DDLParser = Sql()
+ val DDLParser = SqlParser()
val dbDdl = getContent("ddl.txt")
- val db:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+ val db:w3c.sw.sql.RDB.Database = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
val defaultSparqlQuery = getContent("default-sparql-query.txt")
--- a/src/main/scala/SQL.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/main/scala/SQL.scala Fri Oct 15 16:42:41 2010 -0400
@@ -12,14 +12,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 Header (m:Map[AttrName, SQLDatatype]) {
+ case class Relation (name:RelName, header:Header, body:List[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
+
+ 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])
@@ -41,15 +48,20 @@
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 TIMESTAMP = Datatype("Timestamp")
+ val DATETIME = Datatype("Datetime")
+ }
case class Tuple (m:Map[AttrName, CellValue]) {
def apply (a:AttrName) = m(a)
@@ -83,8 +95,6 @@
override def toString = n
}
- case class Relation (name:RelName, header:Header, body:List[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
-
}
object SQLParsers extends RegexParsers {
@@ -218,10 +228,10 @@
case class PrimaryExpressionAttr(fqattribute:RelVarAttr) extends PrimaryExpression {
override def toString = "" + fqattribute
}
-case class PrimaryExpressionTyped(datatype:Datatype, i:Name) extends PrimaryExpression {
+case class PrimaryExpressionTyped(datatype:RDB.Datatype, i:Name) extends PrimaryExpression {
override def toString = /* "'" + i.s + "'" */ /* + datatype */
datatype match {
- case Datatype("Int") => i.s
+ case RDB.Datatype("Int") => i.s
case _ => "\"" + i.s + "\""
}
}
@@ -244,58 +254,36 @@
implicit def fromStringToName(s:String):Name = Name(s)
}
-case class Datatype(name:String) {
- override def toString = "/* " + name + " */"
-}
-object Datatype {
- val STRING = Datatype("String")
- val INTEGER = Datatype("Int")
- val DOUBLE = Datatype("Double")
- val DATE = Datatype("Date")
- val DATETIME = Datatype("Datetime")
-}
-
-// sealed abstract class ValueDescription
-// case class Value(datatype:Datatype) extends ValueDescription
-// case class ForeignKey999(rel:RDB.RelName, attr:RDB.CandidateKey) extends ValueDescription
-
-case class DatabaseDesc(relationdescs:Map[RDB.RelName,RelationDesc])
-object DatabaseDesc {
- def apply (rs:(RelationDesc)*):DatabaseDesc =
- DatabaseDesc(rs.map{r => (r.name -> r)}.toMap)
-}
-// case class Relation (name:RDB.RelName, header:RDB.Header, body:List[RDB.Tuple], candidates:List[RDB.CandidateKey], pk:Option[RDB.CandidateKey], fks:RDB.ForeignKeys)
-case class RelationDesc(name:RDB.RelName, attributes:Map[RDB.AttrName, Datatype], body:List[RDB.Tuple], candidates:List[RDB.CandidateKey], pk:Option[RDB.CandidateKey], fks:RDB.ForeignKeys)
sealed abstract class FieldDescOrKeyDeclaration
-case class FieldDesc(attr:RDB.AttrName, value:Datatype, pkness:Boolean) extends FieldDescOrKeyDeclaration // @@ rename value
+case class FieldDesc(attr:RDB.AttrName, datatype:RDB.Datatype, pkness:Boolean) extends FieldDescOrKeyDeclaration
sealed abstract class KeyDeclaration extends FieldDescOrKeyDeclaration
case class PrimaryKeyDeclaration(key:RDB.CandidateKey) extends KeyDeclaration
case class ForeignKeyDeclaration(fk:List[RDB.AttrName], rel:RDB.RelName, pk:RDB.CandidateKey) extends KeyDeclaration
-case class View(rel:RDB.RelName, defn:SelectORUnion) { // sibling of RelationDesc
+case class View(rel:RDB.RelName, defn:SelectORUnion) { // sibling of RDB.Relation
override def toString = "CREATE VIEW " + rel + " AS\n" + defn
}
-case class Sql() extends JavaTokenParsers {
+case class SqlParser() extends JavaTokenParsers {
def createview:Parser[View] = // @@@ could stick under ddl
"CREATE" ~ "VIEW" ~ relation ~ "AS" ~ selectORunion ^^
{ case "CREATE"~"VIEW"~relation~"AS"~defn => View(relation, defn) }
- def ddl:Parser[DatabaseDesc] =
+ def ddl:Parser[RDB.Database] =
rep1sep(createtable, ";") ~ opt(";") ^^
{
- case l~x => DatabaseDesc(l.foldLeft(Map[RDB.RelName, RelationDesc]())((m, p) => {
- val (rel:RDB.RelName, desc:RelationDesc) = p
+ case l~x => RDB.Database(l.foldLeft(Map[RDB.RelName, RDB.Relation]())((m, p) => {
+ val (rel:RDB.RelName, desc:RDB.Relation) = p
m + (rel -> desc)
}))
}
- def createtable:Parser[(RDB.RelName, RelationDesc)] =
+ def createtable:Parser[(RDB.RelName, RDB.Relation)] =
"CREATE" ~ "TABLE" ~ relation ~ "(" ~ rep1sep(fielddescorkeydef, ",") ~ ")" ^^
{
case "CREATE"~"TABLE"~relation~"("~reldesc~")" => {
val pk0:Option[RDB.CandidateKey] = None
- val attrs0 = Map[RDB.AttrName, Datatype]()
+ val attrs0 = Map[RDB.AttrName, RDB.Datatype]()
val candidates0 = List[RDB.CandidateKey]()
val fks0 = Map[List[RDB.AttrName], RDB.Target]()
/* <pk>: (most recently parsed) PRIMARY KEY
@@ -319,7 +307,7 @@
(pkopt, attrs, candidates, fks + (fk -> RDB.Target(rel, pk)))
}
})
- val rd = RelationDesc(relation, attrs, List(), candidates, pk, RDB.ForeignKeys(fks))
+ val rd = RDB.Relation(relation, RDB.Header(attrs), List(), candidates, pk, RDB.ForeignKeys(fks))
(relation -> rd)
}
}
@@ -333,12 +321,12 @@
{ case "FOREIGN"~"KEY"~"("~fk~")"~"REFERENCES"~relation~"("~pk~")" => ForeignKeyDeclaration(fk, relation, RDB.CandidateKey(pk)) }
)
- def typpe:Parser[Datatype] = (
- "INT" ^^ { case _ => Datatype.INTEGER }
- | "DOUBLE" ^^ { case _ => Datatype.DOUBLE }
- | "STRING" ^^ { case _ => Datatype.STRING }
- | "DATETIME" ^^ { case _ => Datatype.DATETIME }
- | "DATE" ^^ { case _ => Datatype.DATE }
+ def typpe:Parser[RDB.Datatype] = (
+ "INT" ^^ { case _ => RDB.Datatype.INTEGER }
+ | "DOUBLE" ^^ { case _ => RDB.Datatype.DOUBLE }
+ | "STRING" ^^ { case _ => RDB.Datatype.STRING }
+ | "DATETIME" ^^ { case _ => RDB.Datatype.DATETIME }
+ | "DATE" ^^ { case _ => RDB.Datatype.DATE }
)
def selectORunion:Parser[SelectORUnion] =
@@ -469,8 +457,8 @@
def primaryexpression:Parser[Expression] = (
fqattribute ^^ { PrimaryExpressionAttr(_) }
- | int ^^ { i => PrimaryExpressionTyped(Datatype.INTEGER, Name(i)) }
- | chars ^^ { x => PrimaryExpressionTyped(Datatype.STRING, Name(x.substring(1, x.size - 1))) }
+ | int ^^ { i => PrimaryExpressionTyped(RDB.Datatype.INTEGER, Name(i)) }
+ | chars ^^ { x => PrimaryExpressionTyped(RDB.Datatype.STRING, Name(x.substring(1, x.size - 1))) }
| "NULL" ^^ { case "NULL" => ConstNULL() }
| "CONCAT" ~ "(" ~ rep1sep(expression, ",") ~ ")" ^^ { case "CONCAT"~"("~expressions~")" => Concat(expressions) }
| "IF" ~ "(" ~ expression ~ "," ~ expression ~ "," ~ expression ~ ")" ^^ { case "IF"~"("~c~","~p~","~f~")" => IfElse(c, p, f) }
--- a/src/main/scala/Servlet.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/main/scala/Servlet.scala Fri Oct 15 16:42:41 2010 -0400
@@ -9,7 +9,7 @@
import java.net.URI
import w3c.sw.sql.RDB.{RelName,AttrName}
-import w3c.sw.sql.{Sql,DatabaseDesc,RelationDesc,Datatype,Name}
+import w3c.sw.sql.SqlParser
import w3c.sw.sparql
import w3c.sw.sparql.Sparql
import w3c.sw.sql
--- a/src/main/scala/SparqlToSparqlToSql.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/main/scala/SparqlToSparqlToSql.scala Fri Oct 15 16:42:41 2010 -0400
@@ -12,7 +12,7 @@
object SparqlToSparqlToSql {
- def toView (rules:List[sparql.Construct], schema:sql.DatabaseDesc, stemUri:sparql2sql.StemURI) = {
+ def toView (rules:List[sparql.Construct], schema:sql.RDB.Database, stemUri:sparql2sql.StemURI) = {
/* For each rule... */
val disjoints = rules.foldLeft(List[sql.Select]())((allgps, rule) => {
@@ -23,13 +23,13 @@
term match {
case sparql.TermVar(v) => (sparql.TermVar(sparql.Var(label)), List(sparql.Var(label)), List[sql.ProjectAttribute]())
case sparql.TermUri(u) => (term, List[sparql.Var](), List(
- sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name(u.toString)),
+ sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.RDB.Datatype.STRING,sql.Name(u.toString)),
sql.AttrAlias(sql.Name(label)))))
case sparql.TermBNode(b) => (term, List[sparql.Var](), List(
- sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name(b.toString + "(" + triple + ")")),
+ sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.RDB.Datatype.STRING,sql.Name(b.toString + "(" + triple + ")")),
sql.AttrAlias(sql.Name(label)))))
case sparql.TermLit(l) => (term, List[sparql.Var](), List(
- sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.Datatype.STRING,sql.Name(l.toString)),
+ sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.RDB.Datatype.STRING,sql.Name(l.toString)),
sql.AttrAlias(sql.Name(label)))))
}
}
--- a/src/main/scala/SparqlToSql.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/main/scala/SparqlToSql.scala Fri Oct 15 16:42:41 2010 -0400
@@ -243,7 +243,7 @@
R2RState(state.joins,
state.varmap,
state.exprs + sql.RelationalExpressionEq(sql.PrimaryExpressionAttr(constrainMe),
- sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name(u.v.s))))
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name(u.v.s))))
}
/**
@@ -254,7 +254,7 @@
* @param dt sparq datatype, e.g. <code>xsd:integer</code>
* @return state + expression for the URI, e.g. <code>R_18.empid=18</code>
*/
- def literalConstraint(state:R2RState, constrainMe:sql.RelVarAttr, lit:sparql.Literal, dt:sql.Datatype):R2RState = {
+ def literalConstraint(state:R2RState, constrainMe:sql.RelVarAttr, lit:sparql.Literal, dt:sql.RDB.Datatype):R2RState = {
R2RState(state.joins,
state.varmap,
state.exprs + sql.RelationalExpressionEq(sql.PrimaryExpressionAttr(constrainMe),
@@ -279,9 +279,9 @@
* return: (VarAssignable(?emp),RDFNoder(Employee,FullBinding(R_emp.empid)))
* which maps 4 to <http://hr.example/our/favorite/DB/Employee/id.4#record>
*/
- def varConstraint(state:R2RState, alias:sql.RelVar, optAttr:Option[sql.RDB.AttrName], v:sparql.Assignable, db:sql.DatabaseDesc, rel:sql.RDB.RelName):R2RState = {
+ def varConstraint(state:R2RState, alias:sql.RelVar, optAttr:Option[sql.RDB.AttrName], v:sparql.Assignable, db:sql.RDB.Database, rel:sql.RDB.RelName):R2RState = {
val constrainMe = if (optAttr.isDefined) sql.RelVarAttr(alias, optAttr.get) else sql.RelVarAttr(alias, sql.RDB.AttrName("_no_such_attribute"))
- val reldesc = db.relationdescs(rel)
+ val reldesc = db(rel)
val boundTo = FullBinding(constrainMe)
/**
@@ -296,14 +296,14 @@
/** varConstraint(R_patient, Some(SexDE), VarAssignable(?_0_sexEntry), Person) -> RDFNoder(Person,FullBinding(R_patient.SexDE)) */
val sql.RDB.Target(fkrel, fkattr) = reldesc.fks(asKey)
RDFNoder(rel, boundTo)
- } else if (reldesc.attributes.contains(constrainMe.attribute)) {
- reldesc.attributes(constrainMe.attribute) match {
+ } else if (reldesc.header.contains(constrainMe.attribute)) {
+ reldesc.header(constrainMe.attribute) match {
/** varConstraint(R__0_indicDE, Some(NDC), VarAssignable(?_0_indicNDC), Medication_DE) -> IntMapper(FullBinding(R__0_indicDE.NDC)) */
- case sql.Datatype("Int") => IntMapper(boundTo)
+ case sql.RDB.Datatype("Int") => IntMapper(boundTo)
/** varConstraint(R_emp, Some(lastName), VarAssignable(?name), Employee) -> StringMapper(FullBinding(R_emp.lastName)) */
- case sql.Datatype("String") => StringMapper(boundTo)
+ case sql.RDB.Datatype("String") => StringMapper(boundTo)
/** varConstraint(R_patient, Some(DateOfBirth), VarAssignable(?dob), Person) -> DateMapper(FullBinding(R_patient.DateOfBirth)) */
- case sql.Datatype("Date") => DateMapper(boundTo)
+ case sql.RDB.Datatype("Date") => DateMapper(boundTo)
}
} else {
/** Default behavior for unknown attributes. */
@@ -374,7 +374,7 @@
* @return a new R2RState incorporating the new binding
* bindings to RelVarAttrs, and constraints if those variables were
* already bound. */
- def bindOnPredicate(db:sql.DatabaseDesc, stateP:R2RState, triple:sparql.TriplePattern, enforceForeignKeys:Boolean):R2RState = {
+ def bindOnPredicate(db:sql.RDB.Database, stateP:R2RState, triple:sparql.TriplePattern, enforceForeignKeys:Boolean):R2RState = {
val sparql.TriplePattern(s, p, o) = triple
p match {
/** Don't deal with ?s ?p ?o . We could deal with <s> ?p ?o , but haven't yet. */
@@ -398,23 +398,23 @@
val state_postSubj = s match {
case sparql.TermUri(u) =>
/** additional constraint, e.g. R_empid18.empid=18. */
- uriConstraint(stateP, sql.RelVarAttr(relvar, db.relationdescs(rel).pk.get.attrs(0)), parseObjectURI(u)) // !! (0)
+ uriConstraint(stateP, sql.RelVarAttr(relvar, db(rel).pk.get.attrs(0)), parseObjectURI(u)) // !! (0)
case sparql.TermVar(v) =>
/** assignable binding for novel vars, new constraint for previously bound vars. */
try {
- varConstraint(stateP, relvar, __optFirst(db.relationdescs(rel).pk), sparql.VarAssignable(v), db, rel) // !! (0)
+ varConstraint(stateP, relvar, __optFirst(db(rel).pk), sparql.VarAssignable(v), db, rel) // !! (0)
} catch {
case e:java.util.NoSuchElementException =>
/** Tell user that the relation.attribute encoded in the subject was not found in the database description. */
- throw new Exception("error processing { " + s + " " + p + " " + o + " } :db.relationdescs(" + rel + ") not found in " + db)
+ throw new Exception("error processing { " + s + " " + p + " " + o + " } :db(" + rel + ") not found in " + db)
}
case sparql.TermBNode(b) =>
/** assignable binding for novel bnodes, new constraint for previously bound bnodes. */
try {
- varConstraint(stateP, relvar, __optFirst(db.relationdescs(rel).pk), sparql.BNodeAssignable(b), db, rel) // !! (0)
+ varConstraint(stateP, relvar, __optFirst(db(rel).pk), sparql.BNodeAssignable(b), db, rel) // !! (0)
} catch {
case e:java.util.NoSuchElementException =>
- throw new Exception("error processing { " + s + " " + p + " " + o + " } :db.relationdescs(" + rel + ") not found in " + db)
+ throw new Exception("error processing { " + s + " " + p + " " + o + " } :db(" + rel + ") not found in " + db)
}
case _ => error("illegal SPARQL subject: " + s)
}
@@ -423,10 +423,10 @@
* may be redundant as R2RState's joins are a set */
val state_subjJoin = R2RState(state_postSubj.joins + sql.InnerJoin(sql.AliasedResource(rel,relvar), None), state_postSubj.varmap, state_postSubj.exprs)
- try { db.relationdescs(rel).attributes(attr) } catch {
+ try { db(rel).header(attr) } catch {
/** Tell user that the queried attribute was not found in the database description. */
case e:java.util.NoSuchElementException =>
- throw new Exception("error processing { " + s + " " + p + " " + o + " } :db.relationdescs(" + rel + ").attributes(" + attr + ") not found in " + db)
+ throw new Exception("error processing { " + s + " " + p + " " + o + " } :db(" + rel + ").header(" + attr + ") not found in " + db)
}
/**
@@ -435,23 +435,23 @@
*/
val asKey = sql.RDB.CandidateKey(List(attr))
val (targetattr:sql.RelVarAttr, targetrel, dt, state_fkeys:R2RState) =
- if (db.relationdescs(rel).fks.contains(asKey)) { // !! (0)
- val sql.RDB.Target(fkrel, fkattr) = db.relationdescs(rel).fks(asKey)
- try { db.relationdescs(fkrel).attributes(fkattr.attrs(0)) } catch { // !! (0)
+ if (db(rel).fks.contains(asKey)) { // !! (0)
+ val sql.RDB.Target(fkrel, fkattr) = db(rel).fks(asKey)
+ try { db(fkrel).header(fkattr.attrs(0)) } catch { // !! (0)
/** Foreign key relation.attribute was not found in the database description. */
case e:java.util.NoSuchElementException =>
- throw new Exception("db.relationdescs(" + fkrel + ").attributes(" + fkattr + ") not found in " + db)
+ throw new Exception("db(" + fkrel + ").header(" + fkattr + ") not found in " + db)
}
val fkdt =
- if (db.relationdescs(fkrel).fks.contains(fkattr)) {
+ if (db(fkrel).fks.contains(fkattr)) {
/** Foreign key to something which is a foreign key. May have use
* cases, but signal error until we figure out that they are. */
- val sql.RDB.Target(dfkrel, dfkattr) = db.relationdescs(fkrel).fks(fkattr)
+ val sql.RDB.Target(dfkrel, dfkattr) = db(fkrel).fks(fkattr)
error("foreign key " + rel.n + "." + attr.n +
"->" + fkrel.n + "." + fkattr.attrs(0).n + // !! (0)
"->" + dfkrel.n + "." + dfkattr.attrs(0).n) // !! (0)
} else
- db.relationdescs(fkrel).attributes(fkattr(0)) // !! (0)
+ db(fkrel).header(fkattr(0)) // !! (0)
if (enforceForeignKeys) {
/**
* Create an extra join on the foreign key relvar. For instance,
@@ -479,7 +479,7 @@
else
/** not a foreign key, so use the relvarattr and relation calculated
* from the predicate. */
- (objattr, rel, db.relationdescs(rel).attributes(attr), state_subjJoin)
+ (objattr, rel, db(rel).header(attr), state_subjJoin)
o match {
case sparql.TermLit(l) => literalConstraint(state_fkeys, targetattr, l, dt)
@@ -548,24 +548,24 @@
varmap(vvar) match {
case IntMapper(binding) => sql.PrimaryExpressionAttr(bindingToAttribute(binding))
case StringMapper(binding) =>
- sql.Concat(List(sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name("'")),
+ sql.Concat(List(sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name("'")),
sql.PrimaryExpressionAttr(bindingToAttribute(binding)),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name("'^^<http://www.w3.org/2001/XMLSchema#string>"))))
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name("'^^<http://www.w3.org/2001/XMLSchema#string>"))))
case DateMapper(binding) => sql.PrimaryExpressionAttr(bindingToAttribute(binding))
case RDFNoder(relation, binding) =>
- sql.Concat(List(sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name(stem.s)),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),relation.n),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name("/")),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),bindingToAttribute(binding).attribute.n),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name(".")),
+ sql.Concat(List(sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name(stem.s)),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),relation.n),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name("/")),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),bindingToAttribute(binding).attribute.n),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name(".")),
sql.PrimaryExpressionAttr(bindingToAttribute(binding)),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name("#record"))))
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name("#record"))))
case RDFBNoder(relation, binding) =>
- sql.Concat(List(sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name("_:")),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),relation.n),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name(".")),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),bindingToAttribute(binding).attribute.n),
- sql.PrimaryExpressionTyped(sql.Datatype("String"),sql.Name(".")),
+ sql.Concat(List(sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name("_:")),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),relation.n),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name(".")),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),bindingToAttribute(binding).attribute.n),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype("String"),sql.Name(".")),
sql.PrimaryExpressionAttr(bindingToAttribute(binding))))
}
@@ -588,9 +588,9 @@
case sparql.TermLit(sparql.Literal(rdf.RDFLiteral(lit,rdf.Datatype(dt)))) =>
sql.PrimaryExpressionTyped({
dt.toString match {
- case "http://www.w3.org/2001/XMLSchema#string" => sql.Datatype.STRING
- case "http://www.w3.org/2001/XMLSchema#integer" => sql.Datatype.INTEGER
- case "http://www.w3.org/2001/XMLSchema#date" => sql.Datatype.DATE
+ case "http://www.w3.org/2001/XMLSchema#string" => sql.RDB.Datatype.STRING
+ case "http://www.w3.org/2001/XMLSchema#integer" => sql.RDB.Datatype.INTEGER
+ case "http://www.w3.org/2001/XMLSchema#date" => sql.RDB.Datatype.DATE
case _ => error("unable to translate to RDF literal SQL: \"" + lit + "\"^^<" + dt + ">")
}
}, lit)
@@ -725,7 +725,7 @@
* @return a new state including the subquery representing gp in a join.
* This is factored out of mapGraphPattern as it is used for MINUS and probably later for NOT EXISTS.
*/
- def synthesizeOuterJoin(initState:R2RState, gp:sparql.GraphPattern, negate:Boolean, db:sql.DatabaseDesc, enforceForeignKeys:Boolean):R2RState = {
+ def synthesizeOuterJoin(initState:R2RState, gp:sparql.GraphPattern, negate:Boolean, db:sql.RDB.Database, enforceForeignKeys:Boolean):R2RState = {
/** SPARQL OPTIONALs and UNIONs are treated as SQL subselects.
* Set up initial state for this subselect.
*/
@@ -751,7 +751,7 @@
* coreference constraints against ?v2 should only be enforced for
* tuples from the right side of this union.
*/
- val pathNo = sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name("" + initState.joins.size)),
+ val pathNo = sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name("" + initState.joins.size)),
sql.AttrAlias(sql.Name("_DISJOINT_")))
val leftJoinVars = gp.findVars
val attrlist:Set[sql.ProjectAttribute] = leftJoinVars.map(
@@ -795,8 +795,8 @@
val join = sql.LeftOuterJoin(sql.AliasedResource(sql.Subselect(subselect), leftJoinAlias),
outerState2.exprs.size match {
case 0 =>
- sql.RelationalExpressionEq(sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name("1")),
- sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name("1")))
+ sql.RelationalExpressionEq(sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name("1")),
+ sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name("1")))
/** Require corefs unless we have a leading OPTIONAL. */
// if (...)
// else
@@ -823,7 +823,7 @@
* mapGraphPattern maps each of these to an SQL abstract query (which can then be serialized as SQL and executed).
*
*/
- def mapGraphPattern(db:sql.DatabaseDesc, state:R2RState, gp:sparql.GraphPattern, enforceForeignKeys:Boolean):R2RState = {
+ def mapGraphPattern(db:sql.RDB.Database, state:R2RState, gp:sparql.GraphPattern, enforceForeignKeys:Boolean):R2RState = {
gp match {
/** <a href="http://www.w3.org/TR/rdf-sparql-query/#defn_PatternInstanceMapping">Basic Graph Pattern Matching</a>()
@@ -874,7 +874,7 @@
R2RState(state.joins + sql.InnerJoin(sql.AliasedResource(sql.Subselect(
sql.Select(
false,
- sql.Projection(Set(sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name("1")),
+ sql.Projection(Set(sql.ProjectAttribute(sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name("1")),
sql.AttrAlias(sql.Name("_EMPTY_"))))),
sql.TableList(util.AddOrderedSet()),
None, List[sql.OrderElt](), None, None
@@ -927,7 +927,7 @@
val nestedStates = disjoints.map(disjoint => {
val disjointState = mapGraphPattern(db, emptyState, disjoint, enforceForeignKeys)
val disjointVars = disjoint.findVars
- val uniqueConst = sql.PrimaryExpressionTyped(sql.Datatype.INTEGER,sql.Name("" + number))
+ val uniqueConst = sql.PrimaryExpressionTyped(sql.RDB.Datatype.INTEGER,sql.Name("" + number))
number = number + 1 // non-functional, but clearer than wrapping as a parameter in a foldLeft
(disjointState, disjointVars, uniqueConst)
})
@@ -1016,7 +1016,7 @@
* @param concat if true, keys will produce SQL functions to generate a URI, e.g. SELECT CONCAT(stemURI, table, "/", pk, ".", R_who.pk) AS who
* @return an SQL query corresponding to sparquery
*/
- def apply (db:sql.DatabaseDesc, sparquery:sparql.Select, stem:StemURI, enforceForeignKeys:Boolean, concat:Boolean) : (sql.Select, Map[sparql.Assignable, SQL2RDFValueMapper]) = {
+ def apply (db:sql.RDB.Database, sparquery:sparql.Select, stem:StemURI, enforceForeignKeys:Boolean, concat:Boolean) : (sql.Select, Map[sparql.Assignable, SQL2RDFValueMapper]) = {
/** Create an object to hold our compilation state. */
val initState = R2RState(
@@ -1080,9 +1080,9 @@
case sparql.TermLit(sparql.Literal(rdf.RDFLiteral(lit,rdf.Datatype(dt)))) =>
sql.PrimaryExpressionTyped({
dt.toString match {
- case "http://www.w3.org/2001/XMLSchema#string" => sql.Datatype.STRING
- case "http://www.w3.org/2001/XMLSchema#integer" => sql.Datatype.INTEGER
- case "http://www.w3.org/2001/XMLSchema#date" => sql.Datatype.DATE
+ case "http://www.w3.org/2001/XMLSchema#string" => sql.RDB.Datatype.STRING
+ case "http://www.w3.org/2001/XMLSchema#integer" => sql.RDB.Datatype.INTEGER
+ case "http://www.w3.org/2001/XMLSchema#date" => sql.RDB.Datatype.DATE
case _ => error("unable to translate to RDF literal SQL: \"" + lit + "\"^^<" + dt + ">")
}
}, lit)
--- a/src/test/scala/SQLTest.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/test/scala/SQLTest.scala Fri Oct 15 16:42:41 2010 -0400
@@ -7,11 +7,13 @@
class SQLTest extends FunSuite {
implicit def string2relName (n:String) = RDB.RelName(n)
+ implicit def l2db (rs:List[RDB.Relation]):Map[RDB.RelName, RDB.Relation] =
+ rs.map(r => (r.name -> r)).toMap
// implicit def string2attrName (n:String) = RDB.AttrName(n)
test("parse ANDexpression") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
R_manager.id=R_emp.manager AND R_emp.lastName IS NOT NULL AND R_manager.lastName IS NOT NULL
"""
@@ -25,7 +27,7 @@
test("parse ORexpression") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
R_manager.id=R_emp.manager OR R_emp.lastName IS NOT NULL OR R_manager.lastName IS NOT NULL
"""
@@ -39,7 +41,7 @@
test("parse nested expression") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
( R_manager.id=R_emp.manager OR R_emp.lastName IS NOT NULL OR R_manager.lastName IS NOT NULL )
"""
@@ -53,13 +55,13 @@
test("parse no FROM") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT 1 AS _TEST_
"""
val expected = Select(
false,
- Projection(Set(ProjectAttribute(PrimaryExpressionTyped(Datatype.INTEGER,Name("1")),
+ Projection(Set(ProjectAttribute(PrimaryExpressionTyped(RDB.Datatype.INTEGER,Name("1")),
AttrAlias(Name("_TEST_"))))),
TableList(AddOrderedSet()),
None, List[OrderElt](), None, None)
@@ -68,7 +70,7 @@
test("parse WHERE") {
// AliasedResource(Relation(Name("Employee")),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName
FROM Employee AS R_emp
@@ -91,7 +93,7 @@
test("parse INNER JOIN ON") {
// AliasedResource(RDB.RelName("Employee"),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName
FROM Employee AS R_emp
@@ -113,7 +115,7 @@
test("parse SQLbgp") {
// AliasedResource(RDB.RelName("Employee"),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName, R_manager.lastName AS A_managName
FROM Employee AS R_emp
@@ -140,7 +142,7 @@
}
test("parse tup1") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName
FROM Employee AS R_emp
@@ -153,14 +155,14 @@
AttrAlias(Name("A_empName"))))),
TableList(AddOrderedSet(InnerJoin(AliasedResource(RDB.RelName("Employee"),RelVar(Name("R_emp"))), None))),
Some(ExprConjunction(Set(RelationalExpressionEq(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("manager"))),
- PrimaryExpressionTyped(Datatype.INTEGER,Name("18"))),
+ PrimaryExpressionTyped(RDB.Datatype.INTEGER,Name("18"))),
RelationalExpressionNotNull(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("lastName"))))))),
List[OrderElt](), None, None)
assert(expected === (a.parseAll(a.select, e).get))
}
test("parse litConst1") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName
FROM Employee AS R_emp
@@ -177,14 +179,14 @@
Some(ExprConjunction(Set(RelationalExpressionEq(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("manager"))),
PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_manager")),RDB.AttrName("id")))),
RelationalExpressionEq(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_manager")),RDB.AttrName("lastName"))),
- PrimaryExpressionTyped(Datatype.STRING,Name("Johnson"))),
+ PrimaryExpressionTyped(RDB.Datatype.STRING,Name("Johnson"))),
RelationalExpressionNotNull(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("lastName"))))))),
List[OrderElt](), None, None)
assert(expected === (a.parseAll(a.select, e).get))
}
test("parse filter1") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName, R_grandManager.lastName AS A_grandManagName
FROM Employee AS R_emp
@@ -228,7 +230,7 @@
}
test("parse disj1") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_union1.name AS A_name
FROM Employee AS R_who
@@ -283,13 +285,13 @@
Some(ExprConjunction(Set(RelationalExpressionEq(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_union1")),RDB.AttrName("A_who"))),
PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_who")),RDB.AttrName("id")))),
RelationalExpressionEq(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_who")),RDB.AttrName("lastName"))),
- PrimaryExpressionTyped(Datatype.STRING,Name("Smith")))))),
+ PrimaryExpressionTyped(RDB.Datatype.STRING,Name("Smith")))))),
List[OrderElt](), None, None)
assert(expected === (a.parseAll(a.select, e).get))
}
test("parse NULL as A_foo") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_above.manages AS A_who, NULL AS A_bday
FROM Manage AS R_above
@@ -310,20 +312,20 @@
}
test("parse CONCAT") {
- val a = Sql()
+ val a = SqlParser()
val QuotedBaseURI = "\"http://hr.example/DB/\""
val e = """
SELECT CONCAT(""" + QuotedBaseURI + """, "Employee", "/", "id", ".", R_emp.id, "#record") AS A_emp
FROM Employee AS R_emp
"""
val expected = Select(
- false, Projection(Set(ProjectAttribute(Concat(List(PrimaryExpressionTyped(Datatype("String"),Name("http://hr.example/DB/")),
- PrimaryExpressionTyped(Datatype("String"),Name("Employee")),
- PrimaryExpressionTyped(Datatype("String"),Name("/")),
- PrimaryExpressionTyped(Datatype("String"),Name("id")),
- PrimaryExpressionTyped(Datatype("String"),Name(".")),
+ false, Projection(Set(ProjectAttribute(Concat(List(PrimaryExpressionTyped(RDB.Datatype("String"),Name("http://hr.example/DB/")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("Employee")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("/")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("id")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name(".")),
PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("id"))),
- PrimaryExpressionTyped(Datatype("String"),Name("#record")))),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("#record")))),
AttrAlias(Name("A_emp"))))),
TableList(AddOrderedSet(InnerJoin(AliasedResource(RDB.RelName("Employee"),RelVar(Name("R_emp"))), None))),
None, List[OrderElt](), None, None)
@@ -331,7 +333,7 @@
}
test("parse expr disjunction") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_above.manages AS A_who, NULL AS A_bday
FROM Manage AS R_above
@@ -350,16 +352,16 @@
RelationalExpressionNotNull(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_above")),RDB.AttrName("id")))),
ExprConjunction(Set(
RelationalExpressionLt(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_above")),RDB.AttrName("id"))),
- PrimaryExpressionTyped(Datatype.INTEGER,Name("5"))),
+ PrimaryExpressionTyped(RDB.Datatype.INTEGER,Name("5"))),
RelationalExpressionLt(PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_above")),RDB.AttrName("id"))),
- PrimaryExpressionTyped(Datatype.INTEGER,Name("3")))
+ PrimaryExpressionTyped(RDB.Datatype.INTEGER,Name("3")))
))))),
List[OrderElt](), None, None)
assert(expected === (a.parseAll(a.select, e).get))
}
test("parse LEFT OUTER JOIN") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName, R_mang.manageName AS A_manageName
FROM Employee AS R_emp
@@ -387,7 +389,7 @@
}
test("parse LEFT OUTER SELECT") {
- val a = Sql()
+ val a = SqlParser()
val e = """
SELECT R_emp.lastName AS A_empName, R_mang.manageName AS A_manageName
FROM Employee AS R_emp
@@ -419,22 +421,22 @@
}
test("x") {
- val a = Sql()
+ val a = SqlParser()
val e = """
ID INT PRIMARY KEY
"""
- val expected = FieldDesc(RDB.AttrName("ID"), Datatype.INTEGER, true)
+ val expected = FieldDesc(RDB.AttrName("ID"), RDB.Datatype.INTEGER, true)
assert(expected === (a.parseAll(a.fielddescorkeydef, e).get))
}
test("y") {
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE TABLE Sex_DE (ID INT PRIMARY KEY)
"""
- val expected = DatabaseDesc(
- RelationDesc("Sex_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER),
+ val expected = RDB.Database(
+ RDB.Relation("Sex_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
@@ -444,14 +446,14 @@
test("integrated PK") {
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EnterpriseEntryID INT)
"""
- val expected = DatabaseDesc(
- RelationDesc("Sex_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("EnterpriseEntryID") -> Datatype.INTEGER),
+ val expected = RDB.Database(
+ RDB.Relation("Sex_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "EnterpriseEntryID" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
@@ -461,14 +463,14 @@
}
test("post-facto PK") {
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE TABLE Sex_DE (ID INT, EnterpriseEntryID INT, PRIMARY KEY (ID))
"""
- val expected = DatabaseDesc(
- RelationDesc("Sex_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("EnterpriseEntryID") -> Datatype.INTEGER),
+ val expected = RDB.Database(
+ RDB.Relation("Sex_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "EnterpriseEntryID" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
@@ -478,24 +480,24 @@
}
test("foreign key") {
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING)
"""
- val expected:DatabaseDesc = DatabaseDesc(
- RelationDesc("Person",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("MiddleName") -> Datatype.STRING,
- RDB.AttrName("DateOfBirth") -> Datatype.DATE,
- RDB.AttrName("SexDE") -> Datatype.INTEGER),
+ val expected = RDB.Database(
+ RDB.Relation("Person",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "MiddleName" -> RDB.Datatype.STRING,
+ "DateOfBirth" -> RDB.Datatype.DATE,
+ "SexDE" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys(List("SexDE") -> RDB.Target("Sex_DE", RDB.CandidateKey("ID")))),
- RelationDesc("Sex_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("EntryName") -> Datatype.STRING),
+ RDB.Relation("Sex_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "EntryName" -> RDB.Datatype.STRING),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
@@ -505,7 +507,7 @@
}
test("create db") {
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING);
@@ -514,52 +516,52 @@
CREATE TABLE Medication_DE (ID INT PRIMARY KEY, NDC INT);
CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
"""
- val expected:DatabaseDesc = DatabaseDesc(
- RelationDesc("Person",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("MiddleName") -> Datatype.STRING,
- RDB.AttrName("DateOfBirth") -> Datatype.DATE,
- RDB.AttrName("SexDE") -> Datatype.INTEGER),
+ val expected = RDB.Database(
+ RDB.Relation("Person",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "MiddleName" -> RDB.Datatype.STRING,
+ "DateOfBirth" -> RDB.Datatype.DATE,
+ "SexDE" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys(List("SexDE") -> RDB.Target("Sex_DE", RDB.CandidateKey("ID")))),
- RelationDesc("Sex_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("EntryName") -> Datatype.STRING),
+ RDB.Relation("Sex_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "EntryName" -> RDB.Datatype.STRING),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys()),
- RelationDesc("Item_Medication",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("PatientID") -> Datatype.INTEGER,
- RDB.AttrName("PerformedDTTM") -> Datatype.DATE,
- RDB.AttrName("EntryName") -> Datatype.STRING),
+ RDB.Relation("Item_Medication",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "PatientID" -> RDB.Datatype.INTEGER,
+ "PerformedDTTM" -> RDB.Datatype.DATE,
+ "EntryName" -> RDB.Datatype.STRING),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys(List("PatientID") -> RDB.Target("Person", RDB.CandidateKey("ID")))),
- RelationDesc("Medication",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("ItemID") -> Datatype.INTEGER,
- RDB.AttrName("MedDictDE") -> Datatype.INTEGER),
+ RDB.Relation("Medication",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "ItemID" -> RDB.Datatype.INTEGER,
+ "MedDictDE" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys(List("ItemID") -> RDB.Target("Item_Medication", RDB.CandidateKey("ID")),
List("MedDictDE") -> RDB.Target("Medication_DE", RDB.CandidateKey("ID")))),
- RelationDesc("Medication_DE",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("NDC") -> Datatype.INTEGER),
+ RDB.Relation("Medication_DE",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "NDC" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
RDB.ForeignKeys()),
- RelationDesc("NDCcodes",
- Map(RDB.AttrName("ID") -> Datatype.INTEGER,
- RDB.AttrName("NDC") -> Datatype.INTEGER,
- RDB.AttrName("ingredient") -> Datatype.INTEGER),
+ RDB.Relation("NDCcodes",
+ RDB.Header("ID" -> RDB.Datatype.INTEGER,
+ "NDC" -> RDB.Datatype.INTEGER,
+ "ingredient" -> RDB.Datatype.INTEGER),
List(),
List(RDB.CandidateKey("ID")),
Option(RDB.CandidateKey("ID")),
@@ -570,7 +572,7 @@
test("CREATE VIEW") {
// AliasedResource(RDB.RelName("Employee"),RelVar(Name("R_emp")))
- val a = Sql()
+ val a = SqlParser()
val e = """
CREATE VIEW triples AS SELECT
CONCAT("http://hr.example/DB/", "Employee", "/", "empid", ".", R_emp.id, "#record") AS S,
@@ -581,15 +583,15 @@
val expected = View(RDB.RelName("triples"), Select(
false,
Projection(Set(
- ProjectAttribute(Concat(List(PrimaryExpressionTyped(Datatype("String"),Name("http://hr.example/DB/")),
- PrimaryExpressionTyped(Datatype("String"),Name("Employee")),
- PrimaryExpressionTyped(Datatype("String"),Name("/")),
- PrimaryExpressionTyped(Datatype("String"),Name("empid")),
- PrimaryExpressionTyped(Datatype("String"),Name(".")),
+ ProjectAttribute(Concat(List(PrimaryExpressionTyped(RDB.Datatype("String"),Name("http://hr.example/DB/")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("Employee")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("/")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("empid")),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name(".")),
PrimaryExpressionAttr(RelVarAttr(RelVar(Name("R_emp")),RDB.AttrName("id"))),
- PrimaryExpressionTyped(Datatype("String"),Name("#record")))),
+ PrimaryExpressionTyped(RDB.Datatype("String"),Name("#record")))),
AttrAlias(Name("S"))),
- ProjectAttribute(PrimaryExpressionTyped(Datatype.STRING,Name("<http://hr.example/DB/Employee#lastName>")),
+ ProjectAttribute(PrimaryExpressionTyped(RDB.Datatype.STRING,Name("<http://hr.example/DB/Employee#lastName>")),
AttrAlias(Name("P"))),
ProjectAttribute(RelVarAttr(RelVar(Name("R_emp")),
RDB.AttrName("lastName")),
--- a/src/test/scala/SparqlToSparqlToSqlTest.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/test/scala/SparqlToSparqlToSqlTest.scala Fri Oct 15 16:42:41 2010 -0400
@@ -9,8 +9,8 @@
import java.net.URI
import w3c.sw.sparql.Sparql
import w3c.sw.sparql2sparql.{SparqlToSparql,NodePatternMap,NodePattern,SparqlMap}
-import w3c.sw.sql.RDB.{RelName,AttrName}
-import w3c.sw.sql.{Sql,DatabaseDesc,RelationDesc,Datatype,Name}
+import w3c.sw.sql.RDB.{Database,Relation,RelName,AttrName}
+import w3c.sw.sql.SqlParser
import w3c.sw.sparql2sql.{SparqlToSql,StemURI}
import w3c.sw.sparql2sparql2sql.SparqlToSparqlToSql.toView
@@ -19,14 +19,14 @@
*/
class SparqlToSparqlToSqlTest extends FunSuite {
- val DDLParser = Sql()
+ val DDLParser = SqlParser()
val dbDdl = """
CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), firstName STRING, lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
"""
- val HR:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+ val HR:Database = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
test("foaf:last_name FILTER") {
val sparqlParser = Sparql()
@@ -58,7 +58,7 @@
}
""").get
assert(stemQuery == asStem)
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlQuery = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.empid AS emp
FROM Employee AS R_emp
@@ -82,7 +82,7 @@
?who empP:lastName ?lname }
""").get
val view = toView(List(hr2foaf), HR, StemURI("http://hr.example/DB/"))
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val expected = sqlParser.parseAll(sqlParser.createview, """
CREATE VIEW triples AS
SELECT "<http://xmlns.com/foaf/0.1/Person>" AS O, "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>" AS P, CONCAT("http://hr.example/DB/", "Employee", "/", "empid", ".", R_S.empid, "#record") AS S
@@ -115,7 +115,7 @@
CREATE TABLE Medication_DE (ID INT PRIMARY KEY, NDC INT);
CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
"""
- val hosp1:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, hosp1Ddl).get
+ val hosp1:Database = DDLParser.parseAll(DDLParser.ddl, hosp1Ddl).get
val ConstructParser = Sparql()
val db2hl7 = ConstructParser.parseAll(ConstructParser.construct, """
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
@@ -209,7 +209,7 @@
""").get
val asStem = SparqlToSparql(hl7Query, List(SparqlMap(db2hl7, hl7PatternMap)))
assert(asStem === stemQuery)
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlQuery = sqlParser.parseAll(sqlParser.select, """
SELECT R_patient.ID AS patient, R_patient.DateOfBirth AS dob, R__0_sexEntry.EntryName AS sex
FROM Person AS R_patient
@@ -297,7 +297,7 @@
}
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlQuery = sqlParser.parseAll(sqlParser.select, """
SELECT R_patient.ID AS patient, R_patient.DateOfBirth AS dob, R__0_0_indicItem.EntryName AS inclu_name, R__0_0_sexEntry.EntryName AS sex
FROM Person AS R_patient
@@ -345,7 +345,7 @@
println("hospital view...")
val view = toView(List(db2hl7), hosp1, StemURI("http://hospital.example/DB/"))
println("hospital view: " + view)
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val expected = sqlParser.parseAll(sqlParser.createview, """
""").get
assert(expected === view)
@@ -489,7 +489,7 @@
productfeatureproduct ( PRIMARY KEY (product,productFeature) );
producttypeproduct ( PRIMARY KEY (product,productType) );
*/
- val bsbmDb:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, bsbmDdl).get
+ val bsbmDb:Database = DDLParser.parseAll(DDLParser.ddl, bsbmDdl).get
val db2bsbmP = Sparql() // re-use ConstructParser ?
val db2bsbm = db2bsbmP.parseAll(db2bsbmP.construct, """
@@ -617,7 +617,7 @@
LIMIT 10
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlExpected = sqlParser.parseAll(sqlParser.select, """
SELECT DISTINCT R_product.label AS label, R_product.nr AS product
FROM product AS R_product
@@ -685,7 +685,7 @@
LIMIT 10
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlExpected = sqlParser.parseAll(sqlParser.select, """
SELECT DISTINCT R_product.label AS label, R_product.nr AS product, R_feature.productFeature AS feature
FROM product AS R_product
@@ -806,7 +806,7 @@
val sparqlParser = Sparql()
val bsbmQuery = sparqlParser.parseAll(sparqlParser.select, subst(sparql, SPARQL)).get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlExpected = sqlParser.parseAll(sqlParser.select, subst(sql, SQL)).get
val asStem = SparqlToSparql(bsbmQuery, List(SparqlMap(db2bsbm, bsbmPatternMap)))
@@ -862,7 +862,7 @@
val sparqlParser = Sparql()
val bsbmQuery = sparqlParser.parseAll(sparqlParser.select, subst(sparql, SPARQL)).get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val sqlExpected = sqlParser.parseAll(sqlParser.select, subst(sql, SQL)).get
val asDirect = SparqlToSparql(bsbmQuery, List(SparqlMap(db2bsbm, bsbmPatternMap)))
--- a/src/test/scala/SparqlToSqlTest.scala Fri Oct 15 14:58:18 2010 -0400
+++ b/src/test/scala/SparqlToSqlTest.scala Fri Oct 15 16:42:41 2010 -0400
@@ -6,8 +6,8 @@
import org.scalatest.FunSuite
import java.net.URI
-import w3c.sw.sql.RDB.{RelName,AttrName}
-import w3c.sw.sql.{Sql,DatabaseDesc,RelationDesc,Datatype,Name}
+import w3c.sw.sql.RDB.{RelName,AttrName,Database,Relation}
+import w3c.sw.sql.SqlParser
import w3c.sw.sparql.Sparql
import w3c.sw.sparql2sql.{SparqlToSql,StemURI,SqlToXMLRes}
@@ -58,13 +58,13 @@
INSERT INTO TaskAssignments (id, task, employee) VALUES (8, 4, 254);
*/
- val DDLParser = Sql()
+ val DDLParser = SqlParser()
val dbDdl = """
CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
"""
- val db:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+ val db:Database = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
/* The reference RDF representation (queriable with the SPARQL in the tests) is:
*/
@@ -161,7 +161,7 @@
?emp empP:manager <http://hr.example/DB/Employee/empid.18#record>
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.empid AS emp
FROM Employee AS R_emp
@@ -188,7 +188,7 @@
?emp empP:manager <http://hr.example/DB/Employee/empid.18#record>
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.empid AS emp
FROM Employee AS R_emp
@@ -235,7 +235,7 @@
?emp empP:manager <http://hr.example/DB/Employee/empid.18#record>
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT CONCAT("http://hr.example/DB/", "Employee", "/", "empid", ".", R_emp.empid, "#record") AS emp
FROM Employee AS R_emp
@@ -262,7 +262,7 @@
?emp empP:lastName ?name
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT CONCAT("'", R_emp.lastName, "'^^<http://www.w3.org/2001/XMLSchema#string>") AS name
FROM Employee AS R_emp
@@ -291,7 +291,7 @@
<http://hr.example/DB/Employee/empid.253#record> empP:manager ?manager
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_manager.empid AS manager
FROM Employee AS R_empid253
@@ -320,7 +320,7 @@
?emp empP:manager "18"^^xsd:integer
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.empid AS emp
FROM Employee AS R_emp
@@ -348,7 +348,7 @@
FILTER(?task1 < ?task2)
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_task1.id AS task1, R_task2.id AS task2
FROM TaskAssignments AS R_task1
@@ -380,7 +380,7 @@
?manager empP:lastName ?manageName
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName, R_manager.lastName AS manageName
FROM Employee AS R_emp
@@ -409,7 +409,7 @@
?emp empP:manager <http://hr.example/DB/Employee/empid.18#record>
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName
FROM Employee AS R_emp
@@ -435,7 +435,7 @@
?emp empP:manager <http://hr.example/DB/Employee/empid.18#record>
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName
FROM Employee AS R_emp
@@ -465,7 +465,7 @@
?manager empP:lastName "Johnson"^^xsd:string
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName
FROM Employee AS R_emp
@@ -504,7 +504,7 @@
FILTER (?manBday < ?empBday && ?grandManBday < ?manBday)
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName,
R_grandManager.lastName AS grandManagName
@@ -548,7 +548,7 @@
?below tass:employee ?managed .
?managed empP:lastName ?name } }
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT G_union1.name AS name
FROM Employee AS R_who
@@ -617,7 +617,7 @@
?who empP:lastName "Smith"^^xsd:string .
?who empP:birthday ?bday }
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT G_union0.name AS name, G_union0.bday AS bday
FROM (
@@ -672,7 +672,7 @@
?managed empP:birthday ?bday }
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT G_union1.name AS name
FROM Employee AS R_who
@@ -727,7 +727,7 @@
?who empP:birthday ?bday
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT G_union1.name AS name
FROM Employee AS R_who
@@ -771,7 +771,7 @@
?manager emplP:manager ?grandManager .
?grandManager emplP:lastName ?grandManagName } }
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName, G_opt1.grandManagName AS grandManagName, G_opt1.managName AS managName
FROM Employee AS R_emp
@@ -801,7 +801,7 @@
?grandManager emplP:lastName ?grandManagName }
?emp emplP:lastName ?empName }
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName, G_opt1.grandManagName AS grandManagName
FROM ( SELECT 1 AS _EMPTY_ ) AS _EMPTY_
@@ -834,7 +834,7 @@
?grandManager emplP:lastName ?grandManagName } }
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp.lastName AS empName, G_opt1.grandManagName AS grandManagName, G_opt1.managName AS managName
FROM Employee AS R_emp
@@ -875,7 +875,7 @@
?emp4 emplP:birthday ?birthday
FILTER ( ?emp1Name < ?emp2Name && ?emp2Name < ?emp3Name && ?emp3Name < ?emp4Name) }
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_emp1.lastName AS emp1Name, R_emp2.lastName AS emp2Name, R_emp3.lastName AS emp3Name
FROM Employee AS R_emp1
@@ -912,7 +912,7 @@
CREATE TABLE Medication_DE (ID INT PRIMARY KEY, NDC INT);
CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
"""
- val hosp1:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, hosp1Ddl).get
+ val hosp1:Database = DDLParser.parseAll(DDLParser.ddl, hosp1Ddl).get
test("swobjects/tests/healthCare/lists-notBound/db.rq AS OPTIONAL") {
val sparqlParser = Sparql()
@@ -953,7 +953,7 @@
}
}
""").get // FILTER (!BOUND(?disqualItem))
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_patient.ID AS patient
FROM Person AS R_patient
@@ -1029,7 +1029,7 @@
}
}
""").get
- val sqlParser = Sql()
+ val sqlParser = SqlParser()
val parsed = sqlParser.parseAll(sqlParser.select, """
SELECT R_patient.ID AS patient
FROM Person AS R_patient