Commits

Shantanu Kumar committed c1a29e4

add SQLite datasource creation functions and unit tests

Comments (0)

Files changed (2)

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

   :classname :url :username :password)
 
 
+(defn make-datasource-args
+  "Create an instance of 'datasource-args' using given args."
+  [classname url username password]
+  (struct datasource-args
+    classname
+    url
+    username
+    password))
+
+
 (defn set-max-active!
   "Set max active connections at a time in the pool. You should call this before
   creating any connection from the datasource."
       (javax.naming.InitialContext.) resource-ref-name)))
 
 
-(def ^{:doc "Default name for the in-memory database"}
+(def ^{:doc "Default name for the database"}
       default-db-name "default")
 
 
               db-path)]
     (if *show-jdbc-url*
       (println "\n**** Derby JDBC URL ****\n" url))
-    (struct datasource-args
+    (make-datasource-args
       "org.apache.derby.jdbc.EmbeddedDriver" ; classname: must be in classpath
       url ; url
       "sa" ; username
   (let [url (str "jdbc:h2:" db-protocol ":" db-path)]
     (if *show-jdbc-url*
       (println "\n**** H2 JDBC URL ****\n" url))
-    (struct datasource-args
+    (make-datasource-args
       "org.h2.Driver" ; classname: must be in classpath
       url ; url
       "sa" ; username
   (let [url (str "jdbc:hsqldb:" db-protocol ":" db-path)]
     (if *show-jdbc-url*
       (println "\n**** HSQL JDBC URL ****\n" url))
-    (struct datasource-args
+    (make-datasource-args
       "org.hsqldb.jdbcDriver" ; classname: must be in classpath
       url ; url
       "sa" ; username
     (mysql-args \"localhost\"      \"sales\" \"salesuser\" \"secret\")
     (mysql-args \"localhost:3306\" \"emp\"   \"empuser\"   \"SeCrEt\")"
   [db-host:port db-name username password]
-  (struct datasource-args
+  (make-datasource-args
     "com.mysql.jdbc.Driver" ; classname: must be in classpath
     (str "jdbc:mysql://" db-host:port "/" db-name) ; url
     username ; username
     (pgsql-args \"localhost\"      \"sales\" \"salesuser\" \"secret\")
     (pgsql-args \"localhost:5432\" \"emp\"   \"empuser\"   \"SeCrEt\")"
   [db-host:port db-name username password]
-  (struct datasource-args
+  (make-datasource-args
     "org.postgresql.Driver" ; classname: must be in classpath
     (str "jdbc:postgresql://" db-host:port "/" db-name) ; url
     username ; username
     (oracle-args \"localhost\"      \"sales\" \"salesuser\" \"secret\")
     (oracle-args \"localhost:1521\" \"emp\"   \"empuser\"   \"SeCrEt\")"
   [db-host:port system-id username password]
-  (struct datasource-args
+  (make-datasource-args
     "oracle.jdbc.driver.OracleDriver" ; classname: must be in classpath
     (str "jdbc:oracle:thin:@//" db-host:port "/" system-id) ; url
     username ; username
     (db2-args \"localhost\"       \"sales\" \"salesuser\" \"secret\")
     (db2-args \"localhost:50000\" \"emp\"   \"empuser\"   \"SeCrEt\")"
   [db-host:port db-name username password]
-  (struct datasource-args
+  (make-datasource-args
     "com.ibm.db2.jcc.DB2Driver" ; classname: must be in classpath
     (str "jdbc:db2://" db-host:port "/" db-name) ; url
     username ; username
   See also: (1) props-str function
             (2) http://jtds.sourceforge.net/faq.html"
   [server-type db-host:port db-name username password properties]
-  (struct datasource-args
+  (make-datasource-args
     "net.sourceforge.jtds.jdbc.Driver" ; classname: must be in classpath
     ;; JDBC URL
     (format "jdbc:jtds:%s://%s%s%s" server-type db-host:port
   ([db-host:port username password]
     (sybase-datasource
       db-host:port nil username password nil)))
+
+
+; === SQLite-DB config ===
+;
+(defn sqlite-args
+  "Create datasource args for the SQLite database. SQLite database can be
+  hosted off the memory, or a file. Example JDBC URLs it should create are:
+    jdbc:sqlite:C:/work/mydatabase.db        -- file
+    jdbc:sqlite:/home/leo/work/mydatabase.db -- file
+    jdbc:sqlite::memory:                     -- memory
+  Arguments:
+    db-path (String) for file   -- <filepath>
+                     for memory -- :memory:
+  Example:
+    (sqlite-args \"C:/work/mydatabase.db\")
+    (sqlite-args \":memory:\")
+  Maven/Lein/Cake Dependencies:
+    org.xerial/sqlite-jdbc/3.7.2 (or a later version)
+  See also:
+    http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC"
+  [db-path]
+  (let [url (str "jdbc:sqlite:" db-path)]
+    (if *show-jdbc-url*
+      (println "\n**** SQLite JDBC URL ****\n" url))
+    (make-datasource-args
+      "org.sqlite.JDBC" ; classname: must be in classpath
+      url ; url
+      "sa" ; username
+      ""   ; password
+      )))
+
+
+(defn sqlite-datasource
+  "Create an SQLite data source.
+  See also: sqlite-args"
+  ([db-path]
+    (create-datasource (sqlite-args db-path)))
+  ([]
+    (sqlite-datasource ":memory:")))
+
+
+(defn sqlite-filesystem-datasource
+  "Create a filesystem (file) based SQLite datasource.
+  See also: sqlite-datasource, sqlite-args"
+  ([db-path]
+    (sqlite-datasource db-path))
+  ([]
+    (sqlite-datasource (str "sqlite" default-db-name))))
+
+
+(defn sqlite-memory-datasource
+  "Create an in-memory SQLite datasource.
+  See also: sqlite-datasource, sqlite-args"
+  ([]
+    (sqlite-datasource)))
+
+

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

         (stop-hsql-server server)))))
 
 
+(deftest test-sqlite
+  ;; in-memory (no dbname)
+  (test-datasource
+    (sqlite-memory-datasource))
+  ;; file system (default dbname)
+  (test-datasource
+    (sqlite-filesystem-datasource))
+  ;; file system
+  (test-datasource
+    (sqlite-filesystem-datasource "sqlitefile"))
+  )
+
+
 (defn test-network-datasource
   "Test a network JDBC datasource - MySQL, PostgreSQL, Oracle, DB2, jTDS etc.
   dbtoken must be same as first token (before period) in dbcred.properties file.
   (test-derby)
   (test-h2)
   (test-hsql)
+  (test-sqlite)
   (test-mysql)
   (test-pgsql)
   (smoketest-untested-datasources)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.