Fork me on GitHub
#beginners
<
2018-06-12
>
Mario C.03:06:22

Quick question: The Clojure JDBC docs state that the get-by-id function will return a single matching row, or nil. When I run the method it returns a list with a single map inside ({:some-key "JDBC"}). Is this what it's suppose to return or should it have returned just the map? I want to know whether the list was introduced by the JDBC wrapper itself or something I did on my end.

Mario C.03:06:30

I ask because I don't have the code in front of me but I would like to have an idea on what to look for tomorrow.

seancorfield03:06:32

@mario Can you share the code you used to invoke it?

Mario C.03:06:01

@seancorfield I can't because I don't have access to it at the moment

Mario C.03:06:23

(get-by-id db table pk-value) We are using that format to invoke it

seancorfield03:06:50

user=> (def db-spec {:dbtype "h2:mem" :dbname "example"})
#'user/db-spec
user=> (require '[clojure.java.jdbc :as jdbc])
nil
user=> (jdbc/db-do-commands db-spec (jdbc/create-table-ddl :example [[:name "varchar(32)"] [:value :int]]))
(0)
user=> (jdbc/insert! db-spec :example {:name "one" :value 1})
(nil)
user=> (jdbc/insert! db-spec :example {:name "two" :value 2})
(nil)
user=> (jdbc/insert! db-spec :example {:name "three" :value 3})
(nil)
user=> (jdbc/find-by-keys db-spec :example {:name "two"})
({:name "two", :value 2})
user=> (jdbc/get-by-id db-spec :example "two" :name)
{:name "two", :value 2}
user=>
^ @mario.cordova.862

seancorfield03:06:02

You should only ever get a single hash map back from get-by-id...

Mario C.03:06:02

@seancorfield Thank you very much! Although I am a little confused on how you invoked the method. You called it with (jdbc/get-by-id db-spec :example "two" :name). Where as we just did (jdbc/get-by-id db-spec :example id) where id is an integer.

Mario C.03:06:36

Although this tells me that the issue is on our end!

Mario C.03:06:22

hmm is it stating that the pk will be :name in this case?

Mario C.03:06:41

Yes just referenced the docs! Thanks again!

dpsutton04:06:12

now that's service!

Mario C.04:06:33

This man deserves a medal!

dpsutton04:06:51

he also maintains that library. he's quite a boon for the clojure community

Mario C.04:06:25

Yes, I always see him helping on here

seancorfield04:06:21

@mario.cordova.862 Yeah, I was just being a bit lazy at the REPL and didn't create a column called :id 🙂

seancorfield04:06:28

The less lazy version

user=> (jdbc/db-do-commands db-spec (jdbc/create-table-ddl :keyed [[:id :int "not null auto_increment"] [:name "varchar(32)"] [:value :int]]))
(0)
user=> (jdbc/insert! db-spec :keyed {:name "one" :value 1})
({:id 1})
user=> (jdbc/insert! db-spec :keyed {:name "two" :value 2})
({:id 2})
user=> (jdbc/insert! db-spec :keyed {:name "three" :value 3})
({:id 3})
user=> (jdbc/query db-spec ["select * from keyed"] {:result-set-fn clojure.pprint/print-table})

| :id | :name | :value |
|-----+-------+--------|
|   1 |   one |      1 |
|   2 |   two |      2 |
|   3 | three |      3 |
nil
user=> (jdbc/get-by-id db-spec :keyed 2)
{:id 2, :name "two", :value 2}
user=>
There's the 3-arg version with a table that has a primary key called :id @mario.cordova.862 🙂

nakiya08:06:41

Anyone familiar with orchestra? Any idea why (orchestra.spec.test/instrument) will return an empty vector when there are s/fdefs available in the namespace?

sb14:06:58

Guys, I didn’t find in the java-time API documentation.. how to convert (local-date-time) or (java.time.LocalDateTime/now) to unix timestamp. I tried in several methods.. any idea? (I tried with clj-time.coerce/to-long etc)

lispyclouds14:06:08

@sb Does this help? (System/currentTimeMillis)

sb14:06:30

No, that isn’t local time. I know that.

sb14:06:43

That is an application.

lispyclouds14:06:17

So you want it in a particular timezone?

sb14:06:37

yes, exactly.

lispyclouds14:06:18

You mentioned that you tried with clj-time.coerce/to-long. Were you getting the correct DateTime object before that?

lispyclouds14:06:36

Some code would help

sb14:06:28

(.toEpochMilli (jt/instant)) i got.. minus 2 hours (becuase time zones), with (.toEpochMilli (jt/local-date-time)) the error message IllegalArgumentException No matching field found: toEpochMilli for class java.time.LocalDateTime clojure.lang.Reflector.getInstanceField (Reflector.java:271)

dpsutton14:06:33

(.getEpochSecond (java.time.Instant/from (java.time.ZonedDateTime/now))) ;; 1528814124

dpsutton14:06:36

java time is saying that without an offset from UTC, it doesn't know how to match up the local time to the epoch time. If you start with a zoned date time, you can create an instant and then get the epoch second. You could look into how to get a zoned date time from your local date time or start from the zoned date time if possible

sb14:06:34

@dpsutton utton thanks the help.. yes in this case, I don’t have a quick solution.

dpsutton14:06:59

ah. i was looking to see how to go local -> zoned but i didn't see anything obvious. back to the docs

dpsutton14:06:10

atZone is what you are looking for (if this plan works for you of course :)

sb14:06:00

@dpsutton thx the help, I think so!! 👍👏