diff src/cjr_print.sml @ 277:286f734db702

First query execution (not retrieving results yet)
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 15:29:45 -0400
parents ed4af33681d8
children 137744c5b1ae
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