Permalink
Browse files

Add support for URL params

  • Loading branch information...
1 parent 09ddafa commit ed81de35d772490e6e097f4bf12d573665c8155f @jkutner jkutner committed Dec 16, 2015
Showing with 16 additions and 1 deletion.
  1. +7 −1 src/to_jdbc_uri/core.clj
  2. +9 −0 test/to_jdbc_uri/core_test.clj
@@ -14,6 +14,10 @@
(not-empty))]
(str "?" user-and-pass)))))
+(defn- format-query [uri sep-char]
+ (when-let [raw-query (.getRawQuery uri)]
+ (str sep-char raw-query)))
+
(defn- port [port]
(when-not (neg? port)
port))
@@ -25,7 +29,9 @@
(str "jdbc:" db "://"
(host-and-port uri)
(.getPath uri)
- (format-credentials uri)))
+ (if-let [credentials (format-credentials uri)]
+ (str credentials (format-query uri "&"))
+ (format-query uri "?"))))
(defn to-jdbc-uri
"Convert a non-JDBC URI to a JDBC one."
@@ -26,6 +26,15 @@
(testing "Heroku-like MySQL URI with port, username and password"
(is (= (to-jdbc-uri "mysql://username:password@hostname:1234/dbname")
"jdbc:mysql://hostname:1234/dbname?user=username&password=password")))
+ (testing "Retains arguments without credentials"
+ (is (= (to-jdbc-uri "postgres://hostname:1234/dbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")
+ "jdbc:postgresql://hostname:1234/dbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")))
+ (testing "Retains arguments and adds credentials"
+ (is (= (to-jdbc-uri "postgres://username:password@hostname:1234/dbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")
+ "jdbc:postgresql://hostname:1234/dbname?user=username&password=password&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")))
+ (testing "Retains arguments for existing JDBC URI"
+ (is (= (to-jdbc-uri "jdbc:postgresql://hostname:1234/dbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")
+ "jdbc:postgresql://hostname:1234/dbname?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory")))
(testing "Empty uri raises excetion"
(is (thrown? Exception (to-jdbc-uri "")))
(is (thrown? Exception (to-jdbc-uri nil)))))

0 comments on commit ed81de3

Please sign in to comment.