From 6cd384c215863cbe0a70fbe620ca698b2cc8850b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 24 Apr 2020 15:08:26 +0200 Subject: [PATCH] [#372] add support for jdbc + postgres --- deps.edn | 4 +++- doc/dev.md | 3 +++ project.clj | 4 +++- script/compile | 1 + src/babashka/impl/jdbc.clj | 31 +++++++++++++++++++++++++++++++ src/babashka/main.clj | 7 +++++-- 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/babashka/impl/jdbc.clj diff --git a/deps.edn b/deps.edn index 1d43466f..201c364a 100644 --- a/deps.edn +++ b/deps.edn @@ -11,7 +11,9 @@ org.clojure/data.xml {:mvn/version "0.2.0-alpha6"} fipp {:mvn/version "0.6.22"} clj-commons/clj-yaml {:mvn/version "0.7.1"} - com.cognitect/transit-clj {:mvn/version "1.0.324"}} + com.cognitect/transit-clj {:mvn/version "1.0.324"} + seancorfield/next.jdbc {:mvn/version "1.0.424"} + org.postgresql/postgresql {:mvn/version "42.2.5"}} :aliases {:main {:main-opts ["-m" "babashka.main"]} :profile diff --git a/doc/dev.md b/doc/dev.md index 30e29ad3..192e02e4 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -62,6 +62,9 @@ $ BABASHKA_XMX="-J-Xmx4g" script/compile Keep notes here about how adding libraries and classes to Babashka affects the binary size. We're registering the size of the macOS binary (as built on CircleCI). +2020/04/23 Added `next.jdbc` and postgres JDBC driver: +(- 51019836 48099780) = 2920kb added + 2020/04/23 Added BigDecimal (- 48103868 47857732) = 246kb added diff --git a/project.clj b/project.clj index 37f95090..ab6f8a61 100644 --- a/project.clj +++ b/project.clj @@ -23,7 +23,9 @@ [cheshire "5.10.0"] [fipp "0.6.22"] [clj-commons/clj-yaml "0.7.1"] - [com.cognitect/transit-clj "1.0.324"]] + [com.cognitect/transit-clj "1.0.324"] + [seancorfield/next.jdbc "1.0.424"] + [org.postgresql/postgresql "42.2.5"]] :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"] [com.clojure-goes-fast/clj-async-profiler "0.4.0"]]} :uberjar {:global-vars {*assert* false} diff --git a/script/compile b/script/compile index 69ed5424..b8a97590 100755 --- a/script/compile +++ b/script/compile @@ -46,6 +46,7 @@ args=( -jar $BABASHKA_JAR \ --no-fallback \ --no-server \ --report-unsupported-elements-at-runtime \ + "--initialize-at-run-time=org.postgresql.sspi.SSPIClient" \ "$BABASHKA_XMX" ) if [ "$BABASHKA_STATIC" = "true" ]; then diff --git a/src/babashka/impl/jdbc.clj b/src/babashka/impl/jdbc.clj new file mode 100644 index 00000000..c18efd83 --- /dev/null +++ b/src/babashka/impl/jdbc.clj @@ -0,0 +1,31 @@ +(ns babashka.impl.jdbc + {:no-doc true} + (:require [next.jdbc :as njdbc] + [sci.impl.namespaces :refer [copy-var]] + [sci.impl.vars :as vars])) + +(def next-ns (vars/->SciNamespace 'next.jdbc nil)) + +(defn with-transaction + "Given a transactable object, gets a connection and binds it to `sym`, + then executes the `body` in that context, committing any changes if the body + completes successfully, otherwise rolling back any changes made. + The options map supports: + * `:isolation` -- `:none`, `:read-committed`, `:read-uncommitted`, + `:repeatable-read`, `:serializable`, + * `:read-only` -- `true` / `false`, + * `:rollback-only` -- `true` / `false`." + [_ _ [sym transactable opts] & body] + (let [con (vary-meta sym assoc :tag 'java.sql.Connection)] + `(next.jdbc/transact ~transactable (^{:once true} fn* [~con] ~@body) ~(or opts {})))) + +(def njdbc-namespace + {'get-datasource (copy-var njdbc/get-datasource next-ns) + 'execute! (copy-var njdbc/execute! next-ns) + 'execute-one! (copy-var njdbc/execute-one! next-ns) + 'get-connection (copy-var njdbc/get-connection next-ns) + 'plan (copy-var njdbc/plan next-ns) + 'prepare (copy-var njdbc/prepare next-ns) + 'transact (copy-var njdbc/transact next-ns) + 'with-transaction (with-meta with-transaction + {:sci/macro true})}) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 8fe17245..38e08353 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -15,6 +15,7 @@ [babashka.impl.common :as common] [babashka.impl.csv :as csv] [babashka.impl.curl :refer [curl-namespace]] + [babashka.impl.jdbc :as jdbc] [babashka.impl.nrepl-server :as nrepl-server] [babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]] [babashka.impl.repl :as repl] @@ -257,7 +258,8 @@ Everything after that is bound to *command-line-args*.")) yaml clj-yaml.core curl babashka.curl transit cognitect.transit - bencode bencode.core}) + bencode bencode.core + jdbc next.jdbc}) (def cp-state (atom nil)) @@ -292,7 +294,8 @@ Everything after that is bound to *command-line-args*.")) 'clojure.pprint pprint-namespace 'babashka.curl curl-namespace 'cognitect.transit transit-namespace - 'bencode.core bencode-namespace}) + 'bencode.core bencode-namespace + 'next.jdbc jdbc/njdbc-namespace}) (def bindings {'java.lang.System/exit exit ;; override exit, so we have more control