annotate src/cjr_print.sml @ 463:bb27c7efcd90

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