From f956f364668c1830e8b9d4a9add5b5955e05520c Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 28 Aug 2012 12:42:06 -0700 Subject: [PATCH] Respect property aliases when reading/writing to cursors/models --- .../todoroo/andlib/data/AbstractDatabase.java | 8 ++-- .../todoroo/andlib/data/AbstractModel.java | 44 +++++++++---------- api/src/com/todoroo/andlib/data/Property.java | 11 +++++ .../todoroo/andlib/data/TodorooCursor.java | 2 +- .../todoroo/astrid/adapter/TaskAdapter.java | 7 +-- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/api/src/com/todoroo/andlib/data/AbstractDatabase.java b/api/src/com/todoroo/andlib/data/AbstractDatabase.java index c14e14b07..e7c00205b 100644 --- a/api/src/com/todoroo/andlib/data/AbstractDatabase.java +++ b/api/src/com/todoroo/andlib/data/AbstractDatabase.java @@ -335,19 +335,19 @@ abstract public class AbstractDatabase { public static class SqlConstructorVisitor implements PropertyVisitor { public String visitDouble(Property property, Void data) { - return String.format("%s REAL", property.name); + return String.format("%s REAL", property.getColumnName()); } public String visitInteger(Property property, Void data) { - return String.format("%s INTEGER", property.name); + return String.format("%s INTEGER", property.getColumnName()); } public String visitLong(Property property, Void data) { - return String.format("%s INTEGER", property.name); + return String.format("%s INTEGER", property.getColumnName()); } public String visitString(Property property, Void data) { - return String.format("%s TEXT", property.name); + return String.format("%s TEXT", property.getColumnName()); } } } diff --git a/api/src/com/todoroo/andlib/data/AbstractModel.java b/api/src/com/todoroo/andlib/data/AbstractModel.java index 145899e92..132b2a168 100644 --- a/api/src/com/todoroo/andlib/data/AbstractModel.java +++ b/api/src/com/todoroo/andlib/data/AbstractModel.java @@ -186,14 +186,14 @@ public abstract class AbstractModel implements Parcelable, Cloneable { */ public synchronized TYPE getValue(Property property) { Object value; - if(setValues != null && setValues.containsKey(property.name)) - value = setValues.get(property.name); + if(setValues != null && setValues.containsKey(property.getColumnName())) + value = setValues.get(property.getColumnName()); - else if(values != null && values.containsKey(property.name)) - value = values.get(property.name); + else if(values != null && values.containsKey(property.getColumnName())) + value = values.get(property.getColumnName()); - else if(getDefaultValues().containsKey(property.name)) - value = getDefaultValues().get(property.name); + else if(getDefaultValues().containsKey(property.getColumnName())) + value = getDefaultValues().get(property.getColumnName()); else throw new UnsupportedOperationException( @@ -253,9 +253,9 @@ public abstract class AbstractModel implements Parcelable, Cloneable { * @return true if setValues or values contains this property */ public boolean containsValue(Property property) { - if(setValues != null && setValues.containsKey(property.name)) + if(setValues != null && setValues.containsKey(property.getColumnName())) return true; - if(values != null && values.containsKey(property.name)) + if(values != null && values.containsKey(property.getColumnName())) return true; return false; } @@ -266,10 +266,10 @@ public abstract class AbstractModel implements Parcelable, Cloneable { * stored is not null */ public boolean containsNonNullValue(Property property) { - if(setValues != null && setValues.containsKey(property.name)) - return setValues.get(property.name) != null; - if(values != null && values.containsKey(property.name)) - return values.get(property.name) != null; + if(setValues != null && setValues.containsKey(property.getColumnName())) + return setValues.get(property.getColumnName()) != null; + if(values != null && values.containsKey(property.getColumnName())) + return values.get(property.getColumnName()) != null; return false; } @@ -283,11 +283,11 @@ public abstract class AbstractModel implements Parcelable, Cloneable { Property property, TYPE newValue) { // we've already decided to save it, so overwrite old value - if (setValues.containsKey(property.name)) + if (setValues.containsKey(property.getColumnName())) return true; // values contains this key, we should check it out - if(values != null && values.containsKey(property.name)) { + if(values != null && values.containsKey(property.getColumnName())) { TYPE value = getValue(property); if (value == null) { if (newValue == null) @@ -341,10 +341,10 @@ public abstract class AbstractModel implements Parcelable, Cloneable { * @param property */ public synchronized void clearValue(Property property) { - if(setValues != null && setValues.containsKey(property.name)) - setValues.remove(property.name); - if(values != null && values.containsKey(property.name)) - values.remove(property.name); + if(setValues != null && setValues.containsKey(property.getColumnName())) + setValues.remove(property.getColumnName()); + if(values != null && values.containsKey(property.getColumnName())) + values.remove(property.getColumnName()); } /** @@ -460,22 +460,22 @@ public abstract class AbstractModel implements Parcelable, Cloneable { } public Void visitDouble(Property property, Object value) { - store.put(property.name, (Double) value); + store.put(property.getColumnName(), (Double) value); return null; } public Void visitInteger(Property property, Object value) { - store.put(property.name, (Integer) value); + store.put(property.getColumnName(), (Integer) value); return null; } public Void visitLong(Property property, Object value) { - store.put(property.name, (Long) value); + store.put(property.getColumnName(), (Long) value); return null; } public Void visitString(Property property, Object value) { - store.put(property.name, (String) value); + store.put(property.getColumnName(), (String) value); return null; } } diff --git a/api/src/com/todoroo/andlib/data/Property.java b/api/src/com/todoroo/andlib/data/Property.java index e3c892d5f..34e417ae2 100644 --- a/api/src/com/todoroo/andlib/data/Property.java +++ b/api/src/com/todoroo/andlib/data/Property.java @@ -227,6 +227,17 @@ public abstract class Property extends Field implements Cloneable { PropertyVisitor visitor, PARAMETER data) { return visitor.visitLong(this, data); } + + @Override + public LongProperty as(String newAlias) { + return (LongProperty) super.as(newAlias); + } + } + + public String getColumnName() { + if (hasAlias()) + return alias; + return name; } // --- pseudo-properties diff --git a/api/src/com/todoroo/andlib/data/TodorooCursor.java b/api/src/com/todoroo/andlib/data/TodorooCursor.java index add57f79b..a0158a3e4 100644 --- a/api/src/com/todoroo/andlib/data/TodorooCursor.java +++ b/api/src/com/todoroo/andlib/data/TodorooCursor.java @@ -130,7 +130,7 @@ public class TodorooCursor extends CursorWrapper { } private int columnIndex(Property property, TodorooCursor cursor) { - return cursor.getColumnIndexFromCache(property.name); + return cursor.getColumnIndexFromCache(property.getColumnName()); } } diff --git a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java index ae3c2c7ad..f6175e2a6 100644 --- a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java @@ -110,7 +110,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { public static final String BROADCAST_EXTRA_TASK = "model"; //$NON-NLS-1$ private static final LongProperty TASK_RABBIT_ID = new LongProperty(Metadata.TABLE.as(TaskListFragment.TR_METADATA_JOIN), - Metadata.ID.name); + Metadata.ID.name).as("taskRabId"); //$NON-NLS-1$ // --- other constants @@ -790,7 +790,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable { public static final String FILE_COLUMN = "fileId"; private static final String METADATA_JOIN = "for_actions"; - private final LongProperty fileIdProperty = new LongProperty(Metadata.TABLE.as(METADATA_JOIN), Metadata.ID.name); + private final LongProperty fileIdProperty = new LongProperty(Metadata.TABLE.as(METADATA_JOIN), + Metadata.ID.name).as(FILE_COLUMN); @Override public void run() { @@ -802,7 +803,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { groupedQuery = query.get() + " GROUP BY " + Task.ID; Query q = Query.select(Task.ID, Task.TITLE, Task.NOTES, Task.COMPLETION_DATE, - fileIdProperty.as(FILE_COLUMN)) + fileIdProperty) .join(Join.left(Metadata.TABLE.as(METADATA_JOIN), Criterion.and(Field.field(METADATA_JOIN + "." + Metadata.KEY.name).eq(FileMetadata.METADATA_KEY), Task.ID.eq(Field.field(METADATA_JOIN + "." + Metadata.TASK.name))))).withQueryTemplate(groupedQuery);