comparison src/postgres.sml @ 1431:4a6f84092399

Represent 'unit' as C 'int'; change pattern match compilation to avoid 'goto'; change Postgres prepared statement compilation to make life easier for the GCC escape analysis; all this in support of better tail call optimization
author Adam Chlipala <adam@chlipala.net>
date Thu, 10 Mar 2011 18:51:15 -0500
parents 58c9c039582a
children 969b90b1f2f9
comparison
equal deleted inserted replaced
1430:36c0a1be3f5a 1431:4a6f84092399
667 e, 667 e,
668 string " == NULL ? NULL : ", 668 string " == NULL ? NULL : ",
669 p_ensql t (box [string "(*", e, string ")"]), 669 p_ensql t (box [string "(*", e, string ")"]),
670 string ")"] 670 string ")"]
671 671
672 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested = _} = 672 fun makeParams inputs =
673 box [string "PGconn *conn = uw_get_db(ctx);", 673 box [string "static const int paramFormats[] = { ",
674 newline,
675 string "const int paramFormats[] = { ",
676 p_list_sep (box [string ",", space]) 674 p_list_sep (box [string ",", space])
677 (fn t => if isBlob t then string "1" else string "0") inputs, 675 (fn t => if isBlob t then string "1" else string "0") inputs,
678 string " };", 676 string " };",
679 newline, 677 newline,
680 string "const int paramLengths[] = { ", 678 if List.exists isBlob inputs then
681 p_list_sepi (box [string ",", space]) 679 box [string "const int *paramLengths = uw_malloc(ctx, ",
682 (fn i => fn Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size") 680 string (Int.toString (length inputs)),
683 | Nullable Blob => string ("arg" ^ Int.toString (i + 1) 681 string " * sizeof(int));",
684 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0") 682 newline,
685 | _ => string "0") inputs, 683 p_list_sepi (box [])
686 string " };", 684 (fn i => fn t =>
687 newline, 685 box [string "paramLengths[",
688 string "const char *paramValues[] = { ", 686 string (Int.toString i),
689 p_list_sepi (box [string ",", space]) 687 string "] = ",
690 (fn i => fn t => p_ensql t (box [string "arg", 688 case t of
691 string (Int.toString (i + 1))])) 689 Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size")
690 | Nullable Blob => string ("arg" ^ Int.toString (i + 1)
691 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
692 | _ => string "0",
693 string ";",
694 newline]) inputs,
695 string " };",
696 newline]
697 else
698 box [string "const int *paramLengths = paramFormats;",
699 newline],
700
701 string "const char **paramValues = uw_malloc(ctx, ",
702 string (Int.toString (length inputs)),
703 string " * sizeof(char*));",
704 newline,
705 p_list_sepi (box [])
706 (fn i => fn t => box [string "paramValues[",
707 string (Int.toString i),
708 string "] = ",
709 p_ensql t (box [string "arg",
710 string (Int.toString (i + 1))]),
711 string ";",
712 newline])
692 inputs, 713 inputs,
693 string " };", 714 newline]
694 newline, 715
716 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested = _} =
717 box [string "PGconn *conn = uw_get_db(ctx);",
718 newline,
719
720 makeParams inputs,
721
695 newline, 722 newline,
696 string "PGresult *res = ", 723 string "PGresult *res = ",
697 if #persistent (Settings.currentProtocol ()) then 724 if #persistent (Settings.currentProtocol ()) then
698 box [string "PQexecPrepared(conn, \"uw", 725 box [string "PQexecPrepared(conn, \"uw",
699 string (Int.toString id), 726 string (Int.toString id),
829 dmlCommon {loc = loc, dml = string "dml", mode = mode}] 856 dmlCommon {loc = loc, dml = string "dml", mode = mode}]
830 857
831 fun dmlPrepared {loc, id, dml, inputs, mode} = 858 fun dmlPrepared {loc, id, dml, inputs, mode} =
832 box [string "PGconn *conn = uw_get_db(ctx);", 859 box [string "PGconn *conn = uw_get_db(ctx);",
833 newline, 860 newline,
834 string "const int paramFormats[] = { ", 861
835 p_list_sep (box [string ",", space]) 862 makeParams inputs,
836 (fn t => if isBlob t then string "1" else string "0") inputs, 863
837 string " };",
838 newline,
839 string "const int paramLengths[] = { ",
840 p_list_sepi (box [string ",", space])
841 (fn i => fn Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size")
842 | Nullable Blob => string ("arg" ^ Int.toString (i + 1)
843 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
844 | _ => string "0") inputs,
845 string " };",
846 newline,
847 string "const char *paramValues[] = { ",
848 p_list_sepi (box [string ",", space])
849 (fn i => fn t => p_ensql t (box [string "arg",
850 string (Int.toString (i + 1))]))
851 inputs,
852 string " };",
853 newline,
854 newline, 864 newline,
855 string "PGresult *res;", 865 string "PGresult *res;",
856 newline, 866 newline,
857 newline, 867 newline,
858 868