Fork me on GitHub
#honeysql
<
2022-01-14
>
paulbutcher15:01:53

I suspect that this is me being dense, but I am struggling to find a way to pass arguments to functions with Honey SQL. The SQL I’m trying to generate is:

["SELECT name, SQRT(POW(lat - ?, 2) + POW(long - ?, 2)) AS distance FROM circuits" x y]
and I’m damned if I can work out how to persuade Honey SQL to generate it. I’d be very grateful for a nudge in the right direction? Thanks!

aav16:01:00

(let [x 1 y 2]
  (honey/format
    {:select
     [:name
      [[:sqrt
        [[:+
          [[:pow [[:- :lat x]] [:inline 2]]]
          [[:pow [[:- :long y]] [:inline 2]]]]]] :distance]]
     :from [:circuits]}))

paulbutcher16:01:56

Is this mentioned in the documentation anywhere? The only mention of calling a function I could find was the % notation?

aav16:01:19

"Since regular function calls are indicated with vectors and so are aliased pairs, this shorthand can be more convenient due to the extra wrapping needed for the regular function calls in a select:"

(-> (select [[:count :*]]) (from :foo) sql/format)
=> ["SELECT COUNT(*) FROM foo"]

paulbutcher16:01:21

Thanks. I guess I (mis-) interpreted that as only applying to operators. Got it now, thanks.

👍 1
seancorfield17:01:26

@U5KAD4W2E If you have a suggestion for where in the docs that should be called out more clearly, feel free to create issues on the repo! I guess both the readme and the getting started pages should have function call examples in them somewhere?

💯 1
paulbutcher17:01:07

Yes, I think that that would have helped me work things out. Give me a moment and I’ll see if I can come up with something 👍

1