comparison src/postgres.sml @ 1073:b2311dfb3158

Initializers and setval
author Adam Chlipala <adamc@hcoop.net>
date Sun, 13 Dec 2009 14:20:41 -0500
parents ea9f03ac2710
children db52c32dbe42
comparison
equal deleted inserted replaced
1072:9001966ae1c8 1073:b2311dfb3158
865 newline, 865 newline,
866 nextvalCommon {loc = loc, query = box [string "\"", 866 nextvalCommon {loc = loc, query = box [string "\"",
867 string (String.toString query), 867 string (String.toString query),
868 string "\""]}] 868 string "\""]}]
869 869
870 fun setvalCommon {loc, query} =
871 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating setval result.\");",
872 newline,
873 newline,
874
875 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
876 newline,
877 box [string "PQclear(res);",
878 newline,
879 string "uw_error(ctx, FATAL, \"",
880 string (ErrorMsg.spanToString loc),
881 string ": Query failed:\\n%s\\n%s\", ",
882 query,
883 string ", PQerrorMessage(conn));",
884 newline],
885 string "}",
886 newline,
887 newline,
888
889 string "PQclear(res);",
890 newline]
891
892 fun setval {loc, seqE, count} =
893 let
894 val query = box [string "uw_Basis_strcat(ctx, \"SELECT SETVAL('\", uw_Basis_strcat(ctx, ",
895 seqE,
896 string ", uw_Basis_strcat(ctx, \"', \", uw_Basis_strcat(ctx, uw_Basis_sqlifyInt(ctx, ",
897 count,
898 string "), \")\"))))"]
899 in
900 box [string "char *query = ",
901 query,
902 string ";",
903 newline,
904 string "PGconn *conn = uw_get_db(ctx);",
905 newline,
906 string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
907 newline,
908 newline,
909 setvalCommon {loc = loc, query = string "query"}]
910 end
911
870 fun sqlifyString s = "E'" ^ String.translate (fn #"'" => "\\'" 912 fun sqlifyString s = "E'" ^ String.translate (fn #"'" => "\\'"
871 | #"\\" => "\\\\" 913 | #"\\" => "\\\\"
872 | ch => 914 | ch =>
873 if Char.isPrint ch then 915 if Char.isPrint ch then
874 str ch 916 str ch
890 queryPrepared = queryPrepared, 932 queryPrepared = queryPrepared,
891 dml = dml, 933 dml = dml,
892 dmlPrepared = dmlPrepared, 934 dmlPrepared = dmlPrepared,
893 nextval = nextval, 935 nextval = nextval,
894 nextvalPrepared = nextvalPrepared, 936 nextvalPrepared = nextvalPrepared,
937 setval = setval,
895 sqlifyString = sqlifyString, 938 sqlifyString = sqlifyString,
896 p_cast = p_cast, 939 p_cast = p_cast,
897 p_blank = p_blank, 940 p_blank = p_blank,
898 supportsDeleteAs = true, 941 supportsDeleteAs = true,
899 supportsUpdateAs = true, 942 supportsUpdateAs = true,