Respect property aliases when reading/writing to cursors/models

pull/14/head
Sam Bosley 13 years ago
parent 384c2d26e3
commit f956f36466

@ -335,19 +335,19 @@ abstract public class AbstractDatabase {
public static class SqlConstructorVisitor implements PropertyVisitor<String, Void> { public static class SqlConstructorVisitor implements PropertyVisitor<String, Void> {
public String visitDouble(Property<Double> property, Void data) { public String visitDouble(Property<Double> property, Void data) {
return String.format("%s REAL", property.name); return String.format("%s REAL", property.getColumnName());
} }
public String visitInteger(Property<Integer> property, Void data) { public String visitInteger(Property<Integer> property, Void data) {
return String.format("%s INTEGER", property.name); return String.format("%s INTEGER", property.getColumnName());
} }
public String visitLong(Property<Long> property, Void data) { public String visitLong(Property<Long> property, Void data) {
return String.format("%s INTEGER", property.name); return String.format("%s INTEGER", property.getColumnName());
} }
public String visitString(Property<String> property, Void data) { public String visitString(Property<String> property, Void data) {
return String.format("%s TEXT", property.name); return String.format("%s TEXT", property.getColumnName());
} }
} }
} }

@ -186,14 +186,14 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
*/ */
public synchronized <TYPE> TYPE getValue(Property<TYPE> property) { public synchronized <TYPE> TYPE getValue(Property<TYPE> property) {
Object value; Object value;
if(setValues != null && setValues.containsKey(property.name)) if(setValues != null && setValues.containsKey(property.getColumnName()))
value = setValues.get(property.name); value = setValues.get(property.getColumnName());
else if(values != null && values.containsKey(property.name)) else if(values != null && values.containsKey(property.getColumnName()))
value = values.get(property.name); value = values.get(property.getColumnName());
else if(getDefaultValues().containsKey(property.name)) else if(getDefaultValues().containsKey(property.getColumnName()))
value = getDefaultValues().get(property.name); value = getDefaultValues().get(property.getColumnName());
else else
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
@ -253,9 +253,9 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
* @return true if setValues or values contains this property * @return true if setValues or values contains this property
*/ */
public boolean containsValue(Property<?> property) { public boolean containsValue(Property<?> property) {
if(setValues != null && setValues.containsKey(property.name)) if(setValues != null && setValues.containsKey(property.getColumnName()))
return true; return true;
if(values != null && values.containsKey(property.name)) if(values != null && values.containsKey(property.getColumnName()))
return true; return true;
return false; return false;
} }
@ -266,10 +266,10 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
* stored is not null * stored is not null
*/ */
public boolean containsNonNullValue(Property<?> property) { public boolean containsNonNullValue(Property<?> property) {
if(setValues != null && setValues.containsKey(property.name)) if(setValues != null && setValues.containsKey(property.getColumnName()))
return setValues.get(property.name) != null; return setValues.get(property.getColumnName()) != null;
if(values != null && values.containsKey(property.name)) if(values != null && values.containsKey(property.getColumnName()))
return values.get(property.name) != null; return values.get(property.getColumnName()) != null;
return false; return false;
} }
@ -283,11 +283,11 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
Property<TYPE> property, TYPE newValue) { Property<TYPE> property, TYPE newValue) {
// we've already decided to save it, so overwrite old value // we've already decided to save it, so overwrite old value
if (setValues.containsKey(property.name)) if (setValues.containsKey(property.getColumnName()))
return true; return true;
// values contains this key, we should check it out // 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); TYPE value = getValue(property);
if (value == null) { if (value == null) {
if (newValue == null) if (newValue == null)
@ -341,10 +341,10 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
* @param property * @param property
*/ */
public synchronized void clearValue(Property<?> property) { public synchronized void clearValue(Property<?> property) {
if(setValues != null && setValues.containsKey(property.name)) if(setValues != null && setValues.containsKey(property.getColumnName()))
setValues.remove(property.name); setValues.remove(property.getColumnName());
if(values != null && values.containsKey(property.name)) if(values != null && values.containsKey(property.getColumnName()))
values.remove(property.name); values.remove(property.getColumnName());
} }
/** /**
@ -460,22 +460,22 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
} }
public Void visitDouble(Property<Double> property, Object value) { public Void visitDouble(Property<Double> property, Object value) {
store.put(property.name, (Double) value); store.put(property.getColumnName(), (Double) value);
return null; return null;
} }
public Void visitInteger(Property<Integer> property, Object value) { public Void visitInteger(Property<Integer> property, Object value) {
store.put(property.name, (Integer) value); store.put(property.getColumnName(), (Integer) value);
return null; return null;
} }
public Void visitLong(Property<Long> property, Object value) { public Void visitLong(Property<Long> property, Object value) {
store.put(property.name, (Long) value); store.put(property.getColumnName(), (Long) value);
return null; return null;
} }
public Void visitString(Property<String> property, Object value) { public Void visitString(Property<String> property, Object value) {
store.put(property.name, (String) value); store.put(property.getColumnName(), (String) value);
return null; return null;
} }
} }

@ -227,6 +227,17 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
PropertyVisitor<RETURN, PARAMETER> visitor, PARAMETER data) { PropertyVisitor<RETURN, PARAMETER> visitor, PARAMETER data) {
return visitor.visitLong(this, 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 // --- pseudo-properties

@ -130,7 +130,7 @@ public class TodorooCursor<TYPE extends AbstractModel> extends CursorWrapper {
} }
private int columnIndex(Property<?> property, TodorooCursor<?> cursor) { private int columnIndex(Property<?> property, TodorooCursor<?> cursor) {
return cursor.getColumnIndexFromCache(property.name); return cursor.getColumnIndexFromCache(property.getColumnName());
} }
} }

@ -110,7 +110,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
public static final String BROADCAST_EXTRA_TASK = "model"; //$NON-NLS-1$ 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), 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 // --- other constants
@ -790,7 +790,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
public static final String FILE_COLUMN = "fileId"; public static final String FILE_COLUMN = "fileId";
private static final String METADATA_JOIN = "for_actions"; 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 @Override
public void run() { public void run() {
@ -802,7 +803,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
groupedQuery = query.get() + " GROUP BY " + Task.ID; groupedQuery = query.get() + " GROUP BY " + Task.ID;
Query q = Query.select(Task.ID, Task.TITLE, Task.NOTES, Task.COMPLETION_DATE, 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), .join(Join.left(Metadata.TABLE.as(METADATA_JOIN),
Criterion.and(Field.field(METADATA_JOIN + "." + Metadata.KEY.name).eq(FileMetadata.METADATA_KEY), 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); Task.ID.eq(Field.field(METADATA_JOIN + "." + Metadata.TASK.name))))).withQueryTemplate(groupedQuery);

Loading…
Cancel
Save