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 ""})
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With