Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert semantics via INSERT ... ON DUPLICATE KEY UPDATE Syntax #112

Open
rboyd opened this issue Jan 17, 2013 · 5 comments
Open

Upsert semantics via INSERT ... ON DUPLICATE KEY UPDATE Syntax #112

rboyd opened this issue Jan 17, 2013 · 5 comments

Comments

@rboyd
Copy link

rboyd commented Jan 17, 2013

Handling upsert logic in the application can introduce tricky race conditions. MySQL offers upsert semantics as detailed in INSERT ... ON DUPLICATE KEY UPDATE Syntax

What will korma's philosophy be on how to handle dbms-specific implementations? Is there room for introducing per-dbms aux libs?

I attempted to construct an upsert query using existing korma insert/update/raw fns, but was unable to come up with anything workable.

@jmglov
Copy link

jmglov commented Jun 27, 2014

I did this from outside Korma, then Robert Hooked it in:

(ns foo
  (:require [korma.sql.engine :as korma-engine]
            [korma.core :refer :all]
            [robert.hooke :refer [add-hook]]))

(defn upsert
  [f & [q :as args]]
  (let [result (apply f args)]
    (if-let [[k v] (:upsert q)]
      (assoc result :sql-str (format "%s ON DUPLICATE KEY UPDATE %s = %s"
                                     (:sql-str result) (name k) v))
      result)))

(add-hook #'korma-engine/sql-insert #'upsert)

(defentity bar)

(defn upsert-bar! [id timestamp]
  (-> (insert* bar)
      (values [{:id id, :timestamp timestamp}])
      (assoc :upsert [:timestamp timestamp])
      (insert)))

Perhaps something like this could be folded into Korma with an upsert macro so you could say:

(upsert bar [:timestamp timestamp]
 (values [{:id id, :timestamp timestamp}]))

@afandian
Copy link

afandian commented Dec 6, 2014

Bump. It would be great if this was supported properly, with the correct handling of types (e.g. date).

@immoh
Copy link
Member

immoh commented Dec 11, 2014

As far as I know, this is supported by MySQL only. Currently there are now plans to start supporting features specific to single RDBMS.

@ku1ik
Copy link

ku1ik commented Feb 9, 2016

Not: PostgreSQL supports UPSERT since 9.5.

@immoh
Copy link
Member

immoh commented Feb 12, 2016

@sickill: But with different syntax compared to MySQL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants