2

I want to be able to write the following functions for jdbc:

(def db {:classname "com.mysql.jdbc.Driver"
         :subprotocol "mysql"
         :subname "//127.0.0.1:3306/santacsv"
         :server "//127.0.0.1:3306"
         :schema "santa"
         :user "root"
         :password "root"})

(defn has-schema? [db & [schema]])

(defn create-schema [db & [schema]])

(defn drop-schema [db & [schema]])

I'm not sure how to go about it. Any help would be appreciated

2 Answers 2

1

At a minimum, you could use clojure.java.jdbc and write the corresponding SQL to execute!.

Alternatively, I would consider using a dedicated migrations library/framework. flyway is java-based and has a java-api which you could call with interop.

Finally, there are a few clojure-based migrations libraries like lobos, but I can't speak to their maturity, so your mileage may very.

Sign up to request clarification or add additional context in comments.

Comments

0

I implemented my own here:

https://github.com/zcaudate/manas/blob/master/src/manas/core.clj

    (defmacro run-statement [env database body catch-body & [statement]]
      `(do (Class/forName (:classname ~env))
           (let [~'conn (atom nil)]
             (try
               (reset! ~'conn (DriverManager/getConnection
                              (str "jdbc:" (:subprotocol ~env) ":"
                                   (:server ~env) "/" (or ~database (:database ~env)))
                              (:user ~env)
                              (:password ~env)))
               ~@(if statement
                   [(list 'let '[stmt (.createStatement @conn)]
                          (list '.executeUpdate 'stmt statement))])
               ~@body
               (catch SQLException ~'e
                 ~@catch-body)
               (finally
                 (if-not (nil? (deref ~'conn))
                   (.close (deref ~'conn))))))))

    (defn has-connection? [env & [url]]
      (let [env (if url (assoc env :server url) env)]
        (run-statement env "" (true) (false))))

    (defn all-databases [env]
      (run-statement env ""
                     ((let [rs (.getCatalogs (.getMetaData @conn))]
                        (loop [rs rs acc []]
                          (if (.next rs)
                            (recur rs (conj acc (.getString rs "TABLE_CAT")))
                            acc))))
                     ()))

    (defn has-database? [env & [schema]]
      (run-statement env schema (true) (false)))

    (defn create-database [env & [schema]]
      (run-statement env ""
                     (true)
                     (false)
                     (str "create database " (or schema (:database env)))))

    (defn drop-database [env & [schema]]
      (run-statement env ""
                     (true)
                     (false)
                     (str "drop database " (or schema (:database env)))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.