3

Is there a way to just generate the sql queries from ClojureQL's disj! conj! and update-in! functions, instead of executing them directly ?

NielsK
  • 6,886
  • 1
  • 24
  • 46

1 Answers1

1

No, these methods are directly executing their respective prepared statements. They are all pretty basic. For conj! and update-in!, look at format call in the conj-rows and update-vals functions that you can find inside the internal namespace:

 (format "INSERT INTO %s %s VALUES (%s)"
         (to-tablename table) columns template)

 (format "UPDATE %s SET %s WHERE %s"
         (to-tablename table) columns where)

For disj!, ClojureQL is using the clojure.java.jdbc library delete-rows function which contains:

  (format "DELETE FROM %s WHERE %s"
          (as-identifier table) where)

So basically disj! get the ability to use java.jdbc's with-naming-strategy and with-quoted-identifiers macros for the table name while the other don't.

Nicolas Buduroi
  • 3,565
  • 1
  • 28
  • 29
  • So if I mirror those functions I can make a to-str version, thats cool. Any idea how to do that with normal selects as well, since I just noticed they are converted to strings at the REPL, but there's no way to convert them to strings outside of *debug*. – NielsK Sep 12 '11 at 09:51
  • For queries, just use the compiler directly, e.g.: `(compile (table :users) cnx)` – Nicolas Buduroi Sep 12 '11 at 20:19
  • Thanks, found that out yesterday as well. For completeness sake, a where clause can be converted to string by just applying the str function to it e.g.: (str (where (= :id 5))) – NielsK Sep 14 '11 at 11:36