Mercurial > urweb
comparison src/postgres.sml @ 1285:514be09d5018
Better UTF-8 escaping for JavaScript and SQL literals
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Tue, 10 Aug 2010 15:55:43 -0400 |
parents | 134da5110bf7 |
children | acabf3935060 |
comparison
equal
deleted
inserted
replaced
1284:43ca083678f8 | 1285:514be09d5018 |
---|---|
329 | 329 |
330 p_list_sepi newline (fn i => fn (s, n) => | 330 p_list_sepi newline (fn i => fn (s, n) => |
331 box [string "res = PQprepare(conn, \"uw", | 331 box [string "res = PQprepare(conn, \"uw", |
332 string (Int.toString i), | 332 string (Int.toString i), |
333 string "\", \"", | 333 string "\", \"", |
334 string (String.toString s), | 334 string (String.toCString s), |
335 string "\", ", | 335 string "\", ", |
336 string (Int.toString n), | 336 string (Int.toString n), |
337 string ", NULL);", | 337 string ", NULL);", |
338 newline, | 338 newline, |
339 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", | 339 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {", |
347 string "PQclear(res);", | 347 string "PQclear(res);", |
348 newline, | 348 newline, |
349 string "PQfinish(conn);", | 349 string "PQfinish(conn);", |
350 newline, | 350 newline, |
351 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n", | 351 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n", |
352 string (String.toString s), | 352 string (String.toCString s), |
353 string "\\n%s\", msg);", | 353 string "\\n%s\", msg);", |
354 newline], | 354 newline], |
355 string "}", | 355 string "}", |
356 newline, | 356 newline, |
357 string "PQclear(res);", | 357 string "PQclear(res);", |
471 newline, | 471 newline, |
472 | 472 |
473 string "static void uw_db_init(uw_context ctx) {", | 473 string "static void uw_db_init(uw_context ctx) {", |
474 newline, | 474 newline, |
475 string "PGconn *conn = PQconnectdb(\"", | 475 string "PGconn *conn = PQconnectdb(\"", |
476 string (String.toString dbstring), | 476 string (String.toCString dbstring), |
477 string "\");", | 477 string "\");", |
478 newline, | 478 newline, |
479 string "if (conn == NULL) uw_error(ctx, FATAL, ", | 479 string "if (conn == NULL) uw_error(ctx, FATAL, ", |
480 string "\"libpq can't allocate a connection.\");", | 480 string "\"libpq can't allocate a connection.\");", |
481 newline, | 481 newline, |
696 string "\", ", | 696 string "\", ", |
697 string (Int.toString (length inputs)), | 697 string (Int.toString (length inputs)), |
698 string ", paramValues, paramLengths, paramFormats, 0);"] | 698 string ", paramValues, paramLengths, paramFormats, 0);"] |
699 else | 699 else |
700 box [string "PQexecParams(conn, \"", | 700 box [string "PQexecParams(conn, \"", |
701 string (String.toString query), | 701 string (String.toCString query), |
702 string "\", ", | 702 string "\", ", |
703 string (Int.toString (length inputs)), | 703 string (Int.toString (length inputs)), |
704 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], | 704 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], |
705 newline, | 705 newline, |
706 newline, | 706 newline, |
707 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", | 707 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", |
708 string (String.toString query), | 708 string (String.toCString query), |
709 string "\""]}] | 709 string "\""]}] |
710 | 710 |
711 fun dmlCommon {loc, dml} = | 711 fun dmlCommon {loc, dml} = |
712 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");", | 712 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");", |
713 newline, | 713 newline, |
777 string "\", ", | 777 string "\", ", |
778 string (Int.toString (length inputs)), | 778 string (Int.toString (length inputs)), |
779 string ", paramValues, paramLengths, paramFormats, 0);"] | 779 string ", paramValues, paramLengths, paramFormats, 0);"] |
780 else | 780 else |
781 box [string "PQexecParams(conn, \"", | 781 box [string "PQexecParams(conn, \"", |
782 string (String.toString dml), | 782 string (String.toCString dml), |
783 string "\", ", | 783 string "\", ", |
784 string (Int.toString (length inputs)), | 784 string (Int.toString (length inputs)), |
785 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], | 785 string ", NULL, paramValues, paramLengths, paramFormats, 0);"], |
786 newline, | 786 newline, |
787 newline, | 787 newline, |
788 dmlCommon {loc = loc, dml = box [string "\"", | 788 dmlCommon {loc = loc, dml = box [string "\"", |
789 string (String.toString dml), | 789 string (String.toCString dml), |
790 string "\""]}] | 790 string "\""]}] |
791 | 791 |
792 fun nextvalCommon {loc, query} = | 792 fun nextvalCommon {loc, query} = |
793 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");", | 793 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");", |
794 newline, | 794 newline, |
861 box [string "PQexecPrepared(conn, \"uw", | 861 box [string "PQexecPrepared(conn, \"uw", |
862 string (Int.toString id), | 862 string (Int.toString id), |
863 string "\", 0, NULL, NULL, NULL, 0);"] | 863 string "\", 0, NULL, NULL, NULL, 0);"] |
864 else | 864 else |
865 box [string "PQexecParams(conn, \"", | 865 box [string "PQexecParams(conn, \"", |
866 string (String.toString query), | 866 string (String.toCString query), |
867 string "\", 0, NULL, NULL, NULL, NULL, 0);"], | 867 string "\", 0, NULL, NULL, NULL, NULL, 0);"], |
868 newline, | 868 newline, |
869 newline, | 869 newline, |
870 nextvalCommon {loc = loc, query = box [string "\"", | 870 nextvalCommon {loc = loc, query = box [string "\"", |
871 string (String.toString query), | 871 string (String.toCString query), |
872 string "\""]}] | 872 string "\""]}] |
873 | 873 |
874 fun setvalCommon {loc, query} = | 874 fun setvalCommon {loc, query} = |
875 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating setval result.\");", | 875 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating setval result.\");", |
876 newline, | 876 newline, |
919 if Char.isPrint ch then | 919 if Char.isPrint ch then |
920 str ch | 920 str ch |
921 else | 921 else |
922 "\\" ^ StringCvt.padLeft #"0" 3 | 922 "\\" ^ StringCvt.padLeft #"0" 3 |
923 (Int.fmt StringCvt.OCT (ord ch))) | 923 (Int.fmt StringCvt.OCT (ord ch))) |
924 (String.toString s) ^ "'::text" | 924 (String.toCString s) ^ "'::text" |
925 | 925 |
926 fun p_cast (s, t) = s ^ "::" ^ p_sql_type t | 926 fun p_cast (s, t) = s ^ "::" ^ p_sql_type t |
927 | 927 |
928 fun p_blank (n, t) = p_cast ("$" ^ Int.toString n, t) | 928 fun p_blank (n, t) = p_cast ("$" ^ Int.toString n, t) |
929 | 929 |