Fork me on GitHub

Getting a no suitable driver exception when using 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


I required [ :as j]


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 :


wow I feel stupid; thanks...


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


Let me ask you one more question. Any preference between HoneySQL, SQLingvo and Korma?


that's a personal thing, but I like 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?


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)
                                (from      [:user :u])
                                (where     [:in ids])
                                (order-by  [(apply sql/call :field 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
      (merge-select    [(sql/call :<> nil) :platinum])
      (merge-left-join [:userGroup :ug]
                        [:= :ug.userid]
                        [:= :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


Right, right. Yeah I'm thinking I'm understanding how Honey works. What's tripping me up a little right now is functions but I'm assuming that this is just a wrapper for Java JDBC so I need to go look up something like 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 querys, with [”some sql string” param1 param2] etc.


You’ll probably find the community docs for java.jdbc will be helpful


ahh okay perfect, yeah this is what I needed


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


(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

Alex Miller (Clojure team)12:03:06

= on strings will turn into using .equals()

Alex Miller (Clojure team)12:03:43

You can use identical? to get the equivalent of = in Java

Alex Miller (Clojure team)12:03:48

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
  (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)
        (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 move


Each 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


That's pretty useful to know, I haven't seen any of those functions yet


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.