+ parse and ignore SQL type size constraints
authorEric Prud'hommeaux <eric@w3.org>
Wed, 03 Nov 2010 11:22:22 -0400
changeset 258 20255cab7c73
parent 257 781efa593fc6
child 260 cf60d13d95da
+ parse and ignore SQL type size constraints
sql/src/main/scala/SQL.scala
sql/src/test/scala/SQLTest.scala
--- a/sql/src/main/scala/SQL.scala	Wed Nov 03 10:06:56 2010 -0400
+++ b/sql/src/main/scala/SQL.scala	Wed Nov 03 11:22:22 2010 -0400
@@ -259,14 +259,18 @@
   )
 
   def typpe:Parser[RDB.Datatype] = (
-      "INT" ^^ { case _ => RDB.Datatype.INTEGER } // alias for INTEGER
-    | "INTEGER" ^^ { case _ => RDB.Datatype.INTEGER }
+      "INT" ~ opt(size) ^^ { case _ => RDB.Datatype.INTEGER } // alias for INTEGER
+    | "INTEGER" ~ opt(size)^^ { case _ => RDB.Datatype.INTEGER }
     | "DOUBLE" ^^ { case _ => RDB.Datatype.DOUBLE }
     | "STRING" ^^ { case _ => RDB.Datatype.STRING }
+    | "CHAR" ~ opt(size) ^^ { case _ => RDB.Datatype.STRING }
     | "DATETIME" ^^ { case _ => RDB.Datatype.DATETIME }
     | "DATE" ^^ { case _ => RDB.Datatype.DATE }
   )
 
+  def size:Parser[Int] =
+    "(" ~ int ~ ")" ^^ { case l ~ s ~ r => s.toInt }
+
   def selectORunion:Parser[SelectORUnion] =
     rep1sep(select, "UNION") ^^ { l => if (l.size == 1) l(0) else Union(l.toSet) }
 
--- a/sql/src/test/scala/SQLTest.scala	Wed Nov 03 10:06:56 2010 -0400
+++ b/sql/src/test/scala/SQLTest.scala	Wed Nov 03 11:22:22 2010 -0400
@@ -627,6 +627,29 @@
   }
 
 
+  test("parse size modifiers") {
+    val p = SqlParser()
+    val s = """
+CREATE TABLE Addresses (ID INT(5) PRIMARY KEY, city CHAR(10), state CHAR(2));
+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 = """