Fork me on GitHub
#honeysql
<
2021-03-15
>
dcj19:03:41

I am attempting to migrate to honeysql 2 from 1. Many things are working, but I'm having trouble with one very complex CTE: The docs for with state:

The argument to :with (or :with-recursive) is a sequences of pairs, each of a result set name (or description) and a basic SQL statement
I'm having trouble parsing this, and the examples given use a different style than I am used to.... Previously I did something like:
(with [:A "some sql thing"]
      [:B "another sql thing"]
      ...)
Is that still valid?

seancorfield19:03:47

I don’t think that, as written, has ever been valid with strings.

seancorfield19:03:29

(with [:A ["some sql thing" its params]] [:B ["another sql thing" more params]]) is more like it.

seancorfield19:03:12

Can you share exactly what you had with v1 that worked (that no longer works the same in v2)?

seancorfield19:03:25

The syntax should not have changed between v1 and v2 as far as I’m aware @dcj

seancorfield19:03:36

The difference there is that modifiers no longer exists in v2 (`:select-distinct` replaces :select and :modifiers as separate things).

dcj20:03:30

Sorry, I over simplified my example with and confused things. I don't use strings, your re-write is basically indeed what I did both before and now

seancorfield20:03:53

Sounds like you’ll need to share more of what code you actually have then and compare the V1 and V2 data structures produced and what the formatter does with each. That’s why V2 has different namespaces so you can try them side-by-side.

bartuka20:03:31

hi!! is it possible to perform select from values in honeysql? => SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter); ? I am still getting the hang of it.

seancorfield21:03:50

In V2:

user=> (-> (select :*) (from [(values [[1, "one"] [2, "two"] [3, "three"]]) :t]) (sql/format))
["SELECT * FROM (VALUES (?, ?), (?, ?), (?, ?)) AS t" 1 "one" 2 "two" 3 "three"]

👍 3
seancorfield21:03:42

Not sure about the (num,letter) piece at the end… I don’t even understand that SQL syntax…

dcj23:03:34

Here is the raw SQL string I am attempting to model in honey2:

(SELECT fid, pca_raw, dca, X.path[1] AS id, X.geom AS g, ST_M(geom) AS m FROM flts_close CROSS JOIN ST_DumpPoints(t_trajectory) AS X)
So:
(println (sql/format (-> (select :fid :pca_raw :dca [[:raw "X.path[1]"] :id] [:X.geom :g] [[:ST_M :geom] :m])
                                (from :flts_close)
                                (sql.helpers/cross-join [[:ST_DumpPoints :t_trajectory] :X]))))

[SELECT fid, pca_raw, dca, X.path[1] AS id, X.geom AS g, ST_M(geom) AS m FROM flts_close CROSS JOIN ST_DUMPPOINTS(t_trajectory) X]
Is the part after the CROSS JOIN the same as AS X above?

dcj23:03:22

Apparently yes!

seancorfield23:03:53

In cross join x as y, the x as y part is just an aliased pair (like other joins). I’ll have a think about that array indexing syntax but I don’t realistically think there’s anything I can do that would make it easier/neater in HoneySQL than wrapping [:raw ..] around the expressions.