comparison src/mysql.sml @ 879:b2a175a0f2ef

Demo working with MySQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Jul 2009 18:10:29 -0400
parents a8952047e1d3
children 8e9f2d247dba
comparison
equal deleted inserted replaced
878:a8952047e1d3 879:b2a175a0f2ef
1 (* Copyright (c) 2008-2009, Adam Chlipala 1 (* Copyright (c) 2009, Adam Chlipala
2 * All rights reserved. 2 * All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met: 5 * modification, are permitted provided that the following conditions are met:
6 * 6 *
374 newline, 374 newline,
375 newline, 375 newline,
376 376
377 string "void uw_client_init(void) {", 377 string "void uw_client_init(void) {",
378 newline, 378 newline,
379 box [string "if (mysql_library_init(0, NULL, NULL)) {", 379 box [string "uw_sqlfmtInt = \"%lld%n\";",
380 newline,
381 string "uw_sqlfmtFloat = \"%g%n\";",
382 newline,
383 string "uw_Estrings = 0;",
384 newline,
385 string "uw_sqlsuffixString = \"\";",
386 newline,
387 string "uw_sqlsuffixBlob = \"\";",
388 newline,
389 string "uw_sqlfmtUint4 = \"%u%n\";",
390 newline,
391 newline,
392
393 string "if (mysql_library_init(0, NULL, NULL)) {",
380 newline, 394 newline,
381 box [string "fprintf(stderr, \"Could not initialize MySQL library\\n\");", 395 box [string "fprintf(stderr, \"Could not initialize MySQL library\\n\");",
382 newline, 396 newline,
383 string "exit(1);", 397 string "exit(1);",
384 newline], 398 newline],
865 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"}, 879 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"},
866 880
867 string "uw_pop_cleanup(ctx);", 881 string "uw_pop_cleanup(ctx);",
868 newline] 882 newline]
869 883
870 fun queryPrepared {loc, id, query, inputs, cols, doCols} = 884 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested} =
871 box [string "uw_conn *conn = uw_get_db(ctx);", 885 box [string "uw_conn *conn = uw_get_db(ctx);",
872 newline, 886 newline,
873 string "MYSQL_BIND in[", 887 string "MYSQL_BIND in[",
874 string (Int.toString (length inputs)), 888 string (Int.toString (length inputs)),
875 string "];", 889 string "];",
899 newline, 913 newline,
900 buffers t] 914 buffers t]
901 | _ => buffers t, 915 | _ => buffers t,
902 newline] 916 newline]
903 end) inputs, 917 end) inputs,
904 string "MYSQL_STMT *stmt = conn->p", 918
905 string (Int.toString id), 919 if nested then
906 string ";", 920 box [string "MYSQL_STMT *stmt;",
907 newline, 921 newline]
908 newline, 922 else
909 923 box [string "MYSQL_STMT *stmt = conn->p",
910 string "if (stmt == NULL) {", 924 string (Int.toString id),
911 newline, 925 string ";",
926 newline,
927 newline,
928
929 string "if (stmt == NULL) {",
930 newline],
931
912 box [string "stmt = mysql_stmt_init(conn->conn);", 932 box [string "stmt = mysql_stmt_init(conn->conn);",
913 newline, 933 newline,
914 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");", 934 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");",
915 newline, 935 newline,
936 string "uw_push_cleanup(ctx, (void (*)(void *))mysql_stmt_close, stmt);",
916 string "if (mysql_stmt_prepare(stmt, \"", 937 string "if (mysql_stmt_prepare(stmt, \"",
917 string (String.toString query), 938 string (String.toString query),
918 string "\", ", 939 string "\", ",
919 string (Int.toString (size query)), 940 string (Int.toString (size query)),
920 string ")) {", 941 string ")) {",
927 newline, 948 newline,
928 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);", 949 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);",
929 newline], 950 newline],
930 string "}", 951 string "}",
931 newline, 952 newline,
932 string "conn->p", 953 if nested then
933 string (Int.toString id), 954 box []
934 string " = stmt;", 955 else
935 newline], 956 box [string "conn->p",
936 string "}", 957 string (Int.toString id),
937 newline, 958 string " = stmt;",
959 newline]],
960 if nested then
961 box []
962 else
963 box [string "}",
964 newline],
938 newline, 965 newline,
939 966
940 string "memset(in, 0, sizeof in);", 967 string "memset(in, 0, sizeof in);",
941 newline, 968 newline,
942 p_list_sepi (box []) (fn i => fn t => 969 p_list_sepi (box []) (fn i => fn t =>
1084 string ": error binding parameters\");", 1111 string ": error binding parameters\");",
1085 newline, 1112 newline,
1086 1113
1087 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"", 1114 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
1088 string (String.toString query), 1115 string (String.toString query),
1089 string "\""]}] 1116 string "\""]},
1117
1118 if nested then
1119 box [string "uw_pop_cleanup(ctx);",
1120 newline]
1121 else
1122 box []]
1090 1123
1091 fun dmlCommon {loc, dml} = 1124 fun dmlCommon {loc, dml} =
1092 box [string "if (mysql_stmt_execute(stmt)) uw_error(ctx, FATAL, \"", 1125 box [string "if (mysql_stmt_execute(stmt)) uw_error(ctx, FATAL, \"",
1093 string (ErrorMsg.spanToString loc), 1126 string (ErrorMsg.spanToString loc),
1094 string ": Error executing DML: %s\\n%s\", ", 1127 string ": Error executing DML: %s\\n%s\", ",
1400 p_cast = p_cast, 1433 p_cast = p_cast,
1401 p_blank = p_blank, 1434 p_blank = p_blank,
1402 supportsDeleteAs = false, 1435 supportsDeleteAs = false,
1403 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO_INCREMENT)", 1436 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO_INCREMENT)",
1404 textKeysNeedLengths = true, 1437 textKeysNeedLengths = true,
1405 supportsNextval = false} 1438 supportsNextval = false,
1439 supportsNestedPrepared = false}
1406 1440
1407 end 1441 end