blob: 04238c5f813927f870736e01cc3f481aff526061 (
plain)
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
|
;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
;; distribution terms for this software are covered by the Common Public
;; License 1.0 (http://opensource.org/licenses/cpl.php) which can be found
;; in the file CPL.TXT at the root of this distribution. By using this
;; software in any fashion, you are agreeing to be bound by the terms of
;; this license. You must not remove this notice, or any other, from this
;; software.
;;
;; test.clj
;;
;; test/example for clojure.contrib.sql
;;
;; scgilardi (gmail)
;; Created 13 September 2008
(ns clojure.contrib.sql.test
(:require [clojure.contrib.sql :as sql]))
(def db {:classname "org.apache.derby.jdbc.EmbeddedDriver"
:subprotocol "derby"
:subname "/tmp/clojure.contrib.sql.test.db"
:create true})
(defn drop-fruit []
(try
(sql/drop-table :fruit)
(catch Exception e)))
(defn create-fruit []
(sql/create-table :fruit
[:name "varchar(32)" "NOT NULL"]
[:appearance "varchar(32)"]
[:cost :int]
[:grade :real]))
(defn insert-rows-fruit []
(sql/insert-rows :fruit
["Apple" "red" 59 87]
["Banana" "yellow" 29 92.2]
["Peach" "fuzzy" 139 90.0]
["Orange" "juicy" 89 88.6]))
(defn insert-values-fruit []
(sql/insert-values :fruit
[:name :cost]
["Mango" 722]
["Feijoa" 441]))
(defn db-write []
(sql/with-connection db
(sql/transaction
(drop-fruit)
(create-fruit)
(insert-rows-fruit)
(insert-values-fruit)))
nil)
(defn db-read []
(sql/with-connection db
(sql/with-results res
"select * from fruit"
(doseq [rec res]
(println rec)))))
(defn db-read-all []
(sql/with-connection db
(sql/with-results res
"select * from fruit"
(into [] res))))
(defn db-grade-a []
(sql/with-connection db
(sql/with-results res
"select name, cost from fruit where grade >= 90"
(doseq [rec res]
(println rec)))))
(defn db-exception []
(sql/with-connection db
(sql/transaction
(sql/insert-values :fruit
[:name :appearance]
["Grape" "yummy"]
["Pear" "bruised"])
(throw (Exception. "an exception")))))
|