Support for BigInteger properties (stored/parsed as strings)

pull/14/head
Sam Bosley 13 years ago
parent 63f7d328b6
commit 9dd6c58068

@ -6,6 +6,7 @@
package com.todoroo.andlib.data; package com.todoroo.andlib.data;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import android.content.ContentValues; import android.content.ContentValues;
@ -366,6 +367,10 @@ abstract public class AbstractDatabase {
public String visitString(Property<String> property, Void data) { public String visitString(Property<String> property, Void data) {
return String.format("%s TEXT", property.getColumnName()); return String.format("%s TEXT", property.getColumnName());
} }
public String visitBigInteger(Property<BigInteger> property, Void data) {
return String.format("%s TEXT", property.getColumnName());
}
} }
} }

@ -8,6 +8,7 @@ package com.todoroo.andlib.data;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -478,6 +479,11 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
store.put(property.getColumnName(), (String) value); store.put(property.getColumnName(), (String) value);
return null; return null;
} }
public Void visitBigInteger(Property<BigInteger> property, Object value) {
store.put(property.getColumnName(), ((BigInteger) value).toString());
return null;
}
} }
// --- parcelable helpers // --- parcelable helpers

@ -10,6 +10,8 @@ import static com.todoroo.andlib.sql.SqlConstants.LEFT_PARENTHESIS;
import static com.todoroo.andlib.sql.SqlConstants.RIGHT_PARENTHESIS; import static com.todoroo.andlib.sql.SqlConstants.RIGHT_PARENTHESIS;
import static com.todoroo.andlib.sql.SqlConstants.SPACE; import static com.todoroo.andlib.sql.SqlConstants.SPACE;
import java.math.BigInteger;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Field; import com.todoroo.andlib.sql.Field;
import com.todoroo.andlib.sql.Operator; import com.todoroo.andlib.sql.Operator;
@ -101,6 +103,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
public RETURN visitDouble(Property<Double> property, PARAMETER data); public RETURN visitDouble(Property<Double> property, PARAMETER data);
public RETURN visitString(Property<String> property, PARAMETER data); public RETURN visitString(Property<String> property, PARAMETER data);
public RETURN visitBigInteger(Property<BigInteger> property, PARAMETER data);
} }
// --- children // --- children
@ -239,6 +243,27 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
} }
} }
public static class BigIntegerProperty extends Property<BigInteger> {
public BigIntegerProperty(Table table, String name) {
super(table, name);
}
public BigIntegerProperty(Table table, String name, boolean nullable) {
super(table, name, nullable);
}
protected BigIntegerProperty(Table table, String name, String expression) {
super(table, name, expression);
}
@Override
public <RETURN, PARAMETER> RETURN accept(
PropertyVisitor<RETURN, PARAMETER> visitor, PARAMETER data) {
return visitor.visitBigInteger(this, data);
}
}
public String getColumnName() { public String getColumnName() {
if (hasAlias()) if (hasAlias())
return alias; return alias;

@ -5,6 +5,7 @@
*/ */
package com.todoroo.andlib.data; package com.todoroo.andlib.data;
import java.math.BigInteger;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import android.database.Cursor; import android.database.Cursor;
@ -129,9 +130,18 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
return cursor.getString(column); return cursor.getString(column);
} }
public Object visitBigInteger(Property<BigInteger> property,
TodorooCursor<?> cursor) {
int column = columnIndex(property, cursor);
if(property.nullable && cursor.isNull(column))
return null;
return new BigInteger(cursor.getString(column));
}
private int columnIndex(Property<?> property, TodorooCursor<?> cursor) { private int columnIndex(Property<?> property, TodorooCursor<?> cursor) {
return cursor.getColumnIndexFromCache(property.getColumnName()); return cursor.getColumnIndexFromCache(property.getColumnName());
} }
} }
} }

@ -8,6 +8,7 @@ package com.todoroo.astrid.backup;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlSerializer;
@ -316,6 +317,26 @@ public class TasksXmlExporter {
return null; return null;
} }
@Override
public Void visitBigInteger(Property<BigInteger> property,
AbstractModel data) {
try {
BigInteger value = data.getValue(property);
if(value == null)
return null;
xml.attribute(null, property.name, value.toString());
} catch (UnsupportedOperationException e) {
// didn't read this value, do nothing
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalStateException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return null;
}
} }
private void onFinishExport(final String outputFile) { private void onFinishExport(final String outputFile) {

@ -7,6 +7,7 @@ package com.todoroo.astrid.backup;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -385,6 +386,15 @@ public class TasksXmlImporter {
data.setValue(property, value); data.setValue(property, value);
return null; return null;
} }
@Override
public Void visitBigInteger(Property<BigInteger> property,
AbstractModel data) {
String value = xpp.getAttributeValue(null, property.name);
if(value != null)
data.setValue(property, new BigInteger(value));
return null;
}
} }
} }

@ -5,6 +5,7 @@
*/ */
package com.todoroo.astrid.service; package com.todoroo.astrid.service;
import java.math.BigInteger;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -314,6 +315,14 @@ public class Astrid2To3UpgradeHelper {
Log.d("upgrade", "wrote " + value + " to -> " + property + " of model id " + data.cursor.getLong(1)); Log.d("upgrade", "wrote " + value + " to -> " + property + " of model id " + data.cursor.getLong(1));
return null; return null;
} }
@Override
public Void visitBigInteger(Property<BigInteger> property, UpgradeVisitorContainer<?> data) {
BigInteger value = new BigInteger(data.cursor.getString(data.columnIndex));
data.model.setValue(property, value);
Log.d("upgrade", "wrote " + value + " to -> " + property + " of model id " + data.cursor.getLong(1));
return null;
}
} }
/** /**

Loading…
Cancel
Save