annotate src/cjr_print.sml @ 474:70ee90105fce

[getCookie] reads cookie settings from current page generation
author Adam Chlipala <adamc@hcoop.net>
date Sat, 08 Nov 2008 10:46:14 -0500
parents 20fab0e96217
children c9566d49ecfe
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 (* Pretty-printing C jr. *)
adamc@29 29
adamc@29 30 structure CjrPrint :> CJR_PRINT = struct
adamc@29 31
adamc@29 32 open Print.PD
adamc@29 33 open Print
adamc@29 34
adamc@29 35 open Cjr
adamc@29 36
adamc@269 37 val dummyt = (TRecord 0, ErrorMsg.dummySpan)
adamc@269 38
adamc@29 39 structure E = CjrEnv
adamc@29 40 structure EM = ErrorMsg
adamc@29 41
adamc@144 42 structure SK = struct
adamc@144 43 type ord_key = string
adamc@144 44 val compare = String.compare
adamc@144 45 end
adamc@144 46
adamc@144 47 structure SS = BinarySetFn(SK)
adamc@144 48 structure SM = BinaryMapFn(SK)
adamc@144 49 structure IS = IntBinarySet
adamc@144 50
adamc@144 51 structure CM = BinaryMapFn(struct
adamc@144 52 type ord_key = char
adamc@144 53 val compare = Char.compare
adamc@144 54 end)
adamc@144 55
adamc@29 56 val debug = ref false
adamc@29 57
adamc@196 58 val dummyTyp = (TDatatype (Enum, 0, ref []), ErrorMsg.dummySpan)
adamc@29 59
adamc@316 60 val ident = String.translate (fn #"'" => "PRIME"
adamc@316 61 | ch => str ch)
adamc@316 62
adamc@316 63 val p_ident = string o ident
adamc@316 64
adamc@463 65 fun isUnboxable (t : typ) =
adamc@463 66 case #1 t of
adamc@463 67 TDatatype (Default, _, _) => true
adamc@463 68 | TFfi ("Basis", "string") => true
adamc@463 69 | _ => false
adamc@463 70
adamc@29 71 fun p_typ' par env (t, loc) =
adamc@29 72 case t of
adamc@470 73 TFun (t1, t2) => parenIf par (box [string "(",
adamc@470 74 p_typ' true env t2,
adamc@109 75 space,
adamc@109 76 string "(*)",
adamc@109 77 space,
adamc@109 78 string "(",
adamc@109 79 p_typ env t1,
adamc@470 80 string "))"])
adamc@29 81 | TRecord i => box [string "struct",
adamc@29 82 space,
adamc@311 83 string "__uws_",
adamc@29 84 string (Int.toString i)]
adamc@188 85 | TDatatype (Enum, n, _) =>
adamc@188 86 (box [string "enum",
adamc@188 87 space,
adamc@311 88 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@311 89 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@198 90 | TDatatype (Option, n, xncs) =>
adamc@198 91 (case ListUtil.search #3 (!xncs) of
adamc@198 92 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
adamc@198 93 | SOME t =>
adamc@198 94 case #1 t of
adamc@198 95 TDatatype _ => p_typ' par env t
adamc@199 96 | TFfi ("Basis", "string") => p_typ' par env t
adamc@198 97 | _ => box [p_typ' par env t,
adamc@198 98 string "*"])
adamc@188 99 | TDatatype (Default, n, _) =>
adamc@165 100 (box [string "struct",
adamc@165 101 space,
adamc@311 102 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@311 103 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@316 104 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@288 105 | TOption t =>
adamc@463 106 if isUnboxable t then
adamc@463 107 p_typ' par env t
adamc@463 108 else
adamc@463 109 box [p_typ' par env t,
adamc@463 110 string "*"]
adamc@29 111
adamc@29 112 and p_typ env = p_typ' false env
adamc@29 113
adamc@316 114 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@311 115 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 116
adamc@109 117 fun p_enamed env n =
adamc@316 118 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
adamc@311 119 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
adamc@109 120
adamc@182 121 fun p_con_named env n =
adamc@316 122 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
adamc@311 123 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
adamc@182 124
adamc@182 125 fun p_pat_preamble env (p, _) =
adamc@182 126 case p of
adamc@182 127 PWild => (box [],
adamc@182 128 env)
adamc@182 129 | PVar (x, t) => (box [p_typ env t,
adamc@182 130 space,
adamc@311 131 string "__uwr_",
adamc@316 132 p_ident x,
adamc@182 133 string "_",
adamc@182 134 string (Int.toString (E.countERels env)),
adamc@182 135 string ";",
adamc@182 136 newline],
adamc@196 137 E.pushERel env x t)
adamc@182 138 | PPrim _ => (box [], env)
adamc@188 139 | PCon (_, _, NONE) => (box [], env)
adamc@188 140 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 141 | PRecord xps =>
adamc@182 142 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 143 let
adamc@182 144 val (pp', env) = p_pat_preamble env p
adamc@182 145 in
adamc@182 146 (box [pp', pp], env)
adamc@182 147 end) (box [], env) xps
adamc@288 148 | PNone _ => (box [], env)
adamc@288 149 | PSome (_, p) => p_pat_preamble env p
adamc@182 150
adamc@182 151 fun p_patCon env pc =
adamc@182 152 case pc of
adamc@182 153 PConVar n => p_con_named env n
adamc@316 154 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
adamc@182 155
adamc@182 156 fun p_pat (env, exit, depth) (p, _) =
adamc@182 157 case p of
adamc@182 158 PWild =>
adamc@182 159 (box [], env)
adamc@182 160 | PVar (x, t) =>
adamc@311 161 (box [string "__uwr_",
adamc@316 162 p_ident x,
adamc@182 163 string "_",
adamc@182 164 string (Int.toString (E.countERels env)),
adamc@182 165 space,
adamc@182 166 string "=",
adamc@182 167 space,
adamc@182 168 string "disc",
adamc@182 169 string (Int.toString depth),
adamc@182 170 string ";"],
adamc@182 171 E.pushERel env x t)
adamc@182 172 | PPrim (Prim.Int n) =>
adamc@182 173 (box [string "if",
adamc@182 174 space,
adamc@182 175 string "(disc",
adamc@182 176 string (Int.toString depth),
adamc@182 177 space,
adamc@182 178 string "!=",
adamc@182 179 space,
adamc@276 180 Prim.p_t_GCC (Prim.Int n),
adamc@182 181 string ")",
adamc@182 182 space,
adamc@182 183 exit],
adamc@182 184 env)
adamc@182 185 | PPrim (Prim.String s) =>
adamc@182 186 (box [string "if",
adamc@182 187 space,
adamc@182 188 string "(strcmp(disc",
adamc@182 189 string (Int.toString depth),
adamc@182 190 string ",",
adamc@182 191 space,
adamc@276 192 Prim.p_t_GCC (Prim.String s),
adamc@182 193 string "))",
adamc@182 194 space,
adamc@182 195 exit],
adamc@182 196 env)
adamc@182 197 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 198
adamc@188 199 | PCon (dk, pc, po) =>
adamc@182 200 let
adamc@182 201 val (p, env) =
adamc@182 202 case po of
adamc@182 203 NONE => (box [], env)
adamc@182 204 | SOME p =>
adamc@182 205 let
adamc@182 206 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 207
adamc@182 208 val (x, to) = case pc of
adamc@182 209 PConVar n =>
adamc@182 210 let
adamc@182 211 val (x, to, _) = E.lookupConstructor env n
adamc@182 212 in
adamc@316 213 ("uw_" ^ ident x, to)
adamc@182 214 end
adamc@188 215 | PConFfi {mod = m, con, arg, ...} =>
adamc@316 216 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
adamc@182 217
adamc@182 218 val t = case to of
adamc@182 219 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 220 | SOME t => t
adamc@182 221 in
adamc@182 222 (box [string "{",
adamc@182 223 newline,
adamc@182 224 p_typ env t,
adamc@182 225 space,
adamc@182 226 string "disc",
adamc@182 227 string (Int.toString (depth + 1)),
adamc@182 228 space,
adamc@182 229 string "=",
adamc@182 230 space,
adamc@198 231 case dk of
adamc@198 232 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
adamc@198 233 | Default => box [string "disc",
adamc@198 234 string (Int.toString depth),
adamc@198 235 string "->data.",
adamc@198 236 string x]
adamc@198 237 | Option =>
adamc@463 238 if isUnboxable t then
adamc@463 239 box [string "disc",
adamc@463 240 string (Int.toString depth)]
adamc@463 241 else
adamc@463 242 box [string "*disc",
adamc@463 243 string (Int.toString depth)],
adamc@182 244 string ";",
adamc@182 245 newline,
adamc@182 246 p,
adamc@182 247 newline,
adamc@182 248 string "}"],
adamc@182 249 env)
adamc@182 250 end
adamc@182 251 in
adamc@182 252 (box [string "if",
adamc@182 253 space,
adamc@182 254 string "(disc",
adamc@182 255 string (Int.toString depth),
adamc@198 256 case (dk, po) of
adamc@198 257 (Enum, _) => box [space,
adamc@198 258 string "!=",
adamc@198 259 space,
adamc@198 260 p_patCon env pc]
adamc@198 261 | (Default, _) => box [string "->tag",
adamc@198 262 space,
adamc@198 263 string "!=",
adamc@198 264 space,
adamc@198 265 p_patCon env pc]
adamc@198 266 | (Option, NONE) => box [space,
adamc@198 267 string "!=",
adamc@198 268 space,
adamc@198 269 string "NULL"]
adamc@198 270 | (Option, SOME _) => box [space,
adamc@198 271 string "==",
adamc@198 272 space,
adamc@198 273 string "NULL"],
adamc@182 274 string ")",
adamc@182 275 space,
adamc@182 276 exit,
adamc@182 277 newline,
adamc@182 278 p],
adamc@182 279 env)
adamc@182 280 end
adamc@182 281
adamc@182 282 | PRecord xps =>
adamc@182 283 let
adamc@182 284 val (xps, env) =
adamc@182 285 ListUtil.foldlMap (fn ((x, p, t), env) =>
adamc@182 286 let
adamc@182 287 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 288
adamc@182 289 val p = box [string "{",
adamc@182 290 newline,
adamc@182 291 p_typ env t,
adamc@182 292 space,
adamc@182 293 string "disc",
adamc@182 294 string (Int.toString (depth + 1)),
adamc@182 295 space,
adamc@182 296 string "=",
adamc@182 297 space,
adamc@182 298 string "disc",
adamc@182 299 string (Int.toString depth),
adamc@311 300 string ".__uwf_",
adamc@316 301 p_ident x,
adamc@182 302 string ";",
adamc@182 303 newline,
adamc@182 304 p,
adamc@182 305 newline,
adamc@182 306 string "}"]
adamc@182 307 in
adamc@182 308 (p, env)
adamc@182 309 end) env xps
adamc@182 310 in
adamc@182 311 (p_list_sep newline (fn x => x) xps,
adamc@182 312 env)
adamc@182 313 end
adamc@182 314
adamc@288 315 | PNone t =>
adamc@288 316 (box [string "if",
adamc@288 317 space,
adamc@288 318 string "(disc",
adamc@288 319 string (Int.toString depth),
adamc@288 320 space,
adamc@288 321 string "!=",
adamc@288 322 space,
adamc@288 323 string "NULL)",
adamc@288 324 space,
adamc@288 325 exit,
adamc@288 326 newline],
adamc@288 327 env)
adamc@288 328
adamc@288 329 | PSome (t, p) =>
adamc@288 330 let
adamc@288 331 val (p, env) =
adamc@288 332 let
adamc@288 333 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@288 334 in
adamc@288 335 (box [string "{",
adamc@288 336 newline,
adamc@288 337 p_typ env t,
adamc@288 338 space,
adamc@288 339 string "disc",
adamc@288 340 string (Int.toString (depth + 1)),
adamc@288 341 space,
adamc@288 342 string "=",
adamc@288 343 space,
adamc@463 344 if isUnboxable t then
adamc@463 345 box [string "disc",
adamc@463 346 string (Int.toString depth)]
adamc@463 347 else
adamc@463 348 box [string "*disc",
adamc@463 349 string (Int.toString depth)],
adamc@288 350 string ";",
adamc@288 351 newline,
adamc@288 352 p,
adamc@288 353 newline,
adamc@288 354 string "}"],
adamc@288 355 env)
adamc@288 356 end
adamc@288 357 in
adamc@288 358 (box [string "if",
adamc@288 359 space,
adamc@288 360 string "(disc",
adamc@288 361 string (Int.toString depth),
adamc@288 362 space,
adamc@288 363 string "==",
adamc@288 364 space,
adamc@288 365 string "NULL)",
adamc@288 366 space,
adamc@288 367 exit,
adamc@288 368 newline,
adamc@288 369 p],
adamc@288 370 env)
adamc@288 371 end
adamc@288 372
adamc@182 373 local
adamc@182 374 val count = ref 0
adamc@182 375 in
adamc@182 376 fun newGoto () =
adamc@182 377 let
adamc@182 378 val r = !count
adamc@182 379 in
adamc@182 380 count := r + 1;
adamc@182 381 string ("L" ^ Int.toString r)
adamc@182 382 end
adamc@182 383 end
adamc@182 384
adamc@185 385 fun patConInfo env pc =
adamc@185 386 case pc of
adamc@185 387 PConVar n =>
adamc@185 388 let
adamc@185 389 val (x, _, dn) = E.lookupConstructor env n
adamc@185 390 val (dx, _) = E.lookupDatatype env dn
adamc@185 391 in
adamc@316 392 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
adamc@316 393 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
adamc@316 394 "uw_" ^ ident x)
adamc@185 395 end
adamc@186 396 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@316 397 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
adamc@316 398 "uw_" ^ ident m ^ "_" ^ ident con,
adamc@316 399 "uw_" ^ ident con)
adamc@185 400
adamc@324 401 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
adamc@278 402 case t of
adamc@311 403 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
adamc@311 404 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
adamc@324 405 | TFfi ("Basis", "string") =>
adamc@324 406 if wontLeakStrings then
adamc@324 407 e
adamc@324 408 else
adamc@324 409 box [string "uw_Basis_strdup(ctx, ", e, string ")"]
adamc@311 410 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
adamc@438 411 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
adamc@467 412
adamc@278 413 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
adamc@278 414 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@278 415 string "ERROR")
adamc@278 416
adamc@467 417 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
adamc@467 418 case t of
adamc@467 419 TOption t =>
adamc@467 420 box [string "(PQgetisnull (res, i, ",
adamc@467 421 string (Int.toString i),
adamc@467 422 string ") ? NULL : ",
adamc@467 423 case t of
adamc@467 424 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
adamc@467 425 | _ => box [string "({",
adamc@467 426 newline,
adamc@467 427 p_typ env t,
adamc@467 428 space,
adamc@467 429 string "*tmp = uw_malloc(ctx, sizeof(",
adamc@467 430 p_typ env t,
adamc@467 431 string "));",
adamc@467 432 newline,
adamc@467 433 string "*tmp = ",
adamc@467 434 p_getcol wontLeakStrings env t i,
adamc@467 435 string ";",
adamc@467 436 newline,
adamc@467 437 string "tmp;",
adamc@467 438 newline,
adamc@467 439 string "})"],
adamc@467 440 string ")"]
adamc@467 441
adamc@467 442 | _ =>
adamc@467 443 p_unsql wontLeakStrings env tAll
adamc@467 444 (box [string "PQgetvalue(res, i, ",
adamc@467 445 string (Int.toString i),
adamc@467 446 string ")"])
adamc@467 447
adamc@282 448 datatype sql_type =
adamc@282 449 Int
adamc@282 450 | Float
adamc@282 451 | String
adamc@282 452 | Bool
adamc@439 453 | Time
adamc@467 454 | Nullable of sql_type
adamc@282 455
adamc@467 456 fun p_sql_type' t =
adamc@467 457 case t of
adamc@467 458 Int => "uw_Basis_int"
adamc@467 459 | Float => "uw_Basis_float"
adamc@467 460 | String => "uw_Basis_string"
adamc@467 461 | Bool => "uw_Basis_bool"
adamc@467 462 | Time => "uw_Basis_time"
adamc@467 463 | Nullable String => "uw_Basis_string"
adamc@467 464 | Nullable t => p_sql_type' t ^ "*"
adamc@467 465
adamc@467 466 fun p_sql_type t = string (p_sql_type' t)
adamc@282 467
adamc@282 468 fun getPargs (e, _) =
adamc@282 469 case e of
adamc@282 470 EPrim (Prim.String _) => []
adamc@282 471 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
adamc@282 472
adamc@282 473 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
adamc@282 474 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
adamc@282 475 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
adamc@282 476 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
adamc@439 477 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
adamc@468 478
adamc@468 479 | EFfiApp ("Basis", "sqlifyIntN", [e]) => [(e, Nullable Int)]
adamc@468 480 | EFfiApp ("Basis", "sqlifyFloatN", [e]) => [(e, Nullable Float)]
adamc@468 481 | EFfiApp ("Basis", "sqlifyStringN", [e]) => [(e, Nullable String)]
adamc@468 482 | EFfiApp ("Basis", "sqlifyBoolN", [e]) => [(e, Nullable Bool)]
adamc@468 483 | EFfiApp ("Basis", "sqlifyTimeN", [e]) => [(e, Nullable Time)]
adamc@468 484
adamc@322 485 | ECase (e, _, _) => [(e, Bool)]
adamc@282 486
adamc@282 487 | _ => raise Fail "CjrPrint: getPargs"
adamc@282 488
adamc@282 489 fun p_ensql t e =
adamc@282 490 case t of
adamc@311 491 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
adamc@311 492 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
adamc@282 493 | String => e
adamc@295 494 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
adamc@439 495 | Time => box [string "uw_Basis_sqlifyTime(ctx, ", e, string ")"]
adamc@467 496 | Nullable String => e
adamc@467 497 | Nullable t => box [string "(",
adamc@467 498 e,
adamc@467 499 string " == NULL ? NULL : ",
adamc@467 500 p_ensql t (box [string "*", e]),
adamc@467 501 string ")"]
adamc@282 502
adamc@324 503 fun notLeaky env allowHeapAllocated =
adamc@324 504 let
adamc@324 505 fun nl (t, _) =
adamc@324 506 case t of
adamc@324 507 TFun _ => false
adamc@324 508 | TRecord n =>
adamc@324 509 let
adamc@324 510 val xts = E.lookupStruct env n
adamc@324 511 in
adamc@324 512 List.all (fn (_, t) => nl t) xts
adamc@324 513 end
adamc@324 514 | TDatatype (dk, _, ref cons) =>
adamc@324 515 (allowHeapAllocated orelse dk = Enum)
adamc@324 516 andalso List.all (fn (_, _, to) => case to of
adamc@324 517 NONE => true
adamc@324 518 | SOME t => nl t) cons
adamc@324 519 | TFfi ("Basis", "string") => false
adamc@324 520 | TFfi _ => true
adamc@324 521 | TOption t => allowHeapAllocated andalso nl t
adamc@324 522 in
adamc@324 523 nl
adamc@324 524 end
adamc@324 525
adamc@463 526 fun capitalize s =
adamc@463 527 if s = "" then
adamc@463 528 ""
adamc@463 529 else
adamc@463 530 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@463 531
adamc@463 532 fun unurlify env (t, loc) =
adamc@463 533 let
adamc@463 534 fun unurlify' rf t =
adamc@463 535 case t of
adamc@463 536 TFfi ("Basis", "unit") => string ("uw_unit_v")
adamc@463 537 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@463 538
adamc@463 539 | TRecord 0 => string "uw_unit_v"
adamc@463 540 | TRecord i =>
adamc@463 541 let
adamc@463 542 val xts = E.lookupStruct env i
adamc@463 543 in
adamc@463 544 box [string "({",
adamc@463 545 newline,
adamc@463 546 box (map (fn (x, t) =>
adamc@463 547 box [p_typ env t,
adamc@463 548 space,
adamc@463 549 string "uwr_",
adamc@463 550 string x,
adamc@463 551 space,
adamc@463 552 string "=",
adamc@463 553 space,
adamc@463 554 unurlify' rf (#1 t),
adamc@463 555 string ";",
adamc@463 556 newline]) xts),
adamc@463 557 string "struct",
adamc@463 558 space,
adamc@463 559 string "__uws_",
adamc@463 560 string (Int.toString i),
adamc@463 561 space,
adamc@463 562 string "tmp",
adamc@463 563 space,
adamc@463 564 string "=",
adamc@463 565 space,
adamc@463 566 string "{",
adamc@463 567 space,
adamc@463 568 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
adamc@463 569 string x]) xts,
adamc@463 570 space,
adamc@463 571 string "};",
adamc@463 572 newline,
adamc@463 573 string "tmp;",
adamc@463 574 newline,
adamc@463 575 string "})"]
adamc@463 576 end
adamc@463 577
adamc@463 578 | TDatatype (Enum, i, _) =>
adamc@463 579 let
adamc@463 580 val (x, xncs) = E.lookupDatatype env i
adamc@463 581
adamc@463 582 fun doEm xncs =
adamc@463 583 case xncs of
adamc@463 584 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 585 ^ x ^ "\"), (enum __uwe_"
adamc@463 586 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@463 587 | (x', n, to) :: rest =>
adamc@463 588 box [string "((!strncmp(request, \"",
adamc@463 589 string x',
adamc@463 590 string "\", ",
adamc@463 591 string (Int.toString (size x')),
adamc@463 592 string ") && (request[",
adamc@463 593 string (Int.toString (size x')),
adamc@463 594 string "] == 0 || request[",
adamc@463 595 string (Int.toString (size x')),
adamc@463 596 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 597 space,
adamc@463 598 string ":",
adamc@463 599 space,
adamc@463 600 doEm rest,
adamc@463 601 string ")"]
adamc@463 602 in
adamc@463 603 doEm xncs
adamc@463 604 end
adamc@463 605
adamc@463 606 | TDatatype (Option, i, xncs) =>
adamc@463 607 if IS.member (rf, i) then
adamc@463 608 box [string "unurlify_",
adamc@463 609 string (Int.toString i),
adamc@463 610 string "()"]
adamc@463 611 else
adamc@463 612 let
adamc@463 613 val (x, _) = E.lookupDatatype env i
adamc@463 614
adamc@463 615 val (no_arg, has_arg, t) =
adamc@463 616 case !xncs of
adamc@463 617 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@463 618 (no_arg, has_arg, t)
adamc@463 619 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@463 620 (no_arg, has_arg, t)
adamc@463 621 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@463 622
adamc@463 623 val rf = IS.add (rf, i)
adamc@463 624 in
adamc@463 625 box [string "({",
adamc@463 626 space,
adamc@463 627 p_typ env t,
adamc@463 628 space,
adamc@463 629 string "*unurlify_",
adamc@463 630 string (Int.toString i),
adamc@463 631 string "(void) {",
adamc@463 632 newline,
adamc@463 633 box [string "return (request[0] == '/' ? ++request : request,",
adamc@463 634 newline,
adamc@463 635 string "((!strncmp(request, \"",
adamc@463 636 string no_arg,
adamc@463 637 string "\", ",
adamc@463 638 string (Int.toString (size no_arg)),
adamc@463 639 string ") && (request[",
adamc@463 640 string (Int.toString (size no_arg)),
adamc@463 641 string "] == 0 || request[",
adamc@463 642 string (Int.toString (size no_arg)),
adamc@463 643 string "] == '/')) ? (request",
adamc@463 644 space,
adamc@463 645 string "+=",
adamc@463 646 space,
adamc@463 647 string (Int.toString (size no_arg)),
adamc@463 648 string ", NULL) : ((!strncmp(request, \"",
adamc@463 649 string has_arg,
adamc@463 650 string "\", ",
adamc@463 651 string (Int.toString (size has_arg)),
adamc@463 652 string ") && (request[",
adamc@463 653 string (Int.toString (size has_arg)),
adamc@463 654 string "] == 0 || request[",
adamc@463 655 string (Int.toString (size has_arg)),
adamc@463 656 string "] == '/')) ? (request",
adamc@463 657 space,
adamc@463 658 string "+=",
adamc@463 659 space,
adamc@463 660 string (Int.toString (size has_arg)),
adamc@463 661 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@463 662 newline,
adamc@463 663
adamc@463 664 if isUnboxable t then
adamc@463 665 unurlify' rf (#1 t)
adamc@463 666 else
adamc@463 667 box [string "({",
adamc@463 668 newline,
adamc@463 669 p_typ env t,
adamc@463 670 space,
adamc@463 671 string "*tmp",
adamc@463 672 space,
adamc@463 673 string "=",
adamc@463 674 space,
adamc@463 675 string "uw_malloc(ctx, sizeof(",
adamc@463 676 p_typ env t,
adamc@463 677 string "));",
adamc@463 678 newline,
adamc@463 679 string "*tmp",
adamc@463 680 space,
adamc@463 681 string "=",
adamc@463 682 space,
adamc@463 683 unurlify' rf (#1 t),
adamc@463 684 string ";",
adamc@463 685 newline,
adamc@463 686 string "tmp;",
adamc@463 687 newline,
adamc@463 688 string "})"],
adamc@463 689 string ")",
adamc@463 690 newline,
adamc@463 691 string ":",
adamc@463 692 space,
adamc@463 693 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@463 694 ^ "\"), NULL))));"),
adamc@463 695 newline],
adamc@463 696 string "}",
adamc@463 697 newline,
adamc@463 698 newline,
adamc@463 699
adamc@463 700 string "unurlify_",
adamc@463 701 string (Int.toString i),
adamc@463 702 string "();",
adamc@463 703 newline,
adamc@463 704 string "})"]
adamc@463 705 end
adamc@463 706
adamc@463 707 | TDatatype (Default, i, _) =>
adamc@463 708 if IS.member (rf, i) then
adamc@463 709 box [string "unurlify_",
adamc@463 710 string (Int.toString i),
adamc@463 711 string "()"]
adamc@463 712 else
adamc@463 713 let
adamc@463 714 val (x, xncs) = E.lookupDatatype env i
adamc@463 715
adamc@463 716 val rf = IS.add (rf, i)
adamc@463 717
adamc@463 718 fun doEm xncs =
adamc@463 719 case xncs of
adamc@463 720 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 721 ^ x ^ "\"), NULL)")
adamc@463 722 | (x', n, to) :: rest =>
adamc@463 723 box [string "((!strncmp(request, \"",
adamc@463 724 string x',
adamc@463 725 string "\", ",
adamc@463 726 string (Int.toString (size x')),
adamc@463 727 string ") && (request[",
adamc@463 728 string (Int.toString (size x')),
adamc@463 729 string "] == 0 || request[",
adamc@463 730 string (Int.toString (size x')),
adamc@463 731 string "] == '/')) ? ({",
adamc@463 732 newline,
adamc@463 733 string "struct",
adamc@463 734 space,
adamc@463 735 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@463 736 space,
adamc@463 737 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@463 738 string x,
adamc@463 739 string "_",
adamc@463 740 string (Int.toString i),
adamc@463 741 string "));",
adamc@463 742 newline,
adamc@463 743 string "tmp->tag",
adamc@463 744 space,
adamc@463 745 string "=",
adamc@463 746 space,
adamc@463 747 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 748 string ";",
adamc@463 749 newline,
adamc@463 750 string "request",
adamc@463 751 space,
adamc@463 752 string "+=",
adamc@463 753 space,
adamc@463 754 string (Int.toString (size x')),
adamc@463 755 string ";",
adamc@463 756 newline,
adamc@463 757 string "if (request[0] == '/') ++request;",
adamc@463 758 newline,
adamc@463 759 case to of
adamc@463 760 NONE => box []
adamc@463 761 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@463 762 p_ident x',
adamc@463 763 space,
adamc@463 764 string "=",
adamc@463 765 space,
adamc@463 766 unurlify' rf t,
adamc@463 767 string ";",
adamc@463 768 newline],
adamc@463 769 string "tmp;",
adamc@463 770 newline,
adamc@463 771 string "})",
adamc@463 772 space,
adamc@463 773 string ":",
adamc@463 774 space,
adamc@463 775 doEm rest,
adamc@463 776 string ")"]
adamc@463 777 in
adamc@463 778 box [string "({",
adamc@463 779 space,
adamc@463 780 p_typ env (t, ErrorMsg.dummySpan),
adamc@463 781 space,
adamc@463 782 string "unurlify_",
adamc@463 783 string (Int.toString i),
adamc@463 784 string "(void) {",
adamc@463 785 newline,
adamc@463 786 box [string "return",
adamc@463 787 space,
adamc@463 788 doEm xncs,
adamc@463 789 string ";",
adamc@463 790 newline],
adamc@463 791 string "}",
adamc@463 792 newline,
adamc@463 793 newline,
adamc@463 794
adamc@463 795 string "unurlify_",
adamc@463 796 string (Int.toString i),
adamc@463 797 string "();",
adamc@463 798 newline,
adamc@463 799 string "})"]
adamc@463 800 end
adamc@463 801
adamc@471 802 | TOption t =>
adamc@471 803 box [string "(request[0] == '/' ? ++request : request, ",
adamc@471 804 string "((!strncmp(request, \"None\", 4) ",
adamc@471 805 string "&& (request[4] == 0 || request[4] == '/')) ",
adamc@471 806 string "? (request += 4, NULL) ",
adamc@471 807 string ": ((!strncmp(request, \"Some\", 4) ",
adamc@471 808 string "&& request[4] == '/') ",
adamc@471 809 string "? (request += 5, ",
adamc@471 810 if isUnboxable t then
adamc@471 811 unurlify' rf (#1 t)
adamc@471 812 else
adamc@471 813 box [string "({",
adamc@471 814 newline,
adamc@471 815 p_typ env t,
adamc@471 816 space,
adamc@471 817 string "*tmp",
adamc@471 818 space,
adamc@471 819 string "=",
adamc@471 820 space,
adamc@471 821 string "uw_malloc(ctx, sizeof(",
adamc@471 822 p_typ env t,
adamc@471 823 string "));",
adamc@471 824 newline,
adamc@471 825 string "*tmp",
adamc@471 826 space,
adamc@471 827 string "=",
adamc@471 828 space,
adamc@471 829 unurlify' rf (#1 t),
adamc@471 830 string ";",
adamc@471 831 newline,
adamc@471 832 string "tmp;",
adamc@471 833 newline,
adamc@471 834 string "})"],
adamc@471 835 string ") :",
adamc@471 836 space,
adamc@471 837 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
adamc@471 838
adamc@463 839 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@463 840 space)
adamc@463 841 in
adamc@463 842 unurlify' IS.empty t
adamc@463 843 end
adamc@463 844
adamc@182 845 fun p_exp' par env (e, loc) =
adamc@29 846 case e of
adamc@276 847 EPrim p => Prim.p_t_GCC p
adamc@29 848 | ERel n => p_rel env n
adamc@109 849 | ENamed n => p_enamed env n
adamc@188 850 | ECon (Enum, pc, _) => p_patCon env pc
adamc@198 851 | ECon (Option, pc, NONE) => string "NULL"
adamc@198 852 | ECon (Option, pc, SOME e) =>
adamc@198 853 let
adamc@198 854 val to = case pc of
adamc@198 855 PConVar n => #2 (E.lookupConstructor env n)
adamc@198 856 | PConFfi {arg, ...} => arg
adamc@198 857
adamc@198 858 val t = case to of
adamc@198 859 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
adamc@198 860 | SOME t => t
adamc@198 861 in
adamc@463 862 if isUnboxable t then
adamc@463 863 p_exp' par env e
adamc@463 864 else
adamc@463 865 box [string "({",
adamc@463 866 newline,
adamc@463 867 p_typ env t,
adamc@463 868 space,
adamc@463 869 string "*tmp",
adamc@463 870 space,
adamc@463 871 string "=",
adamc@463 872 space,
adamc@463 873 string "uw_malloc(ctx, sizeof(",
adamc@463 874 p_typ env t,
adamc@463 875 string "));",
adamc@463 876 newline,
adamc@463 877 string "*tmp",
adamc@463 878 space,
adamc@463 879 string "=",
adamc@463 880 p_exp' par env e,
adamc@463 881 string ";",
adamc@463 882 newline,
adamc@463 883 string "tmp;",
adamc@463 884 newline,
adamc@463 885 string "})"]
adamc@198 886 end
adamc@188 887 | ECon (Default, pc, eo) =>
adamc@181 888 let
adamc@196 889 val (xd, xc, xn) = patConInfo env pc
adamc@181 890 in
adamc@182 891 box [string "({",
adamc@181 892 newline,
adamc@181 893 string "struct",
adamc@181 894 space,
adamc@185 895 string xd,
adamc@181 896 space,
adamc@181 897 string "*tmp",
adamc@181 898 space,
adamc@181 899 string "=",
adamc@181 900 space,
adamc@311 901 string "uw_malloc(ctx, sizeof(struct ",
adamc@185 902 string xd,
adamc@181 903 string "));",
adamc@181 904 newline,
adamc@181 905 string "tmp->tag",
adamc@181 906 space,
adamc@181 907 string "=",
adamc@181 908 space,
adamc@185 909 string xc,
adamc@181 910 string ";",
adamc@181 911 newline,
adamc@181 912 case eo of
adamc@181 913 NONE => box []
adamc@185 914 | SOME e => box [string "tmp->data.",
adamc@196 915 string xn,
adamc@181 916 space,
adamc@181 917 string "=",
adamc@181 918 space,
adamc@181 919 p_exp env e,
adamc@181 920 string ";",
adamc@181 921 newline],
adamc@181 922 string "tmp;",
adamc@181 923 newline,
adamc@181 924 string "})"]
adamc@181 925 end
adamc@297 926 | ENone _ => string "NULL"
adamc@290 927 | ESome (t, e) =>
adamc@463 928 if isUnboxable t then
adamc@463 929 p_exp' par env e
adamc@463 930 else
adamc@463 931 box [string "({",
adamc@463 932 newline,
adamc@463 933 p_typ env t,
adamc@463 934 space,
adamc@463 935 string "*tmp",
adamc@463 936 space,
adamc@463 937 string "=",
adamc@463 938 space,
adamc@463 939 string "uw_malloc(ctx, sizeof(",
adamc@463 940 p_typ env t,
adamc@463 941 string "));",
adamc@463 942 newline,
adamc@463 943 string "*tmp",
adamc@463 944 space,
adamc@463 945 string "=",
adamc@463 946 p_exp' par env e,
adamc@463 947 string ";",
adamc@463 948 newline,
adamc@463 949 string "tmp;",
adamc@463 950 newline,
adamc@463 951 string "})"]
adamc@109 952
adamc@316 953 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@283 954 | EError (e, t) =>
adamc@283 955 box [string "({",
adamc@283 956 newline,
adamc@283 957 p_typ env t,
adamc@283 958 space,
adamc@283 959 string "tmp;",
adamc@283 960 newline,
adamc@311 961 string "uw_error(ctx, FATAL, \"",
adamc@292 962 string (ErrorMsg.spanToString loc),
adamc@292 963 string ": %s\", ",
adamc@283 964 p_exp env e,
adamc@283 965 string ");",
adamc@283 966 newline,
adamc@283 967 string "tmp;",
adamc@283 968 newline,
adamc@283 969 string "})"]
adamc@311 970 | EFfiApp (m, x, es) => box [string "uw_",
adamc@316 971 p_ident m,
adamc@53 972 string "_",
adamc@316 973 p_ident x,
adamc@117 974 string "(ctx, ",
adamc@53 975 p_list (p_exp env) es,
adamc@53 976 string ")"]
adamc@316 977 | EApp (f, args) =>
adamc@316 978 parenIf par (box [p_exp' true env f,
adamc@316 979 string "(ctx,",
adamc@316 980 space,
adamc@316 981 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@316 982 string ")"])
adamc@29 983
adamc@387 984 | EUnop (s, e1) =>
adamc@387 985 parenIf par (box [string s,
adamc@387 986 space,
adamc@387 987 p_exp' true env e1])
adamc@387 988
adamc@387 989 | EBinop (s, e1, e2) =>
adamc@389 990 if Char.isAlpha (String.sub (s, size s - 1)) then
adamc@389 991 box [string s,
adamc@390 992 string "(",
adamc@389 993 p_exp env e1,
adamc@389 994 string ",",
adamc@389 995 space,
adamc@389 996 p_exp env e2,
adamc@389 997 string ")"]
adamc@389 998 else
adamc@389 999 parenIf par (box [p_exp' true env e1,
adamc@389 1000 space,
adamc@389 1001 string s,
adamc@389 1002 space,
adamc@389 1003 p_exp' true env e2])
adamc@387 1004
adamc@29 1005 | ERecord (i, xes) => box [string "({",
adamc@29 1006 space,
adamc@29 1007 string "struct",
adamc@29 1008 space,
adamc@311 1009 string ("__uws_" ^ Int.toString i),
adamc@29 1010 space,
adamc@181 1011 string "tmp",
adamc@29 1012 space,
adamc@29 1013 string "=",
adamc@29 1014 space,
adamc@29 1015 string "{",
adamc@29 1016 p_list (fn (_, e) =>
adamc@29 1017 p_exp env e) xes,
adamc@29 1018 string "};",
adamc@29 1019 space,
adamc@181 1020 string "tmp;",
adamc@29 1021 space,
adamc@29 1022 string "})" ]
adamc@29 1023 | EField (e, x) =>
adamc@29 1024 box [p_exp' true env e,
adamc@311 1025 string ".__uwf_",
adamc@316 1026 p_ident x]
adamc@29 1027
adamc@182 1028 | ECase (e, pes, {disc, result}) =>
adamc@182 1029 let
adamc@182 1030 val final = newGoto ()
adamc@182 1031
adamc@182 1032 val body = foldl (fn ((p, e), body) =>
adamc@182 1033 let
adamc@182 1034 val exit = newGoto ()
adamc@182 1035 val (pr, _) = p_pat_preamble env p
adamc@182 1036 val (p, env) = p_pat (env,
adamc@182 1037 box [string "goto",
adamc@182 1038 space,
adamc@182 1039 exit,
adamc@182 1040 string ";"],
adamc@182 1041 0) p
adamc@182 1042 in
adamc@182 1043 box [body,
adamc@182 1044 box [string "{",
adamc@182 1045 newline,
adamc@182 1046 pr,
adamc@182 1047 newline,
adamc@182 1048 p,
adamc@182 1049 newline,
adamc@182 1050 string "result",
adamc@182 1051 space,
adamc@182 1052 string "=",
adamc@182 1053 space,
adamc@182 1054 p_exp env e,
adamc@182 1055 string ";",
adamc@182 1056 newline,
adamc@182 1057 string "goto",
adamc@182 1058 space,
adamc@182 1059 final,
adamc@182 1060 string ";",
adamc@182 1061 newline,
adamc@182 1062 string "}"],
adamc@182 1063 newline,
adamc@182 1064 exit,
adamc@182 1065 string ":",
adamc@182 1066 newline]
adamc@182 1067 end) (box []) pes
adamc@182 1068 in
adamc@182 1069 box [string "({",
adamc@182 1070 newline,
adamc@182 1071 p_typ env disc,
adamc@182 1072 space,
adamc@182 1073 string "disc0",
adamc@182 1074 space,
adamc@182 1075 string "=",
adamc@182 1076 space,
adamc@182 1077 p_exp env e,
adamc@182 1078 string ";",
adamc@182 1079 newline,
adamc@182 1080 p_typ env result,
adamc@182 1081 space,
adamc@182 1082 string "result;",
adamc@182 1083 newline,
adamc@182 1084 body,
adamc@311 1085 string "uw_error(ctx, FATAL, \"",
adamc@182 1086 string (ErrorMsg.spanToString loc),
adamc@182 1087 string ": pattern match failure\");",
adamc@182 1088 newline,
adamc@182 1089 final,
adamc@182 1090 string ":",
adamc@182 1091 space,
adamc@182 1092 string "result;",
adamc@182 1093 newline,
adamc@182 1094 string "})"]
adamc@182 1095 end
adamc@181 1096
adamc@311 1097 | EWrite e => box [string "(uw_write(ctx, ",
adamc@102 1098 p_exp env e,
adamc@311 1099 string "), uw_unit_v)"]
adamc@102 1100
adamc@106 1101 | ESeq (e1, e2) => box [string "(",
adamc@106 1102 p_exp env e1,
adamc@106 1103 string ",",
adamc@106 1104 space,
adamc@106 1105 p_exp env e2,
adamc@106 1106 string ")"]
adamc@269 1107 | ELet (x, t, e1, e2) => box [string "({",
adamc@269 1108 newline,
adamc@269 1109 p_typ env t,
adamc@269 1110 space,
adamc@311 1111 string "__uwr_",
adamc@316 1112 p_ident x,
adamc@272 1113 string "_",
adamc@272 1114 string (Int.toString (E.countERels env)),
adamc@269 1115 space,
adamc@269 1116 string "=",
adamc@269 1117 space,
adamc@269 1118 p_exp env e1,
adamc@269 1119 string ";",
adamc@269 1120 newline,
adamc@269 1121 p_exp (E.pushERel env x t) e2,
adamc@269 1122 string ";",
adamc@269 1123 newline,
adamc@269 1124 string "})"]
adamc@269 1125
adamc@282 1126 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
adamc@278 1127 let
adamc@316 1128 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
adamc@278 1129 val tables = ListUtil.mapConcat (fn (x, xts) =>
adamc@316 1130 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
adamc@278 1131 tables
adamc@278 1132
adamc@278 1133 val outputs = exps @ tables
adamc@325 1134 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
adamc@324 1135
adamc@324 1136 val wontLeakStrings = notLeaky env true state
adamc@324 1137 val wontLeakAnything = notLeaky env false state
adamc@278 1138 in
adamc@324 1139 box [string "(uw_begin_region(ctx), ",
adamc@324 1140 if wontLeakAnything then
adamc@331 1141 string "uw_begin_region(ctx), "
adamc@324 1142 else
adamc@324 1143 box [],
adamc@324 1144 string "({",
adamc@278 1145 newline,
adamc@311 1146 string "PGconn *conn = uw_get_db(ctx);",
adamc@278 1147 newline,
adamc@282 1148 case prepared of
adamc@282 1149 NONE => box [string "char *query = ",
adamc@282 1150 p_exp env query,
adamc@282 1151 string ";",
adamc@282 1152 newline]
adamc@282 1153 | SOME _ =>
adamc@282 1154 let
adamc@282 1155 val ets = getPargs query
adamc@282 1156 in
adamc@282 1157 box [p_list_sepi newline
adamc@282 1158 (fn i => fn (e, t) =>
adamc@282 1159 box [p_sql_type t,
adamc@282 1160 space,
adamc@282 1161 string "arg",
adamc@282 1162 string (Int.toString (i + 1)),
adamc@282 1163 space,
adamc@282 1164 string "=",
adamc@282 1165 space,
adamc@282 1166 p_exp env e,
adamc@282 1167 string ";"])
adamc@282 1168 ets,
adamc@282 1169 newline,
adamc@282 1170 newline,
adamc@282 1171
adamc@282 1172 string "const char *paramValues[] = { ",
adamc@282 1173 p_list_sepi (box [string ",", space])
adamc@282 1174 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@282 1175 string (Int.toString (i + 1))]))
adamc@282 1176 ets,
adamc@282 1177 string " };",
adamc@282 1178 newline,
adamc@282 1179 newline]
adamc@282 1180 end,
adamc@278 1181 string "int n, i;",
adamc@278 1182 newline,
adamc@278 1183 p_typ env state,
adamc@278 1184 space,
adamc@278 1185 string "acc",
adamc@278 1186 space,
adamc@278 1187 string "=",
adamc@278 1188 space,
adamc@278 1189 p_exp env initial,
adamc@278 1190 string ";",
adamc@278 1191 newline,
adamc@470 1192 case prepared of
adamc@470 1193 NONE => box [string "printf(\"Executing: %s\\n\", query);",
adamc@470 1194 newline]
adamc@470 1195 | _ => box [],
adamc@282 1196 string "PGresult *res = ",
adamc@282 1197 case prepared of
adamc@295 1198 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1199 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@282 1200 string (Int.toString n),
adamc@282 1201 string "\", ",
adamc@282 1202 string (Int.toString (length (getPargs query))),
adamc@295 1203 string ", paramValues, NULL, NULL, 0);"],
adamc@278 1204 newline,
adamc@278 1205 newline,
adamc@277 1206
adamc@311 1207 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@278 1208 newline,
adamc@278 1209 newline,
adamc@277 1210
adamc@278 1211 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@278 1212 newline,
adamc@278 1213 box [string "PQclear(res);",
adamc@278 1214 newline,
adamc@311 1215 string "uw_error(ctx, FATAL, \"",
adamc@278 1216 string (ErrorMsg.spanToString loc),
adamc@282 1217 string ": Query failed:\\n%s\\n%s\", ",
adamc@282 1218 case prepared of
adamc@282 1219 NONE => string "query"
adamc@282 1220 | SOME _ => p_exp env query,
adamc@282 1221 string ", PQerrorMessage(conn));",
adamc@278 1222 newline],
adamc@278 1223 string "}",
adamc@278 1224 newline,
adamc@278 1225 newline,
adamc@277 1226
adamc@323 1227 string "uw_end_region(ctx);",
adamc@323 1228 newline,
adamc@425 1229 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
adamc@425 1230 newline,
adamc@278 1231 string "n = PQntuples(res);",
adamc@278 1232 newline,
adamc@278 1233 string "for (i = 0; i < n; ++i) {",
adamc@278 1234 newline,
adamc@278 1235 box [string "struct",
adamc@278 1236 space,
adamc@311 1237 string "__uws_",
adamc@278 1238 string (Int.toString rnum),
adamc@278 1239 space,
adamc@311 1240 string "__uwr_r_",
adamc@278 1241 string (Int.toString (E.countERels env)),
adamc@278 1242 string ";",
adamc@278 1243 newline,
adamc@278 1244 p_typ env state,
adamc@278 1245 space,
adamc@311 1246 string "__uwr_acc_",
adamc@278 1247 string (Int.toString (E.countERels env + 1)),
adamc@278 1248 space,
adamc@278 1249 string "=",
adamc@278 1250 space,
adamc@278 1251 string "acc;",
adamc@278 1252 newline,
adamc@278 1253 newline,
adamc@278 1254
adamc@278 1255 p_list_sepi (box []) (fn i =>
adamc@278 1256 fn (proj, t) =>
adamc@311 1257 box [string "__uwr_r_",
adamc@278 1258 string (Int.toString (E.countERels env)),
adamc@278 1259 string ".",
adamc@278 1260 string proj,
adamc@278 1261 space,
adamc@278 1262 string "=",
adamc@278 1263 space,
adamc@467 1264 p_getcol wontLeakStrings env t i,
adamc@278 1265 string ";",
adamc@278 1266 newline]) outputs,
adamc@278 1267
adamc@278 1268 newline,
adamc@278 1269 newline,
adamc@278 1270
adamc@278 1271 string "acc",
adamc@278 1272 space,
adamc@278 1273 string "=",
adamc@278 1274 space,
adamc@278 1275 p_exp (E.pushERel
adamc@278 1276 (E.pushERel env "r" (TRecord rnum, loc))
adamc@278 1277 "acc" state)
adamc@278 1278 body,
adamc@278 1279 string ";",
adamc@278 1280 newline],
adamc@278 1281 string "}",
adamc@278 1282 newline,
adamc@278 1283 newline,
adamc@425 1284 string "uw_pop_cleanup(ctx);",
adamc@278 1285 newline,
adamc@324 1286 if wontLeakAnything then
adamc@324 1287 box [string "uw_end_region(ctx);",
adamc@324 1288 newline]
adamc@324 1289 else
adamc@324 1290 box [],
adamc@278 1291 string "acc;",
adamc@278 1292 newline,
adamc@323 1293 string "}))"]
adamc@278 1294 end
adamc@106 1295
adamc@307 1296 | EDml {dml, prepared} =>
adamc@337 1297 box [string "(uw_begin_region(ctx), ({",
adamc@307 1298 newline,
adamc@311 1299 string "PGconn *conn = uw_get_db(ctx);",
adamc@307 1300 newline,
adamc@307 1301 case prepared of
adamc@307 1302 NONE => box [string "char *dml = ",
adamc@307 1303 p_exp env dml,
adamc@307 1304 string ";",
adamc@307 1305 newline]
adamc@307 1306 | SOME _ =>
adamc@307 1307 let
adamc@307 1308 val ets = getPargs dml
adamc@307 1309 in
adamc@307 1310 box [p_list_sepi newline
adamc@307 1311 (fn i => fn (e, t) =>
adamc@307 1312 box [p_sql_type t,
adamc@307 1313 space,
adamc@307 1314 string "arg",
adamc@307 1315 string (Int.toString (i + 1)),
adamc@307 1316 space,
adamc@307 1317 string "=",
adamc@307 1318 space,
adamc@307 1319 p_exp env e,
adamc@307 1320 string ";"])
adamc@307 1321 ets,
adamc@307 1322 newline,
adamc@307 1323 newline,
adamc@307 1324
adamc@307 1325 string "const char *paramValues[] = { ",
adamc@307 1326 p_list_sepi (box [string ",", space])
adamc@307 1327 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@307 1328 string (Int.toString (i + 1))]))
adamc@307 1329 ets,
adamc@307 1330 string " };",
adamc@307 1331 newline,
adamc@307 1332 newline]
adamc@307 1333 end,
adamc@307 1334 newline,
adamc@307 1335 newline,
adamc@307 1336 string "PGresult *res = ",
adamc@307 1337 case prepared of
adamc@307 1338 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1339 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@307 1340 string (Int.toString n),
adamc@307 1341 string "\", ",
adamc@307 1342 string (Int.toString (length (getPargs dml))),
adamc@307 1343 string ", paramValues, NULL, NULL, 0);"],
adamc@307 1344 newline,
adamc@307 1345 newline,
adamc@307 1346
adamc@311 1347 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
adamc@307 1348 newline,
adamc@307 1349 newline,
adamc@307 1350
adamc@307 1351 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@307 1352 newline,
adamc@307 1353 box [string "PQclear(res);",
adamc@307 1354 newline,
adamc@311 1355 string "uw_error(ctx, FATAL, \"",
adamc@307 1356 string (ErrorMsg.spanToString loc),
adamc@307 1357 string ": DML failed:\\n%s\\n%s\", ",
adamc@307 1358 case prepared of
adamc@307 1359 NONE => string "dml"
adamc@307 1360 | SOME _ => p_exp env dml,
adamc@307 1361 string ", PQerrorMessage(conn));",
adamc@307 1362 newline],
adamc@307 1363 string "}",
adamc@307 1364 newline,
adamc@307 1365 newline,
adamc@307 1366
adamc@307 1367 string "PQclear(res);",
adamc@307 1368 newline,
adamc@337 1369 string "uw_end_region(ctx);",
adamc@337 1370 newline,
adamc@311 1371 string "uw_unit_v;",
adamc@307 1372 newline,
adamc@337 1373 string "}))"]
adamc@307 1374
adamc@338 1375 | ENextval {seq, prepared} =>
adamc@338 1376 let
adamc@338 1377 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@338 1378 val query = (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
adamc@338 1379 in
adamc@338 1380 box [string "(uw_begin_region(ctx), ",
adamc@338 1381 string "({",
adamc@338 1382 newline,
adamc@338 1383 string "PGconn *conn = uw_get_db(ctx);",
adamc@338 1384 newline,
adamc@338 1385 case prepared of
adamc@338 1386 NONE => box [string "char *query = ",
adamc@338 1387 p_exp env query,
adamc@338 1388 string ";",
adamc@338 1389 newline]
adamc@338 1390 | SOME _ =>
adamc@338 1391 box [],
adamc@338 1392 newline,
adamc@338 1393 string "PGresult *res = ",
adamc@338 1394 case prepared of
adamc@338 1395 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@338 1396 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@338 1397 string (Int.toString n),
adamc@338 1398 string "\", 0, NULL, NULL, NULL, 0);"],
adamc@338 1399 newline,
adamc@338 1400 string "uw_Basis_int n;",
adamc@338 1401 newline,
adamc@338 1402 newline,
adamc@338 1403
adamc@338 1404 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
adamc@338 1405 newline,
adamc@338 1406 newline,
adamc@338 1407
adamc@338 1408 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@338 1409 newline,
adamc@338 1410 box [string "PQclear(res);",
adamc@338 1411 newline,
adamc@338 1412 string "uw_error(ctx, FATAL, \"",
adamc@338 1413 string (ErrorMsg.spanToString loc),
adamc@338 1414 string ": Query failed:\\n%s\\n%s\", ",
adamc@338 1415 case prepared of
adamc@338 1416 NONE => string "query"
adamc@338 1417 | SOME _ => p_exp env query,
adamc@338 1418 string ", PQerrorMessage(conn));",
adamc@338 1419 newline],
adamc@338 1420 string "}",
adamc@338 1421 newline,
adamc@338 1422 newline,
adamc@338 1423
adamc@338 1424 string "uw_end_region(ctx);",
adamc@338 1425 newline,
adamc@338 1426 string "n = PQntuples(res);",
adamc@338 1427 newline,
adamc@338 1428 string "if (n != 1) {",
adamc@338 1429 newline,
adamc@338 1430 box [string "PQclear(res);",
adamc@338 1431 newline,
adamc@338 1432 string "uw_error(ctx, FATAL, \"",
adamc@338 1433 string (ErrorMsg.spanToString loc),
adamc@338 1434 string ": Wrong number of result rows:\\n%s\\n%s\", ",
adamc@338 1435 case prepared of
adamc@338 1436 NONE => string "query"
adamc@338 1437 | SOME _ => p_exp env query,
adamc@338 1438 string ", PQerrorMessage(conn));",
adamc@338 1439 newline],
adamc@338 1440 string "}",
adamc@338 1441 newline,
adamc@338 1442 newline,
adamc@338 1443
adamc@338 1444 string "n = ",
adamc@338 1445 p_unsql true env (TFfi ("Basis", "int"), loc)
adamc@338 1446 (string "PQgetvalue(res, 0, 0)"),
adamc@338 1447 string ";",
adamc@338 1448 newline,
adamc@338 1449 string "PQclear(res);",
adamc@338 1450 newline,
adamc@338 1451 string "n;",
adamc@338 1452 newline,
adamc@338 1453 string "}))"]
adamc@338 1454 end
adamc@338 1455
adamc@463 1456 | EUnurlify (e, t) =>
adamc@463 1457 let
adamc@463 1458 fun getIt () =
adamc@463 1459 if isUnboxable t then
adamc@463 1460 unurlify env t
adamc@463 1461 else
adamc@463 1462 box [string "({",
adamc@463 1463 newline,
adamc@463 1464 p_typ env t,
adamc@463 1465 string " *tmp = uw_malloc(ctx, sizeof(",
adamc@463 1466 p_typ env t,
adamc@463 1467 string "));",
adamc@463 1468 newline,
adamc@463 1469 string "*tmp = ",
adamc@463 1470 unurlify env t,
adamc@463 1471 string ";",
adamc@463 1472 newline,
adamc@463 1473 string "tmp;",
adamc@463 1474 newline,
adamc@463 1475 string "})"]
adamc@463 1476 in
adamc@463 1477 box [string "({",
adamc@463 1478 newline,
adamc@463 1479 string "uw_Basis_string request = ",
adamc@463 1480 p_exp env e,
adamc@463 1481 string ";",
adamc@463 1482 newline,
adamc@463 1483 newline,
adamc@463 1484 string "(request ? ",
adamc@463 1485 getIt (),
adamc@463 1486 string " : NULL);",
adamc@463 1487 newline,
adamc@463 1488 string "})"]
adamc@463 1489 end
adamc@463 1490
adamc@29 1491 and p_exp env = p_exp' false env
adamc@29 1492
adamc@129 1493 fun p_fun env (fx, n, args, ran, e) =
adamc@129 1494 let
adamc@129 1495 val nargs = length args
adamc@129 1496 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 1497 in
adamc@129 1498 box [string "static",
adamc@129 1499 space,
adamc@129 1500 p_typ env ran,
adamc@129 1501 space,
adamc@316 1502 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@129 1503 string "(",
adamc@129 1504 p_list_sep (box [string ",", space]) (fn x => x)
adamc@311 1505 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 1506 box [p_typ env dom,
adamc@129 1507 space,
adamc@129 1508 p_rel env' (nargs - i - 1)]) args),
adamc@129 1509 string ")",
adamc@129 1510 space,
adamc@129 1511 string "{",
adamc@129 1512 newline,
adamc@129 1513 box[string "return(",
adamc@129 1514 p_exp env' e,
adamc@129 1515 string ");"],
adamc@129 1516 newline,
adamc@129 1517 string "}"]
adamc@129 1518 end
adamc@129 1519
adamc@129 1520 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 1521 case d of
adamc@29 1522 DStruct (n, xts) =>
adamc@196 1523 let
adamc@196 1524 val env = E.declBinds env dAll
adamc@196 1525 in
adamc@196 1526 box [string "struct",
adamc@196 1527 space,
adamc@311 1528 string ("__uws_" ^ Int.toString n),
adamc@196 1529 space,
adamc@196 1530 string "{",
adamc@196 1531 newline,
adamc@196 1532 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 1533 space,
adamc@311 1534 string "__uwf_",
adamc@316 1535 p_ident x,
adamc@196 1536 string ";",
adamc@196 1537 newline]) xts,
adamc@196 1538 string "};"]
adamc@196 1539 end
adamc@188 1540 | DDatatype (Enum, x, n, xncs) =>
adamc@188 1541 box [string "enum",
adamc@188 1542 space,
adamc@316 1543 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@188 1544 space,
adamc@188 1545 string "{",
adamc@188 1546 space,
adamc@316 1547 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1548 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@188 1549 space,
adamc@188 1550 string "};"]
adamc@198 1551 | DDatatype (Option, _, _, _) => box []
adamc@188 1552 | DDatatype (Default, x, n, xncs) =>
adamc@165 1553 let
adamc@165 1554 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 1555 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 1556 in
adamc@165 1557 box [string "enum",
adamc@165 1558 space,
adamc@316 1559 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1560 space,
adamc@165 1561 string "{",
adamc@165 1562 space,
adamc@316 1563 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1564 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@165 1565 space,
adamc@165 1566 string "};",
adamc@165 1567 newline,
adamc@165 1568 newline,
adamc@165 1569 string "struct",
adamc@165 1570 space,
adamc@316 1571 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1572 space,
adamc@165 1573 string "{",
adamc@165 1574 newline,
adamc@165 1575 string "enum",
adamc@165 1576 space,
adamc@316 1577 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1578 space,
adamc@165 1579 string "tag;",
adamc@165 1580 newline,
adamc@165 1581 box (case xncsArgs of
adamc@165 1582 [] => []
adamc@165 1583 | _ => [string "union",
adamc@165 1584 space,
adamc@165 1585 string "{",
adamc@165 1586 newline,
adamc@165 1587 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 1588 space,
adamc@316 1589 string ("uw_" ^ ident x),
adamc@165 1590 string ";"]) xncsArgs,
adamc@165 1591 newline,
adamc@165 1592 string "}",
adamc@165 1593 space,
adamc@165 1594 string "data;",
adamc@165 1595 newline]),
adamc@165 1596 string "};"]
adamc@188 1597 end
adamc@29 1598
adamc@196 1599 | DDatatypeForward _ => box []
adamc@196 1600
adamc@29 1601 | DVal (x, n, t, e) =>
adamc@29 1602 box [p_typ env t,
adamc@29 1603 space,
adamc@316 1604 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
adamc@29 1605 space,
adamc@29 1606 string "=",
adamc@29 1607 space,
adamc@29 1608 p_exp env e,
adamc@29 1609 string ";"]
adamc@129 1610 | DFun vi => p_fun env vi
adamc@129 1611 | DFunRec vis =>
adamc@29 1612 let
adamc@129 1613 val env = E.declBinds env dAll
adamc@29 1614 in
adamc@129 1615 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 1616 box [string "static",
adamc@129 1617 space,
adamc@129 1618 p_typ env ran,
adamc@129 1619 space,
adamc@316 1620 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@311 1621 string "(uw_context,",
adamc@129 1622 space,
adamc@129 1623 p_list_sep (box [string ",", space])
adamc@129 1624 (fn (_, dom) => p_typ env dom) args,
adamc@129 1625 string ");"]) vis,
adamc@29 1626 newline,
adamc@129 1627 p_list_sep newline (p_fun env) vis,
adamc@129 1628 newline]
adamc@29 1629 end
adamc@273 1630 | DTable (x, _) => box [string "/* SQL table ",
adamc@273 1631 string x,
adamc@273 1632 string " */",
adamc@273 1633 newline]
adamc@338 1634 | DSequence x => box [string "/* SQL sequence ",
adamc@338 1635 string x,
adamc@338 1636 string " */",
adamc@338 1637 newline]
adamc@311 1638 | DDatabase s => box [string "static void uw_db_validate(uw_context);",
adamc@272 1639 newline,
adamc@311 1640 string "static void uw_db_prepare(uw_context);",
adamc@282 1641 newline,
adamc@275 1642 newline,
adamc@311 1643 string "void uw_db_init(uw_context ctx) {",
adamc@273 1644 newline,
adamc@272 1645 string "PGconn *conn = PQconnectdb(\"",
adamc@272 1646 string (String.toString s),
adamc@272 1647 string "\");",
adamc@272 1648 newline,
adamc@311 1649 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1650 string "\"libpq can't allocate a connection.\");",
adamc@272 1651 newline,
adamc@272 1652 string "if (PQstatus(conn) != CONNECTION_OK) {",
adamc@272 1653 newline,
adamc@272 1654 box [string "char msg[1024];",
adamc@272 1655 newline,
adamc@272 1656 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@272 1657 newline,
adamc@272 1658 string "msg[1023] = 0;",
adamc@272 1659 newline,
adamc@272 1660 string "PQfinish(conn);",
adamc@272 1661 newline,
adamc@311 1662 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@272 1663 string "\"Connection to Postgres server failed: %s\", msg);"],
adamc@272 1664 newline,
adamc@272 1665 string "}",
adamc@272 1666 newline,
adamc@311 1667 string "uw_set_db(ctx, conn);",
adamc@272 1668 newline,
adamc@311 1669 string "uw_db_validate(ctx);",
adamc@275 1670 newline,
adamc@311 1671 string "uw_db_prepare(ctx);",
adamc@282 1672 newline,
adamc@272 1673 string "}",
adamc@272 1674 newline,
adamc@272 1675 newline,
adamc@311 1676 string "void uw_db_close(uw_context ctx) {",
adamc@272 1677 newline,
adamc@311 1678 string "PQfinish(uw_get_db(ctx));",
adamc@272 1679 newline,
adamc@272 1680 string "}",
adamc@424 1681 newline,
adamc@424 1682 newline,
adamc@424 1683
adamc@424 1684 string "int uw_db_begin(uw_context ctx) {",
adamc@424 1685 newline,
adamc@424 1686 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1687 newline,
adamc@424 1688 string "PGresult *res = PQexec(conn, \"BEGIN\");",
adamc@424 1689 newline,
adamc@424 1690 newline,
adamc@424 1691 string "if (res == NULL) return 1;",
adamc@424 1692 newline,
adamc@424 1693 newline,
adamc@424 1694 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1695 box [string "PQclear(res);",
adamc@424 1696 newline,
adamc@424 1697 string "return 1;",
adamc@424 1698 newline],
adamc@424 1699 string "}",
adamc@424 1700 newline,
adamc@424 1701 string "return 0;",
adamc@424 1702 newline,
adamc@424 1703 string "}",
adamc@424 1704 newline,
adamc@424 1705 newline,
adamc@424 1706
adamc@424 1707 string "int uw_db_commit(uw_context ctx) {",
adamc@424 1708 newline,
adamc@424 1709 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1710 newline,
adamc@424 1711 string "PGresult *res = PQexec(conn, \"COMMIT\");",
adamc@424 1712 newline,
adamc@424 1713 newline,
adamc@424 1714 string "if (res == NULL) return 1;",
adamc@424 1715 newline,
adamc@424 1716 newline,
adamc@424 1717 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1718 box [string "PQclear(res);",
adamc@424 1719 newline,
adamc@424 1720 string "return 1;",
adamc@424 1721 newline],
adamc@424 1722 string "}",
adamc@424 1723 newline,
adamc@424 1724 string "return 0;",
adamc@424 1725 newline,
adamc@424 1726 string "}",
adamc@424 1727 newline,
adamc@424 1728 newline,
adamc@424 1729
adamc@424 1730 string "int uw_db_rollback(uw_context ctx) {",
adamc@424 1731 newline,
adamc@424 1732 string "PGconn *conn = uw_get_db(ctx);",
adamc@424 1733 newline,
adamc@424 1734 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
adamc@424 1735 newline,
adamc@424 1736 newline,
adamc@424 1737 string "if (res == NULL) return 1;",
adamc@424 1738 newline,
adamc@424 1739 newline,
adamc@424 1740 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@424 1741 box [string "PQclear(res);",
adamc@424 1742 newline,
adamc@424 1743 string "return 1;",
adamc@424 1744 newline],
adamc@424 1745 string "}",
adamc@424 1746 newline,
adamc@424 1747 string "return 0;",
adamc@424 1748 newline,
adamc@424 1749 string "}",
adamc@272 1750 newline]
adamc@29 1751
adamc@467 1752 | DPreparedStatements [] =>
adamc@467 1753 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@467 1754 newline,
adamc@467 1755 string "}"]
adamc@282 1756 | DPreparedStatements ss =>
adamc@311 1757 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@282 1758 newline,
adamc@311 1759 string "PGconn *conn = uw_get_db(ctx);",
adamc@282 1760 newline,
adamc@282 1761 string "PGresult *res;",
adamc@282 1762 newline,
adamc@282 1763 newline,
adamc@282 1764
adamc@282 1765 p_list_sepi newline (fn i => fn (s, n) =>
adamc@311 1766 box [string "res = PQprepare(conn, \"uw",
adamc@282 1767 string (Int.toString i),
adamc@282 1768 string "\", \"",
adamc@282 1769 string (String.toString s),
adamc@282 1770 string "\", ",
adamc@282 1771 string (Int.toString n),
adamc@282 1772 string ", NULL);",
adamc@282 1773 newline,
adamc@282 1774 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@282 1775 newline,
adamc@282 1776 box [string "char msg[1024];",
adamc@282 1777 newline,
adamc@282 1778 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@282 1779 newline,
adamc@282 1780 string "msg[1023] = 0;",
adamc@282 1781 newline,
adamc@282 1782 string "PQclear(res);",
adamc@282 1783 newline,
adamc@282 1784 string "PQfinish(conn);",
adamc@282 1785 newline,
adamc@311 1786 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
adamc@282 1787 string (String.toString s),
adamc@282 1788 string "\\n%s\", msg);",
adamc@282 1789 newline],
adamc@282 1790 string "}",
adamc@282 1791 newline,
adamc@282 1792 string "PQclear(res);",
adamc@282 1793 newline])
adamc@282 1794 ss,
adamc@282 1795
adamc@282 1796 string "}"]
adamc@282 1797
adamc@144 1798 datatype 'a search =
adamc@144 1799 Found of 'a
adamc@144 1800 | NotFound
adamc@144 1801 | Error
adamc@120 1802
adamc@467 1803 fun p_sqltype'' env (tAll as (t, loc)) =
adamc@275 1804 case t of
adamc@275 1805 TFfi ("Basis", "int") => "int8"
adamc@275 1806 | TFfi ("Basis", "float") => "float8"
adamc@275 1807 | TFfi ("Basis", "string") => "text"
adamc@275 1808 | TFfi ("Basis", "bool") => "bool"
adamc@438 1809 | TFfi ("Basis", "time") => "timestamp"
adamc@275 1810 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
adamc@275 1811 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@275 1812 "ERROR")
adamc@275 1813
adamc@467 1814 fun p_sqltype' env (tAll as (t, loc)) =
adamc@467 1815 case t of
adamc@467 1816 (TOption t, _) => p_sqltype'' env t
adamc@467 1817 | _ => p_sqltype'' env t ^ " NOT NULL"
adamc@467 1818
adamc@275 1819 fun p_sqltype env t = string (p_sqltype' env t)
adamc@101 1820
adamc@467 1821 fun p_sqltype_base' env t =
adamc@467 1822 case t of
adamc@467 1823 (TOption t, _) => p_sqltype'' env t
adamc@467 1824 | _ => p_sqltype'' env t
adamc@467 1825
adamc@467 1826 fun p_sqltype_base env t = string (p_sqltype_base' env t)
adamc@467 1827
adamc@467 1828 fun is_not_null t =
adamc@467 1829 case t of
adamc@467 1830 (TOption _, _) => false
adamc@467 1831 | _ => true
adamc@467 1832
adamc@101 1833 fun p_file env (ds, ps) =
adamc@29 1834 let
adamc@101 1835 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 1836 (p_decl env d,
adamc@31 1837 E.declBinds env d))
adamc@101 1838 env ds
adamc@144 1839
adamc@144 1840 val fields = foldl (fn ((ek, _, _, ts), fields) =>
adamc@144 1841 case ek of
adamc@144 1842 Core.Link => fields
adamc@144 1843 | Core.Action =>
adamc@280 1844 case List.nth (ts, length ts - 2) of
adamc@144 1845 (TRecord i, _) =>
adamc@144 1846 let
adamc@144 1847 val xts = E.lookupStruct env i
adamc@144 1848 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 1849 in
adamc@144 1850 foldl (fn ((x, _), fields) =>
adamc@144 1851 let
adamc@144 1852 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 1853 in
adamc@144 1854 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 1855 xtsSet'))
adamc@144 1856 end) fields xts
adamc@144 1857 end
adamc@144 1858 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 1859 SM.empty ps
adamc@144 1860
adamc@144 1861 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 1862 let
adamc@144 1863 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 1864 case SM.find (fnums, x') of
adamc@144 1865 NONE => unusable
adamc@144 1866 | SOME n => IS.add (unusable, n))
adamc@144 1867 IS.empty xs
adamc@144 1868
adamc@144 1869 fun findAvailable n =
adamc@144 1870 if IS.member (unusable, n) then
adamc@144 1871 findAvailable (n + 1)
adamc@144 1872 else
adamc@144 1873 n
adamc@144 1874 in
adamc@144 1875 SM.insert (fnums, x, findAvailable 0)
adamc@144 1876 end)
adamc@144 1877 SM.empty fields
adamc@144 1878
adamc@144 1879 fun makeSwitch (fnums, i) =
adamc@144 1880 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 1881 | (n, Error) => Error
adamc@144 1882 | (n, Found n') => if n = n' then
adamc@144 1883 Found n'
adamc@144 1884 else
adamc@144 1885 Error) NotFound fnums of
adamc@144 1886 NotFound => box [string "return",
adamc@144 1887 space,
adamc@144 1888 string "-1;"]
adamc@144 1889 | Found n => box [string "return",
adamc@144 1890 space,
adamc@144 1891 string (Int.toString n),
adamc@144 1892 string ";"]
adamc@144 1893 | Error =>
adamc@144 1894 let
adamc@144 1895 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 1896 let
adamc@144 1897 val ch = if i < size x then
adamc@144 1898 String.sub (x, i)
adamc@144 1899 else
adamc@144 1900 chr 0
adamc@144 1901
adamc@144 1902 val fnums = case CM.find (cmap, ch) of
adamc@144 1903 NONE => SM.empty
adamc@144 1904 | SOME fnums => fnums
adamc@144 1905 val fnums = SM.insert (fnums, x, n)
adamc@144 1906 in
adamc@144 1907 CM.insert (cmap, ch, fnums)
adamc@144 1908 end)
adamc@144 1909 CM.empty fnums
adamc@144 1910
adamc@144 1911 val cmap = CM.listItemsi cmap
adamc@144 1912 in
adamc@144 1913 case cmap of
adamc@144 1914 [(_, fnums)] =>
adamc@144 1915 box [string "if",
adamc@144 1916 space,
adamc@144 1917 string "(name[",
adamc@144 1918 string (Int.toString i),
adamc@144 1919 string "]",
adamc@144 1920 space,
adamc@144 1921 string "==",
adamc@144 1922 space,
adamc@144 1923 string "0)",
adamc@144 1924 space,
adamc@144 1925 string "return",
adamc@144 1926 space,
adamc@144 1927 string "-1;",
adamc@144 1928 newline,
adamc@144 1929 makeSwitch (fnums, i+1)]
adamc@144 1930 | _ =>
adamc@144 1931 box [string "switch",
adamc@144 1932 space,
adamc@144 1933 string "(name[",
adamc@144 1934 string (Int.toString i),
adamc@144 1935 string "])",
adamc@144 1936 space,
adamc@144 1937 string "{",
adamc@144 1938 newline,
adamc@144 1939 box (map (fn (ch, fnums) =>
adamc@144 1940 box [string "case",
adamc@144 1941 space,
adamc@144 1942 if ch = chr 0 then
adamc@144 1943 string "0:"
adamc@144 1944 else
adamc@144 1945 box [string "'",
adamc@144 1946 string (Char.toString ch),
adamc@144 1947 string "':"],
adamc@144 1948 newline,
adamc@144 1949 makeSwitch (fnums, i+1),
adamc@144 1950 newline]) cmap),
adamc@144 1951 string "default:",
adamc@144 1952 newline,
adamc@144 1953 string "return",
adamc@144 1954 space,
adamc@144 1955 string "-1;",
adamc@144 1956 newline,
adamc@144 1957 string "}"]
adamc@144 1958 end
adamc@144 1959
adamc@144 1960 fun p_page (ek, s, n, ts) =
adamc@144 1961 let
adamc@144 1962 val (ts, defInputs, inputsVar) =
adamc@144 1963 case ek of
adamc@314 1964 Core.Link => (List.take (ts, length ts - 1), string "", string "")
adamc@144 1965 | Core.Action =>
adamc@280 1966 case List.nth (ts, length ts - 2) of
adamc@144 1967 (TRecord i, _) =>
adamc@144 1968 let
adamc@144 1969 val xts = E.lookupStruct env i
adamc@144 1970 in
adamc@280 1971 (List.take (ts, length ts - 2),
adamc@144 1972 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 1973 space,
adamc@311 1974 string "uw_input_",
adamc@316 1975 p_ident x,
adamc@144 1976 string ";",
adamc@144 1977 newline]) xts),
adamc@144 1978 newline,
adamc@144 1979 box (map (fn (x, t) =>
adamc@144 1980 let
adamc@144 1981 val n = case SM.find (fnums, x) of
adamc@144 1982 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 1983 | SOME n => n
adamc@190 1984
adamc@190 1985 val f = case t of
adamc@190 1986 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 1987 | _ => ""
adamc@144 1988 in
adamc@311 1989 box [string "request = uw_get_",
adamc@190 1990 string f,
adamc@190 1991 string "input(ctx, ",
adamc@144 1992 string (Int.toString n),
adamc@144 1993 string ");",
adamc@144 1994 newline,
adamc@433 1995 string "if (request == NULL)",
adamc@144 1996 newline,
adamc@433 1997 box [string "uw_error(ctx, FATAL, \"Missing input ",
adamc@144 1998 string x,
adamc@433 1999 string "\");"],
adamc@144 2000 newline,
adamc@311 2001 string "uw_input_",
adamc@316 2002 p_ident x,
adamc@144 2003 space,
adamc@144 2004 string "=",
adamc@144 2005 space,
adamc@463 2006 unurlify env t,
adamc@144 2007 string ";",
adamc@144 2008 newline]
adamc@144 2009 end) xts),
adamc@311 2010 string "struct __uws_",
adamc@144 2011 string (Int.toString i),
adamc@144 2012 space,
adamc@311 2013 string "uw_inputs",
adamc@144 2014 space,
adamc@144 2015 string "= {",
adamc@144 2016 newline,
adamc@311 2017 box (map (fn (x, _) => box [string "uw_input_",
adamc@316 2018 p_ident x,
adamc@144 2019 string ",",
adamc@144 2020 newline]) xts),
adamc@144 2021 string "};",
adamc@144 2022 newline],
adamc@144 2023 box [string ",",
adamc@144 2024 space,
adamc@311 2025 string "uw_inputs"])
adamc@144 2026 end
adamc@144 2027
adamc@144 2028 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@144 2029 in
adamc@144 2030 box [string "if (!strncmp(request, \"",
adamc@144 2031 string (String.toString s),
adamc@144 2032 string "\", ",
adamc@144 2033 string (Int.toString (size s)),
adamc@198 2034 string ") && (request[",
adamc@198 2035 string (Int.toString (size s)),
adamc@198 2036 string "] == 0 || request[",
adamc@198 2037 string (Int.toString (size s)),
adamc@198 2038 string "] == '/')) {",
adamc@144 2039 newline,
adamc@144 2040 string "request += ",
adamc@144 2041 string (Int.toString (size s)),
adamc@144 2042 string ";",
adamc@144 2043 newline,
adamc@144 2044 string "if (*request == '/') ++request;",
adamc@144 2045 newline,
adamc@144 2046 box [string "{",
adamc@144 2047 newline,
adamc@144 2048 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 2049 space,
adamc@144 2050 string "arg",
adamc@144 2051 string (Int.toString i),
adamc@144 2052 space,
adamc@144 2053 string "=",
adamc@144 2054 space,
adamc@463 2055 unurlify env t,
adamc@144 2056 string ";",
adamc@144 2057 newline]) ts),
adamc@144 2058 defInputs,
adamc@144 2059 p_enamed env n,
adamc@144 2060 string "(",
adamc@144 2061 p_list_sep (box [string ",", space])
adamc@144 2062 (fn x => x)
adamc@272 2063 (string "ctx"
adamc@280 2064 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 2065 inputsVar,
adamc@311 2066 string ", uw_unit_v);",
adamc@144 2067 newline,
adamc@144 2068 string "return;",
adamc@144 2069 newline,
adamc@144 2070 string "}",
adamc@144 2071 newline,
adamc@144 2072 string "}"]
adamc@144 2073 ]
adamc@144 2074 end
adamc@144 2075
adamc@144 2076 val pds' = map p_page ps
adamc@275 2077
adamc@275 2078 val tables = List.mapPartial (fn (DTable (s, xts), _) => SOME (s, xts)
adamc@275 2079 | _ => NONE) ds
adamc@377 2080 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
adamc@377 2081 | _ => NONE) ds
adamc@275 2082
adamc@275 2083 val validate =
adamc@311 2084 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@275 2085 newline,
adamc@311 2086 string "PGconn *conn = uw_get_db(ctx);",
adamc@275 2087 newline,
adamc@275 2088 string "PGresult *res;",
adamc@275 2089 newline,
adamc@275 2090 newline,
adamc@275 2091 p_list_sep newline
adamc@275 2092 (fn (s, xts) =>
adamc@275 2093 let
adamc@377 2094 val sl = CharVector.map Char.toLower s
adamc@377 2095
adamc@275 2096 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2097 ^ sl ^ "'"
adamc@275 2098
adamc@275 2099 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2100 sl,
adamc@275 2101 "') AND (",
adamc@275 2102 String.concatWith " OR "
adamc@275 2103 (map (fn (x, t) =>
adamc@311 2104 String.concat ["(attname = 'uw_",
adamc@275 2105 CharVector.map
adamc@316 2106 Char.toLower (ident x),
adamc@275 2107 "' AND atttypid = (SELECT oid FROM pg_type",
adamc@275 2108 " WHERE typname = '",
adamc@467 2109 p_sqltype_base' env t,
adamc@467 2110 "') AND attnotnull = ",
adamc@467 2111 if is_not_null t then
adamc@467 2112 "TRUE"
adamc@467 2113 else
adamc@467 2114 "FALSE",
adamc@467 2115 ")"]) xts),
adamc@275 2116 ")"]
adamc@275 2117
adamc@275 2118 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2119 sl,
adamc@325 2120 "') AND attname LIKE 'uw_%'"]
adamc@275 2121 in
adamc@275 2122 box [string "res = PQexec(conn, \"",
adamc@275 2123 string q,
adamc@275 2124 string "\");",
adamc@275 2125 newline,
adamc@275 2126 newline,
adamc@275 2127 string "if (res == NULL) {",
adamc@275 2128 newline,
adamc@275 2129 box [string "PQfinish(conn);",
adamc@275 2130 newline,
adamc@311 2131 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2132 newline],
adamc@275 2133 string "}",
adamc@275 2134 newline,
adamc@275 2135 newline,
adamc@275 2136 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2137 newline,
adamc@275 2138 box [string "char msg[1024];",
adamc@275 2139 newline,
adamc@275 2140 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2141 newline,
adamc@275 2142 string "msg[1023] = 0;",
adamc@275 2143 newline,
adamc@275 2144 string "PQclear(res);",
adamc@275 2145 newline,
adamc@275 2146 string "PQfinish(conn);",
adamc@275 2147 newline,
adamc@311 2148 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2149 string q,
adamc@275 2150 string "\\n%s\", msg);",
adamc@275 2151 newline],
adamc@275 2152 string "}",
adamc@275 2153 newline,
adamc@275 2154 newline,
adamc@275 2155 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@275 2156 newline,
adamc@275 2157 box [string "PQclear(res);",
adamc@275 2158 newline,
adamc@275 2159 string "PQfinish(conn);",
adamc@275 2160 newline,
adamc@311 2161 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2162 string s,
adamc@275 2163 string "' does not exist.\");",
adamc@275 2164 newline],
adamc@275 2165 string "}",
adamc@275 2166 newline,
adamc@275 2167 newline,
adamc@275 2168 string "PQclear(res);",
adamc@275 2169 newline,
adamc@275 2170
adamc@275 2171 string "res = PQexec(conn, \"",
adamc@275 2172 string q',
adamc@275 2173 string "\");",
adamc@275 2174 newline,
adamc@275 2175 newline,
adamc@275 2176 string "if (res == NULL) {",
adamc@275 2177 newline,
adamc@275 2178 box [string "PQfinish(conn);",
adamc@275 2179 newline,
adamc@311 2180 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2181 newline],
adamc@275 2182 string "}",
adamc@275 2183 newline,
adamc@275 2184 newline,
adamc@275 2185 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2186 newline,
adamc@275 2187 box [string "char msg[1024];",
adamc@275 2188 newline,
adamc@275 2189 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2190 newline,
adamc@275 2191 string "msg[1023] = 0;",
adamc@275 2192 newline,
adamc@275 2193 string "PQclear(res);",
adamc@275 2194 newline,
adamc@275 2195 string "PQfinish(conn);",
adamc@275 2196 newline,
adamc@311 2197 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2198 string q',
adamc@275 2199 string "\\n%s\", msg);",
adamc@275 2200 newline],
adamc@275 2201 string "}",
adamc@275 2202 newline,
adamc@275 2203 newline,
adamc@275 2204 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2205 string (Int.toString (length xts)),
adamc@275 2206 string "\")) {",
adamc@275 2207 newline,
adamc@275 2208 box [string "PQclear(res);",
adamc@275 2209 newline,
adamc@275 2210 string "PQfinish(conn);",
adamc@275 2211 newline,
adamc@311 2212 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2213 string s,
adamc@275 2214 string "' has the wrong column types.\");",
adamc@275 2215 newline],
adamc@275 2216 string "}",
adamc@275 2217 newline,
adamc@275 2218 newline,
adamc@275 2219 string "PQclear(res);",
adamc@275 2220 newline,
adamc@275 2221 newline,
adamc@275 2222
adamc@275 2223 string "res = PQexec(conn, \"",
adamc@275 2224 string q'',
adamc@275 2225 string "\");",
adamc@275 2226 newline,
adamc@275 2227 newline,
adamc@275 2228 string "if (res == NULL) {",
adamc@275 2229 newline,
adamc@275 2230 box [string "PQfinish(conn);",
adamc@275 2231 newline,
adamc@311 2232 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2233 newline],
adamc@275 2234 string "}",
adamc@275 2235 newline,
adamc@275 2236 newline,
adamc@275 2237 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2238 newline,
adamc@275 2239 box [string "char msg[1024];",
adamc@275 2240 newline,
adamc@275 2241 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2242 newline,
adamc@275 2243 string "msg[1023] = 0;",
adamc@275 2244 newline,
adamc@275 2245 string "PQclear(res);",
adamc@275 2246 newline,
adamc@275 2247 string "PQfinish(conn);",
adamc@275 2248 newline,
adamc@311 2249 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2250 string q'',
adamc@275 2251 string "\\n%s\", msg);",
adamc@275 2252 newline],
adamc@275 2253 string "}",
adamc@275 2254 newline,
adamc@275 2255 newline,
adamc@275 2256 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2257 string (Int.toString (length xts)),
adamc@275 2258 string "\")) {",
adamc@275 2259 newline,
adamc@275 2260 box [string "PQclear(res);",
adamc@275 2261 newline,
adamc@275 2262 string "PQfinish(conn);",
adamc@275 2263 newline,
adamc@311 2264 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2265 string s,
adamc@275 2266 string "' has extra columns.\");",
adamc@275 2267 newline],
adamc@275 2268 string "}",
adamc@275 2269 newline,
adamc@275 2270 newline,
adamc@275 2271 string "PQclear(res);",
adamc@275 2272 newline]
adamc@275 2273 end) tables,
adamc@377 2274
adamc@377 2275 p_list_sep newline
adamc@377 2276 (fn s =>
adamc@377 2277 let
adamc@377 2278 val sl = CharVector.map Char.toLower s
adamc@377 2279
adamc@377 2280 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2281 ^ sl ^ "' AND relkind = 'S'"
adamc@377 2282 in
adamc@377 2283 box [string "res = PQexec(conn, \"",
adamc@377 2284 string q,
adamc@377 2285 string "\");",
adamc@377 2286 newline,
adamc@377 2287 newline,
adamc@377 2288 string "if (res == NULL) {",
adamc@377 2289 newline,
adamc@377 2290 box [string "PQfinish(conn);",
adamc@377 2291 newline,
adamc@377 2292 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@377 2293 newline],
adamc@377 2294 string "}",
adamc@377 2295 newline,
adamc@377 2296 newline,
adamc@377 2297 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@377 2298 newline,
adamc@377 2299 box [string "char msg[1024];",
adamc@377 2300 newline,
adamc@377 2301 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@377 2302 newline,
adamc@377 2303 string "msg[1023] = 0;",
adamc@377 2304 newline,
adamc@377 2305 string "PQclear(res);",
adamc@377 2306 newline,
adamc@377 2307 string "PQfinish(conn);",
adamc@377 2308 newline,
adamc@377 2309 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@377 2310 string q,
adamc@377 2311 string "\\n%s\", msg);",
adamc@377 2312 newline],
adamc@377 2313 string "}",
adamc@377 2314 newline,
adamc@377 2315 newline,
adamc@377 2316 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@377 2317 newline,
adamc@377 2318 box [string "PQclear(res);",
adamc@377 2319 newline,
adamc@377 2320 string "PQfinish(conn);",
adamc@377 2321 newline,
adamc@377 2322 string "uw_error(ctx, FATAL, \"Sequence '",
adamc@377 2323 string s,
adamc@377 2324 string "' does not exist.\");",
adamc@377 2325 newline],
adamc@377 2326 string "}",
adamc@377 2327 newline,
adamc@377 2328 newline,
adamc@377 2329 string "PQclear(res);",
adamc@377 2330 newline]
adamc@377 2331 end) sequences,
adamc@377 2332
adamc@275 2333 string "}"]
adamc@432 2334
adamc@432 2335 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
adamc@29 2336 in
adamc@144 2337 box [string "#include <stdio.h>",
adamc@144 2338 newline,
adamc@144 2339 string "#include <stdlib.h>",
adamc@144 2340 newline,
adamc@272 2341 string "#include <string.h>",
adamc@272 2342 newline,
adamc@390 2343 string "#include <math.h>",
adamc@390 2344 newline,
adamc@432 2345 if hasDb then
adamc@432 2346 box [string "#include <postgresql/libpq-fe.h>",
adamc@432 2347 newline]
adamc@432 2348 else
adamc@432 2349 box [],
adamc@144 2350 newline,
adamc@378 2351 string "#include \"",
adamc@378 2352 string (OS.Path.joinDirFile {dir = Config.includ,
adamc@378 2353 file = "urweb.h"}),
adamc@378 2354 string "\"",
adamc@101 2355 newline,
adamc@101 2356 newline,
adamc@101 2357 p_list_sep newline (fn x => x) pds,
adamc@101 2358 newline,
adamc@311 2359 string "int uw_inputs_len = ",
adamc@144 2360 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 2361 string ";",
adamc@144 2362 newline,
adamc@144 2363 newline,
adamc@311 2364 string "int uw_input_num(char *name) {",
adamc@144 2365 newline,
adamc@144 2366 makeSwitch (fnums, 0),
adamc@144 2367 string "}",
adamc@144 2368 newline,
adamc@144 2369 newline,
adamc@311 2370 string "void uw_handle(uw_context ctx, char *request) {",
adamc@101 2371 newline,
adamc@101 2372 p_list_sep newline (fn x => x) pds',
adamc@101 2373 newline,
adamc@387 2374 string "uw_error(ctx, FATAL, \"Unknown page\");",
adamc@387 2375 newline,
adamc@101 2376 string "}",
adamc@275 2377 newline,
adamc@275 2378 newline,
adamc@432 2379 if hasDb then
adamc@432 2380 validate
adamc@432 2381 else
adamc@432 2382 box [],
adamc@376 2383 newline,
adamc@376 2384 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
adamc@376 2385 box []
adamc@376 2386 else
adamc@376 2387 box [newline,
adamc@376 2388 string "void uw_db_init(uw_context ctx) { };",
adamc@424 2389 newline,
adamc@424 2390 string "int uw_db_begin(uw_context ctx) { return 0; };",
adamc@424 2391 newline,
adamc@424 2392 string "int uw_db_commit(uw_context ctx) { return 0; };",
adamc@424 2393 newline,
adamc@424 2394 string "int uw_db_rollback(uw_context ctx) { return 0; };",
adamc@376 2395 newline]]
adamc@29 2396 end
adamc@29 2397
adamc@274 2398 fun p_sql env (ds, _) =
adamc@274 2399 let
adamc@274 2400 val (pps, _) = ListUtil.foldlMap
adamc@274 2401 (fn (dAll as (d, _), env) =>
adamc@274 2402 let
adamc@274 2403 val pp = case d of
adamc@274 2404 DTable (s, xts) =>
adamc@274 2405 box [string "CREATE TABLE ",
adamc@274 2406 string s,
adamc@274 2407 string "(",
adamc@274 2408 p_list (fn (x, t) =>
adamc@311 2409 box [string "uw_",
adamc@275 2410 string (CharVector.map Char.toLower x),
adamc@274 2411 space,
adamc@467 2412 p_sqltype env (t, ErrorMsg.dummySpan)]) xts,
adamc@274 2413 string ");",
adamc@274 2414 newline,
adamc@274 2415 newline]
adamc@338 2416 | DSequence s =>
adamc@338 2417 box [string "CREATE SEQUENCE ",
adamc@338 2418 string s,
adamc@338 2419 string ";",
adamc@338 2420 newline,
adamc@338 2421 newline]
adamc@274 2422 | _ => box []
adamc@274 2423 in
adamc@274 2424 (pp, E.declBinds env dAll)
adamc@274 2425 end)
adamc@274 2426 env ds
adamc@274 2427 in
adamc@274 2428 box pps
adamc@274 2429 end
adamc@274 2430
adamc@29 2431 end