annotate src/prepare.sml @ 754:8688e01ae469

A view query works
author Adam Chlipala <adamc@hcoop.net>
date Tue, 28 Apr 2009 15:04:37 -0400
parents f7e2026dd5ae
children 346cf1908a17
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@738 50 | EFfiApp ("Basis", "sqlifyBlob", [e]) =>
adamc@738 51 SOME ("$" ^ Int.toString (n + 1) ^ "::bytea" :: ss, n + 1)
adamc@678 52 | EFfiApp ("Basis", "sqlifyChannel", [e]) =>
adamc@682 53 SOME ("$" ^ Int.toString (n + 1) ^ "::int8" :: ss, n + 1)
adamc@682 54 | EFfiApp ("Basis", "sqlifyClient", [e]) =>
adamc@678 55 SOME ("$" ^ Int.toString (n + 1) ^ "::int4" :: ss, n + 1)
adamc@468 56
adamc@678 57 | ECase (e,
adamc@678 58 [((PNone _, _),
adamc@678 59 (EPrim (Prim.String "NULL"), _)),
adamc@678 60 ((PSome (_, (PVar _, _)), _),
adamc@678 61 (EFfiApp (m, x, [(ERel 0, _)]), _))],
adamc@678 62 _) => prepString ((EFfiApp (m, x, [e]), #2 e), ss, n)
adamc@468 63
adamc@322 64 | ECase (e,
adamc@322 65 [((PCon (_, PConFfi {mod = "Basis", con = "True", ...}, _), _),
adamc@322 66 (EPrim (Prim.String "TRUE"), _)),
adamc@322 67 ((PCon (_, PConFfi {mod = "Basis", con = "False", ...}, _), _),
adamc@322 68 (EPrim (Prim.String "FALSE"), _))],
adamc@322 69 _) => SOME ("$" ^ Int.toString (n + 1) ^ "::bool" :: ss, n + 1)
adamc@282 70
adamc@282 71 | _ => NONE
adamc@282 72
adamc@282 73 fun prepExp (e as (_, loc), sns) =
adamc@282 74 case #1 e of
adamc@282 75 EPrim _ => (e, sns)
adamc@282 76 | ERel _ => (e, sns)
adamc@282 77 | ENamed _ => (e, sns)
adamc@282 78 | ECon (_, _, NONE) => (e, sns)
adamc@282 79 | ECon (dk, pc, SOME e) =>
adamc@282 80 let
adamc@282 81 val (e, sns) = prepExp (e, sns)
adamc@282 82 in
adamc@282 83 ((ECon (dk, pc, SOME e), loc), sns)
adamc@282 84 end
adamc@297 85 | ENone t => (e, sns)
adamc@291 86 | ESome (t, e) =>
adamc@291 87 let
adamc@291 88 val (e, sns) = prepExp (e, sns)
adamc@291 89 in
adamc@291 90 ((ESome (t, e), loc), sns)
adamc@291 91 end
adamc@282 92 | EFfi _ => (e, sns)
adamc@282 93 | EFfiApp (m, x, es) =>
adamc@282 94 let
adamc@282 95 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 96 in
adamc@282 97 ((EFfiApp (m, x, es), loc), sns)
adamc@282 98 end
adamc@316 99 | EApp (e1, es) =>
adamc@282 100 let
adamc@282 101 val (e1, sns) = prepExp (e1, sns)
adamc@316 102 val (es, sns) = ListUtil.foldlMap prepExp sns es
adamc@282 103 in
adamc@316 104 ((EApp (e1, es), loc), sns)
adamc@282 105 end
adamc@282 106
adamc@387 107 | EUnop (s, e1) =>
adamc@387 108 let
adamc@387 109 val (e1, sns) = prepExp (e1, sns)
adamc@387 110 in
adamc@387 111 ((EUnop (s, e1), loc), sns)
adamc@387 112 end
adamc@387 113 | EBinop (s, e1, e2) =>
adamc@387 114 let
adamc@387 115 val (e1, sns) = prepExp (e1, sns)
adamc@387 116 val (e2, sns) = prepExp (e2, sns)
adamc@387 117 in
adamc@387 118 ((EBinop (s, e1, e2), loc), sns)
adamc@387 119 end
adamc@387 120
adamc@282 121 | ERecord (rn, xes) =>
adamc@282 122 let
adamc@282 123 val (xes, sns) = ListUtil.foldlMap (fn ((x, e), sns) =>
adamc@282 124 let
adamc@282 125 val (e, sns) = prepExp (e, sns)
adamc@282 126 in
adamc@282 127 ((x, e), sns)
adamc@282 128 end) sns xes
adamc@282 129 in
adamc@282 130 ((ERecord (rn, xes), loc), sns)
adamc@282 131 end
adamc@282 132 | EField (e, s) =>
adamc@282 133 let
adamc@282 134 val (e, sns) = prepExp (e, sns)
adamc@282 135 in
adamc@282 136 ((EField (e, s), loc), sns)
adamc@282 137 end
adamc@282 138
adamc@282 139 | ECase (e, pes, ts) =>
adamc@282 140 let
adamc@282 141 val (e, sns) = prepExp (e, sns)
adamc@282 142 val (pes, sns) = ListUtil.foldlMap (fn ((p, e), sns) =>
adamc@282 143 let
adamc@282 144 val (e, sns) = prepExp (e, sns)
adamc@282 145 in
adamc@282 146 ((p, e), sns)
adamc@282 147 end) sns pes
adamc@282 148 in
adamc@282 149 ((ECase (e, pes, ts), loc), sns)
adamc@282 150 end
adamc@282 151
adamc@283 152 | EError (e, t) =>
adamc@283 153 let
adamc@283 154 val (e, sns) = prepExp (e, sns)
adamc@283 155 in
adamc@283 156 ((EError (e, t), loc), sns)
adamc@283 157 end
adamc@283 158
adamc@741 159 | EReturnBlob {blob, mimeType, t} =>
adamc@741 160 let
adamc@741 161 val (blob, sns) = prepExp (blob, sns)
adamc@741 162 val (mimeType, sns) = prepExp (mimeType, sns)
adamc@741 163 in
adamc@741 164 ((EReturnBlob {blob = blob, mimeType = mimeType, t = t}, loc), sns)
adamc@741 165 end
adamc@741 166
adamc@282 167 | EWrite e =>
adamc@282 168 let
adamc@282 169 val (e, sns) = prepExp (e, sns)
adamc@282 170 in
adamc@282 171 ((EWrite e, loc), sns)
adamc@282 172 end
adamc@282 173 | ESeq (e1, e2) =>
adamc@282 174 let
adamc@282 175 val (e1, sns) = prepExp (e1, sns)
adamc@282 176 val (e2, sns) = prepExp (e2, sns)
adamc@282 177 in
adamc@282 178 ((ESeq (e1, e2), loc), sns)
adamc@282 179 end
adamc@282 180 | ELet (x, t, e1, e2) =>
adamc@282 181 let
adamc@282 182 val (e1, sns) = prepExp (e1, sns)
adamc@282 183 val (e2, sns) = prepExp (e2, sns)
adamc@282 184 in
adamc@282 185 ((ELet (x, t, e1, e2), loc), sns)
adamc@282 186 end
adamc@282 187
adamc@282 188 | EQuery {exps, tables, rnum, state, query, body, initial, ...} =>
adamc@486 189 let
adamc@486 190 val (body, sns) = prepExp (body, sns)
adamc@486 191 in
adamc@486 192 case prepString (query, [], 0) of
adamc@486 193 NONE =>
adamc@486 194 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 195 state = state, query = query, body = body,
adamc@491 196 initial = initial, prepared = NONE}, loc),
adamc@486 197 sns)
adamc@486 198 | SOME (ss, n) =>
adamc@486 199 ((EQuery {exps = exps, tables = tables, rnum = rnum,
adamc@486 200 state = state, query = query, body = body,
adamc@486 201 initial = initial, prepared = SOME (#2 sns)}, loc),
adamc@486 202 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@486 203 end
adamc@282 204
adamc@307 205 | EDml {dml, ...} =>
adamc@307 206 (case prepString (dml, [], 0) of
adamc@307 207 NONE => (e, sns)
adamc@307 208 | SOME (ss, n) =>
adamc@307 209 ((EDml {dml = dml, prepared = SOME (#2 sns)}, loc),
adamc@307 210 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1)))
adamc@307 211
adamc@338 212 | ENextval {seq, ...} =>
adamc@338 213 let
adamc@486 214 val s = case seq of
adamc@486 215 (EPrim (Prim.String s), loc) =>
adamc@486 216 (EPrim (Prim.String ("SELECT NEXTVAL('" ^ s ^ "')")), loc)
adamc@486 217 | _ =>
adamc@486 218 let
adamc@486 219 val s' = (EFfiApp ("Basis", "strcat", [seq, (EPrim (Prim.String "')"), loc)]), loc)
adamc@486 220 in
adamc@486 221 (EFfiApp ("Basis", "strcat", [(EPrim (Prim.String "SELECT NEXTVAL('"), loc), s']), loc)
adamc@486 222 end
adamc@338 223 in
adamc@338 224 case prepString (s, [], 0) of
adamc@338 225 NONE => (e, sns)
adamc@338 226 | SOME (ss, n) =>
adamc@338 227 ((ENextval {seq = seq, prepared = SOME (#2 sns)}, loc),
adamc@338 228 ((String.concat (rev ss), n) :: #1 sns, #2 sns + 1))
adamc@338 229 end
adamc@338 230
adamc@463 231 | EUnurlify (e, t) =>
adamc@463 232 let
adamc@463 233 val (e, sns) = prepExp (e, sns)
adamc@463 234 in
adamc@463 235 ((EUnurlify (e, t), loc), sns)
adamc@463 236 end
adamc@463 237
adamc@282 238 fun prepDecl (d as (_, loc), sns) =
adamc@282 239 case #1 d of
adamc@282 240 DStruct _ => (d, sns)
adamc@282 241 | DDatatype _ => (d, sns)
adamc@282 242 | DDatatypeForward _ => (d, sns)
adamc@282 243 | DVal (x, n, t, e) =>
adamc@282 244 let
adamc@282 245 val (e, sns) = prepExp (e, sns)
adamc@282 246 in
adamc@282 247 ((DVal (x, n, t, e), loc), sns)
adamc@282 248 end
adamc@282 249 | DFun (x, n, xts, t, e) =>
adamc@282 250 let
adamc@282 251 val (e, sns) = prepExp (e, sns)
adamc@282 252 in
adamc@282 253 ((DFun (x, n, xts, t, e), loc), sns)
adamc@282 254 end
adamc@282 255 | DFunRec fs =>
adamc@282 256 let
adamc@282 257 val (fs, sns) = ListUtil.foldlMap (fn ((x, n, xts, t, e), sns) =>
adamc@282 258 let
adamc@282 259 val (e, sns) = prepExp (e, sns)
adamc@282 260 in
adamc@282 261 ((x, n, xts, t, e), sns)
adamc@282 262 end) sns fs
adamc@282 263 in
adamc@282 264 ((DFunRec fs, loc), sns)
adamc@282 265 end
adamc@282 266
adamc@282 267 | DTable _ => (d, sns)
adamc@338 268 | DSequence _ => (d, sns)
adamc@754 269 | DView _ => (d, sns)
adamc@282 270 | DDatabase _ => (d, sns)
adamc@282 271 | DPreparedStatements _ => (d, sns)
adamc@569 272 | DJavaScript _ => (d, sns)
adamc@725 273 | DCookie _ => (d, sns)
adamc@718 274 | DStyle _ => (d, sns)
adamc@282 275
adamc@282 276 fun prepare (ds, ps) =
adamc@282 277 let
adamc@282 278 val (ds, (sns, _)) = ListUtil.foldlMap prepDecl ([], 0) ds
adamc@282 279 in
adamc@282 280 ((DPreparedStatements (rev sns), ErrorMsg.dummySpan) :: ds, ps)
adamc@282 281 end
adamc@282 282
adamc@282 283 end
adamc@282 284