Android : Translating SQLite nested queries into parse queries

on Thursday, July 10, 2014


Take the following where clause for a cursor:



whereClause = DBHelper.SCHEDULE_DAY + " = '" + dayOfTheWeek + "' AND "
+ DBHelper.SCHEDULE_STARTTIME + " = (SELECT " + DBHelper.SCHEDULE_STARTTIME
+ " FROM " + DBHelper.TABLE_SCHEDULE + " WHERE " + DBHelper.SCHEDULE_DAY
+ " = '" + dayOfTheWeek + "' AND " + DBHelper.SCHEDULE_STARTTIME + " < "
+ (currenttime - 100) + " ORDER BY "
+ DBHelper.SCHEDULE_STARTTIME + " DESC LIMIT 1)";


Which, written in SQL syntax, would be:



SELECT className FROM SCHEDULE WHERE day = "Tuesday" AND startTime = (SELECT startTime FROM SCHEDULE WHERE day = "Tuesday" AND startTime < 1200 ORDER BY startTime DESC LIMIT 1);



is it possibly in Parse Queries to nest parameters? With the LocalData store feature, I'd like to remove my SQLite databases in favor of the LocalDatastore, as I already am using it for temporary caching. Maybe I'm overthinking the translation from SQLite to Parse, but this is what I've come up with so far:



ParseQuery<ParseObject> query = ParseQuery.getQuery("classes");
query.whereEqualTo("day", dayOfTheWeek);
// What to add in here for time?
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> classList, ParseException e) {
if (e == null) {
//Do something with results
} else {
// Failed
}
}
});


Classes last for an hour exact, hence the currenttime -100 (IE if it's 8AM, look for classes starting at 0700 or before (0800-100). Is the answer a compound Parse Query, such as the following?:



ParseQuery<ParseObject> startHour = ParseQuery.getQuery("classes");
startHour.whereEqualTo("day", dayOfTheWeek);
query.whereLessThan("startHour", currenttime -100);
query.addDescendingOrder("startHour");
query.setLimit(1);

List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(startHour);

ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.whereEqualTo("day", dayOfTheWeek);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
// result is the classes that start at or before the hour found
}
});


Big thanks to the community on StackOverflow for all the help they provide!


0 comments:

Post a Comment