This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-12
Channels
- # beginners (36)
- # boot (11)
- # cider (10)
- # cljs-dev (10)
- # cljsrn (3)
- # clojure (103)
- # clojure-greece (1)
- # clojure-italy (16)
- # clojure-nl (3)
- # clojure-spec (59)
- # clojure-uk (129)
- # clojurescript (125)
- # data-science (29)
- # datomic (30)
- # emacs (12)
- # events (5)
- # fulcro (61)
- # graphql (5)
- # keechma (3)
- # leiningen (9)
- # luminus (7)
- # onyx (26)
- # re-frame (3)
- # reagent (56)
- # reitit (25)
- # ring-swagger (16)
- # shadow-cljs (44)
- # spacemacs (4)
- # specter (2)
- # tools-deps (7)
- # vim (8)
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.
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.
@mario Can you share the code you used to invoke it?
@seancorfield I can't because I don't have access to it at the moment
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.862You should only ever get a single hash map back from get-by-id
...
...or nil
.
@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.cordova.862 Yeah, I was just being a bit lazy at the REPL and didn't create a column called :id
🙂
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 🙂Anyone familiar with orchestra
?
Any idea why (orchestra.spec.test/instrument)
will return an empty vector when there are s/fdef
s available in the namespace?
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)
@sb Does this help? (System/currentTimeMillis)
So you want it in a particular timezone?
You mentioned that you tried with clj-time.coerce/to-long
. Were you getting the correct DateTime
object before that?
Some code would help
(.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)
(.getEpochSecond (java.time.Instant/from (java.time.ZonedDateTime/now))) ;; 1528814124
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