Fork me on GitHub
#sql
<
2020-09-02
>
Hlodowig05:09:37

Hi, I'm getting java.sql.Date instances from a query. I'm wondering if there's something better than checking for type and applying .toString to those values.

hiredman05:09:51

Better for what?

hiredman05:09:41

And you shouldn't have to check types, columns will always be the same type

Hlodowig06:09:43

By better I mean more succinct @U0NCTKEV8. I will be using different queries, so I don't always know which columns will be dates.

seancorfield05:09:20

@zanategdl Why would you want strings from date/time/timestamp columns in the database?

Hlodowig06:09:57

@seancorfield Because I'm writing this stuff to a Google Sheet.

synthomat07:09:10

might be better to use a date formatter for better readability rather than just calling .toString() , though

thanks2 3
seancorfield16:09:55

^ @zanategdl That would be my recommendation too.

thanks2 3
ben74119:09:17

In honeysql, is it possible to select the result of applying a binary operator without resorting to raw? e.g. SELECT 2 - 1; ? If I try something like (hsql/format {:select [[:- 2 1]]}) or variants, I run into "Alias should have two parts" errors.

ben74119:09:26

I suppose (hsql/format {:select [(hsql/call :int8mi 2 1)]} works, but is it the best/only way?

seancorfield19:09:28

@bschmidt0t9

user=> (require '[honeysql.helpers :refer [select]] '[honeysql.core :as h])
nil
user=> (h/format (select (h/call :- 2 1)))
["SELECT (? - ?)" 2 1]

seancorfield19:09:34

Is that what you're after?

seancorfield19:09:29

Under the hood, it's

user=> (select (h/call :- 2 1))
{:select (#sql/call [:- 2 1])}

ben74119:09:00

That works, thanks @seancorfield! I was just slightly off in one of my other attempts to use call - (h/format {:select [(h/call :- 2 1)]}) works. I was just missing the square brackets. I didn't see any examples to show that call is clever enough to handle operators as well as function calls, I thought it was trying to do _(?,?) .

seancorfield20:09:22

This is an example in the readme but it's easy to miss

(-> (helpers/update :films)
    (sset {:kind "dramatic"
           :watched (sql/call :+ :watched 1)})
    (where [:= :kind "drama"])
    sql/format)

seancorfield20:09:14

(and there are a couple of tests around it -- also easy to miss in the mass of other tests)