summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-04-26 12:47:53 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-04-26 12:47:53 -0400
commit5d19c132376d05d542b8c18a0b4437455d993da1 (patch)
tree09ec9229b6cc239d30c55e60303b59e677001159 /src
parent91a8659bde5f3d6aba9a356f07cc1afb31d67775 (diff)
Extra checks on assumptions about SQL results
Diffstat (limited to 'src')
-rw-r--r--src/cjr_print.sml55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/cjr_print.sml b/src/cjr_print.sml
index 8ce4433f..c870c3ed 100644
--- a/src/cjr_print.sml
+++ b/src/cjr_print.sml
@@ -418,7 +418,7 @@ fun p_unsql wontLeakStrings env (tAll as (t, loc)) e eLen =
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 @@ fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
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 @@ fun p_exp' par env (e, loc) =
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);",