Parse.com equivalent to SQL joins - android

I know that Parse.com is a NoSql database and SQL concepts should not be applied to it, anyway, besides theory, i bet there is a way to do in Parse something like classic SQL joins.
For example, assume we have 3 tables/classes in our DB: People, Cities and Countries, with example values in brackets.
People:
-Name (Mario Rossi)
-Age (23)
-City (Bologna)
Cities:
-Name (Bologna)
-PostalCode (40100)
-Country (Italy)
Countries:
-Name (Italy)
-Continent (Europe)
Given this structure, let's say I want to know in which continent the person Mario Rossi, aged 23, lives. In a classical SQL approach this would be accomplished easily like:
select People.Name,People.Age,Countries.Continent from People
JOIN Cities on People.City = Cities.name
JOIN Countries on City.Country = Countries.Name
Where People.Name = 'Mario Rossi'
The resulting recordset would be "Mario Rossi, 23, Europe". Now, if i want to make the same thing with Parse, or better, if i want to get the same result with Parse, what should my structure look like? Should i use pointers? References? I read about these structures but i don't know if they apply to this case and how to use them.
I'm developing in Android, but since this is a basic question i think i may accept/understand answers based on other platforms too.
Thank you!
Edit after Fosco suggestions:
public class CustomAdapter extends ParseQueryAdapter <ParseObject> implements OnItemClickListener{
public CustomAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("People");
query.include("City");
query.include("City.Country");
query.whereEqualTo("Name","Mario Rossi");
return query;
}
});
}
#Override
public View getItemView(ParseObject parseobject, View v, ViewGroup parent) {
if (v==null){
v = View.inflate(getContext(), R.layout.row_ppl, null);
}
super.getItemView(parseobject, v, parent);
ParseObject city = parseobject.getParseObject("City");
ParseObject country = City.getParseObject("Country");
String continent = country.getString("Continent");
TextView nome = (TextView) v.findViewById(R.id.textView1);
nome.setText(parseobject.getString("Name"));
TextView cont = (TextView) v.findViewById(R.id.textView2);
cont.setText(continent);
return v;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.d("rilevato ", "click");
}
}

If you create Country and City as classes on Parse, and use pointers, you can do this very easily.
Cities contains a pointer to Countries, column name 'country'
People contains a pointer to Cities, column name 'city'
var query = new Parse.Query("People");
query.include('city');
query.include('city.country');
query.equalTo('Name', 'Mario Rossi');
query.first().then(function(person) {
console.log(person);
}, function(err) {
});
You'll have the full city & country records when you pull the people record.
edit to add Android example:
ParseQuery<ParseObject> query = ParseQuery.getQuery("People");
query.include("city");
query.include("city.country");
query.whereEqualTo("Name", "Mario Rossi");
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject person, ParseException e) {
if (e == null) {
ParseObject city = object.get("city");
ParseObject country = city.get("country");
String continent = country.get("continent");
} else {
Log.d("person", "Error: " + e.getMessage());
}
}
});

Related

Converting Parse Relation to list in adapter class

I would like a populate a TextView in one of my fragments with a list of users who are attending a specific trip. On the Parse server, Trip contains a Parse Relation of users attending each instance of a Trip. The following method is in my Trip model class to retrieve a String list of the users' names.
public List<String> getMemberNames(){
final List members = new ArrayList<String>();
try{
ParseRelation<ParseUser> relation = getRelation(KEY_USER);
ParseQuery query = relation.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> parseUsers, ParseException e) {
for (ParseUser parseUser : parseUsers)
{
members.add(parseUser.getUsername());
}
Log.d("relation", members.toString());
}
});
}catch (Exception e){
e.printStackTrace();
}
return members;
}
In my adapter, I call this with the line:
viewHolder.tvMembers.setText(Arrays.toString(trip.getMemberNames().toArray()));
However, when I run the debugger, the list of members remains empty. Any advice on why this is? Is is not possible to query in the Trip model? I appreciate the help!

Android Parse - table of pointers to other tables, want to retrieve objects in the other tables

