diff options
author | scgilardi <scgilardi@gmail.com> | 2008-11-12 22:31:20 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-11-12 22:31:20 +0000 |
commit | 6f7a77e9cdc8df4f58fd86eed0dcd0dd6fc2d6fd (patch) | |
tree | 84939b269427bb8213c7db3e002628aa13816bd3 /src/clojure/contrib/sql/test.clj | |
parent | db748f4c8b6f37da894b3c8f7a3bb683eea3f0aa (diff) |
first cut at changes for Clojure SVN 1094+, my contribs and ones they depend on now load again
Diffstat (limited to 'src/clojure/contrib/sql/test.clj')
-rw-r--r-- | src/clojure/contrib/sql/test.clj | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/clojure/contrib/sql/test.clj b/src/clojure/contrib/sql/test.clj new file mode 100644 index 00000000..04238c5f --- /dev/null +++ b/src/clojure/contrib/sql/test.clj @@ -0,0 +1,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"))))) |