Fork me on GitHub
#sql
<
2018-10-23
>
gleisonsilva00:10:48

the problem is that, sometimes, I don't have only one table, but a query joining two+ tables.. so think I'll manage using db-query-with-resultset function

seancorfield00:10:09

If you think of ways the clojure.java.jdbc API could support your use case better, feel free to open a JIRA issue here https://dev.clojure.org/jira/browse/JDBC

👍 4
mark_d02:10:07

I have a jdbc query question. How do a construct a query or function which will not return nil values. For example,

(jdbc/query db-spec ["SELECT * from feedback"])
some of the columns have nil values. How do I exclude those from the results?

mark_d02:10:29

Hmmm… I can use filter for a single value:

(into {} (filter #(not (nil? (val %))) (first (jdbc/query db-spec ["SELECT * from feedback WHERE id = 1"]))))

seancorfield03:10:35

@mark.davidson Can you explain your use case? Nil-punning generally means not needing to do what you're trying to do there...

mark_d03:10:19

@seancorfield I’m trying to read from a database, validate the rows with schema and then transform it to json for a REST api

mark_d03:10:33

I think I almost have it:

(jdbc/query db-spec ["SELECT * from feedbacks_resolution"] {:row-fn remove-nil})

mark_d03:10:48

Where

(defn remove-nil [row] (into {} (filter #(not (nil? (val %)))) row))

seancorfield03:10:11

What's wrong with nil flowing into the JSON as null?

seancorfield03:10:33

(our REST APIs can accept and return null in the JSON at work)

mark_d03:10:41

That would work. I’m not sure how to use s/defschema to accept nil values:

(s/defschema Resolution
  {:id                                   s/Int
   :feedbacks_id                         Integer
   :device_serial_number                 String
   (s/optional-key :developer_notes)     s/Any
   :device_server_ip                     s/Any
   :commands_sent                        Integer
   :disconnects                          Integer
   (s/optional-key :debug_server_ip)     s/Any

   (s/optional-key :debug_commands_sent) Integer
   :debug_disconnects                    Integer
   (s/optional-key :category_id)         s/Any
   (s/optional-key :resolution_id)       s/Any
   :is_resolved                          Boolean
   })

seancorfield03:10:59

In spec it would be s/nilable

seancorfield03:10:06

No idea about Schema

mark_d03:10:57

yea, I would rather use spec but I don’t have any great examples in here: https://github.com/metosin/compojure-api

seancorfield03:10:32

Looks like s/maybe

mark_d03:10:48

Given a schema which defines a column with a string type, if nil is in the column then it seems to break validation

seancorfield03:10:58

;; maybe
(s/validate (s/maybe s/Keyword) :a)
(s/validate (s/maybe s/Keyword) nil)

seancorfield03:10:28

If your DB schema allows NULL, then your Schema for the data needs to reflect that.

mark_d03:10:26

I’ll try that out. Thanks.

mark_d03:10:56

@seancorfield Using s/maybe did the trick! Thanks for your help!

seancorfield03:10:25

Cool! Glad that worked.

ikitommi05:10:57

@mark.davidson since latest c-api alpha, you can use substitute Schemas with Specs, will work 80% of the time ok.

ikitommi05:10:29

Example app with Schema, Specs and data-specs: https://github.com/metosin/c2

mark_d05:10:18

Thanks I’ll take a look. I’m having a bit of a nightmare with schema and compojure-api. I’m not sure I really understand coercion but those options will radically change my results when do various gets on the similar test cases.

ikitommi05:10:38

nightmare, can't be good. There is also reitit that does both Schema & Spec coercion and has better docs:https://github.com/metosin/reitit.

ikitommi05:10:15

#reitit to get help with that (author for both libs)

mark_d14:10:35

Thanks for the help. I’ll take a look at the libraries. I’m a clojure novice but I really enjoy the platform and community. I’m still going though a ramp up but I’m trying to use clojure as much as I can.

mark_d15:10:44

So the depths of my “nightmare” can be paraphrased as follows: changing the {:coercion <value>} will change the results when I call my endpoint depending on the schema. On some configurations, the call will 400, others will give a 200 and no headers or body. I’m trying to narrow the use case to isolate the problem. @U055NJ5CC can you recommend an article or book to help me understand the concept of coercion?