Commits

Anonymous committed 7362435

separate API for network, filesystem and memory data sources
factor out set-validation-query! into a separate function

  • Participants
  • Parent commits 4cd21db

Comments (0)

Files changed (3)

 * BasicDataSource with driver classname, JDBC url, username and password
 * Support for validation query
 * Auto-select driver classname and validation query for supported database
-* No-arg API for launching in-memory instances of embedded databases
+* API for memory/filesystem/network based data sources for embedded databases
 * Supported+tested: Apache Derby, HSQLDB, H2, MySQL, PostgreSQL
 * Supported but not tested: Oracle, IBM DB2
 
     (ns example.app
       (:use org.bituf.clj-dbcp))
 
+This tutorial does not cover the entire API - feel free to browse the
+documentation and source code/unit tests.
+
 **Important: You must include the JDBC drivers on the classpath on your own.**
 
 
-## Create in-memory instance of Apache Derby database
+## Create data source for Apache Derby database
 
+    ;; in-memory instance
     (db-spec
-      (derby-datasource))
+      (derby-memory-datasource))
+
+    ;; filesystem instance
+    (db-spec
+      (derby-filesystem-datasource))
 
 Example usage (example/app.clj):
 
       (:use [clojure.contrib.sql :as sql :only ()]))
     
     (def db-derby (db-spec
-                    (derby-datasource))) ; creates in-memory instance by default
+                    (derby-memory-datasource))) ; creates in-memory instance
     
     (defn crud
       []
           (drop-table))))
 
 
-## Create in-memory instance of HSQL database
+## Create data source for H2 database
 
+    ;; in-memory instance
     (db-spec
-      (hsql-datasource))
+      (h2-memory-datasource))
+
+    ;; filesystem instance
+    (db-spec
+      (h2-filesystem-datasource))
 
 Example usage: see Derby example
 
 
-## Create in-memory instance of H2 database
+## Create data source for HSQLDB/HyperSQL database
 
+    ;; in-memory instance
     (db-spec
-      (h2-datasource))
+      (hsql-memory-datasource))
+
+    ;; filesystem instance
+    (db-spec
+      (hsql-filesystem-datasource))
+
 
 Example usage: see Derby example
 

File src/main/clj/org/bituf/clj_dbcp.clj

   :classname :url :username :password)
 
 
