This is a fairly restricted composition, though:

(def triangles (compose (select [:color :num_sides] (from :shapes))
                        (where '(= :num_sides 3))))
(def green-triangles (compose triangles
                              (where '(= :color "green"))))
(sql green-triangles)
;=> ["SELECT color, num_sides FROM shapes WHERE (color = ?)" "green"]

We've lost our `num_sides` selection, so now our query is wrong. "Last
clause wins" means you have to be aware of the previous ones to ensure you
don't obliterate them when "composing".

The same example in `clojure-sql`:

(def triangles (-> (table :shapes)
                   (project [:color :num_sides])
                   (select '(= :num_sides 3))))
(def green-triangles (select triangles '(= :color :green)))
(deref green-triangles)
;=> ["SELECT \"shapes2142\".\"color\" AS \"color\",
\"shapes2142\".\"num_sides\" AS \"num_sides\" FROM \"shapes\" AS
\"shapes2142\" WHERE ((\"shapes2142\".\"num_sides\" = 3) AND
(\"shapes2142\".\"color\" = ?))" "green"]

On 6 July 2013 22:37, r0man <roman.sche...@burningswell.com> wrote:

> Composing queries is done via "compose".
>
> Take a look here:
> https://github.com/r0man/sqlingvo/blob/master/test/sqlingvo/test/core.clj#L16
>
>
> On Saturday, July 6, 2013 5:46:06 AM UTC+2, Carlo wrote:
>
>> Hey Roman,
>>
>> The issue that I see with `sqlingvo`, and the thing which I was trying to
>> solve for myself, is that it doesn't compose well. Unless I'm missing
>> something, you have to generate the entire query in the one `sql` form. To
>> me, this is a big restriction and was the number one thing I was trying to
>> fix with `clojure-sql`.
>>
>> The basic sort of thing I want to be able to do is this:
>>
>> (def users (-> (table :users) (project [:id :person :username])))
>> (def people (-> (table :people) (project [:id :first-name])))
>>
>> (def combined-query (-> people
>>                         (rename {:id :person})
>>                         (join users)
>>                         (project [:username :first-name])))
>>
>> So now in queries I can use `people`, `users` and `combined-query` in the
>> same way. The only difference in terms of how I can compose them is that
>> they expose different fields (`users` exposes [:id, :person, :username],
>> `people` exposes [:id :first-name], `combined-query` exposes [:username
>> :first-name]). In this example it's not completely obvious why this would
>> be beneficial, but it means that I can change `users`, for instance, to
>> also have a `(select '(= :deleted false)` in its definition and no other
>> code has to change. They will all join/query against the users where
>> `deleted` is false without any other modifications of code.
>>
>> This freedom of composition is what you have in relational algebra, and
>> what I was trying to get in Clojure as well. All the naming of tables and
>> field aliases and everything is handled by the library, so you only have to
>> worry about constructing the queries. Unfortunately SQL provides a number
>> of operations outside of the relational algebra model (grouping, sorting,
>> take/drop), so they've been "tacked on" as a bit of an afterthought and
>> could probably use some improvement.
>>
>> Looking at `sqlingvo` did show up a mistake that I made in how I was
>> dealing with sorts, though, so thanks for that! I think our libraries just
>> have fairly different concerns at the moment.
>>
>> Carlo
>>
>>
>> On 5 July 2013 20:59, r0man <roman....@burningswell.**com> wrote:
>>
>>> Hi Carlo,
>>>
>>> if you'are looking for generating more complex SQL there's also:
>>>
>>> https://github.com/r0man/**sqlingvo <https://github.com/r0man/sqlingvo>
>>>
>>> Roman.
>>>
>>> On Wednesday, July 3, 2013 10:48:07 AM UTC+2, Carlo wrote:
>>>>
>>>> Hey guys!
>>>>
>>>> I've been working on a small library to make writing SQL queries a
>>>> little bit easier. It's along the same lines as ClojureQL, but takes a
>>>> different approach and compiles into quite different SQL in the end.
>>>>
>>>> At the moment it's quite immature, but it should be able to support any
>>>> queries which can be expressed in relational algebra. There will be some
>>>> SQL queries which can't be expressed in clojure-sql, but hopefully there
>>>> won't be too many of those. A greater limitation is that at the moment the
>>>> SQL generation is specific to the PostgresSQL database (although any
>>>> contributions for other databases are welcome!).
>>>>
>>>> Dependency vector: [clojure-sql "0.1.0"]
>>>> Repository: 
>>>> https://bitbucket.org/czan/**clo**jure-sql<https://bitbucket.org/czan/clojure-sql>
>>>> Clojars link: 
>>>> https://clojars.org/clojure-**sq**l<https://clojars.org/clojure-sql>
>>>>
>>>> Let me know what you think!
>>>>
>>>> Carlo
>>>>
>>>  --
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clo...@googlegroups.com
>>>
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+u...@**googlegroups.com
>>>
>>> For more options, visit this group at
>>> http://groups.google.com/**group/clojure?hl=en<http://groups.google.com/group/clojure?hl=en>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to clojure+u...@**googlegroups.com.
>>>
>>> For more options, visit 
>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>> .
>>>
>>>
>>>
>>
>>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to