hugsql

2019-06-11T10:28:49.003800Z

I have a big query that generates a bunch of transaction-only temp tables and then queries them

2019-06-11T10:29:37.004800Z

I can make this work as - an execute statement to generate 5 temp tablels - folllowed by a query of those tables

2019-06-11T10:29:49.005100Z

I can't make it work in a single query

2019-06-11T10:30:25.006Z

I think I'd rather have 5 execute queries, each generating one temp table

2019-06-11T10:30:43.006400Z

if I cannot roll the whole thing into one hugsql db-fn

2019-06-11T10:31:14.007100Z

is there any received wisdom on the best way to structure temporary-table queries in hugsql ?

2019-06-11T10:54:36.010800Z

so what I've done is create a bunch of distinct :command!s and a single :query? thus

...
-- :name create-stage1-temptable :!
CREATE TEMP TABLE stage1_temp ON COMMIT DROP
...
-- :name create-stage2-temptable :!
-- :name create-stage3-temptable :!
-- :name create-stage4-temptable :!
-- :name select-stuff-from-temptables :? :*
and then stitch them together inside the .clj file
(hugsql/def-db-fns "sql/multistage-query.sql" {:quoting :ansi})

(defn multistage-query
  [db customer-id]
  (let [data {:customer_id customer-id}]
    (jdbc/with-db-transaction
     [trans db]
      (create-stage1-temptable trans data)
      (create-stage2-temptable trans data)
      (create-stage3-temptable trans data)
      (create-stage4-temptable trans data)
      (select-stuff-from-temptables trans data))))

dorab 2019-06-11T15:00:41.011300Z

Sounds like a good approach to me.

1👍
2019-06-11T17:02:03.011600Z

so far so good