comparison src/postgres.sml @ 868:06497beb265b

Moved dml code into Settings
author Adam Chlipala <adamc@hcoop.net>
date Sun, 28 Jun 2009 16:22:17 -0400
parents e7f80d78075b
children 64ba57fa20bf
comparison
equal deleted inserted replaced
867:e7f80d78075b 868:06497beb265b
389 newline, 389 newline,
390 queryCommon {loc = loc, numCols = numCols, doCols = doCols, query = box [string "\"", 390 queryCommon {loc = loc, numCols = numCols, doCols = doCols, query = box [string "\"",
391 string (String.toString query), 391 string (String.toString query),
392 string "\""]}] 392 string "\""]}]
393 393
394 fun dmlCommon {loc, dml} =
395 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
396 newline,
397 newline,
398
399 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
400 newline,
401 box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {",
402 box [newline,
403 string "PQclear(res);",
404 newline,
405 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");",
406 newline],
407 string "}",
408 newline,
409 string "PQclear(res);",
410 newline,
411 string "uw_error(ctx, FATAL, \"",
412 string (ErrorMsg.spanToString loc),
413 string ": DML failed:\\n%s\\n%s\", ",
414 dml,
415 string ", PQerrorMessage(conn));",
416 newline],
417 string "}",
418 newline,
419 newline,
420
421 string "PQclear(res);",
422 newline]
423
424 fun dml loc =
425 box [string "PGconn *conn = uw_get_db(ctx);",
426 newline,
427 string "PGresult *res = PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);",
428 newline,
429 newline,
430 dmlCommon {loc = loc, dml = string "dml"}]
431
432 fun dmlPrepared {loc, id, dml, inputs} =
433 box [string "PGconn *conn = uw_get_db(ctx);",
434 newline,
435 string "const int paramFormats[] = { ",
436 p_list_sep (box [string ",", space])
437 (fn t => if isBlob t then string "1" else string "0") inputs,
438 string " };",
439 newline,
440 string "const int paramLengths[] = { ",
441 p_list_sepi (box [string ",", space])
442 (fn i => fn Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size")
443 | Nullable Blob => string ("arg" ^ Int.toString (i + 1)
444 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
445 | _ => string "0") inputs,
446 string " };",
447 newline,
448 string "const char *paramValues[] = { ",
449 p_list_sepi (box [string ",", space])
450 (fn i => fn t => p_ensql t (box [string "arg",
451 string (Int.toString (i + 1))]))
452 inputs,
453 string " };",
454 newline,
455 newline,
456 string "PGresult *res = ",
457 if #persistent (Settings.currentProtocol ()) then
458 box [string "PQexecPrepared(conn, \"uw",
459 string (Int.toString id),
460 string "\", ",
461 string (Int.toString (length inputs)),
462 string ", paramValues, paramLengths, paramFormats, 0);"]
463 else
464 box [string "PQexecParams(conn, \"",
465 string (String.toString dml),
466 string "\", ",
467 string (Int.toString (length inputs)),
468 string ", NULL, paramValues, paramLengths, paramFormats, 0);"],
469 newline,
470 newline,
471 dmlCommon {loc = loc, dml = box [string "\"",
472 string (String.toString dml),
473 string "\""]}]
474
394 val () = addDbms {name = "postgres", 475 val () = addDbms {name = "postgres",
395 header = "postgresql/libpq-fe.h", 476 header = "postgresql/libpq-fe.h",
396 link = "-lpq", 477 link = "-lpq",
397 global_init = box [string "void uw_client_init() { }", 478 global_init = box [string "void uw_client_init() { }",
398 newline], 479 newline],
399 init = init, 480 init = init,
400 query = query, 481 query = query,
401 queryPrepared = queryPrepared} 482 queryPrepared = queryPrepared,
483 dml = dml,
484 dmlPrepared = dmlPrepared}
402 val () = setDbms "postgres" 485 val () = setDbms "postgres"
403 486
404 end 487 end