changeset 261:ee51e9d35c9b

Monoize ORDER BY
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 16:03:43 -0400
parents 645d0e8da643
children a6cb33f49366
files src/monoize.sml tests/order_by.ur
diffstat 2 files changed, 68 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/monoize.sml	Sun Aug 31 15:47:32 2008 -0400
+++ b/src/monoize.sml	Sun Aug 31 16:03:43 2008 -0400
@@ -555,7 +555,12 @@
                            (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc),
                            s,
                            strcat loc [gf "Rows",
-                                       gf "OrderBy",
+                                       (L'.ECase (gf "OrderBy",
+                                                  [((L'.PPrim (Prim.String ""), loc), sc ""),
+                                                   ((L'.PWild, loc),
+                                                    strcat loc [sc " ORDER BY ",
+                                                                gf "OrderBy"])],
+                                                  {disc = s, result = s}), loc),
                                        gf "Limit",
                                        gf "Offset"]), loc), fm)
             end
@@ -612,7 +617,11 @@
                                 loc),
                                s,
                                strcat loc [sc "SELECT ",
-                                           strcatR loc (gf "SelectExps") sexps,
+                                           strcatComma loc (map (fn (x, t) =>
+                                                                    strcat loc [
+                                                                    (L'.EField (gf "SelectExps", x), loc),
+                                                                    sc (" AS _" ^ x)
+                                                                    ]) sexps),
                                            case sexps of
                                                [] => sc ""
                                              | _ => sc ", ",
@@ -703,6 +712,30 @@
 
           | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
             ((L'.EPrim (Prim.String ""), loc), fm)
+          | L.ECApp (
+            (L.ECApp (
+             (L.ECApp (
+              (L.EFfi ("Basis", "sql_order_by_Cons"), _),
+              _), _),
+             _), _),
+            _) =>
+            let
+                val s = (L'.TFfi ("Basis", "string"), loc)
+                fun sc s = (L'.EPrim (Prim.String s), loc)
+            in
+                ((L'.EAbs ("e1", s, (L'.TFun (s, s), loc),
+                           (L'.EAbs ("e2", s, s,
+                                     (L'.ECase ((L'.ERel 0, loc),
+                                                [((L'.PPrim (Prim.String ""), loc),
+                                                  (L'.ERel 1, loc)),
+                                                 ((L'.PWild, loc),
+                                                  strcat loc [(L'.ERel 1, loc),
+                                                              sc ", ",
+                                                              (L'.ERel 0, loc),
+                                                              sc ")"])],
+                                                {disc = s, result = s}), loc)), loc)), loc),
+                 fm)
+            end
 
           | L.EFfi ("Basis", "sql_no_limit") =>
             ((L'.EPrim (Prim.String ""), loc), fm)
@@ -800,6 +833,18 @@
             (L.ECApp (
              (L.ECApp (
               (L.ECApp (
+               (L.ECApp (
+                (L.EFfi ("Basis", "sql_exp"), _),
+                _), _),
+               _), _),
+              _), _),
+             _), _),
+            (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm)
+
+          | L.ECApp (
+            (L.ECApp (
+             (L.ECApp (
+              (L.ECApp (
                (L.EFfi ("Basis", "sql_relop"), _),
                _), _),
               _), _),
--- a/tests/order_by.ur	Sun Aug 31 15:47:32 2008 -0400
+++ b/tests/order_by.ur	Sun Aug 31 16:03:43 2008 -0400
@@ -10,6 +10,26 @@
 val q4 = (SELECT t1.A, t2.D, t1.A < t2.D AS Lt
         FROM t1, t2
         ORDER BY Lt)
-val q5 = (SELECT t1.A, t2.D, t1.A < t2.D AS Lt
+val q5 = (SELECT t1.A, t1.B, t2.D, t1.A < t2.D AS Lt
         FROM t1, t2
         ORDER BY t1.A, Lt, t2.D)
+
+
+datatype list a = Nil | Cons of a * list a
+
+val r1 : transaction (list string) =
+        query q5
+        (fn fs acc => return (Cons (fs.T1.B, acc)))
+        Nil
+
+val r2 : transaction string =
+        ls <- r1;
+        return (case ls of
+                    Nil => "Problem"
+                  | Cons (b, _) => b)
+
+val main : unit -> transaction page = fn () =>
+        s <- r2;
+        return <html><body>
+                {cdata s}
+        </body></html>