Parse.com equivalent to SQL joins

痴心易碎 提交于 2019-12-10 14:14:34

问题


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");

    }

}

回答1:


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());
        }
    }
});


来源:https://stackoverflow.com/questions/24239626/parse-com-equivalent-to-sql-joins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!