Mercurial > urweb
comparison src/postgres.sml @ 1656:3e7c7e200713
Fix handling of quotes in generating C literals
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Mon, 02 Jan 2012 17:08:39 -0500 |
parents | 1bfd793e7eb8 |
children | ac141fbb313a |
comparison
equal
deleted
inserted
replaced
1655:b694f9153faa | 1656:3e7c7e200713 |
---|---|
335 | 335 |
336 p_list_sepi newline (fn i => fn (s, n) => | 336 p_list_sepi newline (fn i => fn (s, n) => |
337 box [string "res = PQprepare(conn, \"uw", | 337 box [string "res = PQprepare(conn, \"uw", |
338 string (Int.toString i), | 338 string (Int.toString i), |
339 string "\", \"", | 339 string "\", \"", |
340 string (String.toCString s), | 340 string (Prim.toCString s), |
341 string "\", ", | 341 string "\", ", |
342 string (Int.toString n), | 342 string (Int.toString n), |
343 string ", NULL);", | 343 string ", NULL);", |
344 newline, | 344 newline, |
345 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", | 345 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", |
353 string "PQclear(res);", | 353 string "PQclear(res);", |
354 newline, | 354 newline, |
355 string "PQfinish(conn);", | 355 string "PQfinish(conn);", |
356 newline, | 356 newline, |
357 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n", | 357 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n", |
358 string (String.toCString s), | 358 string (Prim.toCString s), |
359 string "\\n%s\", msg);", | 359 string "\\n%s\", msg);", |
360 newline], | 360 newline], |
361 string "}", | 361 string "}", |
362 newline, | 362 newline, |
363 string "PQclear(res);", | 363 string "PQclear(res);", |
479 string "static void uw_db_init(uw_context ctx) {", | 479 string "static void uw_db_init(uw_context ctx) {", |
480 newline, | 480 newline, |
481 string "char *env_db_str = getenv(\"URWEB_PQ_CON\");", | 481 string "char *env_db_str = getenv(\"URWEB_PQ_CON\");", |
482 newline, | 482 newline, |
483 string "PGconn *conn = PQconnectdb(env_db_str == NULL ? \"", | 483 string "PGconn *conn = PQconnectdb(env_db_str == NULL ? \"", |
484 string (String.toCString dbstring), | 484 string (Prim.toCString dbstring), |
485 string "\" : env_db_str);", | 485 string "\" : env_db_str);", |
486 newline, | 486 newline, |
487 string "if (conn == NULL) uw_error(ctx, FATAL, ", | 487 string "if (conn == NULL) uw_error(ctx, FATAL, ", |
488 string "\"libpq can't allocate a connection.\");", | 488 string "\"libpq can't allocate a connection.\");", |
489 newline, | 489 newline, |
730 string "\", ", | 730 string "\", ", |
731 string (Int.toString (length inputs)), | 731 string (Int.toString (length inputs)), |
732 string ", paramValues, paramLengths, paramFormats, 0);"] | 732 string ", paramValues, paramLengths, paramFormats, 0);"] |
733 else | 733 else |
734 box [string "PQexecParams(conn, \"", | 734 box [string "PQexecParams(conn, \"", |
735 string (String.toCString query), | 735 string (Prim.toCString query), |
736 string "\", ", | 736 string "\", ", |
737 string (Int.toString (length inputs)), | 737 string (Int.toString (length inputs)), |
738 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], | 738 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], |
739 newline, | 739 newline, |
740 newline, | 740 newline, |
741 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", | 741 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", |
742 string (String.toCString query), | 742 string (Prim.toCString query), |
743 string "\""]}] | 743 string "\""]}] |
744 | 744 |
745 fun dmlCommon {loc, dml, mode} = | 745 fun dmlCommon {loc, dml, mode} = |
746 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");", | 746 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");", |
747 newline, | 747 newline, |
886 string "\", ", | 886 string "\", ", |
887 string (Int.toString (length inputs)), | 887 string (Int.toString (length inputs)), |
888 string ", paramValues, paramLengths, paramFormats, 0);"] | 888 string ", paramValues, paramLengths, paramFormats, 0);"] |
889 else | 889 else |
890 box [string "PQexecParams(conn, \"", | 890 box [string "PQexecParams(conn, \"", |
891 string (String.toCString dml), | 891 string (Prim.toCString dml), |
892 string "\", ", | 892 string "\", ", |
893 string (Int.toString (length inputs)), | 893 string (Int.toString (length inputs)), |
894 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], | 894 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], |
895 newline, | 895 newline, |
896 newline, | 896 newline, |
897 dmlCommon {loc = loc, dml = box [string "\"", | 897 dmlCommon {loc = loc, dml = box [string "\"", |
898 string (String.toCString dml), | 898 string (Prim.toCString dml), |
899 string "\""], mode = mode}] | 899 string "\""], mode = mode}] |
900 | 900 |
901 fun nextvalCommon {loc, query} = | 901 fun nextvalCommon {loc, query} = |
902 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");", | 902 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");", |
903 newline, | 903 newline, |
970 box [string "PQexecPrepared(conn, \"uw", | 970 box [string "PQexecPrepared(conn, \"uw", |
971 string (Int.toString id), | 971 string (Int.toString id), |
972 string "\", 0, NULL, NULL, NULL, 0);"] | 972 string "\", 0, NULL, NULL, NULL, 0);"] |
973 else | 973 else |
974 box [string "PQexecParams(conn, \"", | 974 box [string "PQexecParams(conn, \"", |
975 string (String.toCString query), | 975 string (Prim.toCString query), |
976 string "\", 0, NULL, NULL, NULL, NULL, 0);"], | 976 string "\", 0, NULL, NULL, NULL, NULL, 0);"], |
977 newline, | 977 newline, |
978 newline, | 978 newline, |
979 nextvalCommon {loc = loc, query = box [string "\"", | 979 nextvalCommon {loc = loc, query = box [string "\"", |
980 string (String.toCString query), | 980 string (Prim.toCString query), |
981 string "\""]}] | 981 string "\""]}] |
982 | 982 |
983 fun setvalCommon {loc, query} = | 983 fun setvalCommon {loc, query} = |
984 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating setval result.\");", | 984 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating setval result.\");", |
985 newline, | 985 newline, |
1028 if Char.isPrint ch then | 1028 if Char.isPrint ch then |
1029 str ch | 1029 str ch |
1030 else | 1030 else |
1031 "\\" ^ StringCvt.padLeft #"0" 3 | 1031 "\\" ^ StringCvt.padLeft #"0" 3 |
1032 (Int.fmt StringCvt.OCT (ord ch))) | 1032 (Int.fmt StringCvt.OCT (ord ch))) |
1033 (String.toCString s) ^ "'::text" | 1033 (Prim.toCString s) ^ "'::text" |
1034 | 1034 |
1035 fun p_cast (s, t) = s ^ "::" ^ p_sql_type t | 1035 fun p_cast (s, t) = s ^ "::" ^ p_sql_type t |
1036 | 1036 |
1037 fun p_blank (n, t) = p_cast ("$" ^ Int.toString n, t) | 1037 fun p_blank (n, t) = p_cast ("$" ^ Int.toString n, t) |
1038 | 1038 |