-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.clj
94 lines (80 loc) · 2.51 KB
/
user.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns user
(:require
[clojure.tools.namespace.repl :as repl]
[next.jdbc :as nxt]))
(repl/disable-unload!)
(repl/disable-reload!)
(require '[manifold.deferred :as d])
(comment
(repl/refresh)
(require 'later.jdbc)
(def db
(nxt/get-datasource
{:dbtype "postgres"
:dbname "postgres"
:host "localhost"
:user "postgres"
:password "postgres"}))
(def bad-db
(nxt/get-datasource
{:dbtype "postgres"
:dbname "postgres"
:host "localhost"
:user "postgres"
:password (reduce str (reverse "postgres"))}))
(def create-tweets-table
'{create-table (tweets if-not-exists)
with-columns
((tweeter_id bigint not-null)
(tweet text not-null)
((constraint tweet_max_length) (check (< (length tweet) 140)))
((constraint tweet_min_length) (check (<= 4 (length tweet)))))})
(-> (later.jdbc/inspect create-tweets-table)
(println))
(later.jdbc/q db create-tweets-table)
(later.jdbc/q db '{insert-into (tweets :tweeter_id :tweet)
returning *
values
((0, "from jack")
(0, "hi @elon")
(2, "go away @jack")
(2, "@jack, i'd buy X from u tho...")
(0, "@elon X what...?"))})
(later.jdbc/q db '{select * from tweets})
(deref
(d/catch
(d/chain
(later.jdbc/q& bad-db '{select * from tweets})
#(mapv
(fn [m]
(reduce-kv
(fn [m' k v]
(assoc m' (keyword (name k)) v))
{}
m))
%))
(fn [e]
(println "error>" e))))
(repl/refresh)
(deref
(later.jdbc/tx& [t db]
(later.jdbc/q t '{insert-into (tweets id msg)
values
((10, "random tweet"))})
; will throw
#_@(later.jdbc/tx& [tt db]
(later.jdbc/q tt '{insert-into (tweets id msg)
values
((100, "im dangerous"))}))
; this'll throw, also
#_(later.jdbc/tx [tt db]
(later.jdbc/q tt '{insert-into (tweets id msg)
values
((101, "im dangerous"))}))
#_(throw (ex-info "error" {}))
(later.jdbc/q t '{insert-into (tweets id msg)
values
((11, "other tweet"))})
(later.jdbc/q t '{select * from tweets}
{:limit 3})))
)