aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure/contrib/sql')
-rw-r--r--src/clojure/contrib/sql/internal.clj43
-rw-r--r--src/clojure/contrib/sql/test.clj85
2 files changed, 128 insertions, 0 deletions
diff --git a/src/clojure/contrib/sql/internal.clj b/src/clojure/contrib/sql/internal.clj
new file mode 100644
index 00000000..6221d1e2
--- /dev/null
+++ b/src/clojure/contrib/sql/internal.clj
@@ -0,0 +1,43 @@
+;; 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.
+;;
+;; internal definitions for clojure.contrib.sql
+;;
+;; scgilardi (gmail)
+;; Created 3 October 2008
+
+(ns clojure.contrib.sql.internal)
+
+(def *db* {:connection nil :level 0})
+
+(defn connection
+ "Returns the current database connection or throws an exception."
+ []
+ (or (:connection *db*)
+ (throw (Exception. "no current database connection"))))
+
+(defn the-str
+ "Returns the name or string representation of x"
+ [x]
+ (if (instance? clojure.lang.Named x)
+ (name x)
+ (str x)))
+
+(defn properties
+ "Converts a map from keywords or symbols to values into a
+ java.util.Properties object that maps the same keys to the values with
+ all represented as strings."
+ [m]
+ (let [p (java.util.Properties.)]
+ (when m
+ (loop [[key & keys] (keys m)
+ [val & vals] (vals m)]
+ (.setProperty p (the-str key) (the-str val))
+ (when keys
+ (recur keys vals))))
+ p))
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")))))