Skip to content
This repository has been archived by the owner on Aug 12, 2019. It is now read-only.

Commit

Permalink
Initial commit, v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jjl committed Apr 21, 2016
0 parents commit cc6f702
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT LICENSE

Copyright (c) 2016 James Laver

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
The irresponsible clojure guild presents...

# utrecht

Modern postgres made easy

## What is it?

Just enough of a shim to pull together database pooling
([HikariCP](https://github.com/brettwooldridge/hikaricp/), conversion
between clojure/java and postgres datatypes
([mpg](https://github.com/ShaneKilKelly/mpg/)) and to make it easy to
work with transactions correctly.

## Platform Support

This module requires JDK 8. Please upgrade to JDK 8 to improve
security and performance of your applications.

This module depends on the latest version of the postgres driver at
time of release. You should be able to use it with any non-ancient
version of postgres.

Note that we have explicitly provided full support for prepared
statements. Prepared statements allows the parser and planner to run
once ahead of time and swap in values as required. They're useful if
you wish to execute one particular query many times in
succession. With that, there is a caveat. Some optimisations are not
performed against prepared queries because the actual values required
are needed to perform them. That will include partial indexes where
you don't provide an explicit value for that column.

## Usage

```
(ns my.db
(:require [irresponsible.utrecht :as u]))
(u/setup!) ;; install the inflation/deflation hooks
(def pool (u/make-pool hikari-pool-opts))
;; Several ways of doing the same thing
(def bars (u/with-conn [conn pool]
(u/with-prep [q "select * from foo where bar = ?"]
(u/query conn q ["bar"])))
(def bazs (u/with-conn [conn pool] ; execute unprepared
(u/run conn "SELECT * FROM foo WHERE bar = ?" ["baz"])))
;; just execute some sql against a pool
(def quuxs (u/q pool "SELECT * FROM foo WHERE bar = ?" ["quux"]))
;;
;; During shutdown you'll want to close the pool
(.close pool)
```

## Copyright and License

Copyright (c) 2016 James Laver

MIT LICENSE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 changes: 39 additions & 0 deletions build.boot
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(set-env!
:project 'irresponsible/utrecht
:version "0.1.0"
:resource-paths #{"src"}
:source-paths #{"src"}
:dependencies '[[org.clojure/clojure "1.8.0" :scope "provided"]
[org.clojure/java.jdbc "0.6.0-alpha1"]
[cheshire "5.6.1"]
[hikari-cp "1.6.1"]
[mpg "0.2.0"]
[clojure.java-time "0.2.0"]
[org.postgresql/postgresql "9.4.1208"]
[adzerk/boot-test "1.1.0" :scope "test"]])


(require '[adzerk.boot-test :as t])

(task-options!
pom {:project (get-env :project)
:version (get-env :version)
:description ""
:url "https://github.com/irresponsible/utrecht"
:scm {:url "https://github.com/irresponsible/utrecht.git"}
:license {"MIT" "https://en.wikipedia.org/MIT_License"}}
target {:dir #{"target"}})

(deftask testing []
(set-env! :source-paths #(conj % "test")
:resource-paths #(conj % "test"))
identity)

(deftask test []
(comp (testing) (speak) (t/test)))

(deftask autotest []
(comp (testing) (watch) (speak) (t/test)))

(deftask make-release-jar []
(comp (pom) (jar)))
109 changes: 109 additions & 0 deletions src/irresponsible/utrecht.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
(ns irresponsible.utrecht
(:require [mpg.core :as mpg]
[clojure.java.jdbc :as j]
[hikari-cp.core :as h]))

(def setup
"Installs conversion hooks for postgres datatypes
args: []
returns: nil"
mpg/patch)

(defn make-pool
"Given a HikariCP configuration map, creates a pool
Args: [conf]
conf should be a map of parameters as accepted by hikari
Returns: pool"
[conf]
;; apply defaults
{:datasource
(-> {:adapter "postgresql"
:port-number 5432
:server-name "localhost"
:auto-commit false}
;; :connection-timeout 30000
;; :validation-timeout 5000
;; :idle-timeout 600000
;; :max-lifetime 1800000
;; :minimum-idle 10
;; :maximum-pool-size 10
;; :pool-name "db-pool"
;; :register-mbeans false
(merge conf)
h/make-datasource)})

(def close-pool
"Closes the given pool
args: [pool]
returns: nil"
(comp h/close-datasource :datasource))

(defmacro with-conn [& exprs]
`(j/with-db-connection ~@exprs))

(def prep j/prepare-statement)

(defmacro with-prep [conn names & exprs]
(when (not= 0 (mod (count names) 2))
(throw (ex-info "with-prepared assignment vector is uneven" {})))
(letfn [(f [[name sql]]
[name `(prep (:connection ~conn) ~sql)])]
(let [parts (partition 2 names)
names (map first parts)
assigns (vec (mapcat f parts))
closes (mapv (partial list '.close) names)]
`(let ~assigns
(try
(do ~@exprs)
(finally ~@closes))))))

(defn query
"Executes a query that returns some results (e.g. select)
args: [conn sql bind-params-vec]
params should be a vector of param values
returns: resultset or nil"
[conn sql bind-params-vec]
(j/query conn (into [sql] bind-params-vec)))

(defn execute
"Executes a query that does not return a result with zero or more sets of params
args: [conn sql & bind-params-vecs]
returns: vector of update counts"
[conn q & bind-params-vecs]
(j/execute! conn (into [q] bind-params-vecs)
{:transaction? false :multi? true}))

(defn q [pool sql args]
(with-conn [conn pool]
(query conn sql args)))

(def isolations
#{:none
:read-uncommitted ; prevent dirty write
:read-committed ; prevent dirty read
:repeatable-read ; prevent fuzzy read
:serializable}) ; prevent phantoms

(defn transact
"Executes a given function within a transaction
args: [conn isolation read-only? func]
conn: a connection as obtained through with-conn
isolation: one of :none, :read-uncommitted, :read-committed,
:repeatable-read, :serializable
read-only?: boolean"
[conn isolation read-only? func]
(when-not (isolations isolation)
(throw (ex-info (str "transact: invalid isolation: " isolation)
{:got isolation :valid isolations})))
(j/db-transaction* conn func {:isolation isolation :read-only? read-only?}))

(defmacro in-transaction
"[macro] Executes code within a transaction
args: [conn isolation roness & exprs]
conn: a connection as obtained through with-conn
isolation: one of :none, :read-uncommitted, :read-committed,
:repeatable-read, :serializable
writability: one of :ro, :rw. Whether to lock for read only"
[conn isolation roness & exprs]
`(transact ~conn ~isolation ~(case roness :ro true :rw false)
(fn [~'_] ~@exprs)))

0 comments on commit cc6f702

Please sign in to comment.