comparison src/postgres.sml @ 869:64ba57fa20bf

Moved nextval code into Settings
author Adam Chlipala <adamc@hcoop.net>
date Sun, 28 Jun 2009 16:41:10 -0400
parents 06497beb265b
children 7fa9a37a34b3
comparison
equal deleted inserted replaced
868:06497beb265b 869:64ba57fa20bf
470 newline, 470 newline,
471 dmlCommon {loc = loc, dml = box [string "\"", 471 dmlCommon {loc = loc, dml = box [string "\"",
472 string (String.toString dml), 472 string (String.toString dml),
473 string "\""]}] 473 string "\""]}]
474 474
475 fun nextvalCommon {loc, query} =
476 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
477 newline,
478 newline,
479
480 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
481 newline,
482 box [string "PQclear(res);",
483 newline,
484 string "uw_error(ctx, FATAL, \"",
485 string (ErrorMsg.spanToString loc),
486 string ": Query failed:\\n%s\\n%s\", ",
487 query,
488 string ", PQerrorMessage(conn));",
489 newline],
490 string "}",
491 newline,
492 newline,
493
494 string "uw_end_region(ctx);",
495 newline,
496 string "n = PQntuples(res);",
497 newline,
498 string "if (n != 1) {",
499 newline,
500 box [string "PQclear(res);",
501 newline,
502 string "uw_error(ctx, FATAL, \"",
503 string (ErrorMsg.spanToString loc),
504 string ": Wrong number of result rows:\\n%s\\n%s\", ",
505 query,
506 string ", PQerrorMessage(conn));",
507 newline],
508 string "}",
509 newline,
510 newline,
511
512 string "n = uw_Basis_stringToInt_error(ctx, PQgetvalue(res, 0, 0));",
513 newline,
514 string "PQclear(res);",
515 newline]
516
517 fun nextval loc =
518 box [string "PGconn *conn = uw_get_db(ctx);",
519 newline,
520 string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
521 newline,
522 newline,
523 nextvalCommon {loc = loc, query = string "query"}]
524
525 fun nextvalPrepared {loc, id, query} =
526 box [string "PGconn *conn = uw_get_db(ctx);",
527 newline,
528 newline,
529 string "PGresult *res = ",
530 if #persistent (Settings.currentProtocol ()) then
531 box [string "PQexecPrepared(conn, \"uw",
532 string (Int.toString id),
533 string "\", 0, NULL, NULL, NULL, 0);"]
534 else
535 box [string "PQexecParams(conn, \"",
536 string (String.toString query),
537 string "\", 0, NULL, NULL, NULL, NULL, 0);"],
538 newline,
539 newline,
540 nextvalCommon {loc = loc, query = box [string "\"",
541 string (String.toString query),
542 string "\""]}]
543
475 val () = addDbms {name = "postgres", 544 val () = addDbms {name = "postgres",
476 header = "postgresql/libpq-fe.h", 545 header = "postgresql/libpq-fe.h",
477 link = "-lpq", 546 link = "-lpq",
478 global_init = box [string "void uw_client_init() { }", 547 global_init = box [string "void uw_client_init() { }",
479 newline], 548 newline],
480 init = init, 549 init = init,
481 query = query, 550 query = query,
482 queryPrepared = queryPrepared, 551 queryPrepared = queryPrepared,
483 dml = dml, 552 dml = dml,
484 dmlPrepared = dmlPrepared} 553 dmlPrepared = dmlPrepared,
554 nextval = nextval,
555 nextvalPrepared = nextvalPrepared}
485 val () = setDbms "postgres" 556 val () = setDbms "postgres"
486 557
487 end 558 end