Introduce a nullable option for properties to fix errors where some long properties would return null instead of 0

pull/14/head
Sam Bosley 14 years ago
parent 33dc23d5db
commit d3ce02dffc

@ -30,6 +30,9 @@ public abstract class Property<TYPE> 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<TYPE> 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<TYPE> 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<TYPE> 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<TYPE> 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<TYPE> 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);
}

@ -87,27 +87,36 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
public Object visitDouble(Property<Double> 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<Integer> 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<Long> 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<String> 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<Long> property, TodorooCursor<?> cursor) {
private int columnIndex(Property<?> property, TodorooCursor<?> cursor) {
return cursor.getColumnIndexFromCache(property.name);
}
}

@ -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(

Loading…
Cancel
Save