Created
April 28, 2018 11:13
-
-
Save abcdw/cb550fcee4ef422b9216cbe2cbce976a to your computer and use it in GitHub Desktop.
Example of sql pretty print. ql/httpkit/cheshire.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def sql-example | |
(-> | |
(ql/sql {:ql/with {:_user #:ql {:select :* | |
:from :user | |
:where [:ql/= :id [:ql/param 5]]} | |
:_group {:ql/select :g.* | |
:ql/from {:u :user} | |
:ql/joins {:g {:ql/rel :group | |
:ql/on [:ql/= :g.user_id :u.id]}}}} | |
:ql/select {:name :u.name :group :g.name} | |
:ql/from {:u :_user :g :_group} | |
:ql/where [:ql/= :g.user_id :u.id]}) | |
:sql)) | |
(def response-result | |
@(http/post "https://sqlformat.org/api/v1/format" | |
{:form-params {"sql" sql-example | |
"reindent" 1}})) | |
(-> response-result | |
:body | |
json/parse-string | |
(get "result") | |
print) | |
;; WITH _user AS | |
;; (SELECT * | |
;; FROM user | |
;; WHERE id = ? ), | |
;; _group AS | |
;; (SELECT g.* | |
;; FROM user u | |
;; JOIN | |
;; group g ON g.user_id = u.id) | |
;; SELECT u.name AS name, | |
;; g.name AS | |
;; group | |
;; FROM _user u, | |
;; _group g | |
;; WHERE g.user_id = u.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment