joelash / ez-database

Handling database queries with ease

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ez-database

Handling database queries with different libraries. Support for yesql, honeysql, sequentials (string + params) and plain strings.

rationale

yesql is nice when I know the query beforehand. Honey SQL is nice when I don't. Just a plain string with some args is sometimes the best option. And sometimes I really want to just send in a vector that's been reduced.

protocol

(defprotocol IEzDatabase
  (query [database query] [database key? query] [database key query args])
  (query! [database query] [database key? query] [database key query args])
  (query<! [database query] [database key? query] [database key query args])
  (databases [database]))
  • database which is a record is always the first field.
  • query always returns a result set.
  • query! executes a command and does not return a result set.
  • query<! executes a command and gives back a value. Used for insert

Usage

Download from clojars

[ez-database "0.3.0"]

Assuming a database with the following schema.

CREATE TABLE test (
  id integer
);
INSERT INTO test VALUES (0), (42);
(require '[ez-database.core :as db])

(def db-spec {:classname "org.postgresql.Driver"
              :subprotocol "postgresql"
              :subname "//localhost:5432/test"
              :user "user"
              :password "password"}})

;; add extra database specs under different keys
;; default is a required key
(def db (db/map->EzDatabase {:db-specs {:default db-spec}}))

Given above code...

query

with strings

(db/query db "select * from test;") ;; => [{:id 0} {:id 42}]
(db/query db "select * from test where id > ?" [0]) ;; => [{:id 42}]

with sequentials

(db/query db ["select * from test where id > ?" 0]) ;; => [{:id 42}]

with yesql functions

--name: sql-select-query
  SELECT * FROM test WHERE id > :id;
(db/query db sql-select-query {:id 0});; => [{:id 42}]

with honeysql hashmaps

(db/query db {:select [:*]
              :from [:test]
              :where [:> :id 0]}) ;; => [{:id 42}]

execute (no return value asked for)

with string

(db/query! db "delete from test;")
(db/query! db "delete from test where id > ?;" [0])

with sequential

(db/query! db ["delete from test where id > ?;" 0])
(db/query! db ["delete from test where id > ?;"] [0])

with yesql functions

--name: sql-delete-higher-than!
  DELETE FROM test WHERE id > :id;
(db/query! db sql-delete-higher-than! {:id 0})

with honeysql hashmaps

(db/query! db {:delete-from :test
               :where [:> :id 0]})

inserts

with strings

;; return scalar value
(db/query<! db "insert into test values (-1);") ;; => [1]
;; return value
(db/query<! db "test" [{:id -1}]) ;; => [{:id -1}]

with sequentials

;; return scalar value
(db/query<! db ["insert into test values (?);" -1]) ;; => [1]

with yesql functions

--name: sql-insert<!
  INSERT INTO test VALUES (:id);

Note that yesql functions returns the actual value, not a sequence of values.

(db/query<! db sql-insert<! {:id -1}) ;; => {:id -1}

with honeysql hashmaps

(db/query<! db {:insert-into :test 
                :values [{:id -1}]}) ;; => [{:id -1}]

databases

Returns a list of keys for the databases in the record.

(db/database db) ;; => (:default)

more than one database

Assuming databases :default and :foobar we can do selects against both of them.

(require '[ez-database.core :as db])

(def db-spec {:classname "org.postgresql.Driver"
              :subprotocol "postgresql"
              :subname "//localhost:5432/test"
              :user "user"
              :password "password"}})
              
(def db-spec-foo {:classname "org.postgresql.Driver"
                  :subprotocol "postgresql"
                  :subname "//localhost:5432/foobar"
                  :user "user"
                 :password "password"}})

;; add extra database specs under different keys
;; default is a required key
(def db (db/map->EzDatabase {:db-specs {:default db-spec
                                        :foobar db-spec-foo}}))

(db/query db "select * from test;") ;; => [{:id 0} {:id 42}]
(db/query db :foobar "select * from foo where id > ?" [0]) ;; => [{:id 42 :what_p "How many bars does it take for foo to be happy?"}]

missing

No good support for rollbacks yet.

License

Copyright © 2015 Emil Bengtsson

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.


Coram Deo

About

Handling database queries with ease

License:Eclipse Public License 1.0


Languages

Language:Clojure 100.0%