Skip to content

Commit

Permalink
v0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Mar 14, 2021
1 parent 92f46df commit d4fffdf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

For a list of breaking changes, check [here](#breaking-changes)

## v0.0.5

- Fix insertion and retrieving of byte arrays

## v0.0.4

- Automatically convert json and jsonb into Clojure values (for PostgreSQL only)
Expand Down
2 changes: 1 addition & 1 deletion resources/POD_BABASHKA_SQL_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.4
0.0.5
25 changes: 14 additions & 11 deletions src/pod/babashka/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@

;; Default implementation
(defn serialize [opts x]
(cond
#_? (instance? java.sql.Array x)
#_=> (let [arr (.getArray ^java.sql.Array x)
coerce-opt (get-in opts [:pod.babashka.sql/read :array])
coerced (case coerce-opt
:array {::val (vec arr)
::read :array}
(vec arr))]
coerced)
:else #_=> x))
(cond
#_? (instance? java.sql.Array x)
#_=> (let [arr (.getArray ^java.sql.Array x)
coerce-opt (get-in opts [:pod.babashka.sql/read :array])
coerced (case coerce-opt
:array {::val (vec arr)
::read :array}
(vec arr))]
coerced)
:else #_=> x))

(when-pg
(defn serialize [opts x]
Expand Down Expand Up @@ -248,7 +248,10 @@
(pr-str '(defn -serialize-1 [x]
(if-let [c (class x)]
(if
(.isArray c)
(and (.isArray c)
(not (bytes? x)) ;; bytes can he handled by transit
;; natively
,)
{::write :array
::val (vec x)}
(let [m (meta x)
Expand Down
43 changes: 24 additions & 19 deletions test/pod/babashka/postgresql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
{:clj-kondo/config
'{:lint-as {pod.babashka.postgresql/with-transaction next.jdbc/with-transaction}}}
(:require [babashka.pods :as pods]
[cheshire.core :as json]
[clojure.test :refer [deftest is testing]])
(:import [com.opentable.db.postgres.embedded EmbeddedPostgres]
[java.util Date]))
[java.util Date Arrays]))

(pods/load-pod (if (= "native" (System/getenv "POD_TEST_ENV"))
"./pod-babashka-postgresql"
Expand Down Expand Up @@ -79,23 +78,29 @@
#:foo{:foo 5} #:foo{:foo 6} #:foo{:foo 7}]
(db/execute! db ["select * from foo;"]))))))
(testing "arrays"
(is (db/execute! db ["create table bar ( bar integer[] );"]))
(is (db/execute! db ["insert into bar values (?);" (into-array [1 2 3])]))
(is (db/execute! db ["insert into bar values (?);" ^{:pod.babashka.sql/write :array} [4 5 6]]))
(is (= [#:bar{:bar [1 2 3]} #:bar{:bar [4 5 6]}] (db/execute! db ["select * from bar"])))
(let [arrays (map :bar/bar
(db/execute! db ["select * from bar"]
{:pod.babashka.sql/read {:array :array}}))
vecs (map vec arrays)]
(is (every? #(.isArray (class %)) arrays))
(is (= [[1 2 3] [4 5 6]] vecs)))
(is (db/execute! db ["create table baz ( baz text[] );"]))
(is (db/execute! db ["insert into baz values (?);" (into-array ["foo" "bar"])]))
(is (= [#:baz{:baz ["foo" "bar"]}] (db/execute! db ["select * from baz"])))
(is (= #:baz{:baz ["foo" "bar"]} (db/execute-one! db ["select * from baz"])))
(is (= [#:baz{:baz ["a" "b"]} #:baz{:baz ["x" "y"]}]
(sql/insert-multi! db :baz [:baz] [[(into-array ["a" "b"])]
[(into-array ["x" "y"])]]))))
(testing "byte arrays"
(let [bs (.getBytes "foo")]
(is (db/execute! db ["create table bytes ( bs bytea );"]))
(is (db/execute! db ["insert into bytes values (?);" bs]))
(is (Arrays/equals bs (:bytes/bs (db/execute-one! db ["select * from bytes;"]))))))
(testing "non-byte arrays"
(is (db/execute! db ["create table bar ( bar integer[] );"]))
(is (db/execute! db ["insert into bar values (?);" (into-array [1 2 3])]))
(is (db/execute! db ["insert into bar values (?);" ^{:pod.babashka.sql/write :array} [4 5 6]]))
(is (= [#:bar{:bar [1 2 3]} #:bar{:bar [4 5 6]}] (db/execute! db ["select * from bar"])))
(let [arrays (map :bar/bar
(db/execute! db ["select * from bar"]
{:pod.babashka.sql/read {:array :array}}))
vecs (map vec arrays)]
(is (every? #(.isArray (class %)) arrays))
(is (= [[1 2 3] [4 5 6]] vecs)))
(is (db/execute! db ["create table baz ( baz text[] );"]))
(is (db/execute! db ["insert into baz values (?);" (into-array ["foo" "bar"])]))
(is (= [#:baz{:baz ["foo" "bar"]}] (db/execute! db ["select * from baz"])))
(is (= #:baz{:baz ["foo" "bar"]} (db/execute-one! db ["select * from baz"])))
(is (= [#:baz{:baz ["a" "b"]} #:baz{:baz ["x" "y"]}]
(sql/insert-multi! db :baz [:baz] [[(into-array ["a" "b"])]
[(into-array ["x" "y"])]])))))
(testing "json"
(is (db/execute! db ["create table json_table ( json_col json );"]))
(is (db/execute! db ["insert into json_table values (?);" ^{:pod.babashka.sql/write :json} {:a 1}]))
Expand Down

0 comments on commit d4fffdf

Please sign in to comment.