Back

get-by-id (clj)

(source)

function

(get-by-id connectable table pk) (get-by-id connectable table pk opts) (get-by-id connectable table pk pk-name opts)
Syntactic sugar over `execute-one!` to make certain common queries easier. Given a connectable object, a table name, and a primary key value, returns a hash map of the first row that matches. By default, the primary key is assumed to be `id` but that can be overridden in the five-argument call. As with `find-by-keys`, you can specify `:columns` to return just a subset of the columns in the returned row. Technically, this also supports `:order-by`, `:top`, `:limit`, `:offset`, and `:fetch` -- like `find-by-keys` -- but they don't make as much sense here since only one row is ever returned.

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-get-by-id
  (let [ds-opts (jdbc/with-options (ds) (default-options))]
    (is (nil? (sql/get-by-id ds-opts :fruit -1)))
    (let [row (sql/get-by-id ds-opts :fruit 3)]
      (is (map? row))
      (is (= "Peach" ((column :FRUIT/NAME) row))))
    (let [row (sql/get-by-id ds-opts :fruit "juicy" :appearance {})]
      (is (map? row))
      (is (= 4 ((column :FRUIT/ID) row)))
      (is (= "Orange" ((column :FRUIT/NAME) row))))
    (let [row (sql/get-by-id ds-opts :fruit "Banana" :FRUIT/NAME {})]
      (is (map? row))
      (is (= 2 ((column :FRUIT/ID) row))))))

(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})))))
seancorfield/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-get-by-id
  (let [ds-opts (jdbc/with-options (ds) (default-options))]
    (is (nil? (sql/get-by-id ds-opts :fruit -1)))
    (let [row (sql/get-by-id ds-opts :fruit 3)]
      (is (map? row))
      (is (= "Peach" ((column :FRUIT/NAME) row))))
    (let [row (sql/get-by-id ds-opts :fruit "juicy" :appearance {})]
      (is (map? row))
      (is (= 4 ((column :FRUIT/ID) row)))
      (is (= "Orange" ((column :FRUIT/NAME) row))))
    (let [row (sql/get-by-id ds-opts :fruit "Banana" :FRUIT/NAME {})]
      (is (map? row))
      (is (= 2 ((column :FRUIT/ID) row))))))

(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})))))