This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-05
Channels
- # bangalore-clj (1)
- # beginners (50)
- # boot (72)
- # cider (53)
- # cljs-dev (303)
- # cljsrn (2)
- # clojure (403)
- # clojure-conj (3)
- # clojure-dev (7)
- # clojure-italy (18)
- # clojure-russia (129)
- # clojure-sg (1)
- # clojure-spec (44)
- # clojure-uk (25)
- # clojurescript (112)
- # core-async (4)
- # core-typed (3)
- # cursive (23)
- # datomic (114)
- # defnpodcast (1)
- # emacs (1)
- # figwheel (2)
- # graphql (18)
- # hoplon (110)
- # instaparse (6)
- # jobs (3)
- # jobs-discuss (10)
- # leiningen (5)
- # luminus (1)
- # lumo (151)
- # off-topic (22)
- # om (3)
- # om-next (3)
- # onyx (4)
- # parinfer (1)
- # pedestal (8)
- # precept (51)
- # re-frame (19)
- # reagent (3)
- # ring (1)
- # ring-swagger (5)
- # spacemacs (21)
- # sql (25)
- # test-check (2)
- # uncomplicate (8)
- # unrepl (33)
- # untangled (20)
- # yada (14)
[org.clojure/java.jdbc "0.7.0-beta4"]
is available with a number of bug fixes compared to earlier Beta builds.
I am trying to insert a file into the Postgres bytea
type. However official Java doc say you can use PreparedStatement#setBinaryStream
(not sure how to use it from Clj since there are two params to this method). I also tried inserting stream directly with (io/input-stream "my-file.pdf")
but that didin't work either (JDBC cannot recognize type java.io.BufferedInputStream
). Thanks for any advice
@petr.mensik If you have access to the PreparedStatement
object you can use Java interop to set the param using setBinaryStream
@kumarshantanu I guess I could construct whole query via PreparedStatement
and interop but I am looking for better option here 🙂
@petr.mensik In case you can try (shameless plug) https://github.com/kumarshantanu/asphalt you can specify (asphalt.core/defsql "the sql" {:params-setter (fn [ps params] (.setBinaryStream ps 1 (first params)))})
— here params-setter
lets you do your thing with the prepared statement
I've solved it like this (maybe it helps somebody)
(doto (.prepareStatement conn "INSERT INTO files (name, size, content_type, user_id, content) VALUES (?, ?, ?, ?, ?)"
Statement/RETURN_GENERATED_KEYS)
(.setString 1 (:filename file))
(.setInt 2 (:size file))
(.setString 3 (:content-type file))
(.setInt 4 (user-model/get-actual-user :id))
(.setBinaryStream 5 (io/input-stream (:tempfile file)))
(.executeUpdate)))))
I'm having some difficulty selecting all the fields and also aggregating COUNT(*) in the same select
@avabinary what does your query look like?
I'd like very much that the result of count to be returned as a new column in the result of SELECT * from 'hotels'
@petr.mensik There's a prepare-statement
function in java.jdbc
that should allow you to do most of that directly -- and you can extend the protocols to handle custom types. See http://clojure-doc.org/articles/ecosystem/java_jdbc/using_sql.html#protocol-extensions-for-transforming-values
@seancorfield thanks, that's what I was looking for