sockeqwe/sqlbrite-dao

Boolean type

Closed this issue · 6 comments

When I try to use @column annotation on a boolean field it tells me that its not supported. I know that you cant insert boolean to database, but we normally use integer type, so it can probably read/write that field in generated file the same way, just like in Parcelable.

Thanks :) I have a workaround for now, a method like

@Column(COL_FAVORITE)
public void setIsFavorite(int isFavorite) {
    this.isFavorite = isFavorite> 0;
}

Unfortunately including support for boolean is not as simple as it seems, because Cursor doesn't have native support for Boolean. Hence it depends on your SQL column definition. I have to elaborate that in more detail. Also related to #2

Hannes:
This can be handled easily in Dao. Normally we store a Java boolean as number in the table. Hence assume that the field will be defined as a number. Check the corresponding mapped column value: if it is 0 or null return false; otherwise true. Similarly a true is saved as 1 and false is saved as 0.

Sure, in theory it's easy (i.e. by using an integer) but that is something the developer has to specify during CREATE TABLE statement, i.e. one could save the String "true" or "false". When reading the column from database we have to choose which format we expect by using cursor.getInt(columnIndex) or cursor.getString(columnIndex). But both will throw an exception if it's not a string or integer.

We can assume that boolean is always a integer, but then the CREATE TABLE , INSERT and UPDATE statements have to reapect that an integer is used. I have to verify what sqlite uses internally for sql statements with true in it

I believe that is a good compromise. Boolean is not supported in SQL. So there is no way other than that. Devs have been handling boolean as String or Number (mostly number). If the DAO throws an exception, the developer will no for sure. See how hibernate is handling this.