+ single-quoted strings
authorEric Prud'hommeaux <eric@w3.org>
Wed, 03 Nov 2010 10:06:56 -0400
changeset 257 781efa593fc6
parent 256 86d60884a3e7
child 258 20255cab7c73
+ single-quoted strings
sql/src/main/scala/SQL.scala
sql/src/test/scala/SQLTest.scala
--- a/sql/src/main/scala/SQL.scala	Wed Nov 03 09:59:49 2010 -0400
+++ b/sql/src/main/scala/SQL.scala	Wed Nov 03 10:06:56 2010 -0400
@@ -8,7 +8,8 @@
 object SQLParsers extends RegexParsers {
 
   val int = """[0-9]+""".r
-  val chars = "\"([^\"\\\\\n\r]|\\\\[tbnrf\\\"'])*\"".r
+  val dquote = "\"([^\"\\\\\n\r]|\\\\[tbnrf\\\"'])*\"".r
+  val squote = "\'([^\'\\\\\n\r]|\\\\[tbnrf\\\''])*\'".r
 }
 
 import SQLParsers._
@@ -395,7 +396,8 @@
   def primaryexpression:Parser[Expression] = (
       fqattribute ^^ { PrimaryExpressionAttr(_) }
     | int ^^ { i => PrimaryExpressionTyped(RDB.Datatype.INTEGER, Name(i)) }
-    | chars  ^^ { x => PrimaryExpressionTyped(RDB.Datatype.STRING, Name(x.substring(1, x.size - 1))) }
+    | dquote  ^^ { x => PrimaryExpressionTyped(RDB.Datatype.STRING, Name(x.substring(1, x.size - 1))) }
+    | squote  ^^ { 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) }
@@ -404,7 +406,8 @@
 
   def value:Parser[RDB.CellValue] = (
       int ^^ { i => RDB.LexicalValue(i) }
-    | chars  ^^ { x => RDB.LexicalValue(x.substring(1, x.size - 1)) }
+    | dquote  ^^ { x => RDB.LexicalValue(x.substring(1, x.size - 1)) }
+    | squote  ^^ { x => RDB.LexicalValue(x.substring(1, x.size - 1)) }
     | "NULL" ^^ { case "NULL" => RDB.␀() }
   )
 
--- a/sql/src/test/scala/SQLTest.scala	Wed Nov 03 09:59:49 2010 -0400
+++ b/sql/src/test/scala/SQLTest.scala	Wed Nov 03 10:06:56 2010 -0400
@@ -604,6 +604,29 @@
   }
 
 
+  test("parse single quotes") {
+    val p = SqlParser()
+    val s = """
+CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
+INSERT INTO Addresses (ID, city, state) VALUES (18, 'Cambridge', 'MA');
+"""
+    val addrs = RDB.Relation(
+      "Addresses",
+      RDB.Header("ID" -> RDB.Datatype.INTEGER,
+		 "city" -> RDB.Datatype.STRING,
+		 "state" -> RDB.Datatype.STRING),
+      List(RDB.Tuple("ID" -> RDB.LexicalValue("18"),
+		     "city" -> RDB.LexicalValue("Cambridge"),
+		     "state" -> RDB.LexicalValue("MA"))),
+      List(RDB.CandidateKey("ID")),
+      Some(RDB.CandidateKey("ID")),
+      RDB.ForeignKeys())
+
+    val expected = RDB.Database(List(addrs))
+    assert(expected === (p.parseAll(p.ddl, s).get))
+  }
+
+
   test("parse 2 People 1 Addresses") {
     val p = SqlParser()
     val s = """