+ VARCHAR
authorEric Prud'hommeaux <eric@w3.org>
Wed, 03 Nov 2010 20:30:26 -0400
changeset 265 8b0128cf1dd1
parent 264 060df0861705
child 266 7cb2057195e7
+ VARCHAR
~ fiddle with toString a bit
sql/src/main/scala/SQL.scala
sql/src/test/scala/SQLTest.scala
--- a/sql/src/main/scala/SQL.scala	Wed Nov 03 15:58:58 2010 -0400
+++ b/sql/src/main/scala/SQL.scala	Wed Nov 03 20:30:26 2010 -0400
@@ -28,7 +28,7 @@
     { if (distinct) "DISTINCT " else "" }+
     projection+"\n"+
     tablelist+
-    { if (expression.isDefined) {"\b WHERE " + expression.get + " "} else "" }+
+    { if (expression.isDefined) {" WHERE " + expression.get + " "} else "" }+
     { if (order.size > 0) {order.map(o => o.toString).mkString("ORDER BY ", " ", " ") } else "" }+
     { if (offset.isDefined) {"OFFSET " + offset + " "} else "" }+
     { if (limit.isDefined) {"LIMIT " + limit + " "} else "" }
@@ -62,23 +62,22 @@
   override def toString = n.s /* "'" + n.s + "'" */
 }
 case class TableList(joins:AddOrderedSet[Join]) {
-  override def toString = 
-    if (joins.size == 0) ""
-    else {
-      "  FROM " + joins.foldLeft(("", 0))(
-	(pair, entry) => (pair._1 + {
-	  if (pair._2 == 0) entry.toString.substring(19) // !!! shameless!
-	  else entry
-	}, pair._2+1))._1
-    }
+  override def toString =
+    joins map { entry =>
+      if (entry == joins.list(0)) entry
+      else entry.joinText + " " + entry
+    } mkString ("  FROM ", "\n       ", "")
 }
 
-sealed abstract class Join(res:AliasedResource)
+sealed abstract class Join(res:AliasedResource) {
+  override def toString = res.toString()
+  def joinText():String
+}
 case class InnerJoin(res:AliasedResource, optOn:Option[Expression]) extends Join(res) {
-  override def toString = "\n       INNER JOIN " + res
+  override def joinText () = "INNER JOIN";
 }
 case class LeftOuterJoin(res:AliasedResource, on:Expression) extends Join(res) {
-  override def toString = "\n       LEFT OUTER JOIN " + res + " ON " + on
+  override def joinText () = "LEFT OUTER JOIN";
 }
 
 case class AliasedResource(rel:RelationORSubselect, as:RelVar) {
@@ -264,6 +263,7 @@
     | "DOUBLE" ^^ { case _ => RDB.Datatype.DOUBLE }
     | "STRING" ^^ { case _ => RDB.Datatype.STRING }
     | "CHAR" ~ opt(size) ^^ { case _ => RDB.Datatype.STRING }
+    | "VARCHAR" ~ opt(size) ^^ { case _ => RDB.Datatype.STRING }
     | "DATETIME" ^^ { case _ => RDB.Datatype.DATETIME }
     | "DATE" ^^ { case _ => RDB.Datatype.DATE }
   )
--- a/sql/src/test/scala/SQLTest.scala	Wed Nov 03 15:58:58 2010 -0400
+++ b/sql/src/test/scala/SQLTest.scala	Wed Nov 03 20:30:26 2010 -0400
@@ -630,7 +630,7 @@
   test("parse size modifiers") {
     val p = SqlParser()
     val s = """
-CREATE TABLE Addresses (ID INT(5) PRIMARY KEY, city CHAR(10), state CHAR(2));
+CREATE TABLE Addresses (ID INT(5) PRIMARY KEY, city VARCHAR(10), state CHAR(2));
 INSERT INTO Addresses (ID, city, state) VALUES (18, 'Cambridge', 'MA');
 """
     val addrs = RDB.Relation(