This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-25
Channels
- # aws (1)
- # beginners (60)
- # boot (12)
- # cider (7)
- # cljs-dev (8)
- # cljsrn (24)
- # clojure (169)
- # clojure-nl (1)
- # clojure-russia (3)
- # clojure-spec (15)
- # clojure-switzerland (1)
- # clojure-uk (24)
- # clojurescript (120)
- # core-async (9)
- # datomic (3)
- # emacs (2)
- # gsoc (3)
- # klipse (2)
- # leiningen (4)
- # numerical-computing (13)
- # off-topic (95)
- # om (7)
- # onyx (1)
- # powderkeg (2)
- # protorepl (2)
- # re-frame (6)
- # reagent (58)
- # ring-swagger (1)
- # rum (28)
- # untangled (42)
- # vim (6)
Getting a no suitable driver exception when using clojure.java.jdbc and I'm not sure why. I added the mysql maven dependency to my project structure via maven (using intelliJ + cursive). Do I need to do something with my project.clj for Lein?
what matters is that the mysql dep shows up in clojure's classpath
mbcev the require only works if your classpath is right
mbcev the point here is that you can use your IDE to add deps - as long as deps your project defines are visible to clojure in the classpath at runtime
usually, I would use lein to declare deps, and I mostly use lein as a tool for setting my project classpath
but what really matters is the classpath the java process running clojure.jar sees
I understand the words that you're saying but I don't understand how to achieve what you're saying. I added [org.clojure/java.jdbc "0.7.0-alpha3"]
to my project.clj but that's just the wrapper for JDBC right? They're saying you also need the JDBC driver you intend to use but there isn't any instruction there regarding adding that to my classpath?
mbcev it's pretty simple - [organization/project version-string]
you can find the mvn coordinates on maven, or via the project usually
oh okay, so it should be the same as what I added via cursive via maven then I assume [mysql:mysql-connector-java 6.0.6]
[mysql/mysql-connector-java "5.1.6"]
(that's from the last mysql project I ever set up)
use the newer version string, but it needs to be a string
and use / instead of :
no need to feel stupid - I don't know if I ever found that documented anywhere, I just figured it out trial and error based on other project.clj entries
there's probably a utility somewhere that takes a maven pom and outputs a vector for project.clj
that's a personal thing, but I like hugsql https://github.com/layerware/hugsql - get input from other people though since there's a big bit of opinion in that
@mbcev Just catching up on the JDBC discussion. Happy to field any further questions you have — I’m the maintainer of org.clojure/java.jdbc
.
We use MySQL very heavily at World Singles with java.jdbc
and the HoneySQL library. We like HoneySQL when we have complex queries that are built up programmatically — because we can build query fragments and then compose them together.
But a lot of people like YeSQL and HugSQL so there are several good options.
Thanks @seancorfield . I'm not sure if I have any obvious questions right now. Just kind of playing around right now I guess. Just trying to query my local db to see what makes sense I guess. I'm sure there are several great options but they're all the same to me since I haven't used any of them hah.
My impression is that Korma is no longer maintained (since you mentioned that) but some people still like its “ORM” style approach.
Okay wait, so Honey outputs vectors that I can pass into jdbc/query ? Am I understanding that correctly?
Correct.
It builds data structures and then turns those into SQL and parameters in a vector.
So you can do stuff like this:
(jdbc/query (-> application :database :pooled-db)
(-> (cond-> (-> (select :u.*)
(cross-equivalence? site)
(platinum?)
(from [:user :u])
(with-primary-photo)
(where [:in :u.id ids])
(order-by [(apply sql/call :field :u.id ids)]))
(and self (site/has-covalence? site))
(with-covalence self))
(sql/format :quoting :mysql)))
and platinum?
is like this (defn platinum?
"Given a query map over user u, add in platinum, along with the
left join on userGroup needed for that."
[query]
(-> query
(merge-select [(sql/call :<> :ug.id nil) :platinum])
(merge-left-join [:userGroup :ug]
[:and
[:= :ug.userid :u.id]
[:= :ug.groupid 2]])))
with-primary-photo
and with-covalence
are two more query-building functions.
The nice thing is that each function can contribute parts to the select, the joins, the where clause etc.
(we have some really complex reporting queries that are built up from tables of metadata)
One of my colleagues gave a talk at Clojure/West 2015 about how we use HoneySQL https://www.youtube.com/watch?v=alkcjyhesjI
Right, right. Yeah I'm thinking I'm understanding how Honey works. What's tripping me up a little right now is http://clojure.github.io/java.jdbc/ functions but I'm assuming that this is just a wrapper for Java JDBC so I need to go look up something like http://docs.oracle.com/javase/tutorial/jdbc/basics/ to better understand how and when to use what?
You shouldn’t need to worry about the underlying JDBC.
Clojure’s java.jdbc
library provides fairly straightforward functions to insert!
, delete!
, and update!
rows, to run query
s, with [”some sql string” param1 param2]
etc.
You’ll probably find the community docs for java.jdbc
will be helpful http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html
I need to update that page (and the README) to no longer reference Korma 🙂
Hopefully this example in the README will get you started with MySQL https://github.com/clojure/java.jdbc/#example-usage
(I need to go help my wife with dinner)
No worries; I appreciate the help @seancorfield . I think I've got it from here. At least enough to get myself into trouble.
I’ll be around later if you have questions. I’m a night owl!
(I'm back around now, in case there are JDBC questions)
will I run into trouble using (= "string" "string")
over (= 0 (compare "str" "str"))
to evaluate two strings being equal like I would in Java?
what kind of trouble is that exactly?
I'm referring to how in pure Java the == operator on strings is comparing by object reference not object value like .equal or .compareTo would.
= in clojure is value
= on strings will turn into using .equals()
You can use identical? to get the equivalent of = in Java
So to answer your original question - no, you won't get in trouble and you should use = in Clojure
I've got a function to move ants randomly in a grid and I iterate over all ants to move them using this function
(defn loop-ant
[ant-ref]
(move-ant! ant-ref (random-direction)))
When I iterate over them with the following functions, it works as expected and all ants walk randomly every loop iteration
(defn loop-ants
[]
(loop [ant (first ants)
left-ants (rest ants)]
(if (nil? ant)
nil
(do
(loop-ant ant)
(recur (first left-ants) (rest left-ants))))))
But if I call it using a map function like in the following code, the ants do nothing and I cry internally
(defn loop-ants
[]
(map loop-ant ants))
If I use pmap
instead, only some of the ants moveEach grid cell (defining if an ant and what is present in that tile) and each ant struct are wrapped in ref
if that changes anything.
Anyone has an idea on what's going on?
@redeu map and pmap are lazy, and do nothing if nothing consumes their results
you can use run! instead of map if you don't need the value from each loop-ant call
@noisesmith Omg I can't believe I didn't notice that. That's exactly what I needed, thank you!
also, if you need to do other collection operations for side effects, two common options are to wrap in dorun
(if you don't need the values) or doall
(if you need them but not right away) or to use doseq
(if you have a lazy operation to make the values, but want to operate on each one imperatively
It's funny... I used run!
for the very first time on Thursday... to show a friend the equivalent Clojure to his JavaScript. It was very non-idiomatic for Clojure (using side-effects). Another friend had already shown him the idiomatic way without side-effects but he said that "wasn't close enough" to his original code.