Feature request - A boolean *show-sql* mode

2 views
Skip to first unread message

Shantanu Kumar

unread,
Oct 30, 2010, 11:25:00 AM10/30/10
to clj-sql
Hi,

I was wondering it would be nice to have a boolean *show-sql* mode
that prints out the SQL query being executed to the STDOUT. It can be
set to off by default, and optionally turned on using a macro:

(showing-sql
..code here..)

and maybe this to turn off:

(not-showing-sql ; turns off *show-sql* (useful when nesting)
..code here..)

Regards,
Shantanu

Saul Hazledine

unread,
Oct 31, 2010, 11:36:11 AM10/31/10
to clj-sql
On 30 Okt., 16:25, Shantanu Kumar <kumar.shant...@gmail.com> wrote:
> Hi,
>
> I was wondering it would be nice to have a boolean *show-sql* mode
> that prints out the SQL query being executed to the STDOUT. It can be
> set to off by default, and optionally turned on using a macro:
>

Cool, I'll look into doing this. I won't be able to do anything until
later this week though.

Would just having the binding variable (and no macro) be good enough?
That seems to be the style in most of contrib.

Thanks for the suggestion.
Saul

Shantanu Kumar

unread,
Oct 31, 2010, 11:40:55 AM10/31/10
to clj...@googlegroups.com
On Sun, Oct 31, 2010 at 9:06 PM, Saul Hazledine <sha...@gmail.com> wrote:
On 30 Okt., 16:25, Shantanu Kumar <kumar.shant...@gmail.com> wrote:
> Hi,
>
> I was wondering it would be nice to have a boolean *show-sql* mode
> that prints out the SQL query being executed to the STDOUT. It can be
> set to off by default, and optionally turned on using a macro:
>

Cool, I'll look into doing this. I won't be able to do anything until
later this week though.

 Would just having the binding variable (and no macro) be good enough?

Yes, even the boolean flag should be enough. The important part is it should cover all kinds of queries -- SELECT, INSERT, UPDATE, CREATE TABLE....etc.
 
Regards
-- 
Shantanu
kumar.shantanu at gmail dot com
http://twitter.com/kumarshantanu

Saul Hazledine

unread,
Nov 4, 2010, 4:33:37 AM11/4/10
to clj-sql
On Oct 31, 4:40 pm, Shantanu Kumar <kumar.shant...@gmail.com> wrote:
> Yes, even the boolean flag should be enough. The important part is it should
> cover all kinds of queries -- SELECT, INSERT, UPDATE, CREATE TABLE....etc.
>

I've looked at this and, while possible, it does mean that clj-sql
would need to implement all functions that handle sql in
clojure.contrib.sql. Currently it just captures part of the
clojure.contrib.sql for most functionality. I'm not opposed to
wrapping clojure.contrib.sql functions but I'm keen to avoid it where
possible.

In this case there may be an easier solution. Both clojure.contrib.sql
and clj-sql support passing a javax.sql.Datasource in the database
spec. This object allows a PrintWriter to be set which usually logs
SQL and the creation of connections etc.

http://download.oracle.com/javase/1.4.2/docs/api/javax/sql/DataSource.html#setLogWriter%28java.io.PrintWriter%29

(.setLogWriter my-datasource (PrintWriter. *out*))

H2 ignores this call but Apache Derby should log fine.

Let me know if this is good enough - if not I'll go ahead and wrap the
contrib functions.

Saul

Shantanu Kumar

unread,
Nov 4, 2010, 4:53:07 AM11/4/10
to clj...@googlegroups.com
I understand your preference for not wanting to wrap all contrib functions. The trouble is 'show-sql' cannot be implemented consistently using the setlogWriter method since it is highly dependent upon the driver implementation. Several DB vendors may choose not to log anything at all (except when debugging). IMHO this is a very useful feature for users and Clj-sql is probably the best place to have this implemented.

Regards,
Shantanu
Reply all
Reply to author
Forward
0 new messages