changeset 747:e42f08f96eb5

Extra checks on assumptions about SQL results
author Adam Chlipala <adamc@hcoop.net>
date Sun, 26 Apr 2009 12:47:53 -0400
parents 2c7244c066f1
children 5f9b9972e6b8
files src/cjr_print.sml
diffstat 1 files changed, 47 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/cjr_print.sml	Sun Apr 26 12:35:45 2009 -0400
+++ b/src/cjr_print.sml	Sun Apr 26 12:47:53 2009 -0400
@@ -418,7 +418,7 @@
 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
     case t of
         TOption t =>
-        box [string "(PQgetisnull (res, i, ",
+        box [string "(PQgetisnull(res, i, ",
              string (Int.toString i),
              string ") ? NULL : ",
              case t of
@@ -440,13 +440,30 @@
                            string "})"],
              string ")"]
       | _ =>
-        p_unsql wontLeakStrings env tAll
-                (box [string "PQgetvalue(res, i, ",
-                      string (Int.toString i),
-                      string ")"])
-                (box [string "PQgetlength(res, i, ",
-                      string (Int.toString i),
-                      string ")"])
+        box [string "(PQgetisnull(res, i, ",
+             string (Int.toString i),
+             string ") ? ",
+             box [string "({",
+                  p_typ env tAll,
+                  space,
+                  string "tmp;",
+                  newline,
+                  string "uw_error(ctx, FATAL, \"Unexpectedly NULL field #",
+                  string (Int.toString i),
+                  string "\");",
+                  newline,
+                  string "tmp;",
+                  newline,
+                  string "})"],
+             string " : ",
+             p_unsql wontLeakStrings env tAll
+                     (box [string "PQgetvalue(res, i, ",
+                           string (Int.toString i),
+                           string ")"])
+                     (box [string "PQgetlength(res, i, ",
+                           string (Int.toString i),
+                           string ")"]),
+             string ")"]
 
 datatype sql_type =
          Int
@@ -1571,6 +1588,28 @@
                  newline,
                  newline,
 
+                 string "if (PQnfields(res) != ",
+                 string (Int.toString (length outputs)),
+                 string ") {",
+                 newline,
+                 box [string "int nf = PQnfields(res);",
+                      newline,
+                      string "PQclear(res);",
+                      newline,
+                      string "uw_error(ctx, FATAL, \"",
+                      string (ErrorMsg.spanToString loc),
+                      string ": Query returned %d columns instead of ",
+                      string (Int.toString (length outputs)),
+                      string ":\\n%s\\n%s\", ",
+                      case prepared of
+                          NONE => string "query"
+                        | SOME _ => p_exp env query,
+                      string ", nf, PQerrorMessage(conn));",
+                      newline],
+                 string "}",
+                 newline,
+                 newline,
+
                  string "uw_end_region(ctx);",
                  newline,
                  string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",