Mercurial > urweb
diff src/cjr_print.sml @ 377:78358e5df273
Proper generation of relation names; checking that sequences exist
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Sun, 19 Oct 2008 12:12:59 -0400 |
parents | 6fd102fa28f9 |
children | 168667cdaa95 |
line wrap: on
line diff
--- a/src/cjr_print.sml Sun Oct 19 11:11:49 2008 -0400 +++ b/src/cjr_print.sml Sun Oct 19 12:12:59 2008 -0400 @@ -1778,6 +1778,8 @@ val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts) | _ => NONE) ds + val sequences = List.mapPartial (fn (DSequence s, _) => SOME s + | _ => NONE) ds val validate = box [string "static void uw_db_validate(uw_context ctx) {", @@ -1790,11 +1792,13 @@ p_list_sep newline (fn (s, xts) => let + val sl = CharVector.map Char.toLower s + val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '" - ^ s ^ "'" + ^ sl ^ "'" val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '", - s, + sl, "') AND (", String.concatWith " OR " (map (fn (x, t) => @@ -1808,7 +1812,7 @@ ")"] val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '", - s, + sl, "') AND attname LIKE 'uw_%'"] in box [string "res = PQexec(conn, \"", @@ -1963,6 +1967,65 @@ string "PQclear(res);", newline] end) tables, + + p_list_sep newline + (fn s => + let + val sl = CharVector.map Char.toLower s + + val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '" + ^ sl ^ "' AND relkind = 'S'" + in + box [string "res = PQexec(conn, \"", + string q, + string "\");", + newline, + newline, + string "if (res == NULL) {", + newline, + box [string "PQfinish(conn);", + newline, + string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");", + newline], + string "}", + newline, + newline, + string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {", + newline, + box [string "char msg[1024];", + newline, + string "strncpy(msg, PQerrorMessage(conn), 1024);", + newline, + string "msg[1023] = 0;", + newline, + string "PQclear(res);", + newline, + string "PQfinish(conn);", + newline, + string "uw_error(ctx, FATAL, \"Query failed:\\n", + string q, + string "\\n%s\", msg);", + newline], + string "}", + newline, + newline, + string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {", + newline, + box [string "PQclear(res);", + newline, + string "PQfinish(conn);", + newline, + string "uw_error(ctx, FATAL, \"Sequence '", + string s, + string "' does not exist.\");", + newline], + string "}", + newline, + newline, + string "PQclear(res);", + newline] + end) sequences, + string "}"] in box [string "#include <stdio.h>",