-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
53 lines (45 loc) · 2.05 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(ns in-any-all.core
(:require [next.jdbc :as jdbc]
[next.jdbc.sql :as sql]
[clojure.string :as str]
[next.jdbc.prepare :as p])
(:import [java.sql PreparedStatement]))
(def db {:dbtype "postgresql" :dbname "databasename"})
(def ds (jdbc/get-datasource db))
(extend-protocol p/SettableParameter
clojure.lang.IPersistentVector
(set-parameter [v ^PreparedStatement s i]
(let [conn (.getConnection s)
meta (.getParameterMetaData s)
type-name (.getParameterTypeName meta i)]
(if-let [elem-type (when (= (first type-name) \_)
(apply str (rest type-name)))]
(.setObject s i (.createArrayOf conn elem-type (to-array v)))
(.setObject i s v)))))
(comment
(jdbc/execute!
ds
["create table user_info (pid serial primary key, name text not null)"])
(jdbc/execute! ds ["create unique index user_info_unique ON user_info(name)"])
(sql/insert! ds :user_info {:name "Bob"})
(sql/insert! ds :user_info {:name "Jane"})
(sql/insert! ds :user_info {:name "Megan"})
(sql/insert! ds :user_info {:name "Alice"})
(sql/query ds ["select * from user_info where name in(?, ?)" "Bob" "Jane"])
(sql/query ds ["select * from user_info where name not in(?, ?)" "Bob" "Jane"])
(sql/query ds
(let [names ["Bob" "Jane"]]
(into [(str "select * from user_info where name in ("
(str/join ", " (repeat (count names) "?"))
")")]
names)))
(sql/query ds
["select * from user_info where name = any(?)"
(into-array String ["Bob" "Jane"])])
(sql/query ds
["select * from user_info where name != all(?)"
(into-array String ["Bob" "Jane"])])
(sql/query ds ["select * from user_info where name = any(?)" ["Bob" "Jane"]])
(sql/query ds ["select * from user_info where name != all(?)" ["Bob" "Jane"]])
(sql/query ds ["select * from user_info where pid != all(?)" [1 2]])
(sql/query ds ["select * from user_info where pid = any(?)" [1 2]]))