babashka/examples/pods/pod-babashka-hsqldb/test.clj

46 lines
1.6 KiB
Clojure
Raw Normal View History

2020-05-06 19:14:14 +00:00
(ns test
(:refer-clojure :exclude [read])
(:require [bencode.core :as bencode]
[clojure.edn :as edn])
#_(:import java.lang.ProcessBuilder$Redirect))
(defn write [stream v]
(bencode/write-bencode stream v)
(.flush stream))
(defn read [stream]
(bencode/read-bencode stream))
(defn bytes->string [^"[B" bytes]
(String. bytes))
(defn query [stream q]
(write stream {"op" "invoke"
2020-05-07 07:46:05 +00:00
"id" "1"
"var" "pod.babashka.hsqldb/execute!"
2020-05-06 19:14:14 +00:00
"args" (pr-str ["jdbc:hsqldb:mem:testdb;sql.syntax_mys=true" q])}))
2020-05-07 07:46:05 +00:00
(let [pb (ProcessBuilder. #_["lein" "run" "-m" "pod.babashka.hsqldb"]
["./pod-babashka-hsqldb"])
2020-05-06 19:14:14 +00:00
_ (.redirectErrorStream pb true)
;; _ (.redirectOutput pb ProcessBuilder$Redirect/INHERIT)
p (.start pb)
stdin (.getOutputStream p)
stdout (.getInputStream p)
stdout (java.io.PushbackInputStream. stdout)]
(write stdin {"op" "describe"})
(let [reply (read stdout)]
2020-05-07 07:46:05 +00:00
(println "format:" (String. (get reply "format")))) ;;=> edn
2020-05-06 19:14:14 +00:00
(query stdin ["create table foo ( foo int );"])
(let [reply (read stdout)]
(println "reply:" (edn/read-string (String. (get reply "value"))))) ;;=> [{:next.jdbc/update-count 0}]
(query stdin ["insert into foo values ( 1, 2, 3);"])
(let [reply (read stdout)]
(println "reply:" (edn/read-string (String. (get reply "value"))))) ;;=> [{:next.jdbc/update-count 3}]
(query stdin ["select * from foo;"])
(let [reply (read stdout)]
(println "reply:" (edn/read-string (String. (get reply "value")))))) ;=> [{:FOO/FOO 1} {:FOO/FOO 2} {:FOO/FOO 3}]