+(defn set-validation-query!
+  "Sets validation query for the datasource. You must call this *before*
+  creating any connection from the datasource."
+  [^BasicDataSource datasource ^String validation-query]
+  (doto datasource
+      (.setValidationQuery validation-query)
+      (.setTestOnBorrow  true)
+      (.setTestOnReturn  true)
+      (.setTestWhileIdle true))
+  datasource)
+
+
 (defn create-datasource
   "Create basic data source (instance of the BasicDataSource class).
   Arguments:
       (:classname datasource-args)
       (:url       datasource-args)
       (:username  datasource-args)
-      (:password  datasource-args)))
-  ([classname url username password validation-query] ; 5 args
-    (doto (create-datasource
-            classname url username password)
-      (.setValidationQuery validation-query)
-      (.setTestOnBorrow  true)
-      (.setTestOnReturn  true)
-      (.setTestWhileIdle true)))
-  ([datasource-args validation-query] ; 2 args
-    (create-datasource
-      (:classname datasource-args)
-      (:url       datasource-args)
-      (:username  datasource-args)
-      (:password  datasource-args)
-      validation-query)))
+      (:password  datasource-args))))
 
 
 (defn db-spec
 
 
 (defn derby-datasource
-  ([db-protocol db-path]
-    (create-datasource
-      (derby-args db-protocol db-path)))
+  "Helper function to create a Derby datasource
+  See also:
+    derby-network-datasource
+    derby-filesystem-datasource
+    derby-memory-datasource
+    derby-args"
+  [db-protocol db-path]
+  (create-datasource
+    (derby-args db-protocol db-path)))
+
+
+(defn derby-network-datasource
+  "Create a network based (connecting to a server) Derby datasource
+  See also: derby-datasource, derby-args"
+  [host:port db-name]
+  (derby-datasource
+    nil (format "//%s/%s" host:port db-name)))
+
+
+(defn derby-filesystem-datasource
+  "Create a filesystem (directory) based Derby datasource
+  See also: derby-datasource, derby-args"
   ([db-path]
-    (derby-datasource nil (str db-path ";create=true;")))
+    (derby-datasource
+      nil (str db-path ";create=true;")))
   ([]
-    (derby-datasource "memory" (str default-db-name ";create=true;"))))
+    (derby-filesystem-datasource default-db-name)))
+
+
+(defn derby-memory-datasource
+  "Create an in-memory Derby datasource
+  See also: derby-datasource, derby-args"
+  ([db-name]
+    (derby-datasource
+      "memory" (str db-name ";create=true;")))
+  ([]
+    (derby-memory-datasource
+      default-db-name)))
 
 
 ; === H2 config ===
 
 
 (defn h2-datasource
-  "Create H2 data source.
+  "Create an H2 data source
   See also: h2-args"
-  ([db-protocol db-path]
-    (create-datasource
-      (h2-args db-protocol db-path)))
+  [db-protocol db-path]
+  (create-datasource
+    (h2-args db-protocol db-path)))
+
+
+(defn h2-network-datasource
+  "Create a network based (connecting to a server) H2 datasource
+  See also: h2-datasource, h2-args"
+  [host:port db-path]
+  (h2-datasource
+    "tcp" (format "//%s/%s" host:port db-path)))
+
+
+(defn h2-filesystem-datasource
+  "Create a filesystem (directory) based H2 datasource
+  See also: h2-datasource, h2-args"
+  ([db-path]
+    (h2-datasource "file" db-path))
   ([]
-    (h2-datasource "mem" "")))
+    (h2-filesystem-datasource default-db-name)))
+
+
+(defn h2-memory-datasource
+  "Create an in-memory H2 datasource
+  See also: h2-datasource, h2-args"
+  ([db-name]
+    (h2-datasource
+      "mem" db-name))
+  ([]
+    (h2-memory-datasource "")))
 
 
 ; === HSQL-DB (HyperSQL) config ===
 
 
 (defn hsql-datasource
-  "Create HyperSQL data source.
+  "Create an HSQLDB/HyperSQL data source.
   See also: hsql-args"
   ([db-protocol db-path]
     (create-datasource
     (hsql-datasource "mem" default-db-name)))
 
 
+(defn hsql-network-datasource
+  "Create a network based (connecting to a server) HSQLDB/HyperSQL datasource
+  See also: hsql-datasource, hsql-args"
+  [host:port db-path]
+  (hsql-datasource
+    "hsql" (format "//%s/%s" host:port db-path)))
+
+
+(defn hsql-filesystem-datasource
+  "Create a filesystem (directory) based HSQLDB/HyperSQL datasource
+  See also: hsql-datasource, hsql-args"
+  ([db-path]
+    (hsql-datasource "file" db-path))
+  ([]
+    (hsql-filesystem-datasource default-db-name)))
+
+
+(defn hsql-memory-datasource
+  "Create an in-memory HSQLDB/HyperSQL datasource
+  See also: hsql-datasource, hsql-args"
+  ([db-name]
+    (hsql-datasource "mem" db-name))
+  ([]
+    (hsql-memory-datasource default-db-name)))
+
+
 ; === MySQL config ===
 ;
 (defn mysql-args
   "Create MySQL data source
   See also: mysql-args"
   [db-host:port db-name username password]
-  (create-datasource
-    (mysql-args
-      db-host:port db-name username password)
-    "SELECT 1;"))
+  (let [args (mysql-args
+               db-host:port db-name username password)
+        ds   (create-datasource args)]
+    (set-validation-query! ds
+      "SELECT 1;")))
 
 
 ; === PostgreSQL config ===
   "Create PostgreSQL data source
   See also: pgsql-args"
   [db-host:port db-name username password]
