sql

All things SQL and JDBC...
2021-05-17T11:57:12.200Z

I have a SQLite DB with a tinyint column (named owner) that takes the values 0 and 1 to act like a boolean. Currently I am retrieving and transforming this column's values to true and false by extending next-jdbc's ReadableColumn:

(extend-protocol rs/ReadableColumn
  Integer
  (read-column-by-index [x mrs i]
    (if (= (.getColumnName mrs i) "owner")
      (if (= 1 x) true false)
      x)))
Is this the best solution knowing that this is the only column for which I cast from Integer to Boolean?

seancorfield 2021-05-17T17:01:15.201800Z

As long as owner is unique across your whole DB, thatโ€™s reasonable. In MySQL, I use the BIT column type and that automatically generates true/`false` at the JDBC level.

seancorfield 2021-05-17T17:01:38.202400Z

(you could just have (= 1 x) instead of the if BTW)

2021-05-17T17:18:55.202500Z

Great, thanks.

2021-05-17T17:49:46.203Z

Oh thanks for the tip, much more cleaner like that!

(when (= (.getColumnName mrs i) "owner")
      (= 1 x))

dpsutton 2021-05-17T17:50:33.203400Z

that when looks like trouble. you are doing this for all Integer columns.

1๐Ÿ‘
emccue 2021-05-17T17:53:17.203700Z

I'd probably use (not= 0 x)

emccue 2021-05-17T17:53:30.204Z

since that sorta tracks in my brain

emccue 2021-05-17T17:53:36.204200Z

where anything non-zero maps to true

seancorfield 2021-05-17T18:06:49.204300Z

Only integer columns called owner.

dpsutton 2021-05-17T18:08:17.204500Z

that code snippet uses that determination in the when test. this would null out all other integer columns right?

seancorfield 2021-05-17T18:22:43.205600Z

(if (= (.getColumnName mrs i) "owner")
  (not (zero? x))
  x)

1๐Ÿ‘Œ
2021-05-17T18:28:24.206100Z

Make sense, Thank you both!