Mercurial > lasercutter
view src/clojure/contrib/test_contrib/test_sql.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; test.clj10 ;;11 ;; test/example for clojure.contrib.sql12 ;;13 ;; scgilardi (gmail)14 ;; Created 13 September 200816 (ns clojure.contrib.test-sql17 (:use [clojure.contrib.sql :as sql :only ()]))19 (def db {:classname "org.apache.derby.jdbc.EmbeddedDriver"20 :subprotocol "derby"21 :subname "/tmp/clojure.contrib.sql.test.db"22 :create true})24 (defn create-fruit25 "Create a table"26 []27 (sql/create-table28 :fruit29 [:name "varchar(32)" "PRIMARY KEY"]30 [:appearance "varchar(32)"]31 [:cost :int]32 [:grade :real]))34 (defn drop-fruit35 "Drop a table"36 []37 (try38 (sql/drop-table :fruit)39 (catch Exception _)))41 (defn insert-rows-fruit42 "Insert complete rows"43 []44 (sql/insert-rows45 :fruit46 ["Apple" "red" 59 87]47 ["Banana" "yellow" 29 92.2]48 ["Peach" "fuzzy" 139 90.0]49 ["Orange" "juicy" 89 88.6]))51 (defn insert-values-fruit52 "Insert rows with values for only specific columns"53 []54 (sql/insert-values55 :fruit56 [:name :cost]57 ["Mango" 722]58 ["Feijoa" 441]))60 (defn insert-records-fruit61 "Insert records, maps from keys specifying columns to values"62 []63 (sql/insert-records64 :fruit65 {:name "Pomegranate" :appearance "fresh" :cost 585}66 {:name "Kiwifruit" :grade 93}))68 (defn db-write69 "Write initial values to the database as a transaction"70 []71 (sql/with-connection db72 (sql/transaction73 (drop-fruit)74 (create-fruit)75 (insert-rows-fruit)76 (insert-values-fruit)77 (insert-records-fruit)))78 nil)80 (defn db-read81 "Read the entire fruit table"82 []83 (sql/with-connection db84 (sql/with-query-results res85 ["SELECT * FROM fruit"]86 (doseq [rec res]87 (println rec)))))89 (defn db-update-appearance-cost90 "Update the appearance and cost of the named fruit"91 [name appearance cost]92 (sql/update-values93 :fruit94 ["name=?" name]95 {:appearance appearance :cost cost}))97 (defn db-update98 "Update two fruits as a transaction"99 []100 (sql/with-connection db101 (sql/transaction102 (db-update-appearance-cost "Banana" "bruised" 14)103 (db-update-appearance-cost "Feijoa" "green" 400)))104 nil)106 (defn db-update-or-insert107 "Updates or inserts a fruit"108 [record]109 (sql/with-connection db110 (sql/update-or-insert-values111 :fruit112 ["name=?" (:name record)]113 record)))115 (defn db-read-all116 "Return all the rows of the fruit table as a vector"117 []118 (sql/with-connection db119 (sql/with-query-results res120 ["SELECT * FROM fruit"]121 (into [] res))))123 (defn db-grade-range124 "Print rows describing fruit that are within a grade range"125 [min max]126 (sql/with-connection db127 (sql/with-query-results res128 [(str "SELECT name, cost, grade "129 "FROM fruit "130 "WHERE grade >= ? AND grade <= ?")131 min max]132 (doseq [rec res]133 (println rec)))))135 (defn db-grade-a136 "Print rows describing all grade a fruit (grade between 90 and 100)"137 []138 (db-grade-range 90 100))140 (defn db-get-tables141 "Demonstrate getting table info"142 []143 (sql/with-connection db144 (into []145 (resultset-seq146 (-> (sql/connection)147 (.getMetaData)148 (.getTables nil nil nil (into-array ["TABLE" "VIEW"])))))))150 (defn db-exception151 "Demonstrate rolling back a partially completed transaction on exception"152 []153 (sql/with-connection db154 (sql/transaction155 (sql/insert-values156 :fruit157 [:name :appearance]158 ["Grape" "yummy"]159 ["Pear" "bruised"])160 ;; at this point the insert-values call is complete, but the transaction161 ;; is not. the exception will cause it to roll back leaving the database162 ;; untouched.163 (throw (Exception. "sql/test exception")))))165 (defn db-sql-exception166 "Demonstrate an sql exception"167 []168 (sql/with-connection db169 (sql/transaction170 (sql/insert-values171 :fruit172 [:name :appearance]173 ["Grape" "yummy"]174 ["Pear" "bruised"]175 ["Apple" "strange" "whoops"]))))177 (defn db-batchupdate-exception178 "Demonstrate a batch update exception"179 []180 (sql/with-connection db181 (sql/transaction182 (sql/do-commands183 "DROP TABLE fruit"184 "DROP TABLE fruit"))))186 (defn db-rollback187 "Demonstrate a rollback-only trasaction"188 []189 (sql/with-connection db190 (sql/transaction191 (prn "is-rollback-only" (sql/is-rollback-only))192 (sql/set-rollback-only)193 (sql/insert-values194 :fruit195 [:name :appearance]196 ["Grape" "yummy"]197 ["Pear" "bruised"])198 (prn "is-rollback-only" (sql/is-rollback-only))199 (sql/with-query-results res200 ["SELECT * FROM fruit"]201 (doseq [rec res]202 (println rec))))203 (prn)204 (sql/with-query-results res205 ["SELECT * FROM fruit"]206 (doseq [rec res]207 (println rec)))))