Skip to content

Commit

Permalink
only create a connection pool if :use-pool? is true
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Jan 18, 2013
1 parent 41d495c commit e1624a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 37 deletions.
46 changes: 27 additions & 19 deletions src/korma/db.clj
Expand Up @@ -57,7 +57,9 @@
(default-connection my-new-conn)"
[spec]
{:pool (delay-pool spec)
{:pool (if (:make-pool? spec)
(delay-pool spec)
spec)
:options (conf/extract-options spec)})

(defmacro defdb
Expand All @@ -71,69 +73,75 @@
(defn postgres
"Create a database specification for a postgres database. Opts should include keys
for :db, :user, and :password. You can also optionally set host and port."
[{:keys [host port db]
:or {host "localhost", port 5432, db ""}
[{:keys [host port db make-pool?]
:or {host "localhost", port 5432, db "", make-pool? true}
:as opts}]
(merge {:classname "org.postgresql.Driver" ; must be in classpath
:subprotocol "postgresql"
:subname (str "//" host ":" port "/" db)}
:subname (str "//" host ":" port "/" db)
:make-pool? make-pool?}
opts))

(defn oracle
"Create a database specification for an Oracle database. Opts should include keys
for :user and :password. You can also optionally set host and port."
[{:keys [host port]
:or {host "localhost", port 1521}
[{:keys [host port make-pool?]
:or {host "localhost", port 1521, make-pool? true}
:as opts}]
(merge {:classname "oracle.jdbc.driver.OracleDriver" ; must be in classpath
:subprotocol "oracle:thin"
:subname (str "@" host ":" port)}
:subname (str "@" host ":" port)
:make-pool? make-pool?}
opts))

(defn mysql
"Create a database specification for a mysql database. Opts should include keys
for :db, :user, and :password. You can also optionally set host and port.
Delimiters are automatically set to \"`\"."
[{:keys [host port db]
:or {host "localhost", port 3306, db ""}
[{:keys [host port db make-pool?]
:or {host "localhost", port 3306, db "", make-pool? true}
:as opts}]
(merge {:classname "com.mysql.jdbc.Driver" ; must be in classpath
:subprotocol "mysql"
:subname (str "//" host ":" port "/" db)
:delimiters "`"}
:delimiters "`"
:make-pool? make-pool?}
opts))

(defn mssql
"Create a database specification for a mssql database. Opts should include keys
for :db, :user, and :password. You can also optionally set host and port."
[{:keys [user password db host port]
:or {user "dbuser", password "dbpassword", db "", host "localhost", port 1433}
[{:keys [user password db host port make-pool?]
:or {user "dbuser", password "dbpassword", db "", host "localhost", port 1433, make-pool? true}
:as opts}]
(merge {:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver" ; must be in classpath
:subprotocol "sqlserver"
:subname (str "//" host ":" port ";database=" db ";user=" user ";password=" password)}
:subname (str "//" host ":" port ";database=" db ";user=" user ";password=" password)
:make-pool? make-pool?}
opts))

(defn sqlite3
"Create a database specification for a SQLite3 database. Opts should include a key
for :db which is the path to the database file."
[{:keys [db]
:or {db "sqlite.db"}
[{:keys [db make-pool?]
:or {db "sqlite.db", make-pool? true}
:as opts}]
(merge {:classname "org.sqlite.JDBC" ; must be in classpath
:subprotocol "sqlite"
:subname db}
:subname db
:make-pool? make-pool?}
opts))

(defn h2
"Create a database specification for a h2 database. Opts should include a key
for :db which is the path to the database file."
[{:keys [db]
:or {db "h2.db"}
[{:keys [db make-pool?]
:or {db "h2.db", make-pool? true}
:as opts}]
(merge {:classname "org.h2.Driver" ; must be in classpath
:subprotocol "h2"
:subname db}
:subname db
:make-pool? make-pool?}
opts))

(defmacro transaction
Expand Down
52 changes: 34 additions & 18 deletions test/korma/test/db.clj
Expand Up @@ -52,40 +52,47 @@
(testing "postgres - defaults"
(is (= {:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname "//localhost:5432/"}
:subname "//localhost:5432/"
:make-pool? true}
(postgres {}))))
(testing "postgres - options selected"
(is (= {:db "db"
:port "port"
:host "host"
:classname "org.postgresql.Driver"
:subprotocol "postgresql"
:subname "//host:port/db"}
:subname "//host:port/db"
:make-pool? false}
(postgres {:host "host"
:port "port"
:db "db"})))))
:db "db"
:make-pool? false})))))

(deftest test-oracle
(testing "oracle - defaults"
(is (= {:classname "oracle.jdbc.driver.OracleDriver"
:subprotocol "oracle:thin"
:subname "@localhost:1521"}
:subname "@localhost:1521"
:make-pool? true}
(oracle {}))))
(testing "oracle - options selected"
(is (= {:port "port"
:host "host"
:classname "oracle.jdbc.driver.OracleDriver"
:subprotocol "oracle:thin"
:subname "@host:port"}
:subname "@host:port"
:make-pool? false}
(oracle {:host "host"
:port "port"})))))
:port "port"
:make-pool? false})))))

(deftest test-mysql
(testing "mysql - defaults"
(is (= {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost:3306/"
:delimiters "`"}
:delimiters "`"
:make-pool? true}
(mysql {}))))
(testing "mysql - options selected"
(is (= {:db "db"
Expand All @@ -94,16 +101,19 @@
:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//host:port/db"
:delimiters "`"}
:delimiters "`"
:make-pool? false}
(mysql {:host "host"
:port "port"
:db "db"})))))
:db "db"
:make-pool? false})))))

(deftest test-mssql
(testing "mssql - defaults"
(is (= {:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver"
:subprotocol "sqlserver"
:subname "//localhost:1433;database=;user=dbuser;password=dbpassword"}
:subname "//localhost:1433;database=;user=dbuser;password=dbpassword"
:make-pool? true}
(mssql {}))))
(testing "mssql - options selected"
(is (= {:db "db"
Expand All @@ -113,35 +123,41 @@
:host "host"
:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver"
:subprotocol "sqlserver"
:subname "//host:port;database=db;user=user;password=password"}
:subname "//host:port;database=db;user=user;password=password"
:make-pool? false}
(mssql {:host "host"
:port "port"
:db "db"
:user "user"
:password "password"})))))
:password "password"
:make-pool? false})))))

(deftest test-sqlite3
(testing "sqlite3 - defaults"
(is (= {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "sqlite.db"}
:subname "sqlite.db"
:make-pool? true}
(sqlite3 {}))))
(testing "sqlite3 - options selected"
(is (= {:db "db"
:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "db"}
(sqlite3 {:db "db"})))))
:subname "db"
:make-pool? false}
(sqlite3 {:db "db" :make-pool? false})))))

(deftest test-h2
(testing "h2 - defaults"
(is (= {:classname "org.h2.Driver"
:subprotocol "h2"
:subname "h2.db"}
:subname "h2.db"
:make-pool? true}
(h2 {}))))
(testing "h2 - options selected"
(is (= {:db "db"
:classname "org.h2.Driver"
:subprotocol "h2"
:subname "db"}
(h2 {:db "db"})))))
:subname "db"
:make-pool? false}
(h2 {:db "db" :make-pool? false})))))

0 comments on commit e1624a3

Please sign in to comment.