PageRenderTime 35ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/clojure/contrib/sql/test.clj

https://github.com/mjm/clojure-contrib
Clojure | 191 lines | 157 code | 17 blank | 17 comment | 4 complexity | c56c433abda85e1d2b3bcdfb30b998d0 MD5 | raw file
  1. ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
  2. ;; distribution terms for this software are covered by the Eclipse Public
  3. ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
  4. ;; be found in the file epl-v10.html at the root of this distribution. By
  5. ;; using this software in any fashion, you are agreeing to be bound by the
  6. ;; terms of this license. You must not remove this notice, or any other,
  7. ;; from this software.
  8. ;;
  9. ;; test.clj
  10. ;;
  11. ;; test/example for clojure.contrib.sql
  12. ;;
  13. ;; scgilardi (gmail)
  14. ;; Created 13 September 2008
  15. (ns clojure.contrib.sql.test
  16. (:require [clojure.contrib.sql :as sql]))
  17. (def db {:classname "org.apache.derby.jdbc.EmbeddedDriver"
  18. :subprotocol "derby"
  19. :subname "/tmp/clojure.contrib.sql.test.db"
  20. :create true})
  21. (defn create-fruit
  22. "Create a table"
  23. []
  24. (sql/create-table
  25. :fruit
  26. [:name "varchar(32)" "PRIMARY KEY"]
  27. [:appearance "varchar(32)"]
  28. [:cost :int]
  29. [:grade :real]))
  30. (defn drop-fruit
  31. "Drop a table"
  32. []
  33. (try
  34. (sql/drop-table :fruit)
  35. (catch Exception _)))
  36. (defn insert-rows-fruit
  37. "Insert complete rows"
  38. []
  39. (sql/insert-rows
  40. :fruit
  41. ["Apple" "red" 59 87]
  42. ["Banana" "yellow" 29 92.2]
  43. ["Peach" "fuzzy" 139 90.0]
  44. ["Orange" "juicy" 89 88.6]))
  45. (defn insert-values-fruit
  46. "Insert rows with values for only specific columns"
  47. []
  48. (sql/insert-values
  49. :fruit
  50. [:name :cost]
  51. ["Mango" 722]
  52. ["Feijoa" 441]))
  53. (defn db-write
  54. "Write initial values to the database as a transaction"
  55. []
  56. (sql/with-connection
  57. db
  58. (sql/transaction
  59. (drop-fruit)
  60. (create-fruit)
  61. (insert-rows-fruit)
  62. (insert-values-fruit)))
  63. nil)
  64. (defn db-read
  65. "Read the entire fruit table"
  66. []
  67. (sql/with-connection
  68. db
  69. (sql/with-query-results
  70. res
  71. ["SELECT * FROM fruit"]
  72. (doseq [rec res]
  73. (println rec)))))
  74. (defn db-update-appearance-cost
  75. "Update the appearance and cost of the named fruit"
  76. [name appearance cost]
  77. (sql/update-values
  78. :fruit
  79. ["name=?" name]
  80. {:appearance appearance :cost cost}))
  81. (defn db-update
  82. "Update two fruits as a transaction"
  83. []
  84. (sql/with-connection
  85. db
  86. (sql/transaction
  87. (db-update-appearance-cost "Banana" "bruised" 14)
  88. (db-update-appearance-cost "Feijoa" "green" 400)))
  89. nil)
  90. (defn db-update-or-insert
  91. "Updates or inserts a fruit"
  92. [record]
  93. (sql/with-connection
  94. db
  95. (sql/update-or-insert-values
  96. :fruit
  97. ["name=?" (:name record)]
  98. record)))
  99. (defn db-read-all
  100. "Return all the rows of the fruit table as a vector"
  101. []
  102. (sql/with-connection
  103. db
  104. (sql/with-query-results
  105. res
  106. ["SELECT * FROM fruit"]
  107. (into [] res))))
  108. (defn db-grade-range
  109. "Print rows describing fruit that are within a grade range"
  110. [min max]
  111. (sql/with-connection
  112. db
  113. (sql/with-query-results
  114. res
  115. [(str "SELECT name, cost, grade "
  116. "FROM fruit "
  117. "WHERE grade >= ? AND grade <= ?")
  118. min max]
  119. (doseq [rec res]
  120. (println rec)))))
  121. (defn db-grade-a
  122. "Print rows describing all grade a fruit (grade between 90 and 100)"
  123. []
  124. (db-grade-range 90 100))
  125. (defn db-get-tables
  126. "Demonstrate getting table info"
  127. []
  128. (sql/with-connection
  129. db
  130. (into []
  131. (resultset-seq
  132. (-> (sql/connection)
  133. (.getMetaData)
  134. (.getTables nil nil nil (into-array ["TABLE" "VIEW"])))))))
  135. (defn db-exception
  136. "Demonstrate rolling back a partially completed transaction on exception"
  137. []
  138. (sql/with-connection
  139. db
  140. (sql/transaction
  141. (sql/insert-values
  142. :fruit
  143. [:name :appearance]
  144. ["Grape" "yummy"]
  145. ["Pear" "bruised"])
  146. ;; at this point the insert-values call is complete, but the transaction
  147. ;; is not. the exception will cause it to roll back leaving the database
  148. ;; untouched.
  149. (throw (Exception. "sql/test exception")))))
  150. (defn db-rollback
  151. "Demonstrate a rollback-only trasaction"
  152. []
  153. (sql/with-connection
  154. db
  155. (sql/transaction
  156. (prn "is-rollback-only" (sql/is-rollback-only))
  157. (sql/set-rollback-only)
  158. (sql/insert-values
  159. :fruit
  160. [:name :appearance]
  161. ["Grape" "yummy"]
  162. ["Pear" "bruised"])
  163. (prn "is-rollback-only" (sql/is-rollback-only))
  164. (sql/with-query-results
  165. res
  166. ["SELECT * FROM fruit"]
  167. (doseq [rec res]
  168. (println rec))))
  169. (prn)
  170. (sql/with-query-results
  171. res
  172. ["SELECT * FROM fruit"]
  173. (doseq [rec res]
  174. (println rec)))))