changeset 277:286f734db702

First query execution (not retrieving results yet)
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 15:29:45 -0400 (2008-09-02)
parents ed4af33681d8
children 137744c5b1ae
files src/cjr_print.sml src/monoize.sml
diffstat 2 files changed, 83 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/cjr_print.sml	Tue Sep 02 14:59:27 2008 -0400
+++ b/src/cjr_print.sml	Tue Sep 02 15:29:45 2008 -0400
@@ -549,7 +549,86 @@
                                     string "})"]
 
       | EQuery {exps, tables, rnum, state, query, body, initial} =>
-        string "(lw_error(ctx, FATAL, \"I would have run a query.\"), NULL)"
+        box [string "({",
+             newline,
+             string "PGconn *conn = lw_get_db(ctx);",
+             newline,
+             string "char *query = ",
+             p_exp env query,
+             string ";",
+             newline,
+             string "int n, i;",
+             newline,
+             p_typ env state,
+             space,
+             string "acc",
+             space,
+             string "=",
+             space,
+             p_exp env initial,
+             string ";",
+             newline,
+             string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 1);",
+             newline,
+             newline,
+
+             string "if (res == NULL) lw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
+             newline,
+             newline,
+
+             string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
+             newline,
+             box [string "PQclear(res);",
+                  newline,
+                  string "lw_error(ctx, FATAL, \"",
+                  string (ErrorMsg.spanToString loc),
+                  string ": Query failed:\\n%s\\n%s\", query, PQerrorMessage(conn));",
+                  newline],
+             string "}",
+             newline,
+             newline,
+
+             string "n = PQntuples(res);",
+             newline,
+             string "for (i = 0; i < n; ++i) {",
+             newline,
+             box [string "struct",
+                  space,
+                  string "__lws_",
+                  string (Int.toString rnum),
+                  space,
+                  string "__lwr_r_",
+                  string (Int.toString (E.countERels env)),
+                  string ";",
+                  newline,
+                  p_typ env state,
+                  space,
+                  string "__lwr_acc_",
+                  string (Int.toString (E.countERels env + 1)),
+                  space,
+                  string "=",
+                  space,
+                  string "acc;",
+                  newline,
+                  newline,
+                  string "acc",
+                  space,
+                  string "=",
+                  space,
+                  p_exp (E.pushERel
+                             (E.pushERel env "r" (TRecord rnum, loc))
+                             "acc" state) 
+                        body,
+                  string ";",
+                  newline],
+             string "}",
+             newline,
+             newline,
+             string "PQclear(res);",
+             newline,
+             string "acc;",
+             newline,
+             string "})"]
 
 and p_exp env = p_exp' false env
 
--- a/src/monoize.sml	Tue Sep 02 14:59:27 2008 -0400
+++ b/src/monoize.sml	Tue Sep 02 15:29:45 2008 -0400
@@ -623,7 +623,7 @@
                                                             @ map (fn (x, xts) =>
                                                                       strcatComma loc
                                                                                   (map (fn (x', _) =>
-                                                                                           sc (x ^ "." ^ x'))
+                                                                                           sc (x ^ ".lw_" ^ x'))
                                                                                        xts)) stables),
                                            sc " FROM ",
                                            strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc),
@@ -650,7 +650,7 @@
                                                strcatComma loc (map (fn (x, xts) =>
                                                                         strcatComma loc
                                                                                     (map (fn (x', _) =>
-                                                                                             sc (x ^ "." ^ x'))
+                                                                                             sc (x ^ ".lw_" ^ x'))
                                                                                          xts)) grouped)
                                                ],
 
@@ -871,7 +871,7 @@
                _), _),
               _), _),
              (L.CName tab, _)), _),
-            (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ "." ^ field)), loc), fm)
+            (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".lw_" ^ field)), loc), fm)
 
           | L.ECApp (
             (L.ECApp (