comparison src/mysql.sml @ 878:a8952047e1d3

Sequence code compiles in MySQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Jul 2009 16:29:13 -0400
parents dae141d911d9
children b2a175a0f2ef
comparison
equal deleted inserted replaced
877:dae141d911d9 878:a8952047e1d3
905 string (Int.toString id), 905 string (Int.toString id),
906 string ";", 906 string ";",
907 newline, 907 newline,
908 newline, 908 newline,
909 909
910 string "if (stmt == NULL) {",
911 newline,
912 box [string "stmt = mysql_stmt_init(conn->conn);",
913 newline,
914 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");",
915 newline,
916 string "if (mysql_stmt_prepare(stmt, \"",
917 string (String.toString query),
918 string "\", ",
919 string (Int.toString (size query)),
920 string ")) {",
921 newline,
922 box [string "char msg[1024];",
923 newline,
924 string "strncpy(msg, mysql_stmt_error(stmt), 1024);",
925 newline,
926 string "msg[1023] = 0;",
927 newline,
928 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);",
929 newline],
930 string "}",
931 newline,
932 string "conn->p",
933 string (Int.toString id),
934 string " = stmt;",
935 newline],
936 string "}",
937 newline,
938 newline,
939
910 string "memset(in, 0, sizeof in);", 940 string "memset(in, 0, sizeof in);",
911 newline, 941 newline,
912 p_list_sepi (box []) (fn i => fn t => 942 p_list_sepi (box []) (fn i => fn t =>
913 let 943 let
914 fun buffers t = 944 fun buffers t =
1127 string (Int.toString id), 1157 string (Int.toString id),
1128 string ";", 1158 string ";",
1129 newline, 1159 newline,
1130 newline, 1160 newline,
1131 1161
1162 string "if (stmt == NULL) {",
1163 newline,
1164 box [string "stmt = mysql_stmt_init(conn->conn);",
1165 newline,
1166 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");",
1167 newline,
1168 string "if (mysql_stmt_prepare(stmt, \"",
1169 string (String.toString dml),
1170 string "\", ",
1171 string (Int.toString (size dml)),
1172 string ")) {",
1173 newline,
1174 box [string "char msg[1024];",
1175 newline,
1176 string "strncpy(msg, mysql_stmt_error(stmt), 1024);",
1177 newline,
1178 string "msg[1023] = 0;",
1179 newline,
1180 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);",
1181 newline],
1182 string "}",
1183 newline,
1184 string "conn->p",
1185 string (Int.toString id),
1186 string " = stmt;",
1187 newline],
1188 string "}",
1189 newline,
1190 newline,
1191
1132 string "memset(in, 0, sizeof in);", 1192 string "memset(in, 0, sizeof in);",
1133 newline, 1193 newline,
1134 p_list_sepi (box []) (fn i => fn t => 1194 p_list_sepi (box []) (fn i => fn t =>
1135 let 1195 let
1136 fun buffers t = 1196 fun buffers t =
1278 1338
1279 dmlCommon {loc = loc, dml = box [string "\"", 1339 dmlCommon {loc = loc, dml = box [string "\"",
1280 string (String.toString dml), 1340 string (String.toString dml),
1281 string "\""]}] 1341 string "\""]}]
1282 1342
1283 fun nextval _ = box [] 1343 fun nextval {loc, seqE, seqName} =
1284 fun nextvalPrepared _ = box [] 1344 box [string "uw_conn *conn = uw_get_db(ctx);",
1345 newline,
1346 string "char *insert = ",
1347 case seqName of
1348 SOME s => string ("\"INSERT INTO " ^ s ^ " VALUES ()\"")
1349 | NONE => box [string "uw_Basis_strcat(ctx, \"INSERT INTO \", uw_Basis_strcat(ctx, ",
1350 seqE,
1351 string ", \" VALUES ()\"))"],
1352 string ";",
1353 newline,
1354 string "char *delete = ",
1355 case seqName of
1356 SOME s => string ("\"DELETE FROM " ^ s ^ "\"")
1357 | NONE => box [string "uw_Basis_strcat(ctx, \"DELETE FROM \", ",
1358 seqE,
1359 string ")"],
1360 string ";",
1361 newline,
1362 newline,
1363
1364 string "if (mysql_query(conn->conn, insert)) uw_error(ctx, FATAL, \"'nextval' INSERT failed\");",
1365 newline,
1366 string "n = mysql_insert_id(conn->conn);",
1367 newline,
1368 string "if (mysql_query(conn->conn, delete)) uw_error(ctx, FATAL, \"'nextval' DELETE failed\");",
1369 newline]
1370
1371 fun nextvalPrepared _ = raise Fail "MySQL.nextvalPrepared called"
1285 1372
1286 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "\\'" 1373 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "\\'"
1287 | #"\\" => "\\\\" 1374 | #"\\" => "\\\\"
1288 | ch => 1375 | ch =>
1289 if Char.isPrint ch then 1376 if Char.isPrint ch then
1312 sqlifyString = sqlifyString, 1399 sqlifyString = sqlifyString,
1313 p_cast = p_cast, 1400 p_cast = p_cast,
1314 p_blank = p_blank, 1401 p_blank = p_blank,
1315 supportsDeleteAs = false, 1402 supportsDeleteAs = false,
1316 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO_INCREMENT)", 1403 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO_INCREMENT)",
1317 textKeysNeedLengths = true} 1404 textKeysNeedLengths = true,
1405 supportsNextval = false}
1318 1406
1319 end 1407 end