Hi Gary et al,

On 15 November 2013 23:55, Gary Johnson <gwjoh...@uvm.edu> wrote:

> I really like your approach, but I've also been feeling the burn a bit
> with having to store each SQL query in its own file (especially since my
> data exploration frequently leaves me cataloging hundreds of them). Take a
> look at the approach used in sql-phrasebook:
>
>   https://github.com/ray1729/sql-phrasebook
>
>   I'm hoping it will give you some ideas to extend Yesql to address this
> particular issue, ideally with a nicer syntax than that used in
> sql-phrasebook. Otherwise, I think Yesql is bound for great things!
>
>
Is it the syntax of the phrasebook you don't like, or the API for the
library?

This was just something I put together quickly, so there's a lot of room
for improvement. The phrasebook parser is very simplistic and, although it
works for the simple cases where I've used it, I'm concerned it's not
robust. It's trivial to change the place-holder syntax from ${var-name} if
that's the problem.

My biggest concern is more philosophical. Although there are advantages to
defining the queries in an SQL file, the code where you use them needs to
know the details of the query (what columns does it return? what bind
parameters are needed? is any post-processing aggregation needed?) So I
don't like the distance between the Clojure and SQL implied by a
phrasebook-based approach.

My current thinking is to do something like:

(defselect select-user
  "SELECT user.* FROM user
   WHERE user_id = {{user_id}}")

with appropriate syntactic sugar. This would return a function of a
database connection, which would return the rows. The next step would be to
support a post-processing option for the cases where you modify the
returned results in some way:

(defselect select-user-roles
  "SELECT user.*, role.name AS role_name
   FROM user
   LEFT OUTER JOIN user_role USING(user_id)
   LEFT OUTER JOIN role USING(role_id)
   ORDER BY user.user_id"
  :post-process (fn [rows] (map (fn [user-rows]
                                                 (assoc (select-keys (first
user-rows) [:user_id :user_name])
                                                           :roles (map
:role_name user-rows))
                                    (partition-by :user_id rows)))

That way, the post-processing of the results is kept close to the query.

Just some food for thought...

Ray.

-- 
-- 
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