annotate src/prepare.sml @ 679:44f23712020d

Chat example working nicely, but without dead channel removal
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Mar 2009 18:26:50 -0400
parents 5ff1ff38e2db
children 5bbb542243e8
rev   line source
adamc@282 1 (* Copyright (c) 2008, Adam Chlipala
adamc@282 2 * All rights reserved.
adamc@282 3 *
adamc@282 4 * Redistribution and use in source and binary forms, with or without
adamc@282 5 * modification, are permitted provided that the following conditions are met:
adamc@282 6 *
adamc@282 7 * - Redistributions of source code must retain the above copyright notice,
adamc@282 8 * this list of conditions and the following disclaimer.
adamc@282 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@282 10 * this list of conditions and the following disclaimer in the documentation
adamc@282 11 * and/or other materials provided with the distribution.
adamc@282 12 * - The names of contributors may not be used to endorse or promote products
adamc@282 13 * derived from this software without specific prior written permission.
adamc@282 14 *
adamc@282 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@282 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@282 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@282 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@282 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@282 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@282 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@282 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@282 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@282 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@282 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@282 26 *)
adamc@282 27
adamc@282 28 structure Prepare :> PREPARE = struct
adamc@282 29
adamc@282 30 open Cjr
adamc@282 31
adamc@282 32 fun prepString (e, ss, n) =
adamc@282 33 case #1 e of
adamc@282 34 EPrim (Prim.String s) =>
adamc@282 35 SOME (s :: ss, n)
adamc@282 36 | EFfiApp ("Basis", "strcat", [e1, e2]) =>
adamc@282 37 (case prepString (e1, ss, n) of
adamc@282 38 NONE => NONE
adamc@282 39 | SOME (ss, n) => prepString (e2, ss, n))
adamc@282 40 | EFfiApp ("Basis", "sqlifyInt", [e]) =>
adamc@282 41 SOME ("$" ^ Int.toString (n + 1) ^ "::int8" :: ss, n + 1)
adamc@282 42 | EFfiApp ("Basis", "sqlifyFloat", [e]) =>
adamc@282 43 SOME ("$" ^ Int.toString (n + 1) ^ "::float8" :: ss, n + 1)
adamc@282 44 | EFfiApp ("Basis", "sqlifyString", [e]) =>
adamc@282 45 SOME ("$" ^ Int.toString (n + 1) ^ "::text" :: ss, n + 1)
adamc@282 46 | EFfiApp ("Basis", "sqlifyBool", [e]) =>
adamc@282 47 SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@439 48 | EFfiApp ("Basis", "sqlifyTime", [e]) =>
adamc@439 49 SOME ("$" ^ Int.toString (n + 1) ^ "::timestamp" :: ss, n + 1)
adamc@678 50 | EFfiApp ("Basis", "sqlifyChannel", [e]) =>
adamc@678 51 SOME ("$" ^ Int.toString (n + 1) ^ "::int4" :: ss, n + 1)
adamc@468 52
adamc@678 53 | ECase (e,
adamc@678 54 [((PNone _, _),
adamc@678 55 (EPrim (Prim.String "NULL"), _)),
adamc@678 56 ((PSome (_, (PVar _, _)), _),
adamc@678 57 (EFfiApp (m, x, [(ERel 0, _)]), _))],
adamc@678 58 _) => prepString ((EFfiApp (m, x, [e]), #2 e), ss, n)
adamc@468 59
adamc@322 60 | ECase (e,
adamc@322 61 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@322 62 (EPrim (Prim.String "TRUE"), _)),
adamc@322 63 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@322 64 (EPrim (Prim.String "FALSE"), _))],
adamc@322 65 _) => SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@282 66
adamc@282 67 | _ => NONE
adamc@282 68
adamc@282 69 fun prepExp (e as (_, loc), sns) =
adamc@282 70 case #1 e of
adamc@282 71 EPrim _ => (e, sns)
adamc@282 72 | ERel _ => (e, sns)
adamc@282 73 | ENamed _ => (e, sns)
adamc@282 74 | ECon (_, _, NONE) => (e, sns)
adamc@282 75 | ECon (dk, pc, SOME e) =>
adamc@282 76 let
adamc@282 77 val (e, sns) = prepExp (e, sns)
adamc@282 78 in
adamc@282 79 ((ECon (dk, pc, SOME e), loc), sns)
adamc@282 80 end
adamc@297 81 | ENone t => (e, sns)
adamc@291 82 | ESome (t, e) =>
adamc@291 83 let
adamc@291 84 val (e, sns) = prepExp (e, sns)
adamc@291 85 in
adamc@291 86 ((ESome (t, e), loc), sns)
adamc@291 87 end
adamc@282 88 | EFfi _ => (e, sns)
adamc@282 89 | EFfiApp (m, x, es) =>
adamc@282 90 let
adamc@282 91 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 92 in
adamc@282 93 ((EFfiApp (m, x, es), loc), sns)
adamc@282 94 end
adamc@316 95 | EApp (e1, es) =>
adamc@282 96 let
adamc@282 97 val (e1, sns) = prepExp (e1, sns)
adamc@316 98 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 99 in
adamc@316 100 ((EApp (e1, es), loc), sns)
adamc@282 101 end
adamc@282 102
adamc@387 103 | EUnop (s, e1) =>
adamc@387 104 let
adamc@387 105 val (e1, sns) = prepExp (e1, sns)
adamc@387 106 in
adamc@387 107 ((EUnop (s, e1), loc), sns)
adamc@387 108 end
adamc@387 109 | EBinop (s, e1, e2) =>
adamc@387 110 let
adamc@387 111 val (e1, sns) = prepExp (e1, sns)
adamc@387 112 val (e2, sns) = prepExp (e2, sns)
adamc@387 113 in
adamc@387 114 ((EBinop (s, e1, e2), loc), sns)
adamc@387 115 end
adamc@387 116
adamc@282 117 | ERecord (rn, xes) =>
adamc@282 118 let
adamc@282 119 val (xes, sns) = ListUtil.foldlMap (fn ((x, e), sns) =>
adamc@282 120 let
adamc@282 121 val (e, sns) = prepExp (e, sns)
adamc@282 122 in
adamc@282 123 ((x, e), sns)
adamc@282 124 end) sns xes
adamc@282 125 in
adamc@282 126 ((ERecord (rn, xes), loc), sns)
adamc@282 127 end
adamc@282 128 | EField (e, s) =>
adamc@282 129 let
adamc@282 130 val (e, sns) = prepExp (e, sns)
adamc@282 131 in
adamc@282 132 ((EField (e, s), loc), sns)
adamc@282 133 end
adamc@282 134
adamc@282 135 | ECase (e, pes, ts) =>
adamc@282 136 let
adamc@282 137 val (e, sns) = prepExp (e, sns)
adamc@282 138 val (pes, sns) = ListUtil.foldlMap (fn ((p, e), sns) =>
adamc@282 139 let
adamc@282 140 val (e, sns) = prepExp (e, sns)
adamc@282 141 in
adamc@282 142 ((p, e), sns)
adamc@282 143 end) sns pes
adamc@282 144 in
adamc@282 145 ((ECase (e, pes, ts), loc), sns)
adamc@282 146 end
adamc@282 147
adamc@283 148 | EError (e, t) =>
adamc@283 149 let
adamc@283 150 val (e, sns) = prepExp (e, sns)
adamc@283 151 in
adamc@283 152 ((EError (e, t), loc), sns)
adamc@283 153 end
adamc@283 154
adamc@282 155 | EWrite e =>
adamc@282 156 let
adamc@282 157 val (e, sns) = prepExp (e, sns)
adamc@282 158 in
adamc@282 159 ((EWrite e, loc), sns)
adamc@282 160 end
adamc@282 161 | ESeq (e1, e2) =>
adamc@282 162 let
adamc@282 163 val (e1, sns) = prepExp (e1, sns)
adamc@282 164 val (e2, sns) = prepExp (e2, sns)
adamc@282 165 in
adamc@282 166 ((ESeq (e1, e2), loc), sns)
adamc@282 167 end
adamc@282 168 | ELet (x, t, e1, e2) =>
adamc@282 169 let
adamc@282 170 val (e1, sns) = prepExp (e1, sns)
adamc@282 171 val (e2, sns) = prepExp (e2, sns)
adamc@282 172 in
adamc@282 173 ((ELet (x, t, e1, e2), loc), sns)
adamc@282 174 end
adamc@282 175
adamc@282 176 | EQuery {exps, tables, rnum, state, query, body, initial, ...} =>
adamc@486 177 let
adamc@486 178 val (body, sns) = prepExp (body, sns)
adamc@486 179 in
adamc@486 180 case prepString (query, [], 0) of
adamc@486 181 NONE =>
adamc@486 182 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 183 state = state, query = query, body = body,
adamc@491 184 initial = initial, prepared = NONE}, loc),
adamc@486 185 sns)
adamc@486 186 | SOME (ss, n) =>
adamc@486 187 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 188 state = state, query = query, body = body,
adamc@486 189 initial = initial, prepared = SOME (#2 sns)}, loc),
adamc@486 190 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@486 191 end
adamc@282 192
adamc@307 193 | EDml {dml, ...} =>
adamc@307 194 (case prepString (dml, [], 0) of
adamc@307 195 NONE => (e, sns)
adamc@307 196 | SOME (ss, n) =>
adamc@307 197 ((EDml {dml = dml, prepared = SOME (#2 sns)}, loc),
adamc@307 198 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1)))
adamc@307 199
adamc@338 200 | ENextval {seq, ...} =>
adamc@338 201 let
adamc@486 202 val s = case seq of
adamc@486 203 (EPrim (Prim.String s), loc) =>
adamc@486 204 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 205 | _ =>
adamc@486 206 let
adamc@486 207 val s' = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 208 in
adamc@486 209 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), s']), loc)
adamc@486 210 end
adamc@338 211 in
adamc@338 212 case prepString (s, [], 0) of
adamc@338 213 NONE => (e, sns)
adamc@338 214 | SOME (ss, n) =>
adamc@338 215 ((ENextval {seq = seq, prepared = SOME (#2 sns)}, loc),
adamc@338 216 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@338 217 end
adamc@338 218
adamc@463 219 | EUnurlify (e, t) =>
adamc@463 220 let
adamc@463 221 val (e, sns) = prepExp (e, sns)
adamc@463 222 in
adamc@463 223 ((EUnurlify (e, t), loc), sns)
adamc@463 224 end
adamc@463 225
adamc@282 226 fun prepDecl (d as (_, loc), sns) =
adamc@282 227 case #1 d of
adamc@282 228 DStruct _ => (d, sns)
adamc@282 229 | DDatatype _ => (d, sns)
adamc@282 230 | DDatatypeForward _ => (d, sns)
adamc@282 231 | DVal (x, n, t, e) =>
adamc@282 232 let
adamc@282 233 val (e, sns) = prepExp (e, sns)
adamc@282 234 in
adamc@282 235 ((DVal (x, n, t, e), loc), sns)
adamc@282 236 end
adamc@282 237 | DFun (x, n, xts, t, e) =>
adamc@282 238 let
adamc@282 239 val (e, sns) = prepExp (e, sns)
adamc@282 240 in
adamc@282 241 ((DFun (x, n, xts, t, e), loc), sns)
adamc@282 242 end
adamc@282 243 | DFunRec fs =>
adamc@282 244 let
adamc@282 245 val (fs, sns) = ListUtil.foldlMap (fn ((x, n, xts, t, e), sns) =>
adamc@282 246 let
adamc@282 247 val (e, sns) = prepExp (e, sns)
adamc@282 248 in
adamc@282 249 ((x, n, xts, t, e), sns)
adamc@282 250 end) sns fs
adamc@282 251 in
adamc@282 252 ((DFunRec fs, loc), sns)
adamc@282 253 end
adamc@282 254
adamc@282 255 | DTable _ => (d, sns)
adamc@338 256 | DSequence _ => (d, sns)
adamc@282 257 | DDatabase _ => (d, sns)
adamc@282 258 | DPreparedStatements _ => (d, sns)
adamc@569 259 | DJavaScript _ => (d, sns)
adamc@282 260
adamc@282 261 fun prepare (ds, ps) =
adamc@282 262 let
adamc@282 263 val (ds, (sns, _)) = ListUtil.foldlMap prepDecl ([], 0) ds
adamc@282 264 in
adamc@282 265 ((DPreparedStatements (rev sns), ErrorMsg.dummySpan) :: ds, ps)
adamc@282 266 end
adamc@282 267
adamc@282 268 end
adamc@282 269