Parse.com is equivalent to SQL join

I know that Parse.com is a NoSql database, and SQL concepts should not be applied to it , in any case, in addition to theory, I am sure that Parse has a way to do something like a classic SQL join.

For example, suppose we have 3 tables / classes in our database: People, Cities and Countries, with examples of 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 on what continent the man Mario Rossi, 23 years old, lives. In the classic SQL approach, this would be easy to do:

 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' 

As a result, the record set will be "Mario Rossi, 23, Europe." Now, if I want to do the same with Parse, or better, if I want to get the same result with Parse, what should my structure look like? Should I use pointers? Recommendations? I read about these structures, but I do not know if they are applicable to this business and how to use them. I am developing in Android, but since this is the main question, I think I can accept / understand the answers on other platforms.

Thanks!

Edit after Fosco's 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"); } } 
+6
source share
1 answer

If you create Country and City as classes in Parse and use pointers, you can do this very easily.

Cities contain a pointer to countries, the column name is 'country'

People contain a pointer to the city, the column name is "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 will have full records of the city and country when you pull out 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()); } } }); 
+3
source

Source: https://habr.com/ru/post/970889/


All Articles