comparison src/postgres.sml @ 1293:acabf3935060

tryDml
author Adam Chlipala <adam@chlipala.net>
date Sun, 05 Sep 2010 14:00:57 -0400
parents 514be09d5018
children 929981850d9d
comparison
equal deleted inserted replaced
1292:a671c986f517 1293:acabf3935060
706 newline, 706 newline,
707 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", 707 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
708 string (String.toCString query), 708 string (String.toCString query),
709 string "\""]}] 709 string "\""]}]
710 710
711 fun dmlCommon {loc, dml} = 711 fun dmlCommon {loc, dml, mode} =
712 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");", 712 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
713 newline, 713 newline,
714 newline, 714 newline,
715 715
716 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", 716 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
721 newline, 721 newline,
722 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");", 722 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");",
723 newline], 723 newline],
724 string "}", 724 string "}",
725 newline, 725 newline,
726 string "PQclear(res);", 726 case mode of
727 newline, 727 Settings.Error => box [string "PQclear(res);",
728 string "uw_error(ctx, FATAL, \"", 728 newline,
729 string (ErrorMsg.spanToString loc), 729 string "uw_error(ctx, FATAL, \"",
730 string ": DML failed:\\n%s\\n%s\", ", 730 string (ErrorMsg.spanToString loc),
731 dml, 731 string ": DML failed:\\n%s\\n%s\", ",
732 string ", PQerrorMessage(conn));", 732 dml,
733 string ", PQerrorMessage(conn));"]
734 | Settings.None => string "uw_errmsg = PQerrorMessage(conn);",
733 newline], 735 newline],
734 string "}", 736 string "}",
735 newline, 737 newline,
736 newline, 738 newline,
737 739
738 string "PQclear(res);", 740 string "PQclear(res);",
739 newline] 741 newline]
740 742
741 fun dml loc = 743 fun dml (loc, mode) =
742 box [string "PGconn *conn = uw_get_db(ctx);", 744 box [string "PGconn *conn = uw_get_db(ctx);",
743 newline, 745 newline,
744 string "PGresult *res = PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);", 746 string "PGresult *res = PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);",
745 newline, 747 newline,
746 newline, 748 newline,
747 dmlCommon {loc = loc, dml = string "dml"}] 749 dmlCommon {loc = loc, dml = string "dml", mode = mode}]
748 750
749 fun dmlPrepared {loc, id, dml, inputs} = 751 fun dmlPrepared {loc, id, dml, inputs, mode} =
750 box [string "PGconn *conn = uw_get_db(ctx);", 752 box [string "PGconn *conn = uw_get_db(ctx);",
751 newline, 753 newline,
752 string "const int paramFormats[] = { ", 754 string "const int paramFormats[] = { ",
753 p_list_sep (box [string ",", space]) 755 p_list_sep (box [string ",", space])
754 (fn t => if isBlob t then string "1" else string "0") inputs, 756 (fn t => if isBlob t then string "1" else string "0") inputs,
785 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], 787 string ", NULL, paramValues, paramLengths, paramFormats, 0);"],
786 newline, 788 newline,
787 newline, 789 newline,
788 dmlCommon {loc = loc, dml = box [string "\"", 790 dmlCommon {loc = loc, dml = box [string "\"",
789 string (String.toCString dml), 791 string (String.toCString dml),
790 string "\""]}] 792 string "\""], mode = mode}]
791 793
792 fun nextvalCommon {loc, query} = 794 fun nextvalCommon {loc, query} =
793 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");", 795 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
794 newline, 796 newline,
795 newline, 797 newline,