annotate src/cjr_print.sml @ 747:e42f08f96eb5

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