annotate lib/basis.lig @ 243:2b9dfaffb008

Transactions and queries, at source level
author Adam Chlipala <adamc@hcoop.net>
date Thu, 28 Aug 2008 14:48:33 -0400
parents f5732dc1316c
children
rev   line source
adamc@56 1 type int
adamc@56 2 type float
adamc@56 3 type string
adamc@91 4
adamc@119 5 type unit = {}
adamc@119 6
adamc@186 7 datatype bool = False | True
adamc@186 8
adamc@91 9
adamc@203 10 (** SQL *)
adamc@203 11
adamc@203 12 con sql_table :: {Type} -> Type
adamc@203 13
adamc@204 14 (*** Queries *)
adamc@204 15
adamc@233 16 con sql_query :: {{Type}} -> {Type} -> Type
adamc@233 17 con sql_query1 :: {{Type}} -> {{Type}} -> {Type} -> Type
adamc@234 18 con sql_exp :: {{Type}} -> {{Type}} -> {Type} -> Type -> Type
adamc@204 19
adamc@223 20 con sql_subset :: {{Type}} -> {{Type}} -> Type
adamc@223 21 val sql_subset : keep_drop :: {({Type} * {Type})}
adamc@223 22 -> sql_subset
adamc@223 23 (fold (fn nm => fn fields :: ({Type} * {Type}) => fn acc =>
adamc@223 24 [nm] ~ acc => fields.1 ~ fields.2 =>
adamc@223 25 [nm = fields.1 ++ fields.2] ++ acc) [] keep_drop)
adamc@223 26 (fold (fn nm => fn fields :: ({Type} * {Type}) => fn acc =>
adamc@223 27 [nm] ~ acc =>
adamc@223 28 [nm = fields.1] ++ acc) [] keep_drop)
adamc@223 29 val sql_subset_all : tables :: {{Type}}
adamc@223 30 -> sql_subset tables tables
adamc@223 31
adamc@229 32 val sql_query1 : tables ::: {{Type}}
adamc@224 33 -> grouped ::: {{Type}}
adamc@233 34 -> selectedFields ::: {{Type}}
adamc@233 35 -> selectedExps ::: {Type}
adamc@223 36 -> {From : $(fold (fn nm => fn fields :: {Type} => fn acc =>
adamc@223 37 [nm] ~ acc => [nm = sql_table fields] ++ acc) [] tables),
adamc@234 38 Where : sql_exp tables [] [] bool,
adamc@224 39 GroupBy : sql_subset tables grouped,
adamc@234 40 Having : sql_exp grouped tables [] bool,
adamc@233 41 SelectFields : sql_subset grouped selectedFields,
adamc@233 42 SelectExps : $(fold (fn nm => fn t :: Type => fn acc =>
adamc@234 43 [nm] ~ acc => [nm = sql_exp grouped tables [] t] ++ acc) [] selectedExps) }
adamc@233 44 -> sql_query1 tables selectedFields selectedExps
adamc@229 45
adamc@229 46 type sql_relop
adamc@229 47 val sql_union : sql_relop
adamc@229 48 val sql_intersect : sql_relop
adamc@229 49 val sql_except : sql_relop
adamc@229 50 val sql_relop : sql_relop
adamc@229 51 -> tables1 ::: {{Type}}
adamc@229 52 -> tables2 ::: {{Type}}
adamc@233 53 -> selectedFields ::: {{Type}}
adamc@233 54 -> selectedExps ::: {Type}
adamc@233 55 -> sql_query1 tables1 selectedFields selectedExps
adamc@233 56 -> sql_query1 tables2 selectedFields selectedExps
adamc@233 57 -> sql_query1 selectedFields selectedFields selectedExps
adamc@229 58
adamc@230 59 type sql_direction
adamc@230 60 val sql_asc : sql_direction
adamc@230 61 val sql_desc : sql_direction
adamc@230 62
adamc@234 63 con sql_order_by :: {{Type}} -> {Type} -> Type
adamc@234 64 val sql_order_by_Nil : tables ::: {{Type}} -> exps :: {Type} -> sql_order_by tables exps
adamc@234 65 val sql_order_by_Cons : tables ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@234 66 -> sql_exp tables [] exps t -> sql_order_by tables exps
adamc@234 67 -> sql_order_by tables exps
adamc@230 68
adamc@231 69 type sql_limit
adamc@231 70 val sql_no_limit : sql_limit
adamc@231 71 val sql_limit : int -> sql_limit
adamc@231 72
adamc@232 73 type sql_offset
adamc@232 74 val sql_no_offset : sql_offset
adamc@232 75 val sql_offset : int -> sql_offset
adamc@232 76
adamc@229 77 val sql_query : tables ::: {{Type}}
adamc@233 78 -> selectedFields ::: {{Type}}
adamc@233 79 -> selectedExps ::: {Type}
adamc@233 80 -> {Rows : sql_query1 tables selectedFields selectedExps,
adamc@234 81 OrderBy : sql_order_by tables selectedExps,
adamc@232 82 Limit : sql_limit,
adamc@232 83 Offset : sql_offset}
adamc@233 84 -> sql_query selectedFields selectedExps
adamc@204 85
adamc@225 86 val sql_field : otherTabs ::: {{Type}} -> otherFields ::: {Type} -> fieldType ::: Type -> agg ::: {{Type}}
adamc@234 87 -> exps ::: {Type}
adamc@221 88 -> tab :: Name -> field :: Name
adamc@234 89 -> sql_exp ([tab = [field = fieldType] ++ otherFields] ++ otherTabs) agg exps fieldType
adamc@234 90
adamc@234 91 val sql_exp : tabs ::: {{Type}} -> agg ::: {{Type}} -> t ::: Type -> rest ::: {Type} -> nm :: Name
adamc@234 92 -> sql_exp tabs agg ([nm = t] ++ rest) t
adamc@209 93
adamc@221 94 class sql_injectable
adamc@221 95 val sql_bool : sql_injectable bool
adamc@221 96 val sql_int : sql_injectable int
adamc@221 97 val sql_float : sql_injectable float
adamc@221 98 val sql_string : sql_injectable string
adamc@234 99 val sql_inject : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@234 100 -> sql_injectable t -> t -> sql_exp tables agg exps t
adamc@209 101
adamc@220 102 con sql_unary :: Type -> Type -> Type
adamc@220 103 val sql_not : sql_unary bool bool
adamc@234 104 val sql_unary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> arg ::: Type -> res ::: Type
adamc@234 105 -> sql_unary arg res -> sql_exp tables agg exps arg -> sql_exp tables agg exps res
adamc@220 106
adamc@220 107 con sql_binary :: Type -> Type -> Type -> Type
adamc@220 108 val sql_and : sql_binary bool bool bool
adamc@220 109 val sql_or : sql_binary bool bool bool
adamc@234 110 val sql_binary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@234 111 -> arg1 ::: Type -> arg2 ::: Type -> res ::: Type
adamc@234 112 -> sql_binary arg1 arg2 res -> sql_exp tables agg exps arg1 -> sql_exp tables agg exps arg2
adamc@234 113 -> sql_exp tables agg exps res
adamc@220 114
adamc@219 115 type sql_comparison
adamc@219 116 val sql_eq : sql_comparison
adamc@219 117 val sql_ne : sql_comparison
adamc@219 118 val sql_lt : sql_comparison
adamc@219 119 val sql_le : sql_comparison
adamc@219 120 val sql_gt : sql_comparison
adamc@219 121 val sql_ge : sql_comparison
adamc@219 122 val sql_comparison : sql_comparison
adamc@234 123 -> tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@236 124 -> t ::: Type
adamc@234 125 -> sql_exp tables agg exps t -> sql_exp tables agg exps t
adamc@234 126 -> sql_exp tables agg exps bool
adamc@203 127
adamc@235 128 val sql_count : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@235 129 -> unit -> sql_exp tables agg exps int
adamc@235 130
adamc@236 131 con sql_aggregate :: Type -> Type
adamc@236 132 val sql_aggregate : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@236 133 -> sql_aggregate t -> sql_exp agg agg exps t -> sql_exp tables agg exps t
adamc@236 134
adamc@236 135 class sql_summable
adamc@236 136 val sql_summable_int : sql_summable int
adamc@236 137 val sql_summable_float : sql_summable float
adamc@236 138 val sql_avg : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 139 val sql_sum : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 140
adamc@236 141 class sql_maxable
adamc@236 142 val sql_maxable_int : sql_maxable int
adamc@236 143 val sql_maxable_float : sql_maxable float
adamc@236 144 val sql_maxable_string : sql_maxable string
adamc@236 145 val sql_max : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 146 val sql_min : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 147
adamc@235 148
adamc@243 149 (*** Executing queries *)
adamc@243 150
adamc@243 151 con transaction :: Type -> Type
adamc@243 152 val return : t ::: Type
adamc@243 153 -> t -> transaction t
adamc@243 154 val bind : t1 ::: Type -> t2 ::: Type
adamc@243 155 -> transaction t1 -> (t1 -> transaction t2)
adamc@243 156 -> transaction t2
adamc@243 157
adamc@243 158 val query : tables ::: {{Type}} -> exps ::: {Type}
adamc@243 159 -> sql_query tables exps
adamc@243 160 -> state ::: Type
adamc@243 161 -> ($(fold (fn nm (fields :: {Type}) acc => [nm] ~ acc => [nm = $fields] ++ acc) [] tables)
adamc@243 162 -> $exps
adamc@243 163 -> state
adamc@243 164 -> transaction state)
adamc@243 165 -> state
adamc@243 166 -> transaction state
adamc@243 167
adamc@243 168
adamc@203 169 (** XML *)
adamc@203 170
adamc@139 171 con tag :: {Type} -> {Unit} -> {Unit} -> {Type} -> {Type} -> Type
adamc@91 172
adamc@91 173
adamc@139 174 con xml :: {Unit} -> {Type} -> {Type} -> Type
adamc@141 175 val cdata : ctx ::: {Unit} -> use ::: {Type} -> string -> xml ctx use []
adamc@104 176 val tag : attrsGiven ::: {Type} -> attrsAbsent ::: {Type} -> attrsGiven ~ attrsAbsent
adamc@139 177 -> ctxOuter ::: {Unit} -> ctxInner ::: {Unit}
adamc@139 178 -> useOuter ::: {Type} -> useInner ::: {Type} -> useOuter ~ useInner
adamc@139 179 -> bindOuter ::: {Type} -> bindInner ::: {Type} -> bindOuter ~ bindInner
adamc@104 180 -> $attrsGiven
adamc@139 181 -> tag (attrsGiven ++ attrsAbsent) ctxOuter ctxInner useOuter bindOuter
adamc@139 182 -> xml ctxInner useInner bindInner
adamc@139 183 -> xml ctxOuter (useOuter ++ useInner) (bindOuter ++ bindInner)
adamc@140 184 val join : ctx ::: {Unit}
adamc@139 185 -> use1 ::: {Type} -> bind1 ::: {Type} -> bind2 ::: {Type}
adamc@139 186 -> use1 ~ bind1 -> bind1 ~ bind2
adamc@140 187 -> xml ctx use1 bind1
adamc@140 188 -> xml ctx (use1 ++ bind1) bind2
adamc@140 189 -> xml ctx use1 (bind1 ++ bind2)
adamc@148 190 val useMore : ctx ::: {Unit} -> use1 ::: {Type} -> use2 ::: {Type} -> bind ::: {Type}
adamc@148 191 -> use1 ~ use2
adamc@148 192 -> xml ctx use1 bind
adamc@148 193 -> xml ctx (use1 ++ use2) bind
adamc@91 194
adamc@110 195 con xhtml = xml [Html]
adamc@139 196 con page = xhtml [] []
adamc@110 197
adamc@204 198 (*** HTML details *)
adamc@204 199
adamc@140 200 con html = [Html]
adamc@140 201 con head = [Head]
adamc@140 202 con body = [Body]
adamc@141 203 con lform = [Body, LForm]
adamc@93 204
adamc@140 205 val head : unit -> tag [] html head [] []
adamc@140 206 val title : unit -> tag [] head [] [] []
adamc@110 207
adamc@140 208 val body : unit -> tag [] html body [] []
adamc@140 209 con bodyTag = fn attrs :: {Type} => ctx ::: {Unit} -> [Body] ~ ctx -> unit
adamc@140 210 -> tag attrs ([Body] ++ ctx) ([Body] ++ ctx) [] []
adamc@141 211 con bodyTagStandalone = fn attrs :: {Type} => ctx ::: {Unit} -> [Body] ~ ctx -> unit
adamc@141 212 -> tag attrs ([Body] ++ ctx) [] [] []
adamc@141 213
adamc@141 214 val br : bodyTagStandalone []
adamc@119 215
adamc@140 216 val p : bodyTag []
adamc@140 217 val b : bodyTag []
adamc@140 218 val i : bodyTag []
adamc@140 219 val font : bodyTag [Size = int, Face = string]
adamc@140 220
adamc@140 221 val h1 : bodyTag []
adamc@140 222 val li : bodyTag []
adamc@140 223
adamc@140 224 val a : bodyTag [Link = page]
adamc@140 225
adamc@141 226 val lform : ctx ::: {Unit} -> [Body] ~ ctx -> bind ::: {Type}
adamc@141 227 -> xml lform [] bind
adamc@141 228 -> xml ([Body] ++ ctx) [] []
adamc@153 229 con lformTag = fn ty :: Type => fn inner :: {Unit} => fn attrs :: {Type} =>
adamc@141 230 ctx ::: {Unit} -> [LForm] ~ ctx
adamc@141 231 -> nm :: Name -> unit
adamc@153 232 -> tag attrs ([LForm] ++ ctx) inner [] [nm = ty]
adamc@153 233 val textbox : lformTag string [] []
adamc@155 234 val password : lformTag string [] []
adamc@153 235 val ltextarea : lformTag string [] []
adamc@153 236
adamc@190 237 val checkbox : lformTag bool [] []
adamc@190 238
adamc@153 239 con radio = [Body, Radio]
adamc@153 240 val radio : lformTag string radio []
adamc@153 241 val radioOption : unit -> tag [Value = string] radio [] [] []
adamc@142 242
adamc@154 243 con select = [Select]
adamc@154 244 val lselect : lformTag string select []
adamc@154 245 val loption : unit -> tag [Value = string] select [] [] []
adamc@154 246
adamc@142 247 val submit : ctx ::: {Unit} -> [LForm] ~ ctx
adamc@142 248 -> use ::: {Type} -> unit
adamc@142 249 -> tag [Action = $use -> page] ([LForm] ++ ctx) ([LForm] ++ ctx) use []