annotate demo/more/dbgrid.ur @ 944:da3ec6014d2f

Filters implementation type-checking
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 15:48:53 -0400
parents 37dd42935dad
children 2412cb10c97c
rev   line source
adamc@915 1 con rawMeta = fn t :: Type =>
adamc@915 2 {New : transaction t,
adamc@915 3 Inj : sql_injectable t}
adamc@915 4
adamc@944 5 con colMeta' = fn (row :: {Type}) (input :: Type) (filter :: Type) =>
adamc@915 6 {Header : string,
adamc@944 7 Project : $row -> transaction input,
adamc@944 8 Update : $row -> input -> transaction ($row),
adamc@944 9 Display : input -> xbody,
adamc@944 10 Edit : input -> xbody,
adamc@944 11 Validate : input -> signal bool,
adamc@944 12 CreateFilter : transaction filter,
adamc@944 13 DisplayFilter : filter -> xbody,
adamc@944 14 Filter : filter -> $row -> signal bool}
adamc@915 15
adamc@944 16 con colMeta = fn (row :: {Type}) (global_input_filter :: (Type * Type * Type)) =>
adamc@944 17 {Initialize : transaction global_input_filter.1,
adamc@944 18 Handlers : global_input_filter.1 -> colMeta' row global_input_filter.2 global_input_filter.3}
adamc@915 19
adamc@935 20 con aggregateMeta = fn (row :: {Type}) (acc :: Type) =>
adamc@935 21 {Initial : acc,
adamc@935 22 Step : $row -> acc -> acc,
adamc@935 23 Display : acc -> xbody}
adamc@935 24
adamc@915 25 structure Direct = struct
adamc@944 26 con metaBase = fn actual_input_filter :: (Type * Type * Type) =>
adamc@944 27 {Display : actual_input_filter.2 -> xbody,
adamc@944 28 Edit : actual_input_filter.2 -> xbody,
adamc@944 29 Initialize : actual_input_filter.1 -> transaction actual_input_filter.2,
adamc@944 30 Parse : actual_input_filter.2 -> signal (option actual_input_filter.1),
adamc@944 31 CreateFilter : transaction actual_input_filter.3,
adamc@944 32 DisplayFilter : actual_input_filter.3 -> xbody,
adamc@944 33 Filter : actual_input_filter.3 -> actual_input_filter.1 -> signal bool}
adamc@930 34
adamc@944 35 datatype metaBoth actual input filter =
adamc@944 36 NonNull of metaBase (actual, input, filter) * metaBase (option actual, input, filter)
adamc@944 37 | Nullable of metaBase (actual, input, filter)
adamc@930 38
adamc@944 39 con meta = fn global_actual_input_filter :: (Type * Type * Type * Type) =>
adamc@944 40 {Initialize : transaction global_actual_input_filter.1,
adamc@944 41 Handlers : global_actual_input_filter.1
adamc@944 42 -> metaBoth global_actual_input_filter.2 global_actual_input_filter.3
adamc@944 43 global_actual_input_filter.4}
adamc@915 44
adamc@944 45 con editableState (ts :: (Type * Type * Type * Type)) = (ts.1, ts.3, ts.4)
adamc@915 46 fun editable [ts] [rest] [nm :: Name] [[nm] ~ rest] name (m : meta ts) : colMeta ([nm = ts.2] ++ rest)
adamc@915 47 (editableState ts) =
adamc@930 48 let
adamc@930 49 fun doMr mr = {Header = name,
adamc@930 50 Project = fn r => mr.Initialize r.nm,
adamc@930 51 Update = fn r s =>
adamc@930 52 vo <- current (mr.Parse s);
adamc@930 53 return (case vo of
adamc@930 54 None => r
adamc@930 55 | Some v => r -- nm ++ {nm = v}),
adamc@930 56 Display = mr.Display,
adamc@930 57 Edit = mr.Edit,
adamc@944 58 Validate = fn s => vo <- mr.Parse s; return (Option.isSome vo),
adamc@944 59 CreateFilter = mr.CreateFilter,
adamc@944 60 DisplayFilter = mr.DisplayFilter,
adamc@944 61 Filter = fn i r => mr.Filter i r.nm}
adamc@930 62 in
adamc@930 63 {Initialize = m.Initialize,
adamc@930 64 Handlers = fn data => case m.Handlers data of
adamc@930 65 NonNull (mr, _) => doMr mr
adamc@930 66 | Nullable mr => doMr mr}
adamc@930 67 end
adamc@915 68
adamc@944 69 con readOnlyState (ts :: (Type * Type * Type * Type)) = (ts.1, ts.3, ts.4)
adamc@915 70 fun readOnly [ts] [rest] [nm :: Name] [[nm] ~ rest] name (m : meta ts) : colMeta ([nm = ts.2] ++ rest)
adamc@915 71 (readOnlyState ts) =
adamc@930 72 let
adamc@930 73 fun doMr mr = {Header = name,
adamc@930 74 Project = fn r => mr.Initialize r.nm,
adamc@930 75 Update = fn r _ => return r,
adamc@930 76 Display = mr.Display,
adamc@930 77 Edit = mr.Display,
adamc@944 78 Validate = fn _ => return True,
adamc@944 79 CreateFilter = mr.CreateFilter,
adamc@944 80 DisplayFilter = mr.DisplayFilter,
adamc@944 81 Filter = fn i r => mr.Filter i r.nm}
adamc@930 82 in
adamc@930 83 {Initialize = m.Initialize,
adamc@930 84 Handlers = fn data => case m.Handlers data of
adamc@930 85 NonNull (mr, _) => doMr mr
adamc@930 86 | Nullable mr => doMr mr}
adamc@930 87 end
adamc@915 88
adamc@944 89 con metaBasic = fn actual_input_filter :: (Type * Type * Type) =>
adamc@944 90 {Display : actual_input_filter.2 -> xbody,
adamc@944 91 Edit : source actual_input_filter.2 -> xbody,
adamc@944 92 Initialize : actual_input_filter.1 -> actual_input_filter.2,
adamc@944 93 InitializeNull : actual_input_filter.2,
adamc@944 94 IsNull : actual_input_filter.2 -> bool,
adamc@944 95 Parse : actual_input_filter.2 -> option actual_input_filter.1,
adamc@944 96 CreateFilter : actual_input_filter.3,
adamc@944 97 DisplayFilter : source actual_input_filter.3 -> xbody,
adamc@944 98 Filter : actual_input_filter.3 -> actual_input_filter.1 -> bool,
adamc@944 99 FilterIsNull : actual_input_filter.3 -> bool}
adamc@915 100
adamc@915 101 con basicState = source
adamc@944 102 con basicFilter = source
adamc@944 103 fun basic [ts ::: (Type * Type * Type)] (m : metaBasic ts) : meta (unit, ts.1, basicState ts.2, basicFilter ts.3) =
adamc@915 104 {Initialize = return (),
adamc@930 105 Handlers = fn () => NonNull (
adamc@930 106 {Display = fn s => <xml><dyn signal={v <- signal s; return (m.Display v)}/></xml>,
adamc@915 107 Edit = m.Edit,
adamc@915 108 Initialize = fn v => source (m.Initialize v),
adamc@944 109 Parse = fn s => v <- signal s; return (m.Parse v),
adamc@944 110 CreateFilter = source m.CreateFilter,
adamc@944 111 DisplayFilter = m.DisplayFilter,
adamc@944 112 Filter = fn f v => f <- signal f; return (m.Filter f v)},
adamc@930 113 {Display = fn s => <xml><dyn signal={v <- signal s; return (m.Display v)}/></xml>,
adamc@930 114 Edit = m.Edit,
adamc@930 115 Initialize = fn v => source (case v of
adamc@930 116 None => m.InitializeNull
adamc@930 117 | Some v => m.Initialize v),
adamc@930 118 Parse = fn s => v <- signal s;
adamc@930 119 return (if m.IsNull v then
adamc@930 120 Some None
adamc@930 121 else
adamc@930 122 case m.Parse v of
adamc@930 123 None => None
adamc@944 124 | Some v' => Some (Some v')),
adamc@944 125 CreateFilter = source m.CreateFilter,
adamc@944 126 DisplayFilter = m.DisplayFilter,
adamc@944 127 Filter = fn f v => f <- signal f;
adamc@944 128 return (if m.FilterIsNull f then
adamc@944 129 Option.isNone v
adamc@944 130 else
adamc@944 131 case v of
adamc@944 132 None => False
adamc@944 133 | Some v => m.Filter f v) : signal bool})}
adamc@930 134
adamc@944 135 fun nullable [global] [actual] [input] [filter] (m : meta (global, actual, input, filter)) =
adamc@930 136 {Initialize = m.Initialize,
adamc@930 137 Handlers = fn d => case m.Handlers d of
adamc@930 138 Nullable _ => error <xml>Don't stack calls to Direct.nullable!</xml>
adamc@930 139 | NonNull (_, ho) => Nullable ho}
adamc@915 140
adamc@915 141 type intGlobal = unit
adamc@915 142 type intInput = basicState string
adamc@944 143 type intFilter = basicFilter string
adamc@944 144 val int : meta (intGlobal, int, intInput, intFilter) =
adamc@915 145 basic {Display = fn s => <xml>{[s]}</xml>,
adamc@915 146 Edit = fn s => <xml><ctextbox source={s}/></xml>,
adamc@915 147 Initialize = fn n => show n,
adamc@930 148 InitializeNull = "",
adamc@930 149 IsNull = eq "",
adamc@944 150 Parse = fn v => read v,
adamc@944 151 CreateFilter = "",
adamc@944 152 DisplayFilter = fn s => <xml><ctextbox source={s}/></xml> : xbody,
adamc@944 153 Filter = fn s n =>
adamc@944 154 case read s of
adamc@944 155 None => True
adamc@944 156 | Some n' => n' = n,
adamc@944 157 FilterIsNull = eq ""}
adamc@915 158
adamc@915 159 type stringGlobal = unit
adamc@915 160 type stringInput = basicState string
adamc@944 161 type stringFilter = basicFilter string
adamc@944 162 val string : meta (stringGlobal, string, stringInput, stringFilter) =
adamc@915 163 basic {Display = fn s => <xml>{[s]}</xml>,
adamc@915 164 Edit = fn s => <xml><ctextbox source={s}/></xml>,
adamc@915 165 Initialize = fn s => s,
adamc@930 166 InitializeNull = "",
adamc@930 167 IsNull = eq "",
adamc@944 168 Parse = fn s => Some s,
adamc@944 169 CreateFilter = "",
adamc@944 170 DisplayFilter = fn s => <xml><ctextbox source={s}/></xml> : xbody,
adamc@944 171 Filter = fn s n =>
adamc@944 172 case read s of
adamc@944 173 None => True
adamc@944 174 | Some n' => n' = n,
adamc@944 175 FilterIsNull = eq ""}
adamc@915 176
adamc@915 177 type boolGlobal = unit
adamc@915 178 type boolInput = basicState bool
adamc@944 179 type boolFilter = basicFilter string
adamc@944 180 val bool : meta (boolGlobal, bool, boolInput, boolFilter) =
adamc@915 181 basic {Display = fn b => <xml>{[b]}</xml>,
adamc@915 182 Edit = fn s => <xml><ccheckbox source={s}/></xml>,
adamc@915 183 Initialize = fn b => b,
adamc@930 184 InitializeNull = False,
adamc@930 185 IsNull = fn _ => False,
adamc@944 186 Parse = fn b => Some b,
adamc@944 187 CreateFilter = "",
adamc@944 188 DisplayFilter = fn s => <xml><cselect source={s}>
adamc@944 189 <coption/>
adamc@944 190 <coption value="0">False</coption>
adamc@944 191 <coption value="1">True</coption>
adamc@944 192 </cselect></xml> : xbody,
adamc@944 193 Filter = fn s b =>
adamc@944 194 case s of
adamc@944 195 "0" => b = False
adamc@944 196 | "1" => b = True
adamc@944 197 | _ => True,
adamc@944 198 FilterIsNull = eq ""}
adamc@915 199
adamc@915 200 functor Foreign (M : sig
adamc@915 201 con row :: {Type}
adamc@915 202 con t :: Type
adamc@915 203 val show_t : show t
adamc@915 204 val read_t : read t
adamc@915 205 val eq_t : eq t
adamc@915 206 val inj_t : sql_injectable t
adamc@915 207 con nm :: Name
adamc@915 208 constraint [nm] ~ row
adamc@915 209 table tab : ([nm = t] ++ row)
adamc@915 210 val render : $([nm = t] ++ row) -> string
adamc@915 211 end) = struct
adamc@915 212 open M
adamc@915 213
adamc@944 214 type global = list (t * string)
adamc@944 215 type input = source string * option (t * $row)
adamc@944 216 type filter = source string
adamc@915 217
adamc@915 218 val getChoices = List.mapQuery (SELECT * FROM tab AS T)
adamc@915 219 (fn r => (r.T.nm, render r.T))
adamc@915 220
adamc@915 221 fun getChoice k =
adamc@915 222 r <- oneRow (SELECT T.{{row}} FROM tab AS T WHERE T.{nm} = {[k]});
adamc@915 223 return r.T
adamc@915 224
adamc@944 225 val meta : meta (global, M.t, input, filter) =
adamc@915 226 {Initialize = getChoices,
adamc@915 227 Handlers = fn choices =>
adamc@930 228 NonNull (
adamc@930 229 {Display = fn (_, kr) => case kr of
adamc@930 230 None => error <xml>Unexpected Foreign null</xml>
adamc@930 231 | Some (k, r) => <xml>{[render ({nm = k} ++ r)]}</xml>,
adamc@930 232 Edit = fn (s, kr) =>
adamc@915 233 <xml><cselect source={s}>
adamc@915 234 {List.mapX (fn (k', rend) =>
adamc@930 235 <xml><coption value={show k'} selected={case kr of
adamc@930 236 None => False
adamc@930 237 | Some (k, _) =>
adamc@930 238 k' = k}>{[rend]}</coption>
adamc@915 239 </xml>)
adamc@915 240 choices}
adamc@915 241 </cselect></xml>,
adamc@915 242 Initialize = fn k => s <- source (show k);
adamc@915 243 r <- rpc (getChoice k);
adamc@930 244 return (s, Some (k, r)),
adamc@944 245 Parse = fn (s, _) => k <- signal s; return (read k : option t),
adamc@944 246 CreateFilter = source "",
adamc@944 247 DisplayFilter = fn s =>
adamc@944 248 <xml><cselect source={s}>
adamc@944 249 <coption/>
adamc@944 250 {List.mapX (fn (k, rend) =>
adamc@944 251 <xml><coption value={show k}>{[rend]}</coption></xml>)
adamc@944 252 choices}
adamc@944 253 </cselect></xml> : xbody,
adamc@944 254 Filter = fn s k => s <- signal s;
adamc@944 255 return (case read s : option t of
adamc@944 256 None => True
adamc@944 257 | Some k' => k' = k)},
adamc@930 258 {Display = fn (_, kr) => case kr of
adamc@930 259 None => <xml>NULL</xml>
adamc@930 260 | Some (k, r) => <xml>{[render ({nm = k} ++ r)]}</xml>,
adamc@930 261 Edit = fn (s, kr) =>
adamc@930 262 <xml><cselect source={s}>
adamc@930 263 <coption value="" selected={case kr of
adamc@930 264 None => True
adamc@930 265 | _ => False}>NULL</coption>
adamc@930 266 {List.mapX (fn (k', rend) =>
adamc@930 267 <xml><coption value={show k'} selected={case kr of
adamc@930 268 None => False
adamc@930 269 | Some (k, _) =>
adamc@930 270 k' = k}>{[rend]}</coption>
adamc@930 271 </xml>)
adamc@930 272 choices}
adamc@930 273 </cselect></xml>,
adamc@930 274 Initialize = fn k => case k of
adamc@930 275 None =>
adamc@930 276 s <- source "";
adamc@930 277 return (s, None)
adamc@930 278 | Some k =>
adamc@930 279 s <- source (show k);
adamc@930 280 r <- rpc (getChoice k);
adamc@930 281 return (s, Some (k, r)),
adamc@930 282 Parse = fn (s, _) => ks <- signal s;
adamc@930 283 return (case ks of
adamc@930 284 "" => Some None
adamc@930 285 | _ => case read ks : option t of
adamc@930 286 None => None
adamc@944 287 | Some k => Some (Some k)),
adamc@944 288 CreateFilter = source "",
adamc@944 289 DisplayFilter = fn s =>
adamc@944 290 <xml><cselect source={s}>
adamc@944 291 <coption/>
adamc@944 292 <coption value="0">NULL</coption>
adamc@944 293 {List.mapX (fn (k, rend) =>
adamc@944 294 <xml><coption value={"1" ^ show k}>{[rend]}</coption>
adamc@944 295 </xml>)
adamc@944 296 choices}
adamc@944 297 </cselect></xml> : xbody,
adamc@944 298 Filter = fn s ko => s <- signal s;
adamc@944 299 return (case s of
adamc@944 300 "" => True
adamc@944 301 | "0" => ko = None
adamc@944 302 | _ =>
adamc@944 303 case read (String.substring s {Start = 1,
adamc@944 304 Len = String.length s - 1})
adamc@944 305 : option t of
adamc@944 306 None => True
adamc@944 307 | Some k => ko = Some k)})}
adamc@915 308 end
adamc@915 309 end
adamc@915 310
adamc@944 311 con computedState = (unit, xbody, unit)
adamc@915 312 fun computed [row] [t] (_ : show t) name (f : $row -> t) : colMeta row computedState =
adamc@915 313 {Initialize = return (),
adamc@915 314 Handlers = fn () => {Header = name,
adamc@915 315 Project = fn r => return <xml>{[f r]}</xml>,
adamc@915 316 Update = fn r _ => return r,
adamc@915 317 Display = fn x => x,
adamc@915 318 Edit = fn _ => <xml>...</xml>,
adamc@944 319 Validate = fn _ => return True,
adamc@944 320 CreateFilter = return (),
adamc@944 321 DisplayFilter = fn _ => <xml/>,
adamc@944 322 Filter = fn _ _ => return True}}
adamc@915 323 fun computedHtml [row] name (f : $row -> xbody) : colMeta row computedState =
adamc@915 324 {Initialize = return (),
adamc@915 325 Handlers = fn () => {Header = name,
adamc@915 326 Project = fn r => return (f r),
adamc@915 327 Update = fn r _ => return r,
adamc@915 328 Display = fn x => x,
adamc@915 329 Edit = fn _ => <xml>...</xml>,
adamc@944 330 Validate = fn _ => return True,
adamc@944 331 CreateFilter = return (),
adamc@944 332 DisplayFilter = fn _ => <xml/>,
adamc@944 333 Filter = fn _ _ => return True}}
adamc@915 334
adamc@915 335 functor Make(M : sig
adamc@915 336 con key :: {Type}
adamc@915 337 con row :: {Type}
adamc@915 338 constraint key ~ row
adamc@915 339 table tab : (key ++ row)
adamc@915 340
adamc@915 341 val raw : $(map rawMeta (key ++ row))
adamc@915 342
adamc@944 343 con cols :: {(Type * Type * Type)}
adamc@915 344 val cols : $(map (colMeta (key ++ row)) cols)
adamc@915 345
adamc@915 346 val keyFolder : folder key
adamc@915 347 val rowFolder : folder row
adamc@915 348 val colsFolder : folder cols
adamc@935 349
adamc@935 350 con aggregates :: {Type}
adamc@935 351 val aggregates : $(map (aggregateMeta (key ++ row)) aggregates)
adamc@937 352 val aggFolder : folder aggregates
adamc@915 353 end) = struct
adamc@915 354 open Grid.Make(struct
adamc@936 355 fun keyOf r = r --- M.row
adamc@936 356
adamc@915 357 val list = query (SELECT * FROM {{M.tab}} AS T) (fn r rs => return (r.T :: rs)) []
adamc@915 358
adamc@915 359 val wholeRow = @Folder.concat ! M.keyFolder M.rowFolder
adamc@915 360
adamc@915 361 fun ensql [env] (r : $(M.key ++ M.row)) =
adamc@915 362 map2 [rawMeta] [id] [sql_exp env [] []]
adamc@915 363 (fn [t] meta v => @sql_inject meta.Inj v)
adamc@915 364 [_] wholeRow M.raw r
adamc@915 365
adamc@915 366 val new =
adamc@915 367 row <- Monad.mapR [rawMeta] [id]
adamc@915 368 (fn [nm :: Name] [t :: Type] meta => meta.New)
adamc@915 369 [_] wholeRow M.raw;
adamc@915 370 dml (insert M.tab (ensql row));
adamc@915 371 return row
adamc@915 372
adamc@936 373 fun selector (r : $M.key) : sql_exp [T = M.key ++ M.row] [] [] bool =
adamc@915 374 foldR2 [rawMeta] [id]
adamc@915 375 [fn key => rest :: {Type} -> [rest ~ key] => sql_exp [T = key ++ rest] [] [] bool]
adamc@915 376 (fn [nm :: Name] [t :: Type] [key :: {Type}] [[nm] ~ key]
adamc@915 377 (meta : rawMeta t) (v : t)
adamc@915 378 (exp : rest :: {Type} -> [rest ~ key] => sql_exp [T = key ++ rest] [] [] bool)
adamc@915 379 [rest :: {Type}] [rest ~ [nm = t] ++ key] =>
adamc@915 380 (WHERE T.{nm} = {@sql_inject meta.Inj v} AND {exp [[nm = t] ++ rest] !}))
adamc@915 381 (fn [rest :: {Type}] [rest ~ []] => (WHERE TRUE))
adamc@936 382 [_] M.keyFolder (M.raw --- map rawMeta M.row) r
adamc@915 383 [_] !
adamc@915 384
adamc@936 385 fun save key row =
adamc@915 386 dml (update [M.key ++ M.row] !
adamc@936 387 (ensql row)
adamc@915 388 M.tab
adamc@936 389 (selector key))
adamc@915 390
adamc@936 391 fun delete key =
adamc@936 392 dml (Basis.delete M.tab (selector key))
adamc@915 393
adamc@915 394 val cols = M.cols
adamc@915 395
adamc@915 396 val folder = M.colsFolder
adamc@935 397
adamc@935 398 val aggregates = M.aggregates
adamc@937 399
adamc@937 400 val aggFolder = M.aggFolder
adamc@915 401 end)
adamc@915 402 end