annotate src/cjr_print.sml @ 742:43553c93dd8c

Reading blobs from the database
author Adam Chlipala <adamc@hcoop.net>
date Sun, 26 Apr 2009 10:45:59 -0400
parents f7e2026dd5ae
children cd67c3a942e3
rev   line source
adamc@643 1 (* Copyright (c) 2008-2009, 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@476 73 TFun (t1, t2) => (EM.errorAt loc "Function type remains";
adamc@476 74 string "<FUNCTION>")
adamc@29 75 | TRecord i => box [string "struct",
adamc@29 76 space,
adamc@311 77 string "__uws_",
adamc@29 78 string (Int.toString i)]
adamc@188 79 | TDatatype (Enum, n, _) =>
adamc@188 80 (box [string "enum",
adamc@188 81 space,
adamc@311 82 string ("__uwe_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n)]
adamc@311 83 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@198 84 | TDatatype (Option, n, xncs) =>
adamc@198 85 (case ListUtil.search #3 (!xncs) of
adamc@198 86 NONE => raise Fail "CjrPrint: TDatatype marked Option has no constructor with an argument"
adamc@198 87 | SOME t =>
adamc@198 88 case #1 t of
adamc@198 89 TDatatype _ => p_typ' par env t
adamc@199 90 | TFfi ("Basis", "string") => p_typ' par env t
adamc@198 91 | _ => box [p_typ' par env t,
adamc@198 92 string "*"])
adamc@188 93 | TDatatype (Default, n, _) =>
adamc@165 94 (box [string "struct",
adamc@165 95 space,
adamc@311 96 string ("__uwd_" ^ #1 (E.lookupDatatype env n) ^ "_" ^ Int.toString n ^ "*")]
adamc@311 97 handle CjrEnv.UnboundNamed _ => string ("__uwd_UNBOUND__" ^ Int.toString n))
adamc@316 98 | TFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@288 99 | TOption t =>
adamc@463 100 if isUnboxable t then
adamc@463 101 p_typ' par env t
adamc@463 102 else
adamc@463 103 box [p_typ' par env t,
adamc@463 104 string "*"]
adamc@29 105
adamc@29 106 and p_typ env = p_typ' false env
adamc@29 107
adamc@316 108 fun p_rel env n = string ("__uwr_" ^ ident (#1 (E.lookupERel env n)) ^ "_" ^ Int.toString (E.countERels env - n - 1))
adamc@311 109 handle CjrEnv.UnboundRel _ => string ("__uwr_UNBOUND_" ^ Int.toString (E.countERels env - n - 1))
adamc@29 110
adamc@109 111 fun p_enamed env n =
adamc@316 112 string ("__uwn_" ^ ident (#1 (E.lookupENamed env n)) ^ "_" ^ Int.toString n)
adamc@311 113 handle CjrEnv.UnboundNamed _ => string ("__uwn_UNBOUND_" ^ Int.toString n)
adamc@109 114
adamc@182 115 fun p_con_named env n =
adamc@316 116 string ("__uwc_" ^ ident (#1 (E.lookupConstructor env n)) ^ "_" ^ Int.toString n)
adamc@311 117 handle CjrEnv.UnboundNamed _ => string ("__uwc_UNBOUND_" ^ Int.toString n)
adamc@182 118
adamc@182 119 fun p_pat_preamble env (p, _) =
adamc@182 120 case p of
adamc@182 121 PWild => (box [],
adamc@182 122 env)
adamc@182 123 | PVar (x, t) => (box [p_typ env t,
adamc@182 124 space,
adamc@311 125 string "__uwr_",
adamc@316 126 p_ident x,
adamc@182 127 string "_",
adamc@182 128 string (Int.toString (E.countERels env)),
adamc@182 129 string ";",
adamc@182 130 newline],
adamc@196 131 E.pushERel env x t)
adamc@182 132 | PPrim _ => (box [], env)
adamc@188 133 | PCon (_, _, NONE) => (box [], env)
adamc@188 134 | PCon (_, _, SOME p) => p_pat_preamble env p
adamc@182 135 | PRecord xps =>
adamc@182 136 foldl (fn ((_, p, _), (pp, env)) =>
adamc@182 137 let
adamc@182 138 val (pp', env) = p_pat_preamble env p
adamc@182 139 in
adamc@182 140 (box [pp', pp], env)
adamc@182 141 end) (box [], env) xps
adamc@288 142 | PNone _ => (box [], env)
adamc@288 143 | PSome (_, p) => p_pat_preamble env p
adamc@182 144
adamc@182 145 fun p_patCon env pc =
adamc@182 146 case pc of
adamc@182 147 PConVar n => p_con_named env n
adamc@316 148 | PConFfi {mod = m, con, ...} => string ("uw_" ^ ident m ^ "_" ^ ident con)
adamc@182 149
adamc@182 150 fun p_pat (env, exit, depth) (p, _) =
adamc@182 151 case p of
adamc@182 152 PWild =>
adamc@182 153 (box [], env)
adamc@182 154 | PVar (x, t) =>
adamc@311 155 (box [string "__uwr_",
adamc@316 156 p_ident x,
adamc@182 157 string "_",
adamc@182 158 string (Int.toString (E.countERels env)),
adamc@182 159 space,
adamc@182 160 string "=",
adamc@182 161 space,
adamc@182 162 string "disc",
adamc@182 163 string (Int.toString depth),
adamc@182 164 string ";"],
adamc@182 165 E.pushERel env x t)
adamc@182 166 | PPrim (Prim.Int n) =>
adamc@182 167 (box [string "if",
adamc@182 168 space,
adamc@182 169 string "(disc",
adamc@182 170 string (Int.toString depth),
adamc@182 171 space,
adamc@182 172 string "!=",
adamc@182 173 space,
adamc@276 174 Prim.p_t_GCC (Prim.Int n),
adamc@182 175 string ")",
adamc@182 176 space,
adamc@182 177 exit],
adamc@182 178 env)
adamc@182 179 | PPrim (Prim.String s) =>
adamc@182 180 (box [string "if",
adamc@182 181 space,
adamc@182 182 string "(strcmp(disc",
adamc@182 183 string (Int.toString depth),
adamc@182 184 string ",",
adamc@182 185 space,
adamc@276 186 Prim.p_t_GCC (Prim.String s),
adamc@182 187 string "))",
adamc@182 188 space,
adamc@182 189 exit],
adamc@182 190 env)
adamc@182 191 | PPrim _ => raise Fail "CjrPrint: Disallowed PPrim primitive"
adamc@182 192
adamc@188 193 | PCon (dk, pc, po) =>
adamc@182 194 let
adamc@182 195 val (p, env) =
adamc@182 196 case po of
adamc@182 197 NONE => (box [], env)
adamc@182 198 | SOME p =>
adamc@182 199 let
adamc@182 200 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 201
adamc@182 202 val (x, to) = case pc of
adamc@182 203 PConVar n =>
adamc@182 204 let
adamc@182 205 val (x, to, _) = E.lookupConstructor env n
adamc@182 206 in
adamc@316 207 ("uw_" ^ ident x, to)
adamc@182 208 end
adamc@188 209 | PConFfi {mod = m, con, arg, ...} =>
adamc@316 210 ("uw_" ^ ident m ^ "_" ^ ident con, arg)
adamc@182 211
adamc@182 212 val t = case to of
adamc@182 213 NONE => raise Fail "CjrPrint: Constructor mismatch"
adamc@182 214 | SOME t => t
adamc@182 215 in
adamc@182 216 (box [string "{",
adamc@182 217 newline,
adamc@182 218 p_typ env t,
adamc@182 219 space,
adamc@182 220 string "disc",
adamc@182 221 string (Int.toString (depth + 1)),
adamc@182 222 space,
adamc@182 223 string "=",
adamc@182 224 space,
adamc@198 225 case dk of
adamc@198 226 Enum => raise Fail "CjrPrint: Looking at argument of no-argument constructor"
adamc@198 227 | Default => box [string "disc",
adamc@198 228 string (Int.toString depth),
adamc@198 229 string "->data.",
adamc@198 230 string x]
adamc@198 231 | Option =>
adamc@463 232 if isUnboxable t then
adamc@463 233 box [string "disc",
adamc@463 234 string (Int.toString depth)]
adamc@463 235 else
adamc@463 236 box [string "*disc",
adamc@463 237 string (Int.toString depth)],
adamc@182 238 string ";",
adamc@182 239 newline,
adamc@182 240 p,
adamc@182 241 newline,
adamc@182 242 string "}"],
adamc@182 243 env)
adamc@182 244 end
adamc@182 245 in
adamc@182 246 (box [string "if",
adamc@182 247 space,
adamc@182 248 string "(disc",
adamc@182 249 string (Int.toString depth),
adamc@198 250 case (dk, po) of
adamc@198 251 (Enum, _) => box [space,
adamc@198 252 string "!=",
adamc@198 253 space,
adamc@198 254 p_patCon env pc]
adamc@198 255 | (Default, _) => box [string "->tag",
adamc@198 256 space,
adamc@198 257 string "!=",
adamc@198 258 space,
adamc@198 259 p_patCon env pc]
adamc@198 260 | (Option, NONE) => box [space,
adamc@198 261 string "!=",
adamc@198 262 space,
adamc@198 263 string "NULL"]
adamc@198 264 | (Option, SOME _) => box [space,
adamc@198 265 string "==",
adamc@198 266 space,
adamc@198 267 string "NULL"],
adamc@182 268 string ")",
adamc@182 269 space,
adamc@182 270 exit,
adamc@182 271 newline,
adamc@182 272 p],
adamc@182 273 env)
adamc@182 274 end
adamc@182 275
adamc@182 276 | PRecord xps =>
adamc@182 277 let
adamc@182 278 val (xps, env) =
adamc@182 279 ListUtil.foldlMap (fn ((x, p, t), env) =>
adamc@182 280 let
adamc@182 281 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@182 282
adamc@182 283 val p = box [string "{",
adamc@182 284 newline,
adamc@182 285 p_typ env t,
adamc@182 286 space,
adamc@182 287 string "disc",
adamc@182 288 string (Int.toString (depth + 1)),
adamc@182 289 space,
adamc@182 290 string "=",
adamc@182 291 space,
adamc@182 292 string "disc",
adamc@182 293 string (Int.toString depth),
adamc@311 294 string ".__uwf_",
adamc@316 295 p_ident x,
adamc@182 296 string ";",
adamc@182 297 newline,
adamc@182 298 p,
adamc@182 299 newline,
adamc@182 300 string "}"]
adamc@182 301 in
adamc@182 302 (p, env)
adamc@182 303 end) env xps
adamc@182 304 in
adamc@182 305 (p_list_sep newline (fn x => x) xps,
adamc@182 306 env)
adamc@182 307 end
adamc@182 308
adamc@288 309 | PNone t =>
adamc@288 310 (box [string "if",
adamc@288 311 space,
adamc@288 312 string "(disc",
adamc@288 313 string (Int.toString depth),
adamc@288 314 space,
adamc@288 315 string "!=",
adamc@288 316 space,
adamc@288 317 string "NULL)",
adamc@288 318 space,
adamc@288 319 exit,
adamc@288 320 newline],
adamc@288 321 env)
adamc@288 322
adamc@288 323 | PSome (t, p) =>
adamc@288 324 let
adamc@288 325 val (p, env) =
adamc@288 326 let
adamc@288 327 val (p, env) = p_pat (env, exit, depth + 1) p
adamc@288 328 in
adamc@288 329 (box [string "{",
adamc@288 330 newline,
adamc@288 331 p_typ env t,
adamc@288 332 space,
adamc@288 333 string "disc",
adamc@288 334 string (Int.toString (depth + 1)),
adamc@288 335 space,
adamc@288 336 string "=",
adamc@288 337 space,
adamc@463 338 if isUnboxable t then
adamc@463 339 box [string "disc",
adamc@463 340 string (Int.toString depth)]
adamc@463 341 else
adamc@463 342 box [string "*disc",
adamc@463 343 string (Int.toString depth)],
adamc@288 344 string ";",
adamc@288 345 newline,
adamc@288 346 p,
adamc@288 347 newline,
adamc@288 348 string "}"],
adamc@288 349 env)
adamc@288 350 end
adamc@288 351 in
adamc@288 352 (box [string "if",
adamc@288 353 space,
adamc@288 354 string "(disc",
adamc@288 355 string (Int.toString depth),
adamc@288 356 space,
adamc@288 357 string "==",
adamc@288 358 space,
adamc@288 359 string "NULL)",
adamc@288 360 space,
adamc@288 361 exit,
adamc@288 362 newline,
adamc@288 363 p],
adamc@288 364 env)
adamc@288 365 end
adamc@288 366
adamc@182 367 local
adamc@182 368 val count = ref 0
adamc@182 369 in
adamc@182 370 fun newGoto () =
adamc@182 371 let
adamc@182 372 val r = !count
adamc@182 373 in
adamc@182 374 count := r + 1;
adamc@182 375 string ("L" ^ Int.toString r)
adamc@182 376 end
adamc@182 377 end
adamc@182 378
adamc@185 379 fun patConInfo env pc =
adamc@185 380 case pc of
adamc@185 381 PConVar n =>
adamc@185 382 let
adamc@185 383 val (x, _, dn) = E.lookupConstructor env n
adamc@185 384 val (dx, _) = E.lookupDatatype env dn
adamc@185 385 in
adamc@316 386 ("__uwd_" ^ ident dx ^ "_" ^ Int.toString dn,
adamc@316 387 "__uwc_" ^ ident x ^ "_" ^ Int.toString n,
adamc@316 388 "uw_" ^ ident x)
adamc@185 389 end
adamc@186 390 | PConFfi {mod = m, datatyp, con, ...} =>
adamc@316 391 ("uw_" ^ ident m ^ "_" ^ ident datatyp,
adamc@316 392 "uw_" ^ ident m ^ "_" ^ ident con,
adamc@316 393 "uw_" ^ ident con)
adamc@185 394
adamc@324 395 fun p_unsql wontLeakStrings env (tAll as (t, loc)) e =
adamc@278 396 case t of
adamc@311 397 TFfi ("Basis", "int") => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
adamc@311 398 | TFfi ("Basis", "float") => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
adamc@324 399 | TFfi ("Basis", "string") =>
adamc@324 400 if wontLeakStrings then
adamc@324 401 e
adamc@324 402 else
adamc@737 403 box [string "uw_strdup(ctx, ", e, string ")"]
adamc@311 404 | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
adamc@438 405 | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
adamc@678 406 | TFfi ("Basis", "channel") => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
adamc@682 407 | TFfi ("Basis", "client") => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"]
adamc@467 408
adamc@278 409 | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
adamc@278 410 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@278 411 string "ERROR")
adamc@278 412
adamc@467 413 fun p_getcol wontLeakStrings env (tAll as (t, loc)) i =
adamc@467 414 case t of
adamc@467 415 TOption t =>
adamc@467 416 box [string "(PQgetisnull (res, i, ",
adamc@467 417 string (Int.toString i),
adamc@467 418 string ") ? NULL : ",
adamc@467 419 case t of
adamc@467 420 (TFfi ("Basis", "string"), _) => p_getcol wontLeakStrings env t i
adamc@467 421 | _ => box [string "({",
adamc@467 422 newline,
adamc@467 423 p_typ env t,
adamc@467 424 space,
adamc@467 425 string "*tmp = uw_malloc(ctx, sizeof(",
adamc@467 426 p_typ env t,
adamc@467 427 string "));",
adamc@467 428 newline,
adamc@467 429 string "*tmp = ",
adamc@467 430 p_getcol wontLeakStrings env t i,
adamc@467 431 string ";",
adamc@467 432 newline,
adamc@467 433 string "tmp;",
adamc@467 434 newline,
adamc@467 435 string "})"],
adamc@467 436 string ")"]
adamc@742 437
adamc@742 438 | TFfi ("Basis", "blob") => box [string "uw_Basis_stringToBlob_error(ctx, PQgetvalue(res, i, ",
adamc@742 439 string (Int.toString i),
adamc@742 440 string "), PQgetlength(res, i, ",
adamc@742 441 string (Int.toString i),
adamc@742 442 string "))"]
adamc@467 443
adamc@467 444 | _ =>
adamc@467 445 p_unsql wontLeakStrings env tAll
adamc@467 446 (box [string "PQgetvalue(res, i, ",
adamc@467 447 string (Int.toString i),
adamc@467 448 string ")"])
adamc@467 449
adamc@282 450 datatype sql_type =
adamc@282 451 Int
adamc@282 452 | Float
adamc@282 453 | String
adamc@282 454 | Bool
adamc@439 455 | Time
adamc@737 456 | Blob
adamc@678 457 | Channel
adamc@682 458 | Client
adamc@467 459 | Nullable of sql_type
adamc@282 460
adamc@737 461 fun isBlob Blob = true
adamc@737 462 | isBlob (Nullable t) = isBlob t
adamc@737 463 | isBlob _ = false
adamc@737 464
adamc@739 465 fun isFile (t : typ) =
adamc@737 466 case #1 t of
adamc@739 467 TFfi ("Basis", "file") => true
adamc@737 468 | _ => false
adamc@737 469
adamc@467 470 fun p_sql_type' t =
adamc@467 471 case t of
adamc@467 472 Int => "uw_Basis_int"
adamc@467 473 | Float => "uw_Basis_float"
adamc@467 474 | String => "uw_Basis_string"
adamc@467 475 | Bool => "uw_Basis_bool"
adamc@467 476 | Time => "uw_Basis_time"
adamc@737 477 | Blob => "uw_Basis_blob"
adamc@678 478 | Channel => "uw_Basis_channel"
adamc@682 479 | Client => "uw_Basis_client"
adamc@467 480 | Nullable String => "uw_Basis_string"
adamc@467 481 | Nullable t => p_sql_type' t ^ "*"
adamc@467 482
adamc@467 483 fun p_sql_type t = string (p_sql_type' t)
adamc@282 484
adamc@282 485 fun getPargs (e, _) =
adamc@282 486 case e of
adamc@282 487 EPrim (Prim.String _) => []
adamc@282 488 | EFfiApp ("Basis", "strcat", [e1, e2]) => getPargs e1 @ getPargs e2
adamc@282 489
adamc@282 490 | EFfiApp ("Basis", "sqlifyInt", [e]) => [(e, Int)]
adamc@282 491 | EFfiApp ("Basis", "sqlifyFloat", [e]) => [(e, Float)]
adamc@282 492 | EFfiApp ("Basis", "sqlifyString", [e]) => [(e, String)]
adamc@282 493 | EFfiApp ("Basis", "sqlifyBool", [e]) => [(e, Bool)]
adamc@439 494 | EFfiApp ("Basis", "sqlifyTime", [e]) => [(e, Time)]
adamc@737 495 | EFfiApp ("Basis", "sqlifyBlob", [e]) => [(e, Blob)]
adamc@678 496 | EFfiApp ("Basis", "sqlifyChannel", [e]) => [(e, Channel)]
adamc@682 497 | EFfiApp ("Basis", "sqlifyClient", [e]) => [(e, Client)]
adamc@468 498
adamc@678 499 | ECase (e,
adamc@678 500 [((PNone _, _),
adamc@678 501 (EPrim (Prim.String "NULL"), _)),
adamc@678 502 ((PSome (_, (PVar _, _)), _),
adamc@678 503 (EFfiApp (m, x, [(ERel 0, _)]), _))],
adamc@678 504 _) => map (fn (x, y) => (x, Nullable y)) (getPargs (EFfiApp (m, x, [e]), #2 e))
adamc@468 505
adamc@491 506 | ECase (e,
adamc@491 507 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@491 508 (EPrim (Prim.String "TRUE"), _)),
adamc@491 509 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@491 510 (EPrim (Prim.String "FALSE"), _))],
adamc@491 511 _) => [(e, Bool)]
adamc@282 512
adamc@282 513 | _ => raise Fail "CjrPrint: getPargs"
adamc@282 514
adamc@282 515 fun p_ensql t e =
adamc@282 516 case t of
adamc@311 517 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
adamc@311 518 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
adamc@282 519 | String => e
adamc@295 520 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
adamc@682 521 | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"]
adamc@737 522 | Blob => box [e, string ".data"]
adamc@678 523 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
adamc@682 524 | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"]
adamc@467 525 | Nullable String => e
adamc@467 526 | Nullable t => box [string "(",
adamc@467 527 e,
adamc@467 528 string " == NULL ? NULL : ",
adamc@467 529 p_ensql t (box [string "*", e]),
adamc@467 530 string ")"]
adamc@282 531
adamc@324 532 fun notLeaky env allowHeapAllocated =
adamc@324 533 let
adamc@638 534 fun nl ok (t, _) =
adamc@324 535 case t of
adamc@324 536 TFun _ => false
adamc@324 537 | TRecord n =>
adamc@324 538 let
adamc@324 539 val xts = E.lookupStruct env n
adamc@324 540 in
adamc@638 541 List.all (fn (_, t) => nl ok t) xts
adamc@324 542 end
adamc@638 543 | TDatatype (dk, n, ref cons) =>
adamc@638 544 IS.member (ok, n)
adamc@638 545 orelse
adamc@638 546 ((allowHeapAllocated orelse dk = Enum)
adamc@638 547 andalso
adamc@638 548 let
adamc@638 549 val ok' = IS.add (ok, n)
adamc@638 550 in
adamc@638 551 List.all (fn (_, _, to) => case to of
adamc@638 552 NONE => true
adamc@638 553 | SOME t => nl ok' t) cons
adamc@638 554 end)
adamc@324 555 | TFfi ("Basis", "string") => false
adamc@742 556 | TFfi ("Basis", "blob") => allowHeapAllocated
adamc@324 557 | TFfi _ => true
adamc@638 558 | TOption t => allowHeapAllocated andalso nl ok t
adamc@324 559 in
adamc@638 560 nl IS.empty
adamc@324 561 end
adamc@324 562
adamc@463 563 fun capitalize s =
adamc@463 564 if s = "" then
adamc@463 565 ""
adamc@463 566 else
adamc@463 567 str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@463 568
adamc@463 569 fun unurlify env (t, loc) =
adamc@463 570 let
adamc@463 571 fun unurlify' rf t =
adamc@463 572 case t of
adamc@463 573 TFfi ("Basis", "unit") => string ("uw_unit_v")
adamc@463 574 | TFfi (m, t) => string ("uw_" ^ ident m ^ "_unurlify" ^ capitalize t ^ "(ctx, &request)")
adamc@463 575
adamc@463 576 | TRecord 0 => string "uw_unit_v"
adamc@463 577 | TRecord i =>
adamc@463 578 let
adamc@463 579 val xts = E.lookupStruct env i
adamc@463 580 in
adamc@463 581 box [string "({",
adamc@463 582 newline,
adamc@463 583 box (map (fn (x, t) =>
adamc@463 584 box [p_typ env t,
adamc@463 585 space,
adamc@463 586 string "uwr_",
adamc@463 587 string x,
adamc@463 588 space,
adamc@463 589 string "=",
adamc@463 590 space,
adamc@463 591 unurlify' rf (#1 t),
adamc@463 592 string ";",
adamc@463 593 newline]) xts),
adamc@463 594 string "struct",
adamc@463 595 space,
adamc@463 596 string "__uws_",
adamc@463 597 string (Int.toString i),
adamc@463 598 space,
adamc@463 599 string "tmp",
adamc@463 600 space,
adamc@463 601 string "=",
adamc@463 602 space,
adamc@463 603 string "{",
adamc@463 604 space,
adamc@463 605 p_list_sep (box [string ",", space]) (fn (x, _) => box [string "uwr_",
adamc@463 606 string x]) xts,
adamc@463 607 space,
adamc@463 608 string "};",
adamc@463 609 newline,
adamc@463 610 string "tmp;",
adamc@463 611 newline,
adamc@463 612 string "})"]
adamc@463 613 end
adamc@463 614
adamc@463 615 | TDatatype (Enum, i, _) =>
adamc@463 616 let
adamc@463 617 val (x, xncs) = E.lookupDatatype env i
adamc@463 618
adamc@463 619 fun doEm xncs =
adamc@463 620 case xncs of
adamc@463 621 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 622 ^ x ^ "\"), (enum __uwe_"
adamc@463 623 ^ x ^ "_" ^ Int.toString i ^ ")0)")
adamc@463 624 | (x', n, to) :: rest =>
adamc@463 625 box [string "((!strncmp(request, \"",
adamc@463 626 string x',
adamc@463 627 string "\", ",
adamc@463 628 string (Int.toString (size x')),
adamc@463 629 string ") && (request[",
adamc@463 630 string (Int.toString (size x')),
adamc@463 631 string "] == 0 || request[",
adamc@463 632 string (Int.toString (size x')),
adamc@463 633 string ("] == '/')) ? __uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 634 space,
adamc@463 635 string ":",
adamc@463 636 space,
adamc@463 637 doEm rest,
adamc@463 638 string ")"]
adamc@463 639 in
adamc@463 640 doEm xncs
adamc@463 641 end
adamc@463 642
adamc@463 643 | TDatatype (Option, i, xncs) =>
adamc@463 644 if IS.member (rf, i) then
adamc@463 645 box [string "unurlify_",
adamc@463 646 string (Int.toString i),
adamc@463 647 string "()"]
adamc@463 648 else
adamc@463 649 let
adamc@463 650 val (x, _) = E.lookupDatatype env i
adamc@463 651
adamc@463 652 val (no_arg, has_arg, t) =
adamc@463 653 case !xncs of
adamc@463 654 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@463 655 (no_arg, has_arg, t)
adamc@463 656 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@463 657 (no_arg, has_arg, t)
adamc@463 658 | _ => raise Fail "CjrPrint: unfooify misclassified Option datatype"
adamc@463 659
adamc@463 660 val rf = IS.add (rf, i)
adamc@463 661 in
adamc@463 662 box [string "({",
adamc@463 663 space,
adamc@463 664 p_typ env t,
adamc@463 665 space,
adamc@463 666 string "*unurlify_",
adamc@463 667 string (Int.toString i),
adamc@463 668 string "(void) {",
adamc@463 669 newline,
adamc@463 670 box [string "return (request[0] == '/' ? ++request : request,",
adamc@463 671 newline,
adamc@463 672 string "((!strncmp(request, \"",
adamc@463 673 string no_arg,
adamc@463 674 string "\", ",
adamc@463 675 string (Int.toString (size no_arg)),
adamc@463 676 string ") && (request[",
adamc@463 677 string (Int.toString (size no_arg)),
adamc@463 678 string "] == 0 || request[",
adamc@463 679 string (Int.toString (size no_arg)),
adamc@463 680 string "] == '/')) ? (request",
adamc@463 681 space,
adamc@463 682 string "+=",
adamc@463 683 space,
adamc@463 684 string (Int.toString (size no_arg)),
adamc@463 685 string ", NULL) : ((!strncmp(request, \"",
adamc@463 686 string has_arg,
adamc@463 687 string "\", ",
adamc@463 688 string (Int.toString (size has_arg)),
adamc@463 689 string ") && (request[",
adamc@463 690 string (Int.toString (size has_arg)),
adamc@463 691 string "] == 0 || request[",
adamc@463 692 string (Int.toString (size has_arg)),
adamc@463 693 string "] == '/')) ? (request",
adamc@463 694 space,
adamc@463 695 string "+=",
adamc@463 696 space,
adamc@463 697 string (Int.toString (size has_arg)),
adamc@463 698 string ", (request[0] == '/' ? ++request : NULL), ",
adamc@463 699 newline,
adamc@463 700
adamc@463 701 if isUnboxable t then
adamc@463 702 unurlify' rf (#1 t)
adamc@463 703 else
adamc@463 704 box [string "({",
adamc@463 705 newline,
adamc@463 706 p_typ env t,
adamc@463 707 space,
adamc@463 708 string "*tmp",
adamc@463 709 space,
adamc@463 710 string "=",
adamc@463 711 space,
adamc@463 712 string "uw_malloc(ctx, sizeof(",
adamc@463 713 p_typ env t,
adamc@463 714 string "));",
adamc@463 715 newline,
adamc@463 716 string "*tmp",
adamc@463 717 space,
adamc@463 718 string "=",
adamc@463 719 space,
adamc@463 720 unurlify' rf (#1 t),
adamc@463 721 string ";",
adamc@463 722 newline,
adamc@463 723 string "tmp;",
adamc@463 724 newline,
adamc@463 725 string "})"],
adamc@463 726 string ")",
adamc@463 727 newline,
adamc@463 728 string ":",
adamc@463 729 space,
adamc@463 730 string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype " ^ x
adamc@463 731 ^ "\"), NULL))));"),
adamc@463 732 newline],
adamc@463 733 string "}",
adamc@463 734 newline,
adamc@463 735 newline,
adamc@463 736
adamc@463 737 string "unurlify_",
adamc@463 738 string (Int.toString i),
adamc@463 739 string "();",
adamc@463 740 newline,
adamc@463 741 string "})"]
adamc@463 742 end
adamc@463 743
adamc@463 744 | TDatatype (Default, i, _) =>
adamc@463 745 if IS.member (rf, i) then
adamc@463 746 box [string "unurlify_",
adamc@463 747 string (Int.toString i),
adamc@463 748 string "()"]
adamc@463 749 else
adamc@463 750 let
adamc@463 751 val (x, xncs) = E.lookupDatatype env i
adamc@463 752
adamc@463 753 val rf = IS.add (rf, i)
adamc@463 754
adamc@463 755 fun doEm xncs =
adamc@463 756 case xncs of
adamc@463 757 [] => string ("(uw_error(ctx, FATAL, \"Error unurlifying datatype "
adamc@463 758 ^ x ^ "\"), NULL)")
adamc@463 759 | (x', n, to) :: rest =>
adamc@463 760 box [string "((!strncmp(request, \"",
adamc@463 761 string x',
adamc@463 762 string "\", ",
adamc@463 763 string (Int.toString (size x')),
adamc@463 764 string ") && (request[",
adamc@463 765 string (Int.toString (size x')),
adamc@463 766 string "] == 0 || request[",
adamc@463 767 string (Int.toString (size x')),
adamc@463 768 string "] == '/')) ? ({",
adamc@463 769 newline,
adamc@463 770 string "struct",
adamc@463 771 space,
adamc@463 772 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString i),
adamc@463 773 space,
adamc@463 774 string "*tmp = uw_malloc(ctx, sizeof(struct __uwd_",
adamc@463 775 string x,
adamc@463 776 string "_",
adamc@463 777 string (Int.toString i),
adamc@463 778 string "));",
adamc@463 779 newline,
adamc@463 780 string "tmp->tag",
adamc@463 781 space,
adamc@463 782 string "=",
adamc@463 783 space,
adamc@463 784 string ("__uwc_" ^ ident x' ^ "_" ^ Int.toString n),
adamc@463 785 string ";",
adamc@463 786 newline,
adamc@463 787 string "request",
adamc@463 788 space,
adamc@463 789 string "+=",
adamc@463 790 space,
adamc@463 791 string (Int.toString (size x')),
adamc@463 792 string ";",
adamc@463 793 newline,
adamc@463 794 string "if (request[0] == '/') ++request;",
adamc@463 795 newline,
adamc@463 796 case to of
adamc@463 797 NONE => box []
adamc@463 798 | SOME (t, _) => box [string "tmp->data.uw_",
adamc@463 799 p_ident x',
adamc@463 800 space,
adamc@463 801 string "=",
adamc@463 802 space,
adamc@463 803 unurlify' rf t,
adamc@463 804 string ";",
adamc@463 805 newline],
adamc@463 806 string "tmp;",
adamc@463 807 newline,
adamc@463 808 string "})",
adamc@463 809 space,
adamc@463 810 string ":",
adamc@463 811 space,
adamc@463 812 doEm rest,
adamc@463 813 string ")"]
adamc@463 814 in
adamc@463 815 box [string "({",
adamc@463 816 space,
adamc@463 817 p_typ env (t, ErrorMsg.dummySpan),
adamc@463 818 space,
adamc@463 819 string "unurlify_",
adamc@463 820 string (Int.toString i),
adamc@463 821 string "(void) {",
adamc@463 822 newline,
adamc@463 823 box [string "return",
adamc@463 824 space,
adamc@463 825 doEm xncs,
adamc@463 826 string ";",
adamc@463 827 newline],
adamc@463 828 string "}",
adamc@463 829 newline,
adamc@463 830 newline,
adamc@463 831
adamc@463 832 string "unurlify_",
adamc@463 833 string (Int.toString i),
adamc@463 834 string "();",
adamc@463 835 newline,
adamc@463 836 string "})"]
adamc@463 837 end
adamc@463 838
adamc@471 839 | TOption t =>
adamc@471 840 box [string "(request[0] == '/' ? ++request : request, ",
adamc@471 841 string "((!strncmp(request, \"None\", 4) ",
adamc@471 842 string "&& (request[4] == 0 || request[4] == '/')) ",
adamc@471 843 string "? (request += 4, NULL) ",
adamc@471 844 string ": ((!strncmp(request, \"Some\", 4) ",
adamc@471 845 string "&& request[4] == '/') ",
adamc@471 846 string "? (request += 5, ",
adamc@471 847 if isUnboxable t then
adamc@471 848 unurlify' rf (#1 t)
adamc@471 849 else
adamc@471 850 box [string "({",
adamc@471 851 newline,
adamc@471 852 p_typ env t,
adamc@471 853 space,
adamc@471 854 string "*tmp",
adamc@471 855 space,
adamc@471 856 string "=",
adamc@471 857 space,
adamc@471 858 string "uw_malloc(ctx, sizeof(",
adamc@471 859 p_typ env t,
adamc@471 860 string "));",
adamc@471 861 newline,
adamc@471 862 string "*tmp",
adamc@471 863 space,
adamc@471 864 string "=",
adamc@471 865 space,
adamc@471 866 unurlify' rf (#1 t),
adamc@471 867 string ";",
adamc@471 868 newline,
adamc@471 869 string "tmp;",
adamc@471 870 newline,
adamc@471 871 string "})"],
adamc@471 872 string ") :",
adamc@471 873 space,
adamc@471 874 string "(uw_error(ctx, FATAL, \"Error unurlifying option type\"), NULL))))"]
adamc@471 875
adamc@463 876 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL decoding function";
adamc@463 877 space)
adamc@463 878 in
adamc@463 879 unurlify' IS.empty t
adamc@463 880 end
adamc@463 881
adamc@610 882 fun urlify env t =
adamc@610 883 let
adamc@610 884 fun urlify' rf level (t as (_, loc)) =
adamc@610 885 case #1 t of
adamc@610 886 TFfi ("Basis", "unit") => box []
adamc@610 887 | TFfi (m, t) => box [string ("uw_" ^ ident m ^ "_urlify" ^ capitalize t
adamc@610 888 ^ "_w(ctx, it" ^ Int.toString level ^ ");"),
adamc@610 889 newline]
adamc@610 890
adamc@610 891 | TRecord 0 => box []
adamc@610 892 | TRecord i =>
adamc@610 893 let
adamc@611 894 fun empty (t, _) =
adamc@611 895 case t of
adamc@611 896 TFfi ("Basis", "unit") => true
adamc@611 897 | TRecord 0 => true
adamc@611 898 | TRecord j =>
adamc@611 899 List.all (fn (_, t) => empty t) (E.lookupStruct env j)
adamc@611 900 | _ => false
adamc@611 901
adamc@610 902 val xts = E.lookupStruct env i
adamc@611 903
adamc@613 904 val (blocks, _) = foldl
adamc@613 905 (fn ((x, t), (blocks, printingSinceLastSlash)) =>
adamc@613 906 let
adamc@613 907 val thisEmpty = empty t
adamc@613 908 in
adamc@613 909 if thisEmpty then
adamc@613 910 (blocks, printingSinceLastSlash)
adamc@613 911 else
adamc@613 912 (box [string "{",
adamc@613 913 newline,
adamc@613 914 p_typ env t,
adamc@613 915 space,
adamc@613 916 string ("it" ^ Int.toString (level + 1)),
adamc@613 917 space,
adamc@613 918 string "=",
adamc@613 919 space,
adamc@613 920 string ("it" ^ Int.toString level ^ ".__uwf_" ^ x ^ ";"),
adamc@613 921 newline,
adamc@613 922 box (if printingSinceLastSlash then
adamc@613 923 [string "uw_write(ctx, \"/\");",
adamc@613 924 newline]
adamc@613 925 else
adamc@613 926 []),
adamc@613 927 urlify' rf (level + 1) t,
adamc@613 928 string "}",
adamc@613 929 newline] :: blocks,
adamc@613 930 true)
adamc@613 931 end)
adamc@613 932 ([], false) xts
adamc@610 933 in
adamc@613 934 box (rev blocks)
adamc@610 935 end
adamc@610 936
adamc@638 937 | TDatatype (Enum, i, _) =>
adamc@638 938 let
adamc@610 939 val (x, xncs) = E.lookupDatatype env i
adamc@610 940
adamc@610 941 fun doEm xncs =
adamc@610 942 case xncs of
adamc@638 943 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
adamc@638 944 ^ x ^ "\");"),
adamc@638 945 newline]
adamc@610 946 | (x', n, to) :: rest =>
adamc@638 947 box [string ("if (it" ^ Int.toString level
adamc@638 948 ^ "==__uwc_" ^ ident x' ^ "_" ^ Int.toString n ^ ") {"),
adamc@638 949 newline,
adamc@638 950 box [string ("uw_write(ctx, \"" ^ x' ^ "\");"),
adamc@638 951 newline],
adamc@638 952 string "} else {",
adamc@638 953 newline,
adamc@638 954 box [doEm rest,
adamc@638 955 newline],
adamc@638 956 string "}"]
adamc@610 957 in
adamc@610 958 doEm xncs
adamc@638 959 end
adamc@610 960
adamc@639 961 | TDatatype (Option, i, xncs) =>
adamc@639 962 if IS.member (rf, i) then
adamc@639 963 box [string "urlify_",
adamc@610 964 string (Int.toString i),
adamc@639 965 string "(it",
adamc@639 966 string (Int.toString level),
adamc@639 967 string ");",
adamc@639 968 newline]
adamc@610 969 else
adamc@610 970 let
adamc@610 971 val (x, _) = E.lookupDatatype env i
adamc@610 972
adamc@610 973 val (no_arg, has_arg, t) =
adamc@610 974 case !xncs of
adamc@610 975 [(no_arg, _, NONE), (has_arg, _, SOME t)] =>
adamc@610 976 (no_arg, has_arg, t)
adamc@610 977 | [(has_arg, _, SOME t), (no_arg, _, NONE)] =>
adamc@610 978 (no_arg, has_arg, t)
adamc@639 979 | _ => raise Fail "CjrPrint: urlify misclassified Option datatype"
adamc@610 980
adamc@610 981 val rf = IS.add (rf, i)
adamc@610 982 in
adamc@610 983 box [string "({",
adamc@610 984 space,
adamc@639 985 string "void",
adamc@639 986 space,
adamc@639 987 string "urlify_",
adamc@639 988 string (Int.toString i),
adamc@639 989 string "(",
adamc@610 990 p_typ env t,
adamc@610 991 space,
adamc@639 992 if isUnboxable t then
adamc@639 993 box []
adamc@639 994 else
adamc@639 995 string "*",
adamc@639 996 string "it0) {",
adamc@610 997 newline,
adamc@639 998 box [string "if (it0) {",
adamc@639 999 if isUnboxable t then
adamc@639 1000 urlify' rf 0 t
adamc@610 1001 else
adamc@639 1002 box [p_typ env t,
adamc@610 1003 space,
adamc@639 1004 string "it1",
adamc@610 1005 space,
adamc@610 1006 string "=",
adamc@610 1007 space,
adamc@639 1008 string "*it0;",
adamc@610 1009 newline,
adamc@639 1010 string "uw_write(ctx, \"",
adamc@639 1011 string has_arg,
adamc@639 1012 string "/\");",
adamc@639 1013 newline,
adamc@639 1014 urlify' rf 1 t,
adamc@610 1015 string ";",
adamc@639 1016 newline],
adamc@639 1017 string "} else {",
adamc@639 1018 box [string "uw_write(ctx, \"",
adamc@639 1019 string no_arg,
adamc@639 1020 string "\");",
adamc@639 1021 newline],
adamc@639 1022 string "}",
adamc@610 1023 newline],
adamc@610 1024 string "}",
adamc@610 1025 newline,
adamc@610 1026 newline,
adamc@610 1027
adamc@639 1028 string "urlify_",
adamc@610 1029 string (Int.toString i),
adamc@639 1030 string "(it",
adamc@639 1031 string (Int.toString level),
adamc@639 1032 string ");",
adamc@610 1033 newline,
adamc@639 1034 string "});",
adamc@639 1035 newline]
adamc@639 1036 end
adamc@610 1037
adamc@640 1038 | TDatatype (Default, i, _) =>
adamc@640 1039 if IS.member (rf, i) then
adamc@640 1040 box [string "urlify_",
adamc@610 1041 string (Int.toString i),
adamc@640 1042 string "(it",
adamc@640 1043 string (Int.toString level),
adamc@640 1044 string ");",
adamc@640 1045 newline]
adamc@610 1046 else
adamc@610 1047 let
adamc@610 1048 val (x, xncs) = E.lookupDatatype env i
adamc@610 1049
adamc@610 1050 val rf = IS.add (rf, i)
adamc@610 1051
adamc@610 1052 fun doEm xncs =
adamc@610 1053 case xncs of
adamc@640 1054 [] => box [string ("uw_error(ctx, FATAL, \"Error urlifying datatype "
adamc@640 1055 ^ x ^ " (%d)\", it0->data);"),
adamc@640 1056 newline]
adamc@610 1057 | (x', n, to) :: rest =>
adamc@640 1058 box [string "if",
adamc@610 1059 space,
adamc@640 1060 string "(it0->tag==__uwc_",
adamc@640 1061 string (ident x'),
adamc@610 1062 string "_",
adamc@640 1063 string (Int.toString n),
adamc@640 1064 string ") {",
adamc@610 1065 newline,
adamc@610 1066 case to of
adamc@640 1067 NONE => box [string "uw_write(ctx, \"",
adamc@640 1068 string x',
adamc@640 1069 string "\");",
adamc@640 1070 newline]
adamc@640 1071 | SOME t => box [string "uw_write(ctx, \"",
adamc@640 1072 string x',
adamc@640 1073 string "/\");",
adamc@640 1074 newline,
adamc@640 1075 p_typ env t,
adamc@640 1076 space,
adamc@640 1077 string "it1",
adamc@640 1078 space,
adamc@640 1079 string "=",
adamc@640 1080 space,
adamc@640 1081 string "it0->data.uw_",
adamc@640 1082 string x',
adamc@640 1083 string ";",
adamc@640 1084 newline,
adamc@640 1085 urlify' rf 1 t,
adamc@640 1086 newline],
adamc@640 1087 string "} else {",
adamc@610 1088 newline,
adamc@640 1089 box [doEm rest,
adamc@640 1090 newline],
adamc@640 1091 string "}",
adamc@640 1092 newline]
adamc@610 1093 in
adamc@610 1094 box [string "({",
adamc@610 1095 space,
adamc@640 1096 string "void",
adamc@610 1097 space,
adamc@640 1098 string "urlify_",
adamc@610 1099 string (Int.toString i),
adamc@640 1100 string "(",
adamc@640 1101 p_typ env t,
adamc@640 1102 space,
adamc@640 1103 string "it0) {",
adamc@610 1104 newline,
adamc@640 1105 box [doEm xncs,
adamc@610 1106 newline],
adamc@640 1107 newline,
adamc@610 1108 string "}",
adamc@610 1109 newline,
adamc@640 1110
adamc@640 1111 string "urlify_",
adamc@640 1112 string (Int.toString i),
adamc@640 1113 string "(it",
adamc@640 1114 string (Int.toString level),
adamc@640 1115 string ");",
adamc@610 1116 newline,
adamc@640 1117 string "});",
adamc@640 1118 newline]
adamc@640 1119 end
adamc@610 1120
adamc@641 1121 | TOption t =>
adamc@641 1122 box [string "if (it",
adamc@641 1123 string (Int.toString level),
adamc@641 1124 string ") {",
adamc@641 1125 if isUnboxable t then
adamc@641 1126 box [string "uw_write(ctx, \"Some/\");",
adamc@641 1127 newline,
adamc@641 1128 urlify' rf level t]
adamc@610 1129 else
adamc@641 1130 box [p_typ env t,
adamc@610 1131 space,
adamc@641 1132 string "it",
adamc@641 1133 string (Int.toString (level + 1)),
adamc@610 1134 space,
adamc@610 1135 string "=",
adamc@610 1136 space,
adamc@641 1137 string "*it",
adamc@641 1138 string (Int.toString level),
adamc@610 1139 string ";",
adamc@610 1140 newline,
adamc@641 1141 string "uw_write(ctx, \"Some/\");",
adamc@610 1142 newline,
adamc@641 1143 urlify' rf (level + 1) t,
adamc@641 1144 string ";",
adamc@641 1145 newline],
adamc@641 1146 string "} else {",
adamc@641 1147 box [string "uw_write(ctx, \"None\");",
adamc@641 1148 newline],
adamc@641 1149 string "}",
adamc@641 1150 newline]
adamc@610 1151
adamc@610 1152 | _ => (ErrorMsg.errorAt loc "Unable to choose a URL encoding function";
adamc@610 1153 space)
adamc@610 1154 in
adamc@610 1155 urlify' IS.empty 0 t
adamc@610 1156 end
adamc@610 1157
adamc@673 1158 val timeout = ref 0
adamc@673 1159
adamc@182 1160 fun p_exp' par env (e, loc) =
adamc@29 1161 case e of
adamc@276 1162 EPrim p => Prim.p_t_GCC p
adamc@29 1163 | ERel n => p_rel env n
adamc@109 1164 | ENamed n => p_enamed env n
adamc@188 1165 | ECon (Enum, pc, _) => p_patCon env pc
adamc@198 1166 | ECon (Option, pc, NONE) => string "NULL"
adamc@198 1167 | ECon (Option, pc, SOME e) =>
adamc@198 1168 let
adamc@198 1169 val to = case pc of
adamc@198 1170 PConVar n => #2 (E.lookupConstructor env n)
adamc@198 1171 | PConFfi {arg, ...} => arg
adamc@198 1172
adamc@198 1173 val t = case to of
adamc@198 1174 NONE => raise Fail "CjrPrint: ECon argument status mismatch"
adamc@198 1175 | SOME t => t
adamc@198 1176 in
adamc@463 1177 if isUnboxable t then
adamc@463 1178 p_exp' par env e
adamc@463 1179 else
adamc@463 1180 box [string "({",
adamc@463 1181 newline,
adamc@463 1182 p_typ env t,
adamc@463 1183 space,
adamc@463 1184 string "*tmp",
adamc@463 1185 space,
adamc@463 1186 string "=",
adamc@463 1187 space,
adamc@463 1188 string "uw_malloc(ctx, sizeof(",
adamc@463 1189 p_typ env t,
adamc@463 1190 string "));",
adamc@463 1191 newline,
adamc@463 1192 string "*tmp",
adamc@463 1193 space,
adamc@463 1194 string "=",
adamc@463 1195 p_exp' par env e,
adamc@463 1196 string ";",
adamc@463 1197 newline,
adamc@463 1198 string "tmp;",
adamc@463 1199 newline,
adamc@463 1200 string "})"]
adamc@198 1201 end
adamc@188 1202 | ECon (Default, pc, eo) =>
adamc@181 1203 let
adamc@196 1204 val (xd, xc, xn) = patConInfo env pc
adamc@181 1205 in
adamc@182 1206 box [string "({",
adamc@181 1207 newline,
adamc@181 1208 string "struct",
adamc@181 1209 space,
adamc@185 1210 string xd,
adamc@181 1211 space,
adamc@181 1212 string "*tmp",
adamc@181 1213 space,
adamc@181 1214 string "=",
adamc@181 1215 space,
adamc@311 1216 string "uw_malloc(ctx, sizeof(struct ",
adamc@185 1217 string xd,
adamc@181 1218 string "));",
adamc@181 1219 newline,
adamc@181 1220 string "tmp->tag",
adamc@181 1221 space,
adamc@181 1222 string "=",
adamc@181 1223 space,
adamc@185 1224 string xc,
adamc@181 1225 string ";",
adamc@181 1226 newline,
adamc@181 1227 case eo of
adamc@181 1228 NONE => box []
adamc@185 1229 | SOME e => box [string "tmp->data.",
adamc@196 1230 string xn,
adamc@181 1231 space,
adamc@181 1232 string "=",
adamc@181 1233 space,
adamc@181 1234 p_exp env e,
adamc@181 1235 string ";",
adamc@181 1236 newline],
adamc@181 1237 string "tmp;",
adamc@181 1238 newline,
adamc@181 1239 string "})"]
adamc@181 1240 end
adamc@297 1241 | ENone _ => string "NULL"
adamc@290 1242 | ESome (t, e) =>
adamc@463 1243 if isUnboxable t then
adamc@463 1244 p_exp' par env e
adamc@463 1245 else
adamc@463 1246 box [string "({",
adamc@463 1247 newline,
adamc@463 1248 p_typ env t,
adamc@463 1249 space,
adamc@463 1250 string "*tmp",
adamc@463 1251 space,
adamc@463 1252 string "=",
adamc@463 1253 space,
adamc@463 1254 string "uw_malloc(ctx, sizeof(",
adamc@463 1255 p_typ env t,
adamc@463 1256 string "));",
adamc@463 1257 newline,
adamc@463 1258 string "*tmp",
adamc@463 1259 space,
adamc@463 1260 string "=",
adamc@463 1261 p_exp' par env e,
adamc@463 1262 string ";",
adamc@463 1263 newline,
adamc@463 1264 string "tmp;",
adamc@463 1265 newline,
adamc@463 1266 string "})"]
adamc@109 1267
adamc@316 1268 | EFfi (m, x) => box [string "uw_", p_ident m, string "_", p_ident x]
adamc@283 1269 | EError (e, t) =>
adamc@283 1270 box [string "({",
adamc@283 1271 newline,
adamc@283 1272 p_typ env t,
adamc@283 1273 space,
adamc@283 1274 string "tmp;",
adamc@283 1275 newline,
adamc@311 1276 string "uw_error(ctx, FATAL, \"",
adamc@292 1277 string (ErrorMsg.spanToString loc),
adamc@292 1278 string ": %s\", ",
adamc@283 1279 p_exp env e,
adamc@283 1280 string ");",
adamc@283 1281 newline,
adamc@283 1282 string "tmp;",
adamc@283 1283 newline,
adamc@283 1284 string "})"]
adamc@741 1285 | EReturnBlob {blob, mimeType, t} =>
adamc@741 1286 box [string "({",
adamc@741 1287 newline,
adamc@741 1288 p_typ env t,
adamc@741 1289 space,
adamc@741 1290 string "tmp;",
adamc@741 1291 newline,
adamc@741 1292 string "uw_return_blob(ctx, ",
adamc@741 1293 p_exp env blob,
adamc@741 1294 string ", ",
adamc@741 1295 p_exp env mimeType,
adamc@741 1296 string ");",
adamc@741 1297 newline,
adamc@741 1298 string "tmp;",
adamc@741 1299 newline,
adamc@741 1300 string "})"]
adamc@476 1301 | EApp ((EError (e, (TFun (_, ran), _)), loc), _) =>
adamc@476 1302 p_exp env (EError (e, ran), loc)
adamc@741 1303 | EApp ((EReturnBlob {blob, mimeType, t = (TFun (_, ran), _)}, loc), _) =>
adamc@741 1304 p_exp env (EReturnBlob {blob = blob, mimeType = mimeType, t = ran}, loc)
adamc@476 1305
adamc@311 1306 | EFfiApp (m, x, es) => box [string "uw_",
adamc@316 1307 p_ident m,
adamc@53 1308 string "_",
adamc@316 1309 p_ident x,
adamc@117 1310 string "(ctx, ",
adamc@53 1311 p_list (p_exp env) es,
adamc@53 1312 string ")"]
adamc@316 1313 | EApp (f, args) =>
adamc@316 1314 parenIf par (box [p_exp' true env f,
adamc@316 1315 string "(ctx,",
adamc@316 1316 space,
adamc@316 1317 p_list_sep (box [string ",", space]) (p_exp env) args,
adamc@316 1318 string ")"])
adamc@29 1319
adamc@387 1320 | EUnop (s, e1) =>
adamc@387 1321 parenIf par (box [string s,
adamc@387 1322 space,
adamc@387 1323 p_exp' true env e1])
adamc@387 1324
adamc@387 1325 | EBinop (s, e1, e2) =>
adamc@389 1326 if Char.isAlpha (String.sub (s, size s - 1)) then
adamc@389 1327 box [string s,
adamc@390 1328 string "(",
adamc@389 1329 p_exp env e1,
adamc@389 1330 string ",",
adamc@389 1331 space,
adamc@389 1332 p_exp env e2,
adamc@389 1333 string ")"]
adamc@389 1334 else
adamc@389 1335 parenIf par (box [p_exp' true env e1,
adamc@389 1336 space,
adamc@389 1337 string s,
adamc@389 1338 space,
adamc@389 1339 p_exp' true env e2])
adamc@387 1340
adamc@29 1341 | ERecord (i, xes) => box [string "({",
adamc@29 1342 space,
adamc@29 1343 string "struct",
adamc@29 1344 space,
adamc@311 1345 string ("__uws_" ^ Int.toString i),
adamc@29 1346 space,
adamc@181 1347 string "tmp",
adamc@29 1348 space,
adamc@29 1349 string "=",
adamc@29 1350 space,
adamc@29 1351 string "{",
adamc@29 1352 p_list (fn (_, e) =>
adamc@29 1353 p_exp env e) xes,
adamc@29 1354 string "};",
adamc@29 1355 space,
adamc@181 1356 string "tmp;",
adamc@29 1357 space,
adamc@29 1358 string "})" ]
adamc@29 1359 | EField (e, x) =>
adamc@29 1360 box [p_exp' true env e,
adamc@311 1361 string ".__uwf_",
adamc@316 1362 p_ident x]
adamc@29 1363
adamc@182 1364 | ECase (e, pes, {disc, result}) =>
adamc@182 1365 let
adamc@182 1366 val final = newGoto ()
adamc@182 1367
adamc@182 1368 val body = foldl (fn ((p, e), body) =>
adamc@182 1369 let
adamc@182 1370 val exit = newGoto ()
adamc@182 1371 val (pr, _) = p_pat_preamble env p
adamc@182 1372 val (p, env) = p_pat (env,
adamc@182 1373 box [string "goto",
adamc@182 1374 space,
adamc@182 1375 exit,
adamc@182 1376 string ";"],
adamc@182 1377 0) p
adamc@182 1378 in
adamc@182 1379 box [body,
adamc@182 1380 box [string "{",
adamc@182 1381 newline,
adamc@182 1382 pr,
adamc@182 1383 newline,
adamc@182 1384 p,
adamc@182 1385 newline,
adamc@182 1386 string "result",
adamc@182 1387 space,
adamc@182 1388 string "=",
adamc@182 1389 space,
adamc@182 1390 p_exp env e,
adamc@182 1391 string ";",
adamc@182 1392 newline,
adamc@182 1393 string "goto",
adamc@182 1394 space,
adamc@182 1395 final,
adamc@182 1396 string ";",
adamc@182 1397 newline,
adamc@182 1398 string "}"],
adamc@182 1399 newline,
adamc@182 1400 exit,
adamc@182 1401 string ":",
adamc@182 1402 newline]
adamc@182 1403 end) (box []) pes
adamc@182 1404 in
adamc@182 1405 box [string "({",
adamc@182 1406 newline,
adamc@182 1407 p_typ env disc,
adamc@182 1408 space,
adamc@182 1409 string "disc0",
adamc@182 1410 space,
adamc@182 1411 string "=",
adamc@182 1412 space,
adamc@182 1413 p_exp env e,
adamc@182 1414 string ";",
adamc@182 1415 newline,
adamc@182 1416 p_typ env result,
adamc@182 1417 space,
adamc@182 1418 string "result;",
adamc@182 1419 newline,
adamc@182 1420 body,
adamc@311 1421 string "uw_error(ctx, FATAL, \"",
adamc@182 1422 string (ErrorMsg.spanToString loc),
adamc@182 1423 string ": pattern match failure\");",
adamc@182 1424 newline,
adamc@182 1425 final,
adamc@182 1426 string ":",
adamc@182 1427 space,
adamc@182 1428 string "result;",
adamc@182 1429 newline,
adamc@182 1430 string "})"]
adamc@182 1431 end
adamc@181 1432
adamc@311 1433 | EWrite e => box [string "(uw_write(ctx, ",
adamc@102 1434 p_exp env e,
adamc@311 1435 string "), uw_unit_v)"]
adamc@102 1436
adamc@106 1437 | ESeq (e1, e2) => box [string "(",
adamc@106 1438 p_exp env e1,
adamc@106 1439 string ",",
adamc@106 1440 space,
adamc@106 1441 p_exp env e2,
adamc@106 1442 string ")"]
adamc@269 1443 | ELet (x, t, e1, e2) => box [string "({",
adamc@269 1444 newline,
adamc@269 1445 p_typ env t,
adamc@269 1446 space,
adamc@311 1447 string "__uwr_",
adamc@316 1448 p_ident x,
adamc@272 1449 string "_",
adamc@272 1450 string (Int.toString (E.countERels env)),
adamc@269 1451 space,
adamc@269 1452 string "=",
adamc@269 1453 space,
adamc@269 1454 p_exp env e1,
adamc@269 1455 string ";",
adamc@269 1456 newline,
adamc@269 1457 p_exp (E.pushERel env x t) e2,
adamc@269 1458 string ";",
adamc@269 1459 newline,
adamc@269 1460 string "})"]
adamc@269 1461
adamc@282 1462 | EQuery {exps, tables, rnum, state, query, body, initial, prepared} =>
adamc@278 1463 let
adamc@316 1464 val exps = map (fn (x, t) => ("__uwf_" ^ ident x, t)) exps
adamc@278 1465 val tables = ListUtil.mapConcat (fn (x, xts) =>
adamc@316 1466 map (fn (x', t) => ("__uwf_" ^ ident x ^ ".__uwf_" ^ ident x', t)) xts)
adamc@278 1467 tables
adamc@638 1468
adamc@278 1469 val outputs = exps @ tables
adamc@325 1470 val outputs = ListMergeSort.sort (fn ((s1, _), (s2, _)) => String.compare (s1, s2) = GREATER) outputs
adamc@324 1471
adamc@324 1472 val wontLeakStrings = notLeaky env true state
adamc@324 1473 val wontLeakAnything = notLeaky env false state
adamc@278 1474 in
adamc@640 1475 box [if wontLeakAnything then
adamc@704 1476 string "(uw_begin_region(ctx), "
adamc@324 1477 else
adamc@324 1478 box [],
adamc@324 1479 string "({",
adamc@278 1480 newline,
adamc@311 1481 string "PGconn *conn = uw_get_db(ctx);",
adamc@278 1482 newline,
adamc@640 1483 p_typ env state,
adamc@640 1484 space,
adamc@640 1485 string "acc",
adamc@640 1486 space,
adamc@640 1487 string "=",
adamc@640 1488 space,
adamc@640 1489 p_exp env initial,
adamc@640 1490 string ";",
adamc@640 1491 newline,
adamc@640 1492 string "int n, i, dummy = (uw_begin_region(ctx), 0);",
adamc@640 1493 newline,
adamc@640 1494
adamc@282 1495 case prepared of
adamc@282 1496 NONE => box [string "char *query = ",
adamc@282 1497 p_exp env query,
adamc@282 1498 string ";",
adamc@282 1499 newline]
adamc@282 1500 | SOME _ =>
adamc@282 1501 let
adamc@282 1502 val ets = getPargs query
adamc@282 1503 in
adamc@282 1504 box [p_list_sepi newline
adamc@282 1505 (fn i => fn (e, t) =>
adamc@282 1506 box [p_sql_type t,
adamc@282 1507 space,
adamc@282 1508 string "arg",
adamc@282 1509 string (Int.toString (i + 1)),
adamc@282 1510 space,
adamc@282 1511 string "=",
adamc@282 1512 space,
adamc@282 1513 p_exp env e,
adamc@282 1514 string ";"])
adamc@282 1515 ets,
adamc@282 1516 newline,
adamc@282 1517 newline,
adamc@282 1518
adamc@737 1519 string "const int paramFormats[] = { ",
adamc@737 1520 p_list_sep (box [string ",", space])
adamc@737 1521 (fn (_, t) => if isBlob t then string "1" else string "0") ets,
adamc@737 1522 string " };",
adamc@737 1523 newline,
adamc@737 1524 string "const int paramLengths[] = { ",
adamc@737 1525 p_list_sepi (box [string ",", space])
adamc@737 1526 (fn i => fn (_, Blob) => string ("arg" ^ Int.toString (i + 1) ^ ".size")
adamc@737 1527 | (_, Nullable Blob) => string ("arg" ^ Int.toString (i + 1)
adamc@737 1528 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
adamc@737 1529 | _ => string "0") ets,
adamc@737 1530 string " };",
adamc@737 1531 newline,
adamc@282 1532 string "const char *paramValues[] = { ",
adamc@282 1533 p_list_sepi (box [string ",", space])
adamc@282 1534 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@282 1535 string (Int.toString (i + 1))]))
adamc@282 1536 ets,
adamc@282 1537 string " };",
adamc@282 1538 newline,
adamc@282 1539 newline]
adamc@282 1540 end,
adamc@640 1541
adamc@282 1542 string "PGresult *res = ",
adamc@282 1543 case prepared of
adamc@295 1544 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1545 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@282 1546 string (Int.toString n),
adamc@282 1547 string "\", ",
adamc@282 1548 string (Int.toString (length (getPargs query))),
adamc@737 1549 string ", paramValues, paramLengths, paramFormats, 0);"],
adamc@278 1550 newline,
adamc@278 1551 newline,
adamc@277 1552
adamc@311 1553 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@278 1554 newline,
adamc@278 1555 newline,
adamc@277 1556
adamc@278 1557 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@278 1558 newline,
adamc@278 1559 box [string "PQclear(res);",
adamc@278 1560 newline,
adamc@311 1561 string "uw_error(ctx, FATAL, \"",
adamc@278 1562 string (ErrorMsg.spanToString loc),
adamc@282 1563 string ": Query failed:\\n%s\\n%s\", ",
adamc@282 1564 case prepared of
adamc@282 1565 NONE => string "query"
adamc@282 1566 | SOME _ => p_exp env query,
adamc@282 1567 string ", PQerrorMessage(conn));",
adamc@278 1568 newline],
adamc@278 1569 string "}",
adamc@278 1570 newline,
adamc@278 1571 newline,
adamc@277 1572
adamc@323 1573 string "uw_end_region(ctx);",
adamc@323 1574 newline,
adamc@425 1575 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
adamc@425 1576 newline,
adamc@278 1577 string "n = PQntuples(res);",
adamc@278 1578 newline,
adamc@278 1579 string "for (i = 0; i < n; ++i) {",
adamc@278 1580 newline,
adamc@278 1581 box [string "struct",
adamc@278 1582 space,
adamc@311 1583 string "__uws_",
adamc@278 1584 string (Int.toString rnum),
adamc@278 1585 space,
adamc@311 1586 string "__uwr_r_",
adamc@278 1587 string (Int.toString (E.countERels env)),
adamc@278 1588 string ";",
adamc@278 1589 newline,
adamc@278 1590 p_typ env state,
adamc@278 1591 space,
adamc@311 1592 string "__uwr_acc_",
adamc@278 1593 string (Int.toString (E.countERels env + 1)),
adamc@278 1594 space,
adamc@278 1595 string "=",
adamc@278 1596 space,
adamc@278 1597 string "acc;",
adamc@278 1598 newline,
adamc@278 1599 newline,
adamc@278 1600
adamc@278 1601 p_list_sepi (box []) (fn i =>
adamc@278 1602 fn (proj, t) =>
adamc@311 1603 box [string "__uwr_r_",
adamc@278 1604 string (Int.toString (E.countERels env)),
adamc@278 1605 string ".",
adamc@278 1606 string proj,
adamc@278 1607 space,
adamc@278 1608 string "=",
adamc@278 1609 space,
adamc@467 1610 p_getcol wontLeakStrings env t i,
adamc@278 1611 string ";",
adamc@278 1612 newline]) outputs,
adamc@278 1613
adamc@278 1614 newline,
adamc@278 1615 newline,
adamc@278 1616
adamc@278 1617 string "acc",
adamc@278 1618 space,
adamc@278 1619 string "=",
adamc@278 1620 space,
adamc@278 1621 p_exp (E.pushERel
adamc@278 1622 (E.pushERel env "r" (TRecord rnum, loc))
adamc@278 1623 "acc" state)
adamc@278 1624 body,
adamc@278 1625 string ";",
adamc@278 1626 newline],
adamc@278 1627 string "}",
adamc@278 1628 newline,
adamc@278 1629 newline,
adamc@425 1630 string "uw_pop_cleanup(ctx);",
adamc@278 1631 newline,
adamc@324 1632 if wontLeakAnything then
adamc@324 1633 box [string "uw_end_region(ctx);",
adamc@324 1634 newline]
adamc@324 1635 else
adamc@324 1636 box [],
adamc@278 1637 string "acc;",
adamc@278 1638 newline,
adamc@704 1639 string "})",
adamc@704 1640 if wontLeakAnything then
adamc@704 1641 string ")"
adamc@704 1642 else
adamc@704 1643 box []]
adamc@278 1644 end
adamc@106 1645
adamc@307 1646 | EDml {dml, prepared} =>
adamc@337 1647 box [string "(uw_begin_region(ctx), ({",
adamc@307 1648 newline,
adamc@311 1649 string "PGconn *conn = uw_get_db(ctx);",
adamc@307 1650 newline,
adamc@307 1651 case prepared of
adamc@307 1652 NONE => box [string "char *dml = ",
adamc@307 1653 p_exp env dml,
adamc@307 1654 string ";",
adamc@307 1655 newline]
adamc@307 1656 | SOME _ =>
adamc@307 1657 let
adamc@307 1658 val ets = getPargs dml
adamc@307 1659 in
adamc@307 1660 box [p_list_sepi newline
adamc@307 1661 (fn i => fn (e, t) =>
adamc@307 1662 box [p_sql_type t,
adamc@307 1663 space,
adamc@307 1664 string "arg",
adamc@307 1665 string (Int.toString (i + 1)),
adamc@307 1666 space,
adamc@307 1667 string "=",
adamc@307 1668 space,
adamc@307 1669 p_exp env e,
adamc@307 1670 string ";"])
adamc@307 1671 ets,
adamc@307 1672 newline,
adamc@307 1673 newline,
adamc@307 1674
adamc@738 1675 string "const int paramFormats[] = { ",
adamc@738 1676 p_list_sep (box [string ",", space])
adamc@738 1677 (fn (_, t) => if isBlob t then string "1" else string "0") ets,
adamc@738 1678 string " };",
adamc@738 1679 newline,
adamc@738 1680 string "const int paramLengths[] = { ",
adamc@738 1681 p_list_sepi (box [string ",", space])
adamc@738 1682 (fn i => fn (_, Blob) => string ("arg" ^ Int.toString (i + 1) ^ ".size")
adamc@738 1683 | (_, Nullable Blob) => string ("arg" ^ Int.toString (i + 1)
adamc@738 1684 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
adamc@738 1685 | _ => string "0") ets,
adamc@738 1686 string " };",
adamc@738 1687 newline,
adamc@307 1688 string "const char *paramValues[] = { ",
adamc@307 1689 p_list_sepi (box [string ",", space])
adamc@307 1690 (fn i => fn (_, t) => p_ensql t (box [string "arg",
adamc@307 1691 string (Int.toString (i + 1))]))
adamc@307 1692 ets,
adamc@307 1693 string " };",
adamc@307 1694 newline,
adamc@307 1695 newline]
adamc@307 1696 end,
adamc@307 1697 newline,
adamc@307 1698 newline,
adamc@307 1699 string "PGresult *res = ",
adamc@307 1700 case prepared of
adamc@307 1701 NONE => string "PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);"
adamc@311 1702 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@307 1703 string (Int.toString n),
adamc@307 1704 string "\", ",
adamc@307 1705 string (Int.toString (length (getPargs dml))),
adamc@738 1706 string ", paramValues, paramLengths, paramFormats, 0);"],
adamc@307 1707 newline,
adamc@307 1708 newline,
adamc@307 1709
adamc@311 1710 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
adamc@307 1711 newline,
adamc@307 1712 newline,
adamc@307 1713
adamc@307 1714 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@307 1715 newline,
adamc@688 1716 box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {",
adamc@688 1717 box [newline,
adamc@688 1718 string "PQclear(res);",
adamc@688 1719 newline,
adamc@688 1720 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");",
adamc@688 1721 newline],
adamc@688 1722 string "}",
adamc@688 1723 newline,
adamc@688 1724 string "PQclear(res);",
adamc@307 1725 newline,
adamc@311 1726 string "uw_error(ctx, FATAL, \"",
adamc@307 1727 string (ErrorMsg.spanToString loc),
adamc@307 1728 string ": DML failed:\\n%s\\n%s\", ",
adamc@307 1729 case prepared of
adamc@307 1730 NONE => string "dml"
adamc@307 1731 | SOME _ => p_exp env dml,
adamc@307 1732 string ", PQerrorMessage(conn));",
adamc@307 1733 newline],
adamc@307 1734 string "}",
adamc@307 1735 newline,
adamc@307 1736 newline,
adamc@307 1737
adamc@307 1738 string "PQclear(res);",
adamc@307 1739 newline,
adamc@337 1740 string "uw_end_region(ctx);",
adamc@337 1741 newline,
adamc@311 1742 string "uw_unit_v;",
adamc@307 1743 newline,
adamc@337 1744 string "}))"]
adamc@307 1745
adamc@338 1746 | ENextval {seq, prepared} =>
adamc@338 1747 let
adamc@486 1748 val query = case seq of
adamc@486 1749 (EPrim (Prim.String s), loc) =>
adamc@486 1750 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 1751 | _ =>
adamc@486 1752 let
adamc@486 1753 val query = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 1754 in
adamc@486 1755 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), query]), loc)
adamc@486 1756 end
adamc@338 1757 in
adamc@338 1758 box [string "(uw_begin_region(ctx), ",
adamc@338 1759 string "({",
adamc@338 1760 newline,
adamc@338 1761 string "PGconn *conn = uw_get_db(ctx);",
adamc@338 1762 newline,
adamc@338 1763 case prepared of
adamc@338 1764 NONE => box [string "char *query = ",
adamc@338 1765 p_exp env query,
adamc@338 1766 string ";",
adamc@338 1767 newline]
adamc@338 1768 | SOME _ =>
adamc@338 1769 box [],
adamc@338 1770 newline,
adamc@338 1771 string "PGresult *res = ",
adamc@338 1772 case prepared of
adamc@338 1773 NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
adamc@338 1774 | SOME n => box [string "PQexecPrepared(conn, \"uw",
adamc@338 1775 string (Int.toString n),
adamc@338 1776 string "\", 0, NULL, NULL, NULL, 0);"],
adamc@338 1777 newline,
adamc@338 1778 string "uw_Basis_int n;",
adamc@338 1779 newline,
adamc@338 1780 newline,
adamc@338 1781
adamc@338 1782 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
adamc@338 1783 newline,
adamc@338 1784 newline,
adamc@338 1785
adamc@338 1786 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@338 1787 newline,
adamc@338 1788 box [string "PQclear(res);",
adamc@338 1789 newline,
adamc@338 1790 string "uw_error(ctx, FATAL, \"",
adamc@338 1791 string (ErrorMsg.spanToString loc),
adamc@338 1792 string ": Query failed:\\n%s\\n%s\", ",
adamc@338 1793 case prepared of
adamc@338 1794 NONE => string "query"
adamc@338 1795 | SOME _ => p_exp env query,
adamc@338 1796 string ", PQerrorMessage(conn));",
adamc@338 1797 newline],
adamc@338 1798 string "}",
adamc@338 1799 newline,
adamc@338 1800 newline,
adamc@338 1801
adamc@338 1802 string "uw_end_region(ctx);",
adamc@338 1803 newline,
adamc@338 1804 string "n = PQntuples(res);",
adamc@338 1805 newline,
adamc@338 1806 string "if (n != 1) {",
adamc@338 1807 newline,
adamc@338 1808 box [string "PQclear(res);",
adamc@338 1809 newline,
adamc@338 1810 string "uw_error(ctx, FATAL, \"",
adamc@338 1811 string (ErrorMsg.spanToString loc),
adamc@338 1812 string ": Wrong number of result rows:\\n%s\\n%s\", ",
adamc@338 1813 case prepared of
adamc@338 1814 NONE => string "query"
adamc@338 1815 | SOME _ => p_exp env query,
adamc@338 1816 string ", PQerrorMessage(conn));",
adamc@338 1817 newline],
adamc@338 1818 string "}",
adamc@338 1819 newline,
adamc@338 1820 newline,
adamc@338 1821
adamc@338 1822 string "n = ",
adamc@338 1823 p_unsql true env (TFfi ("Basis", "int"), loc)
adamc@338 1824 (string "PQgetvalue(res, 0, 0)"),
adamc@338 1825 string ";",
adamc@338 1826 newline,
adamc@338 1827 string "PQclear(res);",
adamc@338 1828 newline,
adamc@338 1829 string "n;",
adamc@338 1830 newline,
adamc@338 1831 string "}))"]
adamc@338 1832 end
adamc@338 1833
adamc@463 1834 | EUnurlify (e, t) =>
adamc@463 1835 let
adamc@463 1836 fun getIt () =
adamc@463 1837 if isUnboxable t then
adamc@463 1838 unurlify env t
adamc@463 1839 else
adamc@463 1840 box [string "({",
adamc@463 1841 newline,
adamc@463 1842 p_typ env t,
adamc@463 1843 string " *tmp = uw_malloc(ctx, sizeof(",
adamc@463 1844 p_typ env t,
adamc@463 1845 string "));",
adamc@463 1846 newline,
adamc@463 1847 string "*tmp = ",
adamc@463 1848 unurlify env t,
adamc@463 1849 string ";",
adamc@463 1850 newline,
adamc@463 1851 string "tmp;",
adamc@463 1852 newline,
adamc@463 1853 string "})"]
adamc@463 1854 in
adamc@463 1855 box [string "({",
adamc@463 1856 newline,
adamc@737 1857 string "uw_Basis_string request = uw_maybe_strdup(ctx, ",
adamc@463 1858 p_exp env e,
adamc@492 1859 string ");",
adamc@463 1860 newline,
adamc@463 1861 newline,
adamc@463 1862 string "(request ? ",
adamc@463 1863 getIt (),
adamc@463 1864 string " : NULL);",
adamc@463 1865 newline,
adamc@463 1866 string "})"]
adamc@463 1867 end
adamc@463 1868
adamc@29 1869 and p_exp env = p_exp' false env
adamc@29 1870
adamc@129 1871 fun p_fun env (fx, n, args, ran, e) =
adamc@129 1872 let
adamc@129 1873 val nargs = length args
adamc@129 1874 val env' = foldl (fn ((x, dom), env) => E.pushERel env x dom) env args
adamc@129 1875 in
adamc@129 1876 box [string "static",
adamc@129 1877 space,
adamc@129 1878 p_typ env ran,
adamc@129 1879 space,
adamc@316 1880 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@129 1881 string "(",
adamc@129 1882 p_list_sep (box [string ",", space]) (fn x => x)
adamc@311 1883 (string "uw_context ctx" :: ListUtil.mapi (fn (i, (_, dom)) =>
adamc@129 1884 box [p_typ env dom,
adamc@129 1885 space,
adamc@129 1886 p_rel env' (nargs - i - 1)]) args),
adamc@129 1887 string ")",
adamc@129 1888 space,
adamc@129 1889 string "{",
adamc@129 1890 newline,
adamc@638 1891 box [string "return(",
adamc@638 1892 p_exp env' e,
adamc@638 1893 string ");"],
adamc@129 1894 newline,
adamc@129 1895 string "}"]
adamc@129 1896 end
adamc@129 1897
adamc@129 1898 fun p_decl env (dAll as (d, _) : decl) =
adamc@29 1899 case d of
adamc@29 1900 DStruct (n, xts) =>
adamc@196 1901 let
adamc@196 1902 val env = E.declBinds env dAll
adamc@196 1903 in
adamc@196 1904 box [string "struct",
adamc@196 1905 space,
adamc@311 1906 string ("__uws_" ^ Int.toString n),
adamc@196 1907 space,
adamc@196 1908 string "{",
adamc@196 1909 newline,
adamc@196 1910 p_list_sep (box []) (fn (x, t) => box [p_typ env t,
adamc@196 1911 space,
adamc@311 1912 string "__uwf_",
adamc@316 1913 p_ident x,
adamc@196 1914 string ";",
adamc@196 1915 newline]) xts,
adamc@196 1916 string "};"]
adamc@196 1917 end
adamc@188 1918 | DDatatype (Enum, x, n, xncs) =>
adamc@188 1919 box [string "enum",
adamc@188 1920 space,
adamc@316 1921 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@188 1922 space,
adamc@188 1923 string "{",
adamc@188 1924 space,
adamc@316 1925 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1926 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@188 1927 space,
adamc@188 1928 string "};"]
adamc@198 1929 | DDatatype (Option, _, _, _) => box []
adamc@188 1930 | DDatatype (Default, x, n, xncs) =>
adamc@165 1931 let
adamc@165 1932 val xncsArgs = List.mapPartial (fn (x, n, NONE) => NONE
adamc@165 1933 | (x, n, SOME t) => SOME (x, n, t)) xncs
adamc@165 1934 in
adamc@165 1935 box [string "enum",
adamc@165 1936 space,
adamc@316 1937 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1938 space,
adamc@165 1939 string "{",
adamc@165 1940 space,
adamc@316 1941 p_list_sep (box [string ",", space]) (fn (x, n, _) =>
adamc@316 1942 string ("__uwc_" ^ ident x ^ "_" ^ Int.toString n)) xncs,
adamc@165 1943 space,
adamc@165 1944 string "};",
adamc@165 1945 newline,
adamc@165 1946 newline,
adamc@165 1947 string "struct",
adamc@165 1948 space,
adamc@316 1949 string ("__uwd_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1950 space,
adamc@165 1951 string "{",
adamc@165 1952 newline,
adamc@165 1953 string "enum",
adamc@165 1954 space,
adamc@316 1955 string ("__uwe_" ^ ident x ^ "_" ^ Int.toString n),
adamc@165 1956 space,
adamc@165 1957 string "tag;",
adamc@165 1958 newline,
adamc@165 1959 box (case xncsArgs of
adamc@165 1960 [] => []
adamc@165 1961 | _ => [string "union",
adamc@165 1962 space,
adamc@165 1963 string "{",
adamc@165 1964 newline,
adamc@165 1965 p_list_sep newline (fn (x, n, t) => box [p_typ env t,
adamc@165 1966 space,
adamc@316 1967 string ("uw_" ^ ident x),
adamc@165 1968 string ";"]) xncsArgs,
adamc@165 1969 newline,
adamc@165 1970 string "}",
adamc@165 1971 space,
adamc@165 1972 string "data;",
adamc@165 1973 newline]),
adamc@165 1974 string "};"]
adamc@188 1975 end
adamc@29 1976
adamc@196 1977 | DDatatypeForward _ => box []
adamc@196 1978
adamc@29 1979 | DVal (x, n, t, e) =>
adamc@29 1980 box [p_typ env t,
adamc@29 1981 space,
adamc@316 1982 string ("__uwn_" ^ ident x ^ "_" ^ Int.toString n),
adamc@29 1983 space,
adamc@29 1984 string "=",
adamc@29 1985 space,
adamc@29 1986 p_exp env e,
adamc@29 1987 string ";"]
adamc@129 1988 | DFun vi => p_fun env vi
adamc@129 1989 | DFunRec vis =>
adamc@29 1990 let
adamc@129 1991 val env = E.declBinds env dAll
adamc@29 1992 in
adamc@129 1993 box [p_list_sep newline (fn (fx, n, args, ran, _) =>
adamc@129 1994 box [string "static",
adamc@129 1995 space,
adamc@129 1996 p_typ env ran,
adamc@129 1997 space,
adamc@316 1998 string ("__uwn_" ^ ident fx ^ "_" ^ Int.toString n),
adamc@311 1999 string "(uw_context,",
adamc@129 2000 space,
adamc@129 2001 p_list_sep (box [string ",", space])
adamc@129 2002 (fn (_, dom) => p_typ env dom) args,
adamc@129 2003 string ");"]) vis,
adamc@29 2004 newline,
adamc@129 2005 p_list_sep newline (p_fun env) vis,
adamc@129 2006 newline]
adamc@29 2007 end
adamc@707 2008 | DTable (x, _, pk, csts) => box [string "/* SQL table ",
adamc@707 2009 string x,
adamc@707 2010 space,
adamc@707 2011 case pk of
adamc@707 2012 "" => box []
adamc@707 2013 | _ => box [string "keys",
adamc@707 2014 space,
adamc@707 2015 string pk,
adamc@707 2016 space],
adamc@707 2017 string "constraints",
adamc@707 2018 space,
adamc@707 2019 p_list (fn (x, v) => box [string x,
adamc@707 2020 space,
adamc@707 2021 string ":",
adamc@707 2022 space,
adamc@707 2023 string v]) csts,
adamc@707 2024 space,
adamc@707 2025 string " */",
adamc@707 2026 newline]
adamc@338 2027 | DSequence x => box [string "/* SQL sequence ",
adamc@338 2028 string x,
adamc@338 2029 string " */",
adamc@338 2030 newline]
adamc@687 2031 | DDatabase {name, expunge, initialize} =>
adamc@687 2032 box [string "static void uw_db_validate(uw_context);",
adamc@687 2033 newline,
adamc@687 2034 string "static void uw_db_prepare(uw_context);",
adamc@687 2035 newline,
adamc@687 2036 newline,
adamc@687 2037 string "void uw_db_init(uw_context ctx) {",
adamc@687 2038 newline,
adamc@687 2039 string "PGconn *conn = PQconnectdb(\"",
adamc@687 2040 string (String.toString name),
adamc@687 2041 string "\");",
adamc@687 2042 newline,
adamc@687 2043 string "if (conn == NULL) uw_error(ctx, BOUNDED_RETRY, ",
adamc@687 2044 string "\"libpq can't allocate a connection.\");",
adamc@687 2045 newline,
adamc@687 2046 string "if (PQstatus(conn) != CONNECTION_OK) {",
adamc@687 2047 newline,
adamc@687 2048 box [string "char msg[1024];",
adamc@687 2049 newline,
adamc@687 2050 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@687 2051 newline,
adamc@687 2052 string "msg[1023] = 0;",
adamc@687 2053 newline,
adamc@687 2054 string "PQfinish(conn);",
adamc@687 2055 newline,
adamc@687 2056 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@687 2057 string "\"Connection to Postgres server failed: %s\", msg);"],
adamc@687 2058 newline,
adamc@687 2059 string "}",
adamc@687 2060 newline,
adamc@687 2061 string "uw_set_db(ctx, conn);",
adamc@687 2062 newline,
adamc@687 2063 string "uw_db_validate(ctx);",
adamc@687 2064 newline,
adamc@687 2065 string "uw_db_prepare(ctx);",
adamc@687 2066 newline,
adamc@687 2067 string "}",
adamc@687 2068 newline,
adamc@687 2069 newline,
adamc@687 2070 string "void uw_db_close(uw_context ctx) {",
adamc@687 2071 newline,
adamc@687 2072 string "PQfinish(uw_get_db(ctx));",
adamc@687 2073 newline,
adamc@687 2074 string "}",
adamc@687 2075 newline,
adamc@687 2076 newline,
adamc@424 2077
adamc@687 2078 string "int uw_db_begin(uw_context ctx) {",
adamc@687 2079 newline,
adamc@687 2080 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2081 newline,
adamc@687 2082 string "PGresult *res = PQexec(conn, \"BEGIN ISOLATION LEVEL SERIALIZABLE\");",
adamc@687 2083 newline,
adamc@687 2084 newline,
adamc@687 2085 string "if (res == NULL) return 1;",
adamc@687 2086 newline,
adamc@687 2087 newline,
adamc@687 2088 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2089 box [string "PQclear(res);",
adamc@687 2090 newline,
adamc@687 2091 string "return 1;",
adamc@687 2092 newline],
adamc@687 2093 string "}",
adamc@687 2094 newline,
adamc@687 2095 string "return 0;",
adamc@687 2096 newline,
adamc@687 2097 string "}",
adamc@687 2098 newline,
adamc@687 2099 newline,
adamc@424 2100
adamc@687 2101 string "int uw_db_commit(uw_context ctx) {",
adamc@687 2102 newline,
adamc@687 2103 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2104 newline,
adamc@687 2105 string "PGresult *res = PQexec(conn, \"COMMIT\");",
adamc@687 2106 newline,
adamc@687 2107 newline,
adamc@687 2108 string "if (res == NULL) return 1;",
adamc@687 2109 newline,
adamc@687 2110 newline,
adamc@687 2111 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2112 box [string "PQclear(res);",
adamc@687 2113 newline,
adamc@687 2114 string "return 1;",
adamc@687 2115 newline],
adamc@687 2116 string "}",
adamc@687 2117 newline,
adamc@687 2118 string "return 0;",
adamc@687 2119 newline,
adamc@687 2120 string "}",
adamc@687 2121 newline,
adamc@687 2122 newline,
adamc@424 2123
adamc@687 2124 string "int uw_db_rollback(uw_context ctx) {",
adamc@687 2125 newline,
adamc@687 2126 string "PGconn *conn = uw_get_db(ctx);",
adamc@687 2127 newline,
adamc@687 2128 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
adamc@687 2129 newline,
adamc@687 2130 newline,
adamc@687 2131 string "if (res == NULL) return 1;",
adamc@687 2132 newline,
adamc@687 2133 newline,
adamc@687 2134 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@687 2135 box [string "PQclear(res);",
adamc@687 2136 newline,
adamc@687 2137 string "return 1;",
adamc@687 2138 newline],
adamc@687 2139 string "}",
adamc@687 2140 newline,
adamc@687 2141 string "return 0;",
adamc@687 2142 newline,
adamc@687 2143 string "}",
adamc@687 2144 newline,
adamc@687 2145 newline,
adamc@683 2146
adamc@687 2147 string "void uw_expunger(uw_context ctx, uw_Basis_client cli) {",
adamc@687 2148 newline,
adamc@687 2149 box [p_enamed env expunge,
adamc@687 2150 string "(ctx, cli);",
adamc@687 2151 newline],
adamc@687 2152 string "}",
adamc@687 2153 newline,
adamc@687 2154 newline,
adamc@687 2155
adamc@687 2156 string "void uw_initializer(uw_context ctx) {",
adamc@687 2157 newline,
adamc@687 2158 box [p_enamed env initialize,
adamc@687 2159 string "(ctx, uw_unit_v);",
adamc@687 2160 newline],
adamc@687 2161 string "}",
adamc@687 2162 newline]
adamc@29 2163
adamc@467 2164 | DPreparedStatements [] =>
adamc@467 2165 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@467 2166 newline,
adamc@467 2167 string "}"]
adamc@282 2168 | DPreparedStatements ss =>
adamc@311 2169 box [string "static void uw_db_prepare(uw_context ctx) {",
adamc@282 2170 newline,
adamc@311 2171 string "PGconn *conn = uw_get_db(ctx);",
adamc@282 2172 newline,
adamc@282 2173 string "PGresult *res;",
adamc@282 2174 newline,
adamc@282 2175 newline,
adamc@282 2176
adamc@282 2177 p_list_sepi newline (fn i => fn (s, n) =>
adamc@311 2178 box [string "res = PQprepare(conn, \"uw",
adamc@282 2179 string (Int.toString i),
adamc@282 2180 string "\", \"",
adamc@282 2181 string (String.toString s),
adamc@282 2182 string "\", ",
adamc@282 2183 string (Int.toString n),
adamc@282 2184 string ", NULL);",
adamc@282 2185 newline,
adamc@282 2186 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
adamc@282 2187 newline,
adamc@282 2188 box [string "char msg[1024];",
adamc@282 2189 newline,
adamc@282 2190 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@282 2191 newline,
adamc@282 2192 string "msg[1023] = 0;",
adamc@282 2193 newline,
adamc@282 2194 string "PQclear(res);",
adamc@282 2195 newline,
adamc@282 2196 string "PQfinish(conn);",
adamc@282 2197 newline,
adamc@311 2198 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
adamc@282 2199 string (String.toString s),
adamc@282 2200 string "\\n%s\", msg);",
adamc@282 2201 newline],
adamc@282 2202 string "}",
adamc@282 2203 newline,
adamc@282 2204 string "PQclear(res);",
adamc@282 2205 newline])
adamc@282 2206 ss,
adamc@282 2207
adamc@282 2208 string "}"]
adamc@282 2209
adamc@569 2210 | DJavaScript s => box [string "static char jslib[] = \"",
adamc@569 2211 string (String.toString s),
adamc@569 2212 string "\";"]
adamc@725 2213 | DCookie s => box [string "/*",
adamc@725 2214 space,
adamc@725 2215 string "cookie",
adamc@725 2216 space,
adamc@725 2217 string s,
adamc@725 2218 space,
adamc@725 2219 string "*/"]
adamc@720 2220 | DStyle s => box [string "/*",
adamc@720 2221 space,
adamc@720 2222 string "style",
adamc@720 2223 space,
adamc@720 2224 string s,
adamc@720 2225 space,
adamc@720 2226 string "*/"]
adamc@569 2227
adamc@144 2228 datatype 'a search =
adamc@144 2229 Found of 'a
adamc@144 2230 | NotFound
adamc@144 2231 | Error
adamc@120 2232
adamc@467 2233 fun p_sqltype'' env (tAll as (t, loc)) =
adamc@275 2234 case t of
adamc@275 2235 TFfi ("Basis", "int") => "int8"
adamc@275 2236 | TFfi ("Basis", "float") => "float8"
adamc@275 2237 | TFfi ("Basis", "string") => "text"
adamc@275 2238 | TFfi ("Basis", "bool") => "bool"
adamc@438 2239 | TFfi ("Basis", "time") => "timestamp"
adamc@737 2240 | TFfi ("Basis", "blob") => "bytea"
adamc@682 2241 | TFfi ("Basis", "channel") => "int8"
adamc@682 2242 | TFfi ("Basis", "client") => "int4"
adamc@275 2243 | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type";
adamc@275 2244 Print.eprefaces' [("Type", p_typ env tAll)];
adamc@275 2245 "ERROR")
adamc@275 2246
adamc@467 2247 fun p_sqltype' env (tAll as (t, loc)) =
adamc@467 2248 case t of
adamc@467 2249 (TOption t, _) => p_sqltype'' env t
adamc@467 2250 | _ => p_sqltype'' env t ^ " NOT NULL"
adamc@467 2251
adamc@275 2252 fun p_sqltype env t = string (p_sqltype' env t)
adamc@101 2253
adamc@467 2254 fun p_sqltype_base' env t =
adamc@467 2255 case t of
adamc@467 2256 (TOption t, _) => p_sqltype'' env t
adamc@467 2257 | _ => p_sqltype'' env t
adamc@467 2258
adamc@467 2259 fun p_sqltype_base env t = string (p_sqltype_base' env t)
adamc@467 2260
adamc@467 2261 fun is_not_null t =
adamc@467 2262 case t of
adamc@467 2263 (TOption _, _) => false
adamc@467 2264 | _ => true
adamc@467 2265
adamc@734 2266 fun sigName fields =
adamc@734 2267 let
adamc@734 2268 fun inFields s = List.exists (fn (s', _) => s' = s) fields
adamc@734 2269
adamc@734 2270 fun getSigName n =
adamc@734 2271 let
adamc@734 2272 val s = "Sig" ^ Int.toString n
adamc@734 2273 in
adamc@734 2274 if inFields s then
adamc@734 2275 getSigName (n + 1)
adamc@734 2276 else
adamc@734 2277 s
adamc@734 2278 end
adamc@734 2279 in
adamc@734 2280 if inFields "Sig" then
adamc@734 2281 getSigName 0
adamc@734 2282 else
adamc@734 2283 "Sig"
adamc@734 2284 end
adamc@734 2285
adamc@101 2286 fun p_file env (ds, ps) =
adamc@29 2287 let
adamc@101 2288 val (pds, env) = ListUtil.foldlMap (fn (d, env) =>
adamc@638 2289 (p_decl env d,
adamc@638 2290 E.declBinds env d))
adamc@101 2291 env ds
adamc@144 2292
adamc@643 2293 val fields = foldl (fn ((ek, _, _, ts, _, _), fields) =>
adamc@144 2294 case ek of
adamc@735 2295 Link => fields
adamc@735 2296 | Rpc _ => fields
adamc@735 2297 | Action eff =>
adamc@280 2298 case List.nth (ts, length ts - 2) of
adamc@144 2299 (TRecord i, _) =>
adamc@144 2300 let
adamc@144 2301 val xts = E.lookupStruct env i
adamc@735 2302 val xts = case eff of
adamc@735 2303 ReadCookieWrite =>
adamc@735 2304 (sigName xts, (TRecord 0, ErrorMsg.dummySpan)) :: xts
adamc@735 2305 | _ => xts
adamc@144 2306 val xtsSet = SS.addList (SS.empty, map #1 xts)
adamc@144 2307 in
adamc@144 2308 foldl (fn ((x, _), fields) =>
adamc@144 2309 let
adamc@144 2310 val xtsSet' = Option.getOpt (SM.find (fields, x), SS.empty)
adamc@144 2311 in
adamc@144 2312 SM.insert (fields, x, SS.union (SS.delete (xtsSet, x),
adamc@144 2313 xtsSet'))
adamc@144 2314 end) fields xts
adamc@144 2315 end
adamc@144 2316 | _ => raise Fail "CjrPrint: Last argument of action isn't record")
adamc@144 2317 SM.empty ps
adamc@144 2318
adamc@144 2319 val fnums = SM.foldli (fn (x, xs, fnums) =>
adamc@144 2320 let
adamc@144 2321 val unusable = SS.foldl (fn (x', unusable) =>
adamc@144 2322 case SM.find (fnums, x') of
adamc@144 2323 NONE => unusable
adamc@144 2324 | SOME n => IS.add (unusable, n))
adamc@144 2325 IS.empty xs
adamc@144 2326
adamc@144 2327 fun findAvailable n =
adamc@144 2328 if IS.member (unusable, n) then
adamc@144 2329 findAvailable (n + 1)
adamc@144 2330 else
adamc@144 2331 n
adamc@144 2332 in
adamc@144 2333 SM.insert (fnums, x, findAvailable 0)
adamc@144 2334 end)
adamc@144 2335 SM.empty fields
adamc@144 2336
adamc@734 2337 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
adamc@734 2338
adamc@144 2339 fun makeSwitch (fnums, i) =
adamc@144 2340 case SM.foldl (fn (n, NotFound) => Found n
adamc@144 2341 | (n, Error) => Error
adamc@144 2342 | (n, Found n') => if n = n' then
adamc@144 2343 Found n'
adamc@144 2344 else
adamc@144 2345 Error) NotFound fnums of
adamc@144 2346 NotFound => box [string "return",
adamc@144 2347 space,
adamc@144 2348 string "-1;"]
adamc@144 2349 | Found n => box [string "return",
adamc@144 2350 space,
adamc@144 2351 string (Int.toString n),
adamc@144 2352 string ";"]
adamc@144 2353 | Error =>
adamc@144 2354 let
adamc@144 2355 val cmap = SM.foldli (fn (x, n, cmap) =>
adamc@144 2356 let
adamc@144 2357 val ch = if i < size x then
adamc@144 2358 String.sub (x, i)
adamc@144 2359 else
adamc@144 2360 chr 0
adamc@144 2361
adamc@144 2362 val fnums = case CM.find (cmap, ch) of
adamc@144 2363 NONE => SM.empty
adamc@144 2364 | SOME fnums => fnums
adamc@144 2365 val fnums = SM.insert (fnums, x, n)
adamc@144 2366 in
adamc@144 2367 CM.insert (cmap, ch, fnums)
adamc@144 2368 end)
adamc@144 2369 CM.empty fnums
adamc@144 2370
adamc@144 2371 val cmap = CM.listItemsi cmap
adamc@144 2372 in
adamc@144 2373 case cmap of
adamc@144 2374 [(_, fnums)] =>
adamc@144 2375 box [string "if",
adamc@144 2376 space,
adamc@144 2377 string "(name[",
adamc@144 2378 string (Int.toString i),
adamc@144 2379 string "]",
adamc@144 2380 space,
adamc@144 2381 string "==",
adamc@144 2382 space,
adamc@144 2383 string "0)",
adamc@144 2384 space,
adamc@144 2385 string "return",
adamc@144 2386 space,
adamc@144 2387 string "-1;",
adamc@144 2388 newline,
adamc@144 2389 makeSwitch (fnums, i+1)]
adamc@144 2390 | _ =>
adamc@144 2391 box [string "switch",
adamc@144 2392 space,
adamc@144 2393 string "(name[",
adamc@144 2394 string (Int.toString i),
adamc@144 2395 string "])",
adamc@144 2396 space,
adamc@144 2397 string "{",
adamc@144 2398 newline,
adamc@144 2399 box (map (fn (ch, fnums) =>
adamc@144 2400 box [string "case",
adamc@144 2401 space,
adamc@144 2402 if ch = chr 0 then
adamc@144 2403 string "0:"
adamc@144 2404 else
adamc@144 2405 box [string "'",
adamc@144 2406 string (Char.toString ch),
adamc@144 2407 string "':"],
adamc@144 2408 newline,
adamc@144 2409 makeSwitch (fnums, i+1),
adamc@144 2410 newline]) cmap),
adamc@144 2411 string "default:",
adamc@144 2412 newline,
adamc@144 2413 string "return",
adamc@144 2414 space,
adamc@144 2415 string "-1;",
adamc@144 2416 newline,
adamc@144 2417 string "}"]
adamc@144 2418 end
adamc@144 2419
adamc@643 2420 fun p_page (ek, s, n, ts, ran, side) =
adamc@144 2421 let
adamc@734 2422 val (ts, defInputs, inputsVar, fields) =
adamc@144 2423 case ek of
adamc@734 2424 Core.Link => (List.take (ts, length ts - 1), string "", string "", NONE)
adamc@734 2425 | Core.Rpc _ => (List.take (ts, length ts - 1), string "", string "", NONE)
adamc@731 2426 | Core.Action _ =>
adamc@280 2427 case List.nth (ts, length ts - 2) of
adamc@144 2428 (TRecord i, _) =>
adamc@144 2429 let
adamc@144 2430 val xts = E.lookupStruct env i
adamc@144 2431 in
adamc@280 2432 (List.take (ts, length ts - 2),
adamc@144 2433 box [box (map (fn (x, t) => box [p_typ env t,
adamc@144 2434 space,
adamc@311 2435 string "uw_input_",
adamc@316 2436 p_ident x,
adamc@144 2437 string ";",
adamc@144 2438 newline]) xts),
adamc@144 2439 newline,
adamc@144 2440 box (map (fn (x, t) =>
adamc@144 2441 let
adamc@144 2442 val n = case SM.find (fnums, x) of
adamc@144 2443 NONE => raise Fail "CjrPrint: Can't find in fnums"
adamc@144 2444 | SOME n => n
adamc@190 2445
adamc@190 2446 val f = case t of
adamc@190 2447 (TFfi ("Basis", "bool"), _) => "optional_"
adamc@190 2448 | _ => ""
adamc@144 2449 in
adamc@739 2450 if isFile t then
adamc@737 2451 box [string "uw_input_",
adamc@737 2452 p_ident x,
adamc@737 2453 space,
adamc@737 2454 string "=",
adamc@737 2455 space,
adamc@737 2456 string "uw_get_file_input(ctx, ",
adamc@737 2457 string (Int.toString n),
adamc@737 2458 string ");",
adamc@737 2459 newline]
adamc@737 2460 else
adamc@737 2461 box [string "request = uw_get_",
adamc@737 2462 string f,
adamc@737 2463 string "input(ctx, ",
adamc@737 2464 string (Int.toString n),
adamc@737 2465 string ");",
adamc@737 2466 newline,
adamc@737 2467 string "if (request == NULL)",
adamc@737 2468 newline,
adamc@737 2469 box [string "uw_error(ctx, FATAL, \"Missing input ",
adamc@737 2470 string x,
adamc@737 2471 string "\");"],
adamc@737 2472 newline,
adamc@737 2473 string "uw_input_",
adamc@737 2474 p_ident x,
adamc@737 2475 space,
adamc@737 2476 string "=",
adamc@737 2477 space,
adamc@737 2478 unurlify env t,
adamc@737 2479 string ";",
adamc@737 2480 newline]
adamc@144 2481 end) xts),
adamc@311 2482 string "struct __uws_",
adamc@144 2483 string (Int.toString i),
adamc@144 2484 space,
adamc@311 2485 string "uw_inputs",
adamc@144 2486 space,
adamc@144 2487 string "= {",
adamc@144 2488 newline,
adamc@311 2489 box (map (fn (x, _) => box [string "uw_input_",
adamc@316 2490 p_ident x,
adamc@144 2491 string ",",
adamc@144 2492 newline]) xts),
adamc@144 2493 string "};",
adamc@144 2494 newline],
adamc@144 2495 box [string ",",
adamc@144 2496 space,
adamc@734 2497 string "uw_inputs"],
adamc@734 2498 SOME xts)
adamc@144 2499 end
adamc@144 2500
adamc@144 2501 | _ => raise Fail "CjrPrint: Last argument to an action isn't a record"
adamc@734 2502
adamc@734 2503 fun couldWrite ek =
adamc@734 2504 case ek of
adamc@734 2505 Link => false
adamc@735 2506 | Action ef => ef = ReadCookieWrite
adamc@735 2507 | Rpc ef => ef = ReadCookieWrite
adamc@144 2508 in
adamc@735 2509 box [string "if (!strncmp(request, \"",
adamc@735 2510 string (String.toString s),
adamc@735 2511 string "\", ",
adamc@735 2512 string (Int.toString (size s)),
adamc@735 2513 string ") && (request[",
adamc@735 2514 string (Int.toString (size s)),
adamc@735 2515 string "] == 0 || request[",
adamc@735 2516 string (Int.toString (size s)),
adamc@735 2517 string "] == '/')) {",
adamc@735 2518 newline,
adamc@735 2519 string "request += ",
adamc@735 2520 string (Int.toString (size s)),
adamc@735 2521 string ";",
adamc@735 2522 newline,
adamc@735 2523 string "if (*request == '/') ++request;",
adamc@735 2524 newline,
adamc@735 2525 if couldWrite ek then
adamc@734 2526 box [string "{",
adamc@734 2527 newline,
adamc@734 2528 string "uw_Basis_string sig = ",
adamc@734 2529 case fields of
adamc@734 2530 NONE => string "uw_Basis_requestHeader(ctx, \"UrWeb-Sig\")"
adamc@734 2531 | SOME fields =>
adamc@734 2532 case SM.find (fnums, sigName fields) of
adamc@734 2533 NONE => raise Fail "CjrPrint: sig name wasn't assigned a number"
adamc@734 2534 | SOME inum =>
adamc@734 2535 string ("uw_get_input(ctx, " ^ Int.toString inum ^ ")"),
adamc@734 2536 string ";",
adamc@734 2537 newline,
adamc@734 2538 string "if (sig == NULL) uw_error(ctx, FATAL, \"Missing cookie signature\");",
adamc@734 2539 newline,
adamc@734 2540 string "if (strcmp(sig, uw_cookie_sig(ctx)))",
adamc@734 2541 newline,
adamc@734 2542 box [string "uw_error(ctx, FATAL, \"Wrong cookie signature\");",
adamc@734 2543 newline],
adamc@734 2544 string "}",
adamc@734 2545 newline]
adamc@734 2546 else
adamc@734 2547 box [],
adamc@609 2548 box (case ek of
adamc@731 2549 Core.Rpc _ => [string "uw_write_header(ctx, \"Content-type: text/plain\\r\\n\");",
adamc@731 2550 newline]
adamc@609 2551 | _ => [string "uw_write_header(ctx, \"Content-type: text/html\\r\\n\");",
adamc@609 2552 newline,
adamc@609 2553 string "uw_write_header(ctx, \"Content-script-type: text/javascript\\r\\n\");",
adamc@609 2554 newline,
adamc@609 2555 string "uw_write(ctx, \"<html>\");",
adamc@643 2556 newline,
adamc@643 2557 string "uw_set_script_header(ctx, \"",
adamc@643 2558 string (case side of
adamc@693 2559 ServerOnly => ""
adamc@693 2560 | _ => "<script src=\\\""
adamc@693 2561 ^ OS.Path.joinDirFile {dir = !Monoize.urlPrefix,
adamc@693 2562 file = "app.js"}
adamc@693 2563 ^ "\\\"></script>\\n"),
adamc@643 2564 string "\");",
adamc@693 2565 newline,
adamc@693 2566 string "uw_set_needs_push(ctx, ",
adamc@693 2567 string (case side of
adamc@693 2568 ServerAndPullAndPush => "1"
adamc@693 2569 | _ => "0"),
adamc@693 2570 string ");",
adamc@693 2571 newline,
adamc@667 2572 string "uw_set_url_prefix(ctx, \"",
adamc@667 2573 string (!Monoize.urlPrefix),
adamc@667 2574 string "\");",
adamc@609 2575 newline]),
adamc@736 2576 string "uw_set_needs_sig(ctx, ",
adamc@736 2577 string (if couldWrite ek then
adamc@736 2578 "1"
adamc@736 2579 else
adamc@736 2580 "0"),
adamc@736 2581 string ");",
adamc@736 2582 newline,
adamc@682 2583 string "uw_login(ctx);",
adamc@682 2584 newline,
adamc@144 2585 box [string "{",
adamc@144 2586 newline,
adamc@144 2587 box (ListUtil.mapi (fn (i, t) => box [p_typ env t,
adamc@144 2588 space,
adamc@144 2589 string "arg",
adamc@144 2590 string (Int.toString i),
adamc@144 2591 space,
adamc@144 2592 string "=",
adamc@144 2593 space,
adamc@463 2594 unurlify env t,
adamc@144 2595 string ";",
adamc@144 2596 newline]) ts),
adamc@144 2597 defInputs,
adamc@609 2598 box (case ek of
adamc@731 2599 Core.Rpc _ => [p_typ env ran,
adamc@731 2600 space,
adamc@731 2601 string "it0",
adamc@731 2602 space,
adamc@731 2603 string "=",
adamc@731 2604 space]
adamc@609 2605 | _ => []),
adamc@144 2606 p_enamed env n,
adamc@144 2607 string "(",
adamc@144 2608 p_list_sep (box [string ",", space])
adamc@144 2609 (fn x => x)
adamc@272 2610 (string "ctx"
adamc@280 2611 :: ListUtil.mapi (fn (i, _) => string ("arg" ^ Int.toString i)) ts),
adamc@144 2612 inputsVar,
adamc@311 2613 string ", uw_unit_v);",
adamc@144 2614 newline,
adamc@609 2615 box (case ek of
adamc@731 2616 Core.Rpc _ => [urlify env ran]
adamc@609 2617 | _ => [string "uw_write(ctx, \"</html>\");",
adamc@609 2618 newline]),
adamc@144 2619 string "return;",
adamc@144 2620 newline,
adamc@144 2621 string "}",
adamc@144 2622 newline,
adamc@144 2623 string "}"]
adamc@144 2624 ]
adamc@144 2625 end
adamc@144 2626
adamc@144 2627 val pds' = map p_page ps
adamc@275 2628
adamc@707 2629 val tables = List.mapPartial (fn (DTable (s, xts, _, _), _) => SOME (s, xts)
adamc@275 2630 | _ => NONE) ds
adamc@377 2631 val sequences = List.mapPartial (fn (DSequence s, _) => SOME s
adamc@377 2632 | _ => NONE) ds
adamc@275 2633
adamc@275 2634 val validate =
adamc@311 2635 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@275 2636 newline,
adamc@311 2637 string "PGconn *conn = uw_get_db(ctx);",
adamc@275 2638 newline,
adamc@275 2639 string "PGresult *res;",
adamc@275 2640 newline,
adamc@275 2641 newline,
adamc@275 2642 p_list_sep newline
adamc@275 2643 (fn (s, xts) =>
adamc@275 2644 let
adamc@377 2645 val sl = CharVector.map Char.toLower s
adamc@377 2646
adamc@275 2647 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2648 ^ sl ^ "'"
adamc@275 2649
adamc@275 2650 val q' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2651 sl,
adamc@275 2652 "') AND (",
adamc@275 2653 String.concatWith " OR "
adamc@275 2654 (map (fn (x, t) =>
adamc@311 2655 String.concat ["(attname = 'uw_",
adamc@275 2656 CharVector.map
adamc@316 2657 Char.toLower (ident x),
adamc@275 2658 "' AND atttypid = (SELECT oid FROM pg_type",
adamc@275 2659 " WHERE typname = '",
adamc@467 2660 p_sqltype_base' env t,
adamc@467 2661 "') AND attnotnull = ",
adamc@467 2662 if is_not_null t then
adamc@467 2663 "TRUE"
adamc@467 2664 else
adamc@467 2665 "FALSE",
adamc@467 2666 ")"]) xts),
adamc@275 2667 ")"]
adamc@275 2668
adamc@275 2669 val q'' = String.concat ["SELECT COUNT(*) FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '",
adamc@377 2670 sl,
adamc@325 2671 "') AND attname LIKE 'uw_%'"]
adamc@275 2672 in
adamc@275 2673 box [string "res = PQexec(conn, \"",
adamc@275 2674 string q,
adamc@275 2675 string "\");",
adamc@275 2676 newline,
adamc@275 2677 newline,
adamc@275 2678 string "if (res == NULL) {",
adamc@275 2679 newline,
adamc@275 2680 box [string "PQfinish(conn);",
adamc@275 2681 newline,
adamc@311 2682 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2683 newline],
adamc@275 2684 string "}",
adamc@275 2685 newline,
adamc@275 2686 newline,
adamc@275 2687 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2688 newline,
adamc@275 2689 box [string "char msg[1024];",
adamc@275 2690 newline,
adamc@275 2691 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2692 newline,
adamc@275 2693 string "msg[1023] = 0;",
adamc@275 2694 newline,
adamc@275 2695 string "PQclear(res);",
adamc@275 2696 newline,
adamc@275 2697 string "PQfinish(conn);",
adamc@275 2698 newline,
adamc@311 2699 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2700 string q,
adamc@275 2701 string "\\n%s\", msg);",
adamc@275 2702 newline],
adamc@275 2703 string "}",
adamc@275 2704 newline,
adamc@275 2705 newline,
adamc@275 2706 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@275 2707 newline,
adamc@275 2708 box [string "PQclear(res);",
adamc@275 2709 newline,
adamc@275 2710 string "PQfinish(conn);",
adamc@275 2711 newline,
adamc@311 2712 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2713 string s,
adamc@275 2714 string "' does not exist.\");",
adamc@275 2715 newline],
adamc@275 2716 string "}",
adamc@275 2717 newline,
adamc@275 2718 newline,
adamc@275 2719 string "PQclear(res);",
adamc@275 2720 newline,
adamc@275 2721
adamc@275 2722 string "res = PQexec(conn, \"",
adamc@275 2723 string q',
adamc@275 2724 string "\");",
adamc@275 2725 newline,
adamc@275 2726 newline,
adamc@275 2727 string "if (res == NULL) {",
adamc@275 2728 newline,
adamc@275 2729 box [string "PQfinish(conn);",
adamc@275 2730 newline,
adamc@311 2731 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2732 newline],
adamc@275 2733 string "}",
adamc@275 2734 newline,
adamc@275 2735 newline,
adamc@275 2736 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2737 newline,
adamc@275 2738 box [string "char msg[1024];",
adamc@275 2739 newline,
adamc@275 2740 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2741 newline,
adamc@275 2742 string "msg[1023] = 0;",
adamc@275 2743 newline,
adamc@275 2744 string "PQclear(res);",
adamc@275 2745 newline,
adamc@275 2746 string "PQfinish(conn);",
adamc@275 2747 newline,
adamc@311 2748 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2749 string q',
adamc@275 2750 string "\\n%s\", msg);",
adamc@275 2751 newline],
adamc@275 2752 string "}",
adamc@275 2753 newline,
adamc@275 2754 newline,
adamc@275 2755 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2756 string (Int.toString (length xts)),
adamc@275 2757 string "\")) {",
adamc@275 2758 newline,
adamc@275 2759 box [string "PQclear(res);",
adamc@275 2760 newline,
adamc@275 2761 string "PQfinish(conn);",
adamc@275 2762 newline,
adamc@311 2763 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2764 string s,
adamc@275 2765 string "' has the wrong column types.\");",
adamc@275 2766 newline],
adamc@275 2767 string "}",
adamc@275 2768 newline,
adamc@275 2769 newline,
adamc@275 2770 string "PQclear(res);",
adamc@275 2771 newline,
adamc@275 2772 newline,
adamc@275 2773
adamc@275 2774 string "res = PQexec(conn, \"",
adamc@275 2775 string q'',
adamc@275 2776 string "\");",
adamc@275 2777 newline,
adamc@275 2778 newline,
adamc@275 2779 string "if (res == NULL) {",
adamc@275 2780 newline,
adamc@275 2781 box [string "PQfinish(conn);",
adamc@275 2782 newline,
adamc@311 2783 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@275 2784 newline],
adamc@275 2785 string "}",
adamc@275 2786 newline,
adamc@275 2787 newline,
adamc@275 2788 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@275 2789 newline,
adamc@275 2790 box [string "char msg[1024];",
adamc@275 2791 newline,
adamc@275 2792 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@275 2793 newline,
adamc@275 2794 string "msg[1023] = 0;",
adamc@275 2795 newline,
adamc@275 2796 string "PQclear(res);",
adamc@275 2797 newline,
adamc@275 2798 string "PQfinish(conn);",
adamc@275 2799 newline,
adamc@311 2800 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@275 2801 string q'',
adamc@275 2802 string "\\n%s\", msg);",
adamc@275 2803 newline],
adamc@275 2804 string "}",
adamc@275 2805 newline,
adamc@275 2806 newline,
adamc@275 2807 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
adamc@275 2808 string (Int.toString (length xts)),
adamc@275 2809 string "\")) {",
adamc@275 2810 newline,
adamc@275 2811 box [string "PQclear(res);",
adamc@275 2812 newline,
adamc@275 2813 string "PQfinish(conn);",
adamc@275 2814 newline,
adamc@311 2815 string "uw_error(ctx, FATAL, \"Table '",
adamc@275 2816 string s,
adamc@275 2817 string "' has extra columns.\");",
adamc@275 2818 newline],
adamc@275 2819 string "}",
adamc@275 2820 newline,
adamc@275 2821 newline,
adamc@275 2822 string "PQclear(res);",
adamc@275 2823 newline]
adamc@275 2824 end) tables,
adamc@377 2825
adamc@377 2826 p_list_sep newline
adamc@377 2827 (fn s =>
adamc@377 2828 let
adamc@377 2829 val sl = CharVector.map Char.toLower s
adamc@377 2830
adamc@377 2831 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
adamc@377 2832 ^ sl ^ "' AND relkind = 'S'"
adamc@377 2833 in
adamc@377 2834 box [string "res = PQexec(conn, \"",
adamc@377 2835 string q,
adamc@377 2836 string "\");",
adamc@377 2837 newline,
adamc@377 2838 newline,
adamc@377 2839 string "if (res == NULL) {",
adamc@377 2840 newline,
adamc@377 2841 box [string "PQfinish(conn);",
adamc@377 2842 newline,
adamc@377 2843 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
adamc@377 2844 newline],
adamc@377 2845 string "}",
adamc@377 2846 newline,
adamc@377 2847 newline,
adamc@377 2848 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
adamc@377 2849 newline,
adamc@377 2850 box [string "char msg[1024];",
adamc@377 2851 newline,
adamc@377 2852 string "strncpy(msg, PQerrorMessage(conn), 1024);",
adamc@377 2853 newline,
adamc@377 2854 string "msg[1023] = 0;",
adamc@377 2855 newline,
adamc@377 2856 string "PQclear(res);",
adamc@377 2857 newline,
adamc@377 2858 string "PQfinish(conn);",
adamc@377 2859 newline,
adamc@377 2860 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@377 2861 string q,
adamc@377 2862 string "\\n%s\", msg);",
adamc@377 2863 newline],
adamc@377 2864 string "}",
adamc@377 2865 newline,
adamc@377 2866 newline,
adamc@377 2867 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
adamc@377 2868 newline,
adamc@377 2869 box [string "PQclear(res);",
adamc@377 2870 newline,
adamc@377 2871 string "PQfinish(conn);",
adamc@377 2872 newline,
adamc@377 2873 string "uw_error(ctx, FATAL, \"Sequence '",
adamc@377 2874 string s,
adamc@377 2875 string "' does not exist.\");",
adamc@377 2876 newline],
adamc@377 2877 string "}",
adamc@377 2878 newline,
adamc@377 2879 newline,
adamc@377 2880 string "PQclear(res);",
adamc@377 2881 newline]
adamc@377 2882 end) sequences,
adamc@377 2883
adamc@275 2884 string "}"]
adamc@432 2885
adamc@432 2886 val hasDb = List.exists (fn (DDatabase _, _) => true | _ => false) ds
adamc@734 2887
adamc@734 2888 val cookies = List.mapPartial (fn (DCookie s, _) => SOME s | _ => NONE) ds
adamc@734 2889
adamc@734 2890 val cookieCode = foldl (fn (cookie, acc) =>
adamc@734 2891 SOME (case acc of
adamc@734 2892 NONE => string ("uw_unnull(uw_Basis_get_cookie(ctx, \""
adamc@734 2893 ^ cookie ^ "\"))")
adamc@734 2894 | SOME acc => box [string ("uw_Basis_strcat(ctx, uw_unnull(uw_Basis_get_cookie(ctx, \""
adamc@734 2895 ^ cookie ^ "\")), uw_Basis_strcat(ctx, \"/\", "),
adamc@734 2896 acc,
adamc@734 2897 string "))"]))
adamc@734 2898 NONE cookies
adamc@29 2899 in
adamc@144 2900 box [string "#include <stdio.h>",
adamc@144 2901 newline,
adamc@144 2902 string "#include <stdlib.h>",
adamc@144 2903 newline,
adamc@272 2904 string "#include <string.h>",
adamc@272 2905 newline,
adamc@390 2906 string "#include <math.h>",
adamc@390 2907 newline,
adamc@432 2908 if hasDb then
adamc@432 2909 box [string "#include <postgresql/libpq-fe.h>",
adamc@432 2910 newline]
adamc@432 2911 else
adamc@432 2912 box [],
adamc@144 2913 newline,
adamc@378 2914 string "#include \"",
adamc@378 2915 string (OS.Path.joinDirFile {dir = Config.includ,
adamc@378 2916 file = "urweb.h"}),
adamc@378 2917 string "\"",
adamc@101 2918 newline,
adamc@101 2919 newline,
adamc@101 2920 p_list_sep newline (fn x => x) pds,
adamc@101 2921 newline,
adamc@311 2922 string "int uw_inputs_len = ",
adamc@144 2923 string (Int.toString (SM.foldl Int.max 0 fnums + 1)),
adamc@144 2924 string ";",
adamc@144 2925 newline,
adamc@673 2926 string "int uw_timeout = ",
adamc@673 2927 string (Int.toString (!timeout)),
adamc@673 2928 string ";",
adamc@673 2929 newline,
adamc@144 2930 newline,
adamc@311 2931 string "int uw_input_num(char *name) {",
adamc@144 2932 newline,
adamc@144 2933 makeSwitch (fnums, 0),
adamc@144 2934 string "}",
adamc@144 2935 newline,
adamc@144 2936 newline,
adamc@734 2937
adamc@734 2938 string "extern void uw_sign(const char *in, char *out);",
adamc@734 2939 newline,
adamc@734 2940 string "extern int uw_hash_blocksize;",
adamc@734 2941 newline,
adamc@734 2942 string "uw_Basis_string uw_cookie_sig(uw_context ctx) {",
adamc@734 2943 newline,
adamc@734 2944 box [string "uw_Basis_string r = uw_malloc(ctx, uw_hash_blocksize);",
adamc@734 2945 newline,
adamc@734 2946 string "uw_sign(",
adamc@734 2947 case cookieCode of
adamc@734 2948 NONE => string "\"\""
adamc@734 2949 | SOME code => code,
adamc@734 2950 string ", r);",
adamc@734 2951 newline,
adamc@734 2952 string "return uw_Basis_makeSigString(ctx, r);",
adamc@734 2953 newline],
adamc@734 2954 string "}",
adamc@734 2955 newline,
adamc@734 2956 newline,
adamc@734 2957
adamc@311 2958 string "void uw_handle(uw_context ctx, char *request) {",
adamc@101 2959 newline,
adamc@569 2960 string "if (!strcmp(request, \"/app.js\")) {",
adamc@569 2961 newline,
adamc@569 2962 box [string "uw_write_header(ctx, \"Content-type: text/javascript\\r\\n\");",
adamc@569 2963 newline,
adamc@569 2964 string "uw_write(ctx, jslib);",
adamc@569 2965 newline,
adamc@569 2966 string "return;",
adamc@569 2967 newline],
adamc@569 2968 string "}",
adamc@569 2969 newline,
adamc@101 2970 p_list_sep newline (fn x => x) pds',
adamc@101 2971 newline,
adamc@387 2972 string "uw_error(ctx, FATAL, \"Unknown page\");",
adamc@387 2973 newline,
adamc@101 2974 string "}",
adamc@275 2975 newline,
adamc@275 2976 newline,
adamc@432 2977 if hasDb then
adamc@432 2978 validate
adamc@432 2979 else
adamc@432 2980 box [],
adamc@376 2981 newline,
adamc@376 2982 if List.exists (fn (DDatabase _, _) => true | _ => false) ds then
adamc@376 2983 box []
adamc@376 2984 else
adamc@376 2985 box [newline,
adamc@376 2986 string "void uw_db_init(uw_context ctx) { };",
adamc@424 2987 newline,
adamc@424 2988 string "int uw_db_begin(uw_context ctx) { return 0; };",
adamc@424 2989 newline,
adamc@424 2990 string "int uw_db_commit(uw_context ctx) { return 0; };",
adamc@424 2991 newline,
adamc@424 2992 string "int uw_db_rollback(uw_context ctx) { return 0; };",
adamc@684 2993 newline,
adamc@684 2994 string "void uw_expunger(uw_context ctx, uw_Basis_client cli) { };",
adamc@687 2995 newline,
adamc@687 2996 string "void uw_initializer(uw_context ctx) { };",
adamc@376 2997 newline]]
adamc@29 2998 end
adamc@29 2999
adamc@274 3000 fun p_sql env (ds, _) =
adamc@274 3001 let
adamc@274 3002 val (pps, _) = ListUtil.foldlMap
adamc@274 3003 (fn (dAll as (d, _), env) =>
adamc@274 3004 let
adamc@274 3005 val pp = case d of
adamc@707 3006 DTable (s, xts, pk, csts) =>
adamc@274 3007 box [string "CREATE TABLE ",
adamc@274 3008 string s,
adamc@274 3009 string "(",
adamc@274 3010 p_list (fn (x, t) =>
adamc@311 3011 box [string "uw_",
adamc@275 3012 string (CharVector.map Char.toLower x),
adamc@274 3013 space,
adamc@467 3014 p_sqltype env (t, ErrorMsg.dummySpan)]) xts,
adamc@707 3015 case (pk, csts) of
adamc@707 3016 ("", []) => box []
adamc@707 3017 | _ => string ",",
adamc@704 3018 cut,
adamc@707 3019 case pk of
adamc@707 3020 "" => box []
adamc@707 3021 | _ => box [string "PRIMARY",
adamc@707 3022 space,
adamc@707 3023 string "KEY",
adamc@707 3024 space,
adamc@707 3025 string "(",
adamc@707 3026 string pk,
adamc@707 3027 string ")",
adamc@707 3028 case csts of
adamc@707 3029 [] => box []
adamc@707 3030 | _ => string ",",
adamc@707 3031 newline],
adamc@704 3032 p_list_sep (box [string ",", newline])
adamc@704 3033 (fn (x, c) =>
adamc@704 3034 box [string "CONSTRAINT",
adamc@704 3035 space,
adamc@704 3036 string s,
adamc@704 3037 string "_",
adamc@704 3038 string x,
adamc@704 3039 space,
adamc@704 3040 string c]) csts,
adamc@704 3041 newline,
adamc@274 3042 string ");",
adamc@274 3043 newline,
adamc@274 3044 newline]
adamc@338 3045 | DSequence s =>
adamc@338 3046 box [string "CREATE SEQUENCE ",
adamc@338 3047 string s,
adamc@338 3048 string ";",
adamc@338 3049 newline,
adamc@338 3050 newline]
adamc@274 3051 | _ => box []
adamc@274 3052 in
adamc@274 3053 (pp, E.declBinds env dAll)
adamc@274 3054 end)
adamc@274 3055 env ds
adamc@274 3056 in
adamc@274 3057 box pps
adamc@274 3058 end
adamc@274 3059
adamc@29 3060 end