comparison src/cjr_print.sml @ 737:d049d31a1966

Initial support for blobs and upload
author Adam Chlipala <adamc@hcoop.net>
date Sat, 25 Apr 2009 13:59:11 -0400
parents 796e42c93c48
children 7fa4871e8272
comparison
equal deleted inserted replaced
736:796e42c93c48 737:d049d31a1966
398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"] 398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
399 | TFfi ("Basis", "string") => 399 | TFfi ("Basis", "string") =>
400 if wontLeakStrings then 400 if wontLeakStrings then
401 e 401 e
402 else 402 else
403 box [string "uw_Basis_strdup(ctx, ", e, string ")"] 403 box [string "uw_strdup(ctx, ", e, string ")"]
404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"] 404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"] 405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
406 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"] 406 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
407 | TFfi ("Basis", "client") => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"] 407 | TFfi ("Basis", "client") => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"]
408 408
445 Int 445 Int
446 | Float 446 | Float
447 | String 447 | String
448 | Bool 448 | Bool
449 | Time 449 | Time
450 | Blob
450 | Channel 451 | Channel
451 | Client 452 | Client
452 | Nullable of sql_type 453 | Nullable of sql_type
454
455 fun isBlob Blob = true
456 | isBlob (Nullable t) = isBlob t
457 | isBlob _ = false
458
459 fun isFiles (t : typ) =
460 case #1 t of
461 TFfi ("Basis", "files") => true
462 | _ => false
453 463
454 fun p_sql_type' t = 464 fun p_sql_type' t =
455 case t of 465 case t of
456 Int => "uw_Basis_int" 466 Int => "uw_Basis_int"
457 | Float => "uw_Basis_float" 467 | Float => "uw_Basis_float"
458 | String => "uw_Basis_string" 468 | String => "uw_Basis_string"
459 | Bool => "uw_Basis_bool" 469 | Bool => "uw_Basis_bool"
460 | Time => "uw_Basis_time" 470 | Time => "uw_Basis_time"
471 | Blob => "uw_Basis_blob"
461 | Channel => "uw_Basis_channel" 472 | Channel => "uw_Basis_channel"
462 | Client => "uw_Basis_client" 473 | Client => "uw_Basis_client"
463 | Nullable String => "uw_Basis_string" 474 | Nullable String => "uw_Basis_string"
464 | Nullable t => p_sql_type' t ^ "*" 475 | Nullable t => p_sql_type' t ^ "*"
465 476
473 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)] 484 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
474 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)] 485 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
475 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)] 486 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
476 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)] 487 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
477 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)] 488 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
489 | EFfiApp ("Basis", "sqlifyBlob", [e]) => [(e, Blob)]
478 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)] 490 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)]
479 | EFfiApp ("Basis", "sqlifyClient", [e]) => [(e, Client)] 491 | EFfiApp ("Basis", "sqlifyClient", [e]) => [(e, Client)]
480 492
481 | ECase (e, 493 | ECase (e,
482 [((PNone _, _), 494 [((PNone _, _),
499 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"] 511 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
500 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"] 512 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
501 | String => e 513 | String => e
502 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"] 514 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
503 | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"] 515 | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"]
516 | Blob => box [e, string ".data"]
504 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"] 517 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
505 | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"] 518 | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"]
506 | Nullable String => e 519 | Nullable String => e
507 | Nullable t => box [string "(", 520 | Nullable t => box [string "(",
508 e, 521 e,
532 List.all (fn (_, _, to) => case to of 545 List.all (fn (_, _, to) => case to of
533 NONE => true 546 NONE => true
534 | SOME t => nl ok' t) cons 547 | SOME t => nl ok' t) cons
535 end) 548 end)
536 | TFfi ("Basis", "string") => false 549 | TFfi ("Basis", "string") => false
550 | TFfi ("Basis", "blob") => false
537 | TFfi _ => true 551 | TFfi _ => true
538 | TOption t => allowHeapAllocated andalso nl ok t 552 | TOption t => allowHeapAllocated andalso nl ok t
539 in 553 in
540 nl IS.empty 554 nl IS.empty
541 end 555 end
1476 string ";"]) 1490 string ";"])
1477 ets, 1491 ets,
1478 newline, 1492 newline,
1479 newline, 1493 newline,
1480 1494
1495 string "const int paramFormats[] = { ",
1496 p_list_sep (box [string ",", space])
1497 (fn (_, t) => if isBlob t then string "1" else string "0") ets,
1498 string " };",
1499 newline,
1500 string "const int paramLengths[] = { ",
1501 p_list_sepi (box [string ",", space])
1502 (fn i => fn (_, Blob) => string ("arg" ^ Int.toString (i + 1) ^ ".size")
1503 | (_, Nullable Blob) => string ("arg" ^ Int.toString (i + 1)
1504 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
1505 | _ => string "0") ets,
1506 string " };",
1507 newline,
1481 string "const char *paramValues[] = { ", 1508 string "const char *paramValues[] = { ",
1482 p_list_sepi (box [string ",", space]) 1509 p_list_sepi (box [string ",", space])
1483 (fn i => fn (_, t) => p_ensql t (box [string "arg", 1510 (fn i => fn (_, t) => p_ensql t (box [string "arg",
1484 string (Int.toString (i + 1))])) 1511 string (Int.toString (i + 1))]))
1485 ets, 1512 ets,
1493 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);" 1520 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
1494 | SOME n => box [string "PQexecPrepared(conn, \"uw", 1521 | SOME n => box [string "PQexecPrepared(conn, \"uw",
1495 string (Int.toString n), 1522 string (Int.toString n),
1496 string "\", ", 1523 string "\", ",
1497 string (Int.toString (length (getPargs query))), 1524 string (Int.toString (length (getPargs query))),
1498 string ", paramValues, NULL, NULL, 0);"], 1525 string ", paramValues, paramLengths, paramFormats, 0);"],
1499 newline, 1526 newline,
1500 newline, 1527 newline,
1501 1528
1502 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");", 1529 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
1503 newline, 1530 newline,
1788 newline, 1815 newline,
1789 string "})"] 1816 string "})"]
1790 in 1817 in
1791 box [string "({", 1818 box [string "({",
1792 newline, 1819 newline,
1793 string "uw_Basis_string request = uw_Basis_maybe_strdup(ctx, ", 1820 string "uw_Basis_string request = uw_maybe_strdup(ctx, ",
1794 p_exp env e, 1821 p_exp env e,
1795 string ");", 1822 string ");",
1796 newline, 1823 newline,
1797 newline, 1824 newline,
1798 string "(request ? ", 1825 string "(request ? ",
2171 TFfi ("Basis", "int") => "int8" 2198 TFfi ("Basis", "int") => "int8"
2172 | TFfi ("Basis", "float") => "float8" 2199 | TFfi ("Basis", "float") => "float8"
2173 | TFfi ("Basis", "string") => "text" 2200 | TFfi ("Basis", "string") => "text"
2174 | TFfi ("Basis", "bool") => "bool" 2201 | TFfi ("Basis", "bool") => "bool"
2175 | TFfi ("Basis", "time") => "timestamp" 2202 | TFfi ("Basis", "time") => "timestamp"
2203 | TFfi ("Basis", "blob") => "bytea"
2176 | TFfi ("Basis", "channel") => "int8" 2204 | TFfi ("Basis", "channel") => "int8"
2177 | TFfi ("Basis", "client") => "int4" 2205 | TFfi ("Basis", "client") => "int4"
2178 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type"; 2206 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
2179 Print.eprefaces' [("Type", p_typ env tAll)]; 2207 Print.eprefaces' [("Type", p_typ env tAll)];
2180 "ERROR") 2208 "ERROR")
2380 2408
2381 val f = case t of 2409 val f = case t of
2382 (TFfi ("Basis", "bool"), _) => "optional_" 2410 (TFfi ("Basis", "bool"), _) => "optional_"
2383 | _ => "" 2411 | _ => ""
2384 in 2412 in
2385 box [string "request = uw_get_", 2413 if isFiles t then
2386 string f, 2414 box [string "uw_input_",
2387 string "input(ctx, ", 2415 p_ident x,
2388 string (Int.toString n), 2416 space,
2389 string ");", 2417 string "=",
2390 newline, 2418 space,
2391 string "if (request == NULL)", 2419 string "uw_get_file_input(ctx, ",
2392 newline, 2420 string (Int.toString n),
2393 box [string "uw_error(ctx, FATAL, \"Missing input ", 2421 string ");",
2394 string x, 2422 newline]
2395 string "\");"], 2423 else
2396 newline, 2424 box [string "request = uw_get_",
2397 string "uw_input_", 2425 string f,
2398 p_ident x, 2426 string "input(ctx, ",
2399 space, 2427 string (Int.toString n),
2400 string "=", 2428 string ");",
2401 space, 2429 newline,
2402 unurlify env t, 2430 string "if (request == NULL)",
2403 string ";", 2431 newline,
2404 newline] 2432 box [string "uw_error(ctx, FATAL, \"Missing input ",
2433 string x,
2434 string "\");"],
2435 newline,
2436 string "uw_input_",
2437 p_ident x,
2438 space,
2439 string "=",
2440 space,
2441 unurlify env t,
2442 string ";",
2443 newline]
2405 end) xts), 2444 end) xts),
2406 string "struct __uws_", 2445 string "struct __uws_",
2407 string (Int.toString i), 2446 string (Int.toString i),
2408 space, 2447 space,
2409 string "uw_inputs", 2448 string "uw_inputs",