-  (create-datasource
-    (pgsql-args
-      db-host:port db-name username password)
-    "SELECT version();"))
+  (let [args (pgsql-args
+               db-host:port db-name username password)
+        ds   (create-datasource args)]
+    (set-validation-query! ds
+      "SELECT version();")))
 
 
 ; === Oracle config ===
   "Create Oracle data source
   See also: oracle-args"
   [db-host:port system-id username password]
-  (create-datasource
-    (oracle-args
-      db-host:port system-id username password)
-    "SELECT 1 FROM DUAL;"))
+  (let [args (oracle-args
+               db-host:port system-id username password)
+        ds   (create-datasource args)]
+    (set-validation-query! ds
+      "SELECT 1 FROM DUAL;")))
 
 
 ; === DB2 config ===
   "Create DB2 data source
   See also: db2-args"
   [db-host:port db-name username password]
-  (create-datasource
-    (oracle-args
-      db-host:port db-name username password)
-    "select * from sysibm.SYSDUMMY1;"))
+  (let [args (db2-args
+               db-host:port db-name username password)
+        ds   (create-datasource args)]
+    (set-validation-query! ds
+      "select * from sysibm.SYSDUMMY1;")))

File src/test/clj/org/bituf/test_clj_dbcp.clj

 
 
 (deftest test-derby
+  ;; in-memory (default dbname)
+  (test-datasource
+    (derby-memory-datasource))
   ;; in-memory
   (test-datasource
-    (derby-datasource))
+    (derby-memory-datasource "somesuch"))
+  ;; file system (default dbname)
+  (test-datasource
+    (derby-filesystem-datasource))
   ;; file system
   (test-datasource
-    (derby-datasource "derbyfile"))
+    (derby-filesystem-datasource "derbyfile"))
   ;; network server
   (let [server (start-derby-server)]
     (try
       (test-datasource
-        (derby-datasource "//localhost/derbyserver"))
+        (derby-network-datasource "localhost" "derbyserver"))
       (finally
         (stop-derby-server server)))))
 
 
-(deftest test-hsql
+(deftest test-h2
+  ;; in-memory (default dbname)
+  (test-datasource
+    (h2-memory-datasource))
   ;; in-memory
   (test-datasource
-    (hsql-datasource))
+    (h2-memory-datasource "somesuch"))
+  ;; file system (default dbname)
+  (test-datasource
+    (h2-filesystem-datasource))
   ;; file system
   (test-datasource
-    (hsql-datasource "file" "hsqlfile"))
+    (h2-filesystem-datasource "h2file"))
+  ;; network server
+  (let [server (start-h2-server)]
+    (try
+      (test-datasource
+        (h2-network-datasource "localhost" "h2server"))
+      (finally
+        (stop-h2-server server)))))
+
+
+(deftest test-hsql
+  ;; in-memory (default dbname)
+  (test-datasource
+    (hsql-memory-datasource))
+  ;; in-memory
+  (test-datasource
+    (hsql-memory-datasource "somesuch"))
+  ;; file system (default dbname)
+  (test-datasource
+    (hsql-filesystem-datasource))
+  ;; file system
+  (test-datasource
+    (hsql-filesystem-datasource "hsqlfile"))
   ;; network server
   (let [server (start-hsql-server "hsqlserver")]
     (try
       (test-datasource
-        (hsql-datasource "hsql" "//localhost/hsqlserver"))
+        (hsql-network-datasource "localhost" "hsqlserver"))
       (finally
         (stop-hsql-server server)))))
 
 
-(deftest test-h2
-  ;; in-memory
-  (test-datasource
-    (h2-datasource))
-  ;; file system
-  (test-datasource
-    (h2-datasource "file" "h2file"))
-  ;; network server
-  (let [server (start-h2-server)]
-    (try
-      (test-datasource
-        (h2-datasource "tcp" "//localhost/h2server"))
-      (finally
-        (stop-h2-server server)))))
-
-
 (deftest test-mysql
   (test-datasource
     (mysql-datasource "localhost" "cljdbcp" "root" "")))
 
 (defn test-ns-hook []
   (test-derby)
+  (test-h2)
   (test-hsql)
-  (test-h2)
   (test-mysql)
   (test-pgsql))