Fork me on GitHub
#honeysql
<
2022-09-30
>
slipset11:09:26

New day, new question. I would love to be able to create a temporary table, but there doesn’t seem to be a hh/create-tmp-table (or underlying support for {:create-tmp-table ...} ?

slipset11:09:28

I ended up with (at least for now)

(honey.sql/register-clause! :create-temp-table (fn [_ args]
                                                 [(str "CREATE TEMP TABLE " (name args) " AS")])
                            :select)

slipset11:09:57

Next question: I want to duplicate some rows in a table while changing one column. I don’t want to enumerate all the columns, so I’ve come up with this:

CREATE TEMP TABLE tmp AS (select * from foo where lol = 42);
UPDATE tmp SET lol = 43;
INSERT INTO foo (SELECT * from tmp);
DROP TABLE tmp;
(I’d be happy to get recommendations for better solutions) So these seem to be four separate sql statements, which means I need to call honey/format on a vector containing these, as my honey atm looks like:
(let [tmp-table (gensym)]
    [(-> {:create-temp-table (keyword tmp-table)}
         (hh/select :*)
         (hh/from (keyword "foo")
         (hh/where [:= :lol 42))
     (-> (hh/update (keyword tmp-table))
         (hh/set {:lol 43}))
     (-> (hh/insert-into [(keyword "foo")
                          (-> (hh/select :*)
                              (hh/from (keyword tmp-table)))]))
     (hh/drop-table tmp-table)]))
And then, somewhere else, I’d need: (map honey/format xs) Is this the approach or am I missing somehting?

slipset12:09:38

Oh, my colleague was smarter than me: (hh/create-table-as :temp (keyword tmp-table)) gives me a temp table 🙂

seancorfield17:09:01

Yeah, I should probably call that out in the docs that all the :create* operations accept one or more keywords and just make SQL out of them 🙂

seancorfield17:09:05

user=> (sql/format (-> (create-table-as :fee :fie :foe :fum) (select :*) (from :bar)))
["CREATE FEE FIE FOE TABLE fum AS SELECT * FROM bar"]
user=>

slipset19:09:29

And, just in case you weren't aware, I'm really appreciative of the work you do with next.jdbc and honey. They're such a pleasure to work with!

gratitude 3
seancorfield19:09:29

Thank you! It's always nice to hear that. As you well know, as a project maintainer, all you tend to see is a steady stream of complaints about documentation and bugs and questions that are often answered by RTFM 🙂

❤️ 4
lread20:09:57

To jump on the gratitude bandwagon: I’ve only used honeysql once, but absolutely loved it! It was early on in my Clojure journey and the power of seeing SQL statements as easy to manipulate data really opened my eyes. I look forward to using it again!