I feel like I'm pretty close on this one, just need the last bit.
I have the following tables:
_User (standard Parse table)
Category (object Id, name)
Exercises (object Id, name, description, thumbnail, image, etc)
and UserFavourites which is where I store the user's preferred exercises
(objectId, user->users table, exercise->exercises table, category->category table)
I have writing to Parse using pointers just fine:
//create new parse object
ParseObject favouriteExercise = new ParseObject("UserFavourites");
//create pointers to the Exercise table and Category table
ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
//put those pointers into the Userfavourites table and save
favouriteExercise.put("user",ParseUser.getCurrentUser());
favouriteExercise.put("exercise",exercise);
favouriteExercise.put("category",category);
//save
favouriteExercise.saveInBackground();
Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> exercises, ParseException e) {
if (exercises != null) {
Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
mAdapter.clear();
//add all the exercises to the list
mAdapter.addAll(exercises);
//sort the list alphabetically
mAdapter.sort(new Comparator<Exercises>() {
#Override
public int compare(Exercises exercises, Exercises t1) {
return exercises.getName().compareTo(t1.getName());
}
});
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.
Thanks.
Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground
myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});
I figured it out based on the logic kishore jethava gave.
I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.
public void getFavourites() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
query.include("exercise");
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> objects, ParseException e) {
if (objects.size() != 0) {
for(ParseObject object : objects)
{
//for each pointer found, retrieve the object it points to
obj = object.getParseObject("exercise");
mAdapter.add((Exercises) obj);
}
});
}
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
});
}

Android sqllite, querying an object that is a property of another object

How do I query an object that has another object as a property? I need to get the values of the property as well. Here's my model:
public class Department {
public int DeptId;
public string DeptName;
}
public class Employee {
public int Id;
public string Name;
public int DeptId;
public Department Department;
}
I'm coming from a c# background, and I could do this with c# using Entity Framework. Now it seems like this model works but when I included a sqllite functionality for the objects I'm not sure how to query it.
Here's my first try, but I'm not sure if this is the best way
public List<Employee> getAllEmployeesWithDepartments(){
List<Employee> employees = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM Employee e LEFT JOIN Department d on e.DeptId = d.Id" , null);
if(res.moveToFirst()){
do{
Employee emp = new Employee();
emp.Id = res.getInt(res.getColumnIndex("Id"));
emp.Name = res.getString(res.getColumnIndex("Name"));
Department dep = new Department();
dep.Id = res.getInt(res.getColumnIndex("Id"));
dep.Name = res.getString(res.getColumnIndex("Name"));
emp.Department = dep;
employees.add(emp);
}while (res.moveToNext());
}
return employees;
}
You're confused. SQLite doesn't have objects. Its a table based database (like almost all databases). You can map objects into tables, but you have to create those mappings yourself. Since SQLite doesn't have objects, it definitely doesn't have subobjects. You generally make something like it by joining another table with a foreign key constraint on the main table, but it really is defined by your schema. There is no generic answer for you.

Retrieving data from another class in one query

