This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-29
Channels
- # announcements (6)
- # babashka (23)
- # beginners (15)
- # biff (15)
- # calva (17)
- # clara (5)
- # clj-kondo (41)
- # cljdoc (2)
- # cljs-dev (67)
- # cljsrn (18)
- # clojure (19)
- # clojure-europe (25)
- # clojure-nl (2)
- # clojure-norway (9)
- # clojure-uk (2)
- # clojurescript (26)
- # core-typed (6)
- # cursive (15)
- # data-science (30)
- # datahike (1)
- # datomic (18)
- # docker (6)
- # emacs (10)
- # events (2)
- # graalvm (15)
- # graphql (5)
- # hugsql (4)
- # jobs-discuss (1)
- # joker (7)
- # lsp (36)
- # malli (28)
- # off-topic (46)
- # other-languages (1)
- # pathom (5)
- # pedestal (6)
- # polylith (5)
- # reitit (2)
- # releases (1)
- # rewrite-clj (63)
- # shadow-cljs (7)
- # spacemacs (16)
- # squint (6)
- # tools-deps (6)
- # xtdb (13)
Hi, everyone
I have a question in here, so what's the equivalent syntax of clojure.java.jdbc/with-db-transaction
with jdbc/with-transaction
in hugsql ?
because when i use the clojure.java.jdbc/with-db-transaction
the code works perfectly (according to the book i'm studying), but when i use the next.jdbc
library, the error appears to be something like this :
Execution error (ExceptionInfo) at hugsql.core/validate-parameters! (core.clj:83).
Parameter Mismatch: :id parameter data not found.
Here's the code :
(defn add-user-transaction [user]
(clojure.java.jdbc/with-db-transaction [t-conn db]
(if-not
(find-user t-conn {:id (:id user)})
(add-user! t-conn user))))
(defn add-user-transaction2 [user]
(jdbc/with-transaction [t-conn db]
(if-not
(jdbc/execute! t-conn (find-user {:id (:id user)}))
(jdbc/execute! t-conn (add-user! user)))))
and here's the sql resources :
- :name find-user :? :1
-- find the user with a matching ID
SELECT *
FROM users
WHERE id = :id
-- :name add-user! :! :n
-- :doc adds a new user
INSERT INTO users
(id, pass)
VALUES (:id, :pass)
What I've missed in here? Am i doing it wrong?HugSQL can generate two types of functions based on your SQL file:
• find-user
that has the following signature (find-user connection params)
- it actually executes your query
• find-user-sqlvec
- which just generates the sql vec + your params that you can then pass to jdbc/execute
so in your code you're using the first form - to fix it, you have to just call (find-user t-conn {:id (:id user})
and not pass anything to jdbc/execute!
2nd thing is, if you're using postgres or mysql you can use upserts to make this into a single query that either finds a user or creates a new one
Thanks,
After do trials and errors, read and learn some https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.834/doc/getting-started/transactions, i think i should use the upserts that you suggest (since in tutorial book use the postgresql) because everything i've tried, i'm always facing the error code in above or error like this :
Execution error (IllegalArgumentException) at clojure.java.jdbc/get-connection (jdbc.clj:430).
db-spec is a raw Connection object!
Did you call get-connection in the wrong context?
You should only call that to pass a Connection into prepare-statement.
(and don't forget to close it via with-open or .close)
Welp, much way to go to learn, Thank you!