Skip to content

Instantly share code, notes, and snippets.

@ebellani
Last active December 12, 2015 05:48
Show Gist options
  • Save ebellani/4724313 to your computer and use it in GitHub Desktop.
Save ebellani/4724313 to your computer and use it in GitHub Desktop.
(ns mimo-vacman.endurance
"Provides some functions and macros to abstract the endurance to a
RDBMS."
(:require [clojure.tools.logging :refer [info]]
[clojure.java.jdbc :refer [with-connection
with-query-results]]))
(def
^{:dynamic true
:doc "Should be used by all functions as the database. All those
functions should be surrounded by the 'with-db' macro."}
*current-db* nil)
(def test-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/mimo_vacman_test"
:user "root"
:password ""})
(def dev-db {:subprotocol "mysql"
:subname "//127.0.0.1:3306/mimo_vacman"
:user "root"
:password ""})
(defmacro with-db
"Sets the '*current-db* var. This is used so the database
environment can be easily adjusted, allowing for something similar
to rails environments. "
[db & body]
`(binding [*current-db* ~db]
(with-connection *current-db* ~@body)))
(with-db test-db
(with-query-results res
["SELECT * FROM application WHERE serial = ?" 1]
(first (doall res)))
(with-query-results res
["SELECT * FROM application WHERE serial = ?" 1]
(first (doall res)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment