sql

All things SQL and JDBC...
Hlodowig 2020-09-02T05:06:37.160100Z

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.

2020-09-02T05:26:51.160400Z

Better for what?

2020-09-02T05:27:41.161300Z

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

seancorfield 2020-09-02T05:33:20.161800Z

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

Hlodowig 2020-09-02T06:27:57.162200Z

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

Hlodowig 2020-09-02T06:29:43.162300Z

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

synthomat 2020-09-02T07:01:10.164Z

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

1
seancorfield 2020-09-02T16:29:55.165300Z

^ @zanategdl That would be my recommendation too.

1
ben741 2020-09-02T19:06:17.168400Z

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.

ben741 2020-09-02T19:14:26.169900Z

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

seancorfield 2020-09-02T19:28:28.170200Z

@bschmidt0t9

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

seancorfield 2020-09-02T19:28:34.170400Z

Is that what you're after?

seancorfield 2020-09-02T19:29:29.170700Z

Under the hood, it's

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

ben741 2020-09-02T19:36:00.173600Z

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 _(?,?) .

seancorfield 2020-09-02T20:03:22.174Z

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)

seancorfield 2020-09-02T20:04:14.174400Z

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