Babashka pods for interacting with SQL databases.
Supported databases:
PRs for other SQL databases are welcome.
The following installation methods are available:
- Download a binary from Github releases
- With brew:
brew install borkdude/brew/pod-babashka-<db>
where<db>
must be substited with the database type, eitherhsqldb
orpostgresql
.
Pods from this repo require babashka v0.0.96 or later.
The pods expose these namespaces with vars, where <db>
must be substited with
the database type, either hsqldb
or postgresql
:
pod.babashka.<db>
:execute!
: similar tonext.jdbc/execute!
get-connection
: returns connection serialized using maps with a unique identifier keyclose-connection
: closes a connection returned fromget-connection
with-transaction
: similar tonext.jdbc/with-transaction
pod.babashka.<db>.sql
:insert-multi!
: similar tonext.jdbc.sql/insert-multi!
pod.babashka.<db>.transaction
:begin
: marks the begin of a transaction, expects connection returned fromget-connection
rollback
: rolls back transaction, expects connection returned fromget-connection
commit
: commits transaction, expects connection returned fromget-connection
More functions from next.jdbc can be added. PRs welcome.
An example using pod-babashka-postgresql
:
(require '[babashka.pods :as pods])
(pods/load-pod "pod-babashka-postgresql")
(require '[pod.babashka.postgresql :as pg])
(def db {:dbtype "postgresql"
:host "your-db-host-name"
:dbname "your-db"
:user "develop"
:password "develop"
:port 5432})
(pg/execute! db ["select version()"])
;;=> [{:version "PostgreSQL 9.5.18 on x86_64-pc-linux-gnu (Debian 9.5.18-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit"}]
An example using pod-babashka-hsqldb
:
(require '[babashka.pods :as pods])
(pods/load-pod "pod-babashka-hsqldb")
(require '[pod.babashka.hsqldb :as db])
(def db "jdbc:hsqldb:mem:testdb;sql.syntax_mys=true")
(db/execute! db ["create table foo ( foo int );"])
;;=> [#:next.jdbc{:update-count 0}]
(db/execute! db ["create table foo ( foo int );"])
;;=> ... error output from pod omitted
;;=> clojure.lang.ExceptionInfo: object name already exists: FOO in statement [create table foo ( foo int )] [at line 6, column 1]
(db/execute! db ["insert into foo values (1, 2, 3);"])
;;=> [#:next.jdbc{:update-count 3}]
(db/execute! db ["select * from foo;"])
;;=> [#:FOO{:FOO 1} #:FOO{:FOO 2} #:FOO{:FOO 3}]
A more elaborate example can be found here.
Set POD_DB_TYPE
to either hsqldb
or postgresql
.
Run script/compile
Run script/test
.
Copyright © 2020 Michiel Borkent
Distributed under the EPL License. See LICENSE.