view sql.ur @ 13:d05943db55e8

Sql.joiner
author Adam Chlipala <adam@chlipala.net>
date Mon, 21 Mar 2011 10:16:12 -0400
parents 799f43bce62b
children 744bf911dcc6
line wrap: on
line source
fun sqexps [env] [fields] (fl : folder fields) (inj : $(map sql_injectable fields)) (r : $fields) =
    @map2 [sql_injectable] [id] [sql_exp env [] []]
     (fn [t] => @sql_inject)
     fl inj r

fun selector [tn :: Name] [fs] [ofs] [fs ~ ofs] (fl : folder fs) (m : $(map sql_injectable fs)) (r : $fs) =
    @foldR2 [sql_injectable] [id]
     [fn key => rest :: {Type} -> [rest ~ key] => sql_exp [tn = key ++ rest] [] [] bool]
     (fn [nm :: Name] [t :: Type] [key :: {Type}] [[nm] ~ key]
                      (inj : sql_injectable t) (v : t)
                      (exp : rest :: {Type} -> [rest ~ key] => sql_exp [tn = key ++ rest] [] [] bool)
                      [rest :: {Type}] [rest ~ [nm = t] ++ key] =>
         (WHERE {{tn}}.{nm} = {@sql_inject inj v} AND {exp [[nm = t] ++ rest] !}))
     (fn [rest :: {Type}] [rest ~ []] => (WHERE TRUE))
     fl m r [_] !

fun joiner [tn1 :: Name] [tn2 :: Name] [fs] [ofs1] [ofs2] [[tn1] ~ [tn2]] [fs ~ ofs1] [fs ~ ofs2]
           (fl : folder fs) =
    @fold
     [fn key => rest1 :: {Type} -> rest2 :: {Type} -> [rest1 ~ key] => [rest2 ~ key] => sql_exp [tn1 = key ++ rest1, tn2 = key ++ rest2] [] [] bool]
     (fn [nm :: Name] [t :: Type] [key :: {Type}] [[nm] ~ key]
                      (exp : rest1 :: {Type} -> rest2 :: {Type} -> [rest1 ~ key] => [rest2 ~ key]
                       => sql_exp [tn1 = key ++ rest1, tn2 = key ++ rest2] [] [] bool)
                      [rest1 :: {Type}] [rest2 :: {Type}] [rest1 ~ [nm = t] ++ key] [rest2 ~ [nm = t] ++ key] =>
         (WHERE {{tn1}}.{nm} = {{tn2}}.{nm} AND {exp [[nm = t] ++ rest1] [[nm = t] ++ rest2] ! !}))
     (fn [rest1 :: {Type}] [rest2 :: {Type}] [rest1 ~ []] [rest2 ~ []] => (WHERE TRUE))
     fl [_] [_] ! !