hata
hata

Reputation: 197

How do you get clojure yesql output the SQL it would execute?

I want to debug my yesql query. So I'm trying to get SQL it would execute. Can anyone give me an example of how to make yesql output the SQL it would execute?

It's not good example... I mistake parameter name 'tel' for 'phone'.

users.sql

-- name: insert-user
-- insert a user data to users table. 
insert into
users  ( name,  age,  tel,  address,  remark)
values (:name, :age, :tel, :address, :remark);

query.clj

(ns example.sql.query
  (:require [yesql.core :as yesql]
            [example.sql.datasource :as ds]))

(yesql/defqueries {:connection {:datasource ds/datasource}})
(yesql/insert-user {:name "joe" :age 22 :phone nil :address "xxxxxx" :remark ""})

Upvotes: 5

Views: 620

Answers (1)

Jim Brusstar
Jim Brusstar

Reputation: 31

In general (as mentioned in the comments), yesql shouldn't be doing a whole lot of SQL generation; the concept is that it passes through the queries you've written.

With that said, if you still want to try to see the raw query yesql is passing to JDBC, here's a hack to get pretty close:

(require 'yesql.generate)
(require 'yesql.statement-parser)

(defn debug-yesql-query [queryfn args]
  ;; queryfn should be a query function generated by yesql
  ;; args should be a map of query args, just as if you were
  ;;   calling the query function
  (let [sql-source (-> queryfn meta :yesql.generate/source)]
    (yesql.generate/rewrite-query-for-jdbc
     (yesql.statement-parser/tokenize sql-source)
     args))

Tested with yesql version 0.5.2.

Upvotes: 3

Related Questions