Fork me on GitHub
#sql
<
2021-05-17
>
Michaël Salihi11:05:12

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?

seancorfield17:05:15

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.

seancorfield17:05:38

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

Michaël Salihi17:05:46

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

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

dpsutton17:05:33

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

👍 3
seancorfield18:05:49

Only integer columns called owner.

dpsutton18:05:17

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

seancorfield18:05:02

Oh, gotcha! Yeah, missed that. Yeah, needs to be if not when and return the original x in the else case @admin055

👌 3
Michaël Salihi18:05:24

Make sense, Thank you both!

emccue17:05:17

I'd probably use (not= 0 x)

emccue17:05:30

since that sorta tracks in my brain

emccue17:05:36

where anything non-zero maps to true

seancorfield18:05:02

Oh, gotcha! Yeah, missed that. Yeah, needs to be if not when and return the original x in the else case @admin055

👌 3
seancorfield18:05:43

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

👌 3