honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
exit2 2020-01-27T16:49:45.000500Z

Is it possible to do bulk delete w/ honeysql?

exit2 2020-01-27T16:49:59.000800Z

(sql/format {:delete-from :distributors
                        :where [:> :did :10]
                        :returning [:*] }))

exit2 2020-01-27T16:50:15.001300Z

Looking at the tests I see something like this, but canโ€™t figure out how to allow it to have multiple values

seancorfield 2020-01-27T17:49:50.001700Z

@njj Not sure what you mean by "have multiple values"?

seancorfield 2020-01-27T17:50:18.002300Z

The :where clause can be [:and cond1 cond2] etc if that's what you mean.

๐Ÿ‘ 1
seancorfield 2020-01-27T17:50:53.002800Z

Not sure what you expect :returning to produce on a delete?

exit2 2020-01-27T18:03:31.003100Z

I donโ€™t need the return

exit2 2020-01-27T18:03:37.003400Z

but basically delete multiple rows by id

seancorfield 2020-01-27T18:13:35.005100Z

I find the HoneySQL helper functions to be a much easier way to build the data structures, just FYI:

(-> (delete-from :distributors)
    (where [:> :did 10] [:> :foo 42])
    (sql/format))

exit2 2020-01-27T18:13:54.005300Z

I agree

seancorfield 2020-01-27T18:17:42.006200Z

Are you trying to delete where id in [vector of ids]? [:in :id vector-of-ids]?

exit2 2020-01-27T18:18:10.006600Z

yes basically Iโ€™ll have ["123", "321", "456"], etc..

seancorfield 2020-01-27T18:19:05.007400Z

(-> (delete-from :distributors)
    (where [:in :id vector-of-ids])
    (sql/format))

๐Ÿ‘ 1
dharrigan 2020-01-27T18:49:19.007700Z

honeysql ftw! ๐Ÿ™‚

๐ŸŽ‰ 1
exit2 2020-01-27T21:29:28.008200Z

@seancorfield How would this for multi update?

exit2 2020-01-27T21:29:48.008800Z

(-> (update table)
    (values data)
    hsql/format)

seancorfield 2020-01-27T21:40:18.009300Z

@njj Does it generate the SQL you want?

seancorfield 2020-01-27T21:40:37.009800Z

(I've no idea what you're trying to do so I can't comment on what HoneySQL code you would need)

exit2 2020-01-27T21:53:15.010100Z

no it doesnโ€™t seem to

exit2 2020-01-27T21:53:28.010500Z

Iโ€™m just trying to do a batch update

exit2 2020-01-27T21:53:54.010700Z

upsert works:

exit2 2020-01-27T21:54:08.011200Z

(-> (insert-into table)
                  (values data-vec)
                  (upsert (-> (on-conflict :id)
                              (do-update-set :record)))
                  hsql/format)

seancorfield 2020-01-27T22:35:56.011600Z

upsert isn't part of HoneySQL so I don't know what it does.

seancorfield 2020-01-27T22:36:34.012500Z

But that's an insert with something additional added. Not a batch update. But if that's what you're trying to do, fair enough.

2020-01-27T22:38:48.014200Z

If you do want to update a bunch of records with update instead of upsert, you might look into batch execution with a prepared statement.

2020-01-27T22:39:11.015200Z

Which doesn't really have anything to do with honeysql

seancorfield 2020-01-27T22:39:38.015800Z

Yeah, doing a batch insert and expecting it to do it all in one round-trip to the DB is going to depend on a lot of things. I have some information about that in the next.jdbc docs...

seancorfield 2020-01-27T22:41:35.017300Z

https://cljdoc.org/d/seancorfield/next.jdbc/1.0.13/doc/getting-started/prepared-statements covers some of the quirks around this, including the (different) JDBC connection options that MySQL and PostgreSQL need as hints in order to rewrite multi-row inserts behind the scenes so they might happen as a single DB statement -- assuming you don't run foul of the various limits on parameters etc.

seancorfield 2020-01-27T22:42:09.018200Z

(and as that page indicates, you may be better off using execute-batch! anyway if you have a large set of parameter groups)

seancorfield 2020-01-27T22:43:36.019200Z

^ @njj

๐Ÿ‘ 1