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

I want to debug my yesql request. So I am trying to get the SQL that it will execute. Can someone give me an example of how to make yesql the SQL output that it would execute?

This is not a good example ... I accept the 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 ""})
+4
source share
1 answer

In general (as mentioned in the comments) yesql should not do a lot of SQL generation; the concept is that it goes through the letters you requested.

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

(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.

+2
source

All Articles