|
From: Artyom S. <art...@gm...> - 2009-07-23 08:37:36
|
Hi,
2009/7/22 Keren Lenz <ker...@gm...>:
> Here is an example (in imperative psudo code of what I mean by dynamic
> query:
> Query generate(bool cond1, bool cond2) {
> Colums c = null;
> if(cond1)
> c.add(col1);
> if(cond3)
> c.add(col2);
> Query q = SELECT + c + FROM table_name;
> return q;
> }
> That is, the selected columns are chosen based on runtime conditions.
> Is that possible with HaskellDB?
I would like to stress how HaskellDB works. You are given atomic building
blocks (relations, tuples and fields) and some combinators ("glue" that helps
you combine your building blocks: relational and query operations). What you
are really doing with HaskellDB is manipulating an abstract syntax tree of a DSL
embedded in Haskell.
A simple example would be something along these lines [1]:
> q val = do
> s <- table stock
> restrict (fromNull (constant "") (s!company_name)
> `like` constant ("%"++val++"%"))
> r <- project (company_name << s!company_name #
> ticker << s!ticker)
> return r
It is already somewhat "dynamic". Can we do anything to make it more "dynamic"?
Yes, we can:
> q flds val = do
> s <- table stock
> restrict (fromNull (constant "") (s!company_name)
> `like` constant ("%"++val++"%"))
> r <- project (flds s)
> return r
where flds is simply
> flds s = company_name << s!company_name # ticker << s!ticker)
So now you can do:
> flds cond1 cond2 s = let a = if cond1
> then (vendor_name << s!vendor_name) # fld
> else fld
> b = if cond2
> then (customer_id << s!customer_id) # a
> else a
> in b
> where
> fld = company_name << s!company_name # ticker << s!ticker
(don't forget to change the definition of q!)
As the number of conditions grows, this style becomes very inconvenient to
program in, so you'd need a writer monad over a monoid or something I suppose.
Also, I would like to note that I haven't tested the code given above,
so I don't
know if there are any bugs or not. But feel free to ask if there are,
anyway. :-)
Cheers,
Artyom Shalkhakov.
[1] http://pseudofish.com/blog/2008/05/11/haskelldb-basics/
|