diff --git a/api/src/com/todoroo/andlib/data/Property.java b/api/src/com/todoroo/andlib/data/Property.java index 9a4665bf2..b1526e629 100644 --- a/api/src/com/todoroo/andlib/data/Property.java +++ b/api/src/com/todoroo/andlib/data/Property.java @@ -30,6 +30,9 @@ public abstract class Property extends Field implements Cloneable { /** The database column name for this property */ public final String name; + /** Can this field be null? */ + public boolean nullable = false; + /** * Create a property by table and column name. Uses the default property * expression which is derived from default table name @@ -38,6 +41,15 @@ public abstract class Property extends Field implements Cloneable { this(table, columnName, (table == null) ? (columnName) : (table.name + "." + columnName)); } + /** + * Create a property by table and column name. Uses the default property + * expression which is derived from default table name + */ + protected Property(Table table, String columnName, boolean nullable) { + this(table, columnName, (table == null) ? (columnName) : (table.name + "." + columnName)); + this.nullable = nullable; + } + /** * Create a property by table and column name, manually specifying an * expression to use in SQL @@ -98,6 +110,10 @@ public abstract class Property extends Field implements Cloneable { super(table, name); } + public IntegerProperty(Table table, String name, boolean nullable) { + super(table, name, nullable); + } + protected IntegerProperty(Table table, String name, String expression) { super(table, name, expression); } @@ -121,6 +137,10 @@ public abstract class Property extends Field implements Cloneable { super(table, name); } + public StringProperty(Table table, String name, boolean nullable) { + super(table, name, nullable); + } + protected StringProperty(Table table, String name, String expression) { super(table, name, expression); } @@ -144,6 +164,10 @@ public abstract class Property extends Field implements Cloneable { super(table, name); } + public DoubleProperty(Table table, String name, boolean nullable) { + super(table, name, nullable); + } + protected DoubleProperty(Table table, String name, String expression) { super(table, name, expression); } @@ -168,6 +192,10 @@ public abstract class Property extends Field implements Cloneable { super(table, name); } + public LongProperty(Table table, String name, boolean nullable) { + super(table, name, nullable); + } + protected LongProperty(Table table, String name, String expression) { super(table, name, expression); } diff --git a/api/src/com/todoroo/andlib/data/TodorooCursor.java b/api/src/com/todoroo/andlib/data/TodorooCursor.java index af0be8034..2974245bf 100644 --- a/api/src/com/todoroo/andlib/data/TodorooCursor.java +++ b/api/src/com/todoroo/andlib/data/TodorooCursor.java @@ -87,27 +87,36 @@ public class TodorooCursor extends CursorWrapper { public Object visitDouble(Property property, TodorooCursor cursor) { - return cursor.getDouble(cursor.getColumnIndexFromCache(property.name)); + int column = columnIndex(property, cursor); + if(property.nullable && cursor.isNull(column)) + return null; + return cursor.getDouble(column); } public Object visitInteger(Property property, TodorooCursor cursor) { - return cursor.getInt(cursor.getColumnIndexFromCache(property.name)); + int column = columnIndex(property, cursor); + if(property.nullable && cursor.isNull(column)) + return null; + return cursor.getInt(column); } public Object visitLong(Property property, TodorooCursor cursor) { int column = columnIndex(property, cursor); - if(cursor.isNull(column)) + if(property.nullable && cursor.isNull(column)) return null; return cursor.getLong(column); } public Object visitString(Property property, TodorooCursor cursor) { - return cursor.getString(cursor.getColumnIndexFromCache(property.name)); + int column = columnIndex(property, cursor); + if(property.nullable && cursor.isNull(column)) + return null; + return cursor.getString(column); } - private int columnIndex(Property property, TodorooCursor cursor) { + private int columnIndex(Property property, TodorooCursor cursor) { return cursor.getColumnIndexFromCache(property.name); } } diff --git a/api/src/com/todoroo/astrid/data/Task.java b/api/src/com/todoroo/astrid/data/Task.java index 76009812a..889bddeeb 100644 --- a/api/src/com/todoroo/astrid/data/Task.java +++ b/api/src/com/todoroo/astrid/data/Task.java @@ -135,7 +135,7 @@ public final class Task extends RemoteModel { /** Remote id */ public static final LongProperty REMOTE_ID = new LongProperty( - TABLE, REMOTE_ID_PROPERTY_NAME); + TABLE, REMOTE_ID_PROPERTY_NAME, true); /** Assigned user id */ public static final LongProperty USER_ID = new LongProperty(