korma / Korma

Tasty SQL for Clojure.
http://sqlkorma.com
1.47k stars 222 forks source link

Using operators in update set-fields #206

Open terhechte opened 10 years ago

terhechte commented 10 years ago

Is there a way to use database operators in the set-fields part of an update operation?

i.e.

UPDATE statistics SET visits = visits + 1 WHERE id = 5;

I'm currently doing this with a complete (exec-raw) call. I know that I could query for document 5 and then process the increments or other operations in Clojure, but I'd rather not have to perform an additional select query to achieve this.

I've checked the docs and had a look at the source but could not find anything, I may have missed it though.

Gastove commented 9 years ago

Same problem here. Is there any hope?

We can do:

(update entity
  (set-fields {:col (raw "col +1")})
  (where {:id [= 5]}))

Any chance of a way to avoid raw all together?

immoh commented 9 years ago

There's no support for this in Korma.

To be honest, I don't even know how the API should look like but I'm happy to take a pull request if somebody wants to take a stab at this.

AlexAti commented 8 years ago

I think it would be best if you just passed a function that takes one argument, the old value, and returns the new value, like how one normally updates atoms with assoc(-in), update(-in), swap(-in) functions. That could be:

(update entity
  (swap-fields {:col inc})
  (where {:id [= 5]}))