Back

update! (clj)

(source)

function

(update! connectable table key-map where-params) (update! connectable table key-map where-params opts)
Syntactic sugar over `execute-one!` to make certain common updates easier. Given a connectable object, a table name, a hash map of columns and values to set, and either a hash map of columns and values to search on or a vector of a SQL where clause and parameters, perform an update on the table.

Examples

next-jdbc
(ns next.jdbc.sql-test
  "Tests for the syntactic sugar SQL functions."
  (:require [clojure.test :refer [deftest is testing use-fixtures]]
            [next.jdbc :as jdbc]
            [next.jdbc.specs :as specs]
            [next.jdbc.sql :as sql]
            [next.jdbc.test-fixtures
             :refer [with-test-db ds column default-options
                     derby? jtds? maria? mssql? mysql? postgres? sqlite?]]
            [next.jdbc.types :refer [as-other as-real as-varchar]]))

(deftest test-update!
  (let [ds-opts (jdbc/with-options (ds) (default-options))]
    (try
      (is (= {:next.jdbc/update-count 1}
             (sql/update! ds-opts :fruit {:appearance "brown"} {:id 2})))
      (is (= "brown" ((column :FRUIT/APPEARANCE)
                      (sql/get-by-id ds-opts :fruit 2))))
      (finally
        (sql/update! ds-opts :fruit {:appearance "yellow"} {:id 2})))
    (try
      (is (= {:next.jdbc/update-count 1}
             (sql/update! ds-opts :fruit {:appearance "green"}
                          ["name = ?" "Banana"])))
      (is (= "green" ((column :FRUIT/APPEARANCE)
                      (sql/get-by-id ds-opts :fruit 2))))
      (finally
        (sql/update! ds-opts :fruit {:appearance "yellow"} {:id 2})))))

(deftest no-empty-example-maps
  (is (thrown? clojure.lang.ExceptionInfo
               (sql/find-by-keys (ds) :fruit {})))
  (is (thrown? clojure.lang.ExceptionInfo
               (sql/update! (ds) :fruit {} {})))
  (is (thrown? clojure.lang.ExceptionInfo
               (sql/delete! (ds) :fruit {}))))
jacekschae/learn-reitit-course-files
(ns user
  (:require [integrant.repl :as ig-repl]
            [integrant.core :as ig]
            [integrant.repl.state :as state]
            [cheffy.server]
            [next.jdbc :as jdbc]
            [next.jdbc.sql :as sql]))

  (sql/update! db :recipe {:name "my-recipe"} {:recipe-id "a3dde84c-4a33-45aa-b0f3-4bf9ac997680"})
jacekschae/learn-reitit-course-files
(ns cheffy.conversation.db
  (:require [next.jdbc.sql :as sql]
            [next.jdbc :as jdbc])
  (:import (java.util UUID)))

(defmethod dispatch :clear-notifications
  [[_ db data]]
  (-> (sql/update! db :conversation {:notifications 0} data)
    :next.jdbc/update-count
    (pos?)))
jacekschae/learn-reitit-course-files
(ns cheffy.conversation.db
  (:require [next.jdbc.sql :as sql]
            [next.jdbc :as jdbc])
  (:import (java.util UUID)))

(defmethod dispatch :clear-notifications
  [[_ db data]]
  (-> (sql/update! db :conversation {:notifications 0} data)
      :next.jdbc/update-count
      (pos?)))
jacekschae/learn-reitit-course-files
(ns cheffy.conversation.db
  (:require [next.jdbc.sql :as sql]
            [next.jdbc :as jdbc])
  (:import (java.util UUID)))

(defmethod dispatch :clear-notifications
  [[_ db data]]
  (-> (sql/update! db :conversation {:notifications 0} data)
    :next.jdbc/update-count
    (pos?)))
jacekschae/learn-reitit-course-files
(ns cheffy.conversation.db
  (:require [next.jdbc.sql :as sql]
            [next.jdbc :as jdbc])
  (:import (java.util UUID)))

(defmethod dispatch :clear-notifications
  [[_ db data]]
  (-> (sql/update! db :conversation {:notifications 0} data)
      :next.jdbc/update-count
      (pos?)))