Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

clojure

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 ""})
like image 249
hata Avatar asked Oct 20 '22 09:10

hata


1 Answers

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.

like image 101
Jim Brusstar Avatar answered Oct 22 '22 23:10

Jim Brusstar