I've built an application that uses Parse. My application allows users to register, login and then post to a parse cloud database.
I have two Parse classes, one called User and one called Posts. User is made up of ObjectId, username and password, and Posts is made up of ObjectId, text and user. Of which user is a pointer to ObjectId within the User class.
I've created a method in my app called getData() which contains a ParseQuery, this queries the Posts class, selects the text field and includes the user field. The query then retrieves the data into a List and then loops through each row of the List, collecting the String from the text field and then adds it into a ListView on the UI using postList.add(textList.get(i).getString("text")); each time the program goes through the loop.Within the loop is another query, which queries the User class, selects the objectId field, I then add a constraint to the query to tell it to only retrieve data where the objectId field is equal to the user field within the Posts class(I think).
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
Next I want to take the collected username data that the query retrieved, put it into a String and display it on screen in a toast. So basically the getData() method should collect all of the strings from the text field and the username of the user that posted it. The problem is that I'm unsure if i'm trying to go about this in the right way. My app throws an error when this piece of code is executed so I'm obviously doing something wrong.
java.lang.IllegalStateException: ParseObject has no data for this key. Call fetchIfNeeded() to get the data.
at com.parse.ParseObject.checkGetAccess(ParseObject.java:3235)
at com.parse.ParseObject.getString(ParseObject.java:2817)
at com.text.parse.MainActivity$3.done(MainActivity.java:186)
Code at line 186 : queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
My questions are:
1. Am I trying to do this in the right way?
2. Why am I receiving this error?
Code for getData() method:
public void getData() {
final ArrayList<String> postList = new ArrayList<String>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);
final ParseQuery<ParseObject> queryPosts = ParseQuery.getQuery("Posts");
queryPosts.selectKeys(Arrays.asList("text"));
queryPosts.include("user");
queryPosts.addDescendingOrder("createdAt");
queryPosts.setLimit(20);
queryPosts.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> textList, ParseException e) {
if (e == null) {
//query successful
for (int i = 0; i < textList.size(); i++) {
postList.add(textList.get(i).getString("text"));
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
queryUser.setLimit(20);
queryUser.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> userList, ParseException e) {
if (e == null) {
String s = userList.get(0).getString("username").toString();
Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
}
else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
lvText.setAdapter(listAdapter);
} else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
Sorry for the long question. Any help would be greatly appreciated, thanks.
UPDATE:For anyone stuck with a similar problem, here's how I got it to work:
public void getData() {
final ArrayList<String> postList = new ArrayList<String>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);
final ParseQuery<ParseObject> queryPosts = ParseQuery.getQuery("Posts");
queryPosts.include("user");
queryPosts.addDescendingOrder("createdAt");
queryPosts.setLimit(20);
queryPosts.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> textList, ParseException e) {
if (e == null) {
//query successful
for (int i = 0; i < textList.size(); i++) {
postList.add(textList.get(i).getString("text"));
ParseObject po1 = textList.get(i);
ParseObject po2 = po1.getParseObject("user");
String username = po2.getString("username");
Toast.makeText(MainActivity.this, username, Toast.LENGTH_SHORT).show();
}
lvText.setAdapter(listAdapter);
} else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
}
You simply include the column in the class you are querying that holds a pointer to another class, that then gives you access to all of the columns of data within the second class.
This method as shown is doing nothing useful:
ParseQuery<ParseObject> queryUser = ParseQuery.getQuery("User");
queryUser.selectKeys(Arrays.asList("objectId"));
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
The selectKeys statement is telling it to only return the contents of the objectId column, which you are passing in to the whereEqualTo statement as a parameter... seems silly to run a query to get a value you already have!?. I would not user selectKeys until you think you need to optimise your queries. The only use this query would have is to let you know if the objectId is valid, since the query will return null if it isn't a valid objectId for a User.
I'm hoping that you want to get more information about the user, so if you remove selectKeys then the other columns will be returned.
The fact that fetchIfNeeded is throwing an exception on due to this line:
queryUser.whereEqualTo("objectId", textList.get(i).getString("user"));
That suggests that textList.get(i).getString("user") is not returning an objectId for a user. If that is instead returning a username as suggested by some of your other comments (not sure here), then you need to change that line of code to read:
queryUser.whereEqualTo("username", textList.get(i).getString("user"));
If there are some other questions you have, you'll need to be a bit more precise in your questions as it isn't really clear what you're asking at the moment.

ORMLite Select Distinct Fields

I have a SQLite table (on Android) that has numerous fields, but certain fields are repeated/denormalized. I would like to select a distinct set of this data and use them as actual objects.
Example
books table
title summary author
Little Johnny A funny kid Johnny Himself
Big Johnny A funny adult Johnny Himself
I would like to extract one author from this list ("Johnny Himself") and would expect I should be able to do this with ORMLite instead of manually with Java.
I would like to select a distinct set of this data and use them as actual objects.
ORMLite supports a distinct() method on the QueryBuilder that should do what you want. So your code would look something like:
List<Book> results = booksDao.queryBuilder()
.distinct().selectColumns("author").query();
In this case, the resulting Book objects would only have the author field set and not the id field or anything else. If you just wanted the author names instead of objects then you could do:
GenericRawResults<String[]> rawResults =
booksDao.queryRaw("SELECT DISTINCT author FROM books");
for (String[] resultColumns : rawResults) {
String author = resultColumns[0];
...
}
This is my application code
public class DbHelper<T> {
private Class<T> c;
private DatabaseHelper db;
public DbHelper(Class<T> c) {
this.c = c;
db = DatabaseHelper.getInstance();
}
This is a good idea
public List<T> queryForBuilderDistinct(int offset, int limit, String ColumnsName,
String orderName, boolean isAsc) {
try {
Dao<T, Integer> dao = db.getDao(c);
QueryBuilder<T, Integer> queryBuilder = dao.queryBuilder();
if (offset != 0) {
queryBuilder.offset((long) offset);
}
if (limit != 0) {
queryBuilder.limit((long) limit);
}
if (orderName != null) {
queryBuilder.orderBy(orderName, isAsc);
}
queryBuilder.distinct().selectColumns(ColumnsName);
return dao.query(queryBuilder.prepare());
} catch (SQLException e) {
LogUtil.e(TAG, "queryForBuilderDistinct", e);
}
return new ArrayList<T>();
}

Categories

Resources