aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/postgres.sml
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-06-28 16:41:10 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-06-28 16:41:10 -0400
commitcc1ee568ed458ba1e693074d0dad9a27235b7963 (patch)
treef7c0ceec6bc23f7f071fd62af0d4fe01bc00858e /src/postgres.sml
parentb6bb5d0ed06785194ec9a717232fc95618758c66 (diff)
Moved nextval code into Settings
Diffstat (limited to 'src/postgres.sml')
-rw-r--r--src/postgres.sml73
1 files changed, 72 insertions, 1 deletions
diff --git a/src/postgres.sml b/src/postgres.sml
index 01777843..b1390bc4 100644
--- a/src/postgres.sml
+++ b/src/postgres.sml
@@ -472,6 +472,75 @@ fun dmlPrepared {loc, id, dml, inputs} =
string (String.toString dml),
string "\""]}]
+fun nextvalCommon {loc, query} =
+ box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
+ newline,
+ newline,
+
+ string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
+ newline,
+ box [string "PQclear(res);",
+ newline,
+ string "uw_error(ctx, FATAL, \"",
+ string (ErrorMsg.spanToString loc),
+ string ": Query failed:\\n%s\\n%s\", ",
+ query,
+ string ", PQerrorMessage(conn));",
+ newline],
+ string "}",
+ newline,
+ newline,
+
+ string "uw_end_region(ctx);",
+ newline,
+ string "n = PQntuples(res);",
+ newline,
+ string "if (n != 1) {",
+ newline,
+ box [string "PQclear(res);",
+ newline,
+ string "uw_error(ctx, FATAL, \"",
+ string (ErrorMsg.spanToString loc),
+ string ": Wrong number of result rows:\\n%s\\n%s\", ",
+ query,
+ string ", PQerrorMessage(conn));",
+ newline],
+ string "}",
+ newline,
+ newline,
+
+ string "n = uw_Basis_stringToInt_error(ctx, PQgetvalue(res, 0, 0));",
+ newline,
+ string "PQclear(res);",
+ newline]
+
+fun nextval loc =
+ box [string "PGconn *conn = uw_get_db(ctx);",
+ newline,
+ string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
+ newline,
+ newline,
+ nextvalCommon {loc = loc, query = string "query"}]
+
+fun nextvalPrepared {loc, id, query} =
+ box [string "PGconn *conn = uw_get_db(ctx);",
+ newline,
+ newline,
+ string "PGresult *res = ",
+ if #persistent (Settings.currentProtocol ()) then
+ box [string "PQexecPrepared(conn, \"uw",
+ string (Int.toString id),
+ string "\", 0, NULL, NULL, NULL, 0);"]
+ else
+ box [string "PQexecParams(conn, \"",
+ string (String.toString query),
+ string "\", 0, NULL, NULL, NULL, NULL, 0);"],
+ newline,
+ newline,
+ nextvalCommon {loc = loc, query = box [string "\"",
+ string (String.toString query),
+ string "\""]}]
+
val () = addDbms {name = "postgres",
header = "postgresql/libpq-fe.h",
link = "-lpq",
@@ -481,7 +550,9 @@ val () = addDbms {name = "postgres",
query = query,
queryPrepared = queryPrepared,
dml = dml,
- dmlPrepared = dmlPrepared}
+ dmlPrepared = dmlPrepared,
+ nextval = nextval,
+ nextvalPrepared = nextvalPrepared}
val () = setDbms "postgres"
end