adamc@623
|
1 (** Row folding *)
|
adamc@623
|
2
|
adamc@623
|
3 con folder = K ==> fn r :: {K} =>
|
adamc@623
|
4 tf :: ({K} -> Type)
|
adamc@623
|
5 -> (nm :: Name -> v :: K -> r :: {K} -> tf r
|
adamc@623
|
6 -> fn [[nm] ~ r] => tf ([nm = v] ++ r))
|
adamc@623
|
7 -> tf [] -> tf r
|
adamc@623
|
8
|
adamc@627
|
9 structure Folder = struct
|
adamc@627
|
10 fun nil K (tf :: {K} -> Type)
|
adamc@627
|
11 (f : nm :: Name -> v :: K -> r :: {K} -> tf r
|
adamc@627
|
12 -> fn [[nm] ~ r] => tf ([nm = v] ++ r))
|
adamc@627
|
13 (i : tf []) = i
|
adamc@627
|
14
|
adamc@627
|
15 fun cons K (r ::: {K}) (nm :: Name) (v :: K) [[nm] ~ r] (fold : folder r)
|
adamc@627
|
16 (tf :: {K} -> Type)
|
adamc@627
|
17 (f : nm :: Name -> v :: K -> r :: {K} -> tf r
|
adamc@627
|
18 -> fn [[nm] ~ r] => tf ([nm = v] ++ r))
|
adamc@627
|
19 (i : tf []) = f [nm] [v] [r] (fold [tf] f i)
|
adamc@627
|
20 end
|
adamc@627
|
21
|
adamc@623
|
22
|
adamc@422
|
23 fun not b = if b then False else True
|
adamc@422
|
24
|
adamc@356
|
25 con idT (t :: Type) = t
|
adamc@356
|
26 con record (t :: {Type}) = $t
|
adamc@356
|
27 con fstTT (t :: (Type * Type)) = t.1
|
adamc@356
|
28 con sndTT (t :: (Type * Type)) = t.2
|
adamc@445
|
29 con fstTTT (t :: (Type * Type * Type)) = t.1
|
adamc@445
|
30 con sndTTT (t :: (Type * Type * Type)) = t.2
|
adamc@445
|
31 con thdTTT (t :: (Type * Type * Type)) = t.3
|
adamc@329
|
32
|
adamc@621
|
33 con mapUT = fn f :: Type => map (fn _ :: Unit => f)
|
adamc@445
|
34
|
adamc@339
|
35 con ex = fn tf :: (Type -> Type) =>
|
adamc@356
|
36 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
|
adamc@339
|
37
|
adamc@339
|
38 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
|
adamc@356
|
39 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
|
adamc@356
|
40 f [choice] body
|
adamc@339
|
41
|
adamc@356
|
42 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
|
adamc@356
|
43 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
|
adamc@325
|
44
|
adamc@564
|
45 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (_ : show t) (v : t) =
|
adamc@564
|
46 cdata (show v)
|
adamc@328
|
47
|
adamc@411
|
48 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
|
adamc@411
|
49 (f : nm :: Name -> rest :: {Unit}
|
adamc@411
|
50 -> fn [[nm] ~ rest] =>
|
adamc@411
|
51 tf -> tr rest -> tr ([nm] ++ rest))
|
adamc@623
|
52 (i : tr []) (r ::: {Unit}) (fold : folder r)=
|
adamc@411
|
53 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
|
adamc@623
|
54 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@623
|
55 [[nm] ~ rest] r =>
|
adamc@623
|
56 f [nm] [rest] r.nm (acc (r -- nm)))
|
adamc@623
|
57 (fn _ => i)
|
adamc@411
|
58
|
adamc@418
|
59 fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
|
adamc@418
|
60 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
61 -> fn [[nm] ~ rest] =>
|
adamc@418
|
62 tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
|
adamc@623
|
63 (i : tr []) (r ::: {Unit}) (fold : folder r) =
|
adamc@418
|
64 fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
|
adamc@623
|
65 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
|
adamc@623
|
66 [[nm] ~ rest] r1 r2 =>
|
adamc@623
|
67 f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@623
|
68 (fn _ _ => i)
|
adamc@418
|
69
|
adamc@418
|
70 fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
|
adamc@418
|
71 (f : nm :: Name -> rest :: {Unit}
|
adamc@418
|
72 -> fn [[nm] ~ rest] =>
|
adamc@418
|
73 tf1 -> tf2 -> xml ctx [] []) =
|
adamc@418
|
74 foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@418
|
75 (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
|
adamc@418
|
76 <xml>{f [nm] [rest] v1 v2}{acc}</xml>)
|
adamc@418
|
77 <xml/>
|
adamc@418
|
78
|
adamc@623
|
79 fun foldR K (tf :: K -> Type) (tr :: {K} -> Type)
|
adamc@623
|
80 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@356
|
81 -> fn [[nm] ~ rest] =>
|
adamc@356
|
82 tf t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@623
|
83 (i : tr []) (r ::: {K}) (fold : folder r) =
|
adamc@623
|
84 fold [fn r :: {K} => $(map tf r) -> tr r]
|
adamc@623
|
85 (fn (nm :: Name) (t :: K) (rest :: {K}) (acc : _ -> tr rest)
|
adamc@356
|
86 [[nm] ~ rest] r =>
|
adamc@356
|
87 f [nm] [t] [rest] r.nm (acc (r -- nm)))
|
adamc@356
|
88 (fn _ => i)
|
adamc@336
|
89
|
adamc@623
|
90 fun foldR2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (tr :: {K} -> Type)
|
adamc@623
|
91 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@367
|
92 -> fn [[nm] ~ rest] =>
|
adamc@367
|
93 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
|
adamc@623
|
94 (i : tr []) (r ::: {K}) (fold : folder r) =
|
adamc@623
|
95 fold [fn r :: {K} => $(map tf1 r) -> $(map tf2 r) -> tr r]
|
adamc@623
|
96 (fn (nm :: Name) (t :: K) (rest :: {K})
|
adamc@623
|
97 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
|
adamc@623
|
98 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
|
adamc@623
|
99 (fn _ _ => i)
|
adamc@332
|
100
|
adamc@623
|
101 fun foldRX K (tf :: K -> Type) (ctx :: {Unit})
|
adamc@623
|
102 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@356
|
103 -> fn [[nm] ~ rest] =>
|
adamc@356
|
104 tf t -> xml ctx [] []) =
|
adamc@623
|
105 foldR [tf] [fn _ => xml ctx [] []]
|
adamc@623
|
106 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest] r acc =>
|
adamc@623
|
107 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
|
adamc@623
|
108 <xml/>
|
adamc@336
|
109
|
adamc@623
|
110 fun foldRX2 K (tf1 :: K -> Type) (tf2 :: K -> Type) (ctx :: {Unit})
|
adamc@623
|
111 (f : nm :: Name -> t :: K -> rest :: {K}
|
adamc@356
|
112 -> fn [[nm] ~ rest] =>
|
adamc@356
|
113 tf1 t -> tf2 t -> xml ctx [] []) =
|
adamc@623
|
114 foldR2 [tf1] [tf2] [fn _ => xml ctx [] []]
|
adamc@623
|
115 (fn (nm :: Name) (t :: K) (rest :: {K}) [[nm] ~ rest]
|
adamc@623
|
116 r1 r2 acc =>
|
adamc@623
|
117 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
|
adamc@623
|
118 <xml/>
|
adamc@445
|
119
|
adamc@356
|
120 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@356
|
121 (q : sql_query tables exps) [tables ~ exps]
|
adamc@621
|
122 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@356
|
123 -> xml ctx [] []) =
|
adamc@356
|
124 query q
|
adamc@356
|
125 (fn fs acc => return <xml>{acc}{f fs}</xml>)
|
adamc@360
|
126 <xml/>
|
adamc@341
|
127
|
adamc@469
|
128 fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
|
adamc@469
|
129 (q : sql_query tables exps) [tables ~ exps]
|
adamc@621
|
130 (f : $(exps ++ map (fn fields :: {Type} => $fields) tables)
|
adamc@469
|
131 -> transaction (xml ctx [] [])) =
|
adamc@469
|
132 query q
|
adamc@469
|
133 (fn fs acc =>
|
adamc@469
|
134 r <- f fs;
|
adamc@469
|
135 return <xml>{acc}{r}</xml>)
|
adamc@469
|
136 <xml/>
|
adamc@469
|
137
|
adamc@356
|
138 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@356
|
139 (q : sql_query tables exps) [tables ~ exps] =
|
adamc@356
|
140 query q
|
adamc@356
|
141 (fn fs _ => return (Some fs))
|
adamc@356
|
142 None
|
adamc@440
|
143
|
adamc@440
|
144 fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
|
adamc@440
|
145 (q : sql_query tables exps) [tables ~ exps] =
|
adamc@440
|
146 o <- oneOrNoRows q;
|
adamc@440
|
147 return (case o of
|
adamc@440
|
148 None => error <xml>Query returned no rows</xml>
|
adamc@440
|
149 | Some r => r)
|
adamc@440
|
150
|
adamc@470
|
151 fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@470
|
152 (t ::: Type) (_ : sql_injectable (option t))
|
adamc@470
|
153 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
154 (e2 : sql_exp tables agg exps (option t)) =
|
adamc@471
|
155 (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
|
adamc@470
|
156
|
adamc@470
|
157 fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
|
adamc@564
|
158 (t ::: Type) (_ : sql_injectable (option t))
|
adamc@470
|
159 (e1 : sql_exp tables agg exps (option t))
|
adamc@470
|
160 (e2 : option t) =
|
adamc@470
|
161 case e2 of
|
adamc@471
|
162 None => (SQL {e1} IS NULL)
|
adamc@564
|
163 | Some _ => sql_binary sql_eq e1 (sql_inject e2)
|