sockeqwe / sqlbrite-dao

DAO for SQLBrite
http://hannesdorfmann.com/android/sqlbrite-dao
Apache License 2.0
182 stars 22 forks source link

Boolean type #3

Closed davidbilik closed 8 years ago

davidbilik commented 9 years ago

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.

sockeqwe commented 9 years ago

I see, I simply forgot to add boolean type, sorry! I will try to find time to fix that on the weekend

https://github.com/sockeqwe/sqlbrite-dao/blob/master/objectmapper-processor/src/main/java/com/hannesdorfmann/sqlbrite/objectmapper/processor/generator/field/FieldCodeFactory.java

davidbilik commented 9 years ago

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

@Column(COL_FAVORITE)
public void setIsFavorite(int isFavorite) {
    this.isFavorite = isFavorite> 0;
}
sockeqwe commented 9 years ago

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

harinair commented 9 years ago

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.

sockeqwe commented 9 years ago

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

harinair commented 8 years ago

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.