Added ability to do union queries, started refactoring UpdateAdapter to expect either UserActivity or History

pull/14/head
Sam Bosley 12 years ago
parent 9d8ee61b0b
commit 009a6420cc

@ -16,6 +16,7 @@ import static com.todoroo.andlib.sql.SqlConstants.ORDER_BY;
import static com.todoroo.andlib.sql.SqlConstants.RIGHT_PARENTHESIS;
import static com.todoroo.andlib.sql.SqlConstants.SELECT;
import static com.todoroo.andlib.sql.SqlConstants.SPACE;
import static com.todoroo.andlib.sql.SqlConstants.UNION;
import static com.todoroo.andlib.sql.SqlConstants.WHERE;
import static com.todoroo.andlib.sql.SqlTable.table;
import static java.util.Arrays.asList;
@ -39,6 +40,7 @@ public final class Query {
private final ArrayList<Field> fields = new ArrayList<Field>();
private final ArrayList<Join> joins = new ArrayList<Join>();
private final ArrayList<Field> groupBies = new ArrayList<Field>();
private final ArrayList<Query> unions = new ArrayList<Query>();
private final ArrayList<Order> orders = new ArrayList<Order>();
private final ArrayList<Criterion> havings = new ArrayList<Criterion>();
private int limits = -1;
@ -78,6 +80,11 @@ public final class Query {
return this;
}
public Query union(Query query) {
unions.add(query);
return this;
}
public Query orderBy(Order... order) {
orders.addAll(asList(order));
return this;
@ -113,6 +120,7 @@ public final class Query {
if(queryTemplate == null) {
visitWhereClause(sql);
visitGroupByClause(sql);
visitUnionClause(sql);
visitOrderByClause(sql);
visitLimitClause(sql);
} else {
@ -156,6 +164,15 @@ public final class Query {
sql.deleteCharAt(sql.length() - 1).append(SPACE);
}
private void visitUnionClause(StringBuilder sql) {
if (unions.isEmpty()) {
return;
}
for (Query query : unions) {
sql.append(UNION).append(SPACE).append(query).append(SPACE);
}
}
private void visitWhereClause(StringBuilder sql) {
if (criterions.isEmpty()) {
return;

@ -24,6 +24,7 @@ public final class SqlConstants {
public static final String OR = "OR";
public static final String ORDER_BY = "ORDER BY";
public static final String GROUP_BY = "GROUP BY";
public static final String UNION = "UNION";
public static final String WHERE = "WHERE";
public static final String EXISTS = "EXISTS";
public static final String NOT = "NOT";

@ -71,7 +71,7 @@ public class TagCommentsFragment extends CommentsFragment {
@Override
protected Cursor getCursor() {
return tagDataService.getUpdates(tagData, UpdateAdapter.USER_TABLE_ALIAS, UpdateAdapter.USER_PROPERTIES);
return tagDataService.getActivityAndHistoryForTagData(tagData, null, UpdateAdapter.USER_TABLE_ALIAS, UpdateAdapter.USER_PROPERTIES);
}
@Override

@ -64,6 +64,7 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass);
this.outstandingDao = outstandingDao;
this.changes = new ArrayList<OE>();
Log.e("CH", "Allocating", new Throwable());
}

@ -39,7 +39,9 @@ import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.data.History;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.User;
@ -71,6 +73,10 @@ public class UpdateAdapter extends CursorAdapter {
private static final StringProperty USER_LAST_NAME = User.LAST_NAME.cloneAs(USER_TABLE_ALIAS, "userLastName"); //$NON-NLS-1$
private static final StringProperty USER_NAME = User.NAME.cloneAs(USER_TABLE_ALIAS, "userName"); //$NON-NLS-1$
public static final StringProperty ACTIVITY_TYPE_PROPERTY = new StringProperty(null, "'" + NameMaps.TABLE_ID_USER_ACTIVITY + "' as type"); //$NON-NLS-1$//$NON-NLS-2$
public static final StringProperty HISTORY_TYPE_PROPERTY = new StringProperty(null, "'" + NameMaps.TABLE_ID_HISTORY + "'"); //$NON-NLS-1$
public static final StringProperty PADDING_PROPERTY = new StringProperty(null, "'0'"); //$NON-NLS-1$
public static final Property<?>[] USER_PROPERTIES = {
USER_PICTURE,
USER_FIRST_NAME,
@ -78,6 +84,33 @@ public class UpdateAdapter extends CursorAdapter {
USER_NAME
};
public static final Property<?>[] USER_ACTIVITY_PROPERTIES = {
UserActivity.CREATED_AT,
UserActivity.UUID,
UserActivity.ACTION,
UserActivity.MESSAGE,
UserActivity.TARGET_ID,
UserActivity.TARGET_NAME,
UserActivity.PICTURE,
UserActivity.ID,
ACTIVITY_TYPE_PROPERTY,
};
public static final Property<?>[] HISTORY_PROPERTIES = {
History.CREATED_AT,
History.UUID,
History.COLUMN,
History.TABLE_ID,
History.OLD_VALUE,
History.NEW_VALUE,
PADDING_PROPERTY,
History.ID,
HISTORY_TYPE_PROPERTY,
};
public static final int TYPE_PROPERTY_INDEX = USER_ACTIVITY_PROPERTIES.length - 1;
private static final String TARGET_LINK_PREFIX = "$link_"; //$NON-NLS-1$
private static final Pattern TARGET_LINK_PATTERN = Pattern.compile("\\" + TARGET_LINK_PREFIX + "(\\w*)"); //$NON-NLS-1$//$NON-NLS-2$
private static final String TASK_LINK_TYPE = "task"; //$NON-NLS-1$
@ -129,6 +162,7 @@ public class UpdateAdapter extends CursorAdapter {
private class ModelHolder {
public UserActivity activity = new UserActivity();
public User user = new User();
public History history = new History();
}
/** Creates a new view for use in the list view */
@ -150,15 +184,42 @@ public class UpdateAdapter extends CursorAdapter {
TodorooCursor<UserActivity> cursor = (TodorooCursor<UserActivity>)c;
ModelHolder mh = ((ModelHolder) view.getTag());
String type = cursor.getString(TYPE_PROPERTY_INDEX);
UserActivity update = mh.activity;
update.clear();
update.readFromCursor(cursor);
User user = mh.user;
user.clear();
readUserProperties(cursor, user);
setFieldContentsAndVisibility(view, update, user);
History history = mh.history;
if (NameMaps.TABLE_ID_USER_ACTIVITY.equals(type)) {
readUserActivityProperties(cursor, update);
readUserProperties(cursor, user);
} else {
readHistoryProperties(cursor, history);
}
setFieldContentsAndVisibility(view, update, user, history, type);
}
private static void readUserActivityProperties(TodorooCursor<UserActivity> unionCursor, UserActivity activity) {
activity.setValue(UserActivity.CREATED_AT, unionCursor.getLong(0));
activity.setValue(UserActivity.UUID, unionCursor.getString(1));
activity.setValue(UserActivity.ACTION, unionCursor.getString(2));
activity.setValue(UserActivity.MESSAGE, unionCursor.getString(3));
activity.setValue(UserActivity.TARGET_ID, unionCursor.getString(4));
activity.setValue(UserActivity.TARGET_NAME, unionCursor.getString(5));
activity.setValue(UserActivity.PICTURE, unionCursor.getString(6));
}
private static void readHistoryProperties(TodorooCursor<UserActivity> unionCursor, History history) {
history.setValue(History.CREATED_AT, unionCursor.getLong(0));
history.setValue(History.UUID, unionCursor.getString(1));
history.setValue(History.COLUMN, unionCursor.getString(2));
history.setValue(History.TABLE_ID, unionCursor.getString(3));
history.setValue(History.OLD_VALUE, unionCursor.getString(4));
history.setValue(History.NEW_VALUE, unionCursor.getString(5));
}
public static void readUserProperties(TodorooCursor<UserActivity> joinCursor, User user) {
@ -169,8 +230,17 @@ public class UpdateAdapter extends CursorAdapter {
}
/** Helper method to set the contents and visibility of each field */
public synchronized void setFieldContentsAndVisibility(View view, UserActivity activity, User user) {
public synchronized void setFieldContentsAndVisibility(View view, UserActivity activity, User user, History history, String type) {
// picture
if (NameMaps.TABLE_ID_USER_ACTIVITY.equals(type)) {
setupUserActivityRow(view, activity, user);
} else if (NameMaps.TABLE_ID_HISTORY.equals(type)) {
setupHistoryRow(view, history);
}
}
private void setupUserActivityRow(View view, UserActivity activity, User user) {
final AsyncImageView pictureView = (AsyncImageView)view.findViewById(R.id.picture); {
if (user.containsNonNullValue(USER_PICTURE)) {
String pictureUrl = user.getPictureUrl(USER_PICTURE, RemoteModel.PICTURE_THUMB);
@ -201,7 +271,10 @@ public class UpdateAdapter extends CursorAdapter {
DateUtils.FORMAT_ABBREV_RELATIVE);
date.setText(dateString);
}
}
private void setupHistoryRow(View view, History history) {
//
}
@Override

@ -5,6 +5,7 @@
*/
package com.todoroo.astrid.service;
import android.database.Cursor;
import android.text.TextUtils;
import com.todoroo.andlib.data.Property;
@ -18,11 +19,14 @@ import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.adapter.UpdateAdapter;
import com.todoroo.astrid.api.PermaSql;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.UserActivityDao;
import com.todoroo.astrid.data.History;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
@ -149,7 +153,7 @@ public class TagDataService {
return getUpdatesWithExtraCriteria(tagData, null, userTableAlias, userProperties);
}
private static Query queryForTagData(TagData tagData, Criterion extraCriterion, String userTableAlias, Property<?>[] userProperties) {
private static Query queryForTagData(TagData tagData, Criterion extraCriterion, String userTableAlias, Property<?>[] activityProperties, Property<?>[] userProperties) {
Criterion criteria = Criterion.or(
Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TAG_COMMENT), UserActivity.TARGET_ID.eq(tagData.getUuid())),
Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT),
@ -158,7 +162,7 @@ public class TagDataService {
if (extraCriterion != null)
criteria = Criterion.and(criteria, extraCriterion);
Query result = Query.select(AndroidUtilities.addToArray(UserActivity.PROPERTIES, userProperties)).where(criteria);
Query result = Query.select(AndroidUtilities.addToArray(activityProperties, userProperties)).where(criteria);
if (!TextUtils.isEmpty(userTableAlias))
result = result.join(Join.left(User.TABLE.as(userTableAlias), UserActivity.USER_UUID.eq(Field.field(userTableAlias + "." + User.UUID.name)))); //$NON-NLS-1$
return result;
@ -170,7 +174,29 @@ public class TagDataService {
criterion).
orderBy(Order.desc(UserActivity.CREATED_AT)));
return userActivityDao.query(queryForTagData(tagData, criterion, userTableAlias, userProperties).orderBy(Order.desc(UserActivity.CREATED_AT)));
return userActivityDao.query(queryForTagData(tagData, criterion, userTableAlias, UserActivity.PROPERTIES, userProperties).orderBy(Order.desc(UserActivity.CREATED_AT)));
}
public Cursor getActivityAndHistoryForTagData(TagData tagData, Criterion extraCriterion, String userTableAlias, Property<?>...userProperties) {
Query activityQuery = queryForTagData(tagData, extraCriterion, userTableAlias, UpdateAdapter.USER_ACTIVITY_PROPERTIES, userProperties)
.from(UserActivity.TABLE);
int length = UpdateAdapter.USER_ACTIVITY_PROPERTIES.length;
if (userProperties != null)
length += userProperties.length;
Property<?>[] paddingArray = new Property<?>[Math.max(0, length - UpdateAdapter.HISTORY_PROPERTIES.length)];
for (int i = 0; i < paddingArray.length; i++) {
paddingArray[i] = UpdateAdapter.PADDING_PROPERTY;
}
Query historyQuery = Query.select(AndroidUtilities.addToArray(UpdateAdapter.HISTORY_PROPERTIES, paddingArray)).from(History.TABLE)
.where(Criterion.and(History.TABLE_ID.eq(NameMaps.TABLE_ID_TAGS), History.TARGET_ID.eq(tagData.getUuid())))
.from(History.TABLE);
Query resultQuery = activityQuery.union(historyQuery).orderBy(Order.desc("1")); //$NON-NLS-1$
System.err.println("QUERY: " + resultQuery);
return userActivityDao.query(resultQuery);
}
/**
@ -182,7 +208,7 @@ public class TagDataService {
if(RemoteModel.NO_UUID.equals(tagData.getValue(TagData.UUID)))
return null;
TodorooCursor<UserActivity> updates = userActivityDao.query(queryForTagData(tagData, null, null, null).orderBy(Order.desc(UserActivity.CREATED_AT)).limit(1));
TodorooCursor<UserActivity> updates = userActivityDao.query(queryForTagData(tagData, null, null, UserActivity.PROPERTIES, null).orderBy(Order.desc(UserActivity.CREATED_AT)).limit(1));
try {
if(updates.getCount() == 0)
return null;

Loading…
Cancel
Save