annotate src/cjr_print.sml @ 469:b393c2fc80f8

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