This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-21
Channels
- # announcements (26)
- # babashka (115)
- # babashka-sci-dev (5)
- # beginners (48)
- # calva (69)
- # cider (4)
- # clj-commons (11)
- # clj-kondo (1)
- # cljfx (29)
- # clojure (109)
- # clojure-art (1)
- # clojure-czech (1)
- # clojure-europe (33)
- # clojure-nl (1)
- # clojure-nlp (3)
- # clojure-norway (7)
- # clojure-uk (1)
- # clojurescript (63)
- # clr (1)
- # data-science (41)
- # datalevin (1)
- # datomic (11)
- # emacs (58)
- # etaoin (11)
- # figwheel-main (1)
- # fulcro (5)
- # google-cloud (12)
- # helix (2)
- # honeysql (21)
- # hyperfiddle (22)
- # joyride (53)
- # malli (52)
- # off-topic (27)
- # portal (4)
- # re-frame (19)
- # releases (3)
- # ring-swagger (5)
- # xtdb (30)
So at work, we do quite a bit of logging, and in that logging we add a trace-id
so we can correlate log messages to the same request. This is not rocket science.
It turns out however, that if we do
=> /* trace-id: 123456789 */ select * from foo;
from the psql console, that comment gets propagated to the logs as:
2023-03-21 11:47:23.146 UTC [29737] LOG: duration: 36.834 ms statement: /* trace-id: 123456789 */ select * from foo;
Which would then let us correlate (slow) queries to specific requests, which would again make it very much easier to debug performance problems.
Because of how we’ve structured our code, this would be very easy to implement if honey-sql had a (hh/comment )
fn, so we could say something like:
(->> (hh/comment (str "trace-id: " id))
(hh/select ..))
which would render out a comment before the actual statement.
Is this already possible (and if not, would it be possible to add)?TBH, that sounds a bit more like something next.jdbc
should be providing but it's an interesting question and certainly something that is easy to add to HoneySQL -- as you found with register-clause!
🙂 Is it (comments preceding SQL) an ANSI feature or something peculiar to PG?
But, say I wanted to register :comments
so that it is added before :insert
, :update
, :delete
:with
as well? do I just supply all those keywords as last args?
Just pick the earliest one you need it to be before...
So basically :with
(and then honey will figure out that if :with
is not present, it will go before whatever comes after :with
)?
(def ^:private default-clause-order
"The (default) order for known clauses. Can have items added and removed."
[;; DDL comes first (these don't really have a precedence):
:alter-table :add-column :drop-column
:alter-column :modify-column :rename-column
:add-index :drop-index :rename-table
:create-table :create-table-as :with-columns
:create-view :create-materialized-view :create-extension
:drop-table :drop-view :drop-materialized-view :drop-extension
:refresh-materialized-view
;; then SQL clauses in priority order:
:raw :nest :with :with-recursive :intersect :union :union-all :except :except-all
:table
:select :select-distinct :select-distinct-on :select-top :select-distinct-top
...
Not sure whether you'd want this to be "before" raw/nest, but with sounds like a reasonable starting point?
We’re not currently using :raw
at top-level, and I don’t think we’re using :nest
but we’re deffo using :with
I guess something along the lines of
(honey.sql/register-clause! :comment
(fn [clause x]
[(str"/* " x " */")])
:select)
Hi all. Wondering how I might achieve the following sql chunk:
ARRAY_AGG(DISTINCT (i.name)) FILTER (WHERE i.name IS NOT NULL)
I have this in honey, but can't figure out how to not use a aggregate function as explained in the docs
[[:array_agg
[:distinct :i.name]
[:filter {:where [:not= nil :i.name]}]]]
If I don't use something like :%count, the filter clause doesn't show up in the output. Any thoughts?(sql/format {:select [[[:filter
[:array_agg [:distinct :i.name]]
{:where [:is-not :i.name nil]}]]]})
=> ["SELECT ARRAY_AGG(DISTINCT i.name) FILTER (WHERE i.name IS NOT NULL)"]
Documented at https://cljdoc.org/d/com.github.seancorfield/honeysql/2.4.980/doc/getting-started/sql-special-syntax-#filter-within-groupHi @U2FRKM4TW, thank you. I just don't see how to create a filter without an aggregate. My query doesn't work when I use Count