Parse.com相当于SQL连接 [英] Parse.com equivalent to SQL joins
问题描述
我知道Parse.com是一个NoSQL数据库和SQL概念不应该被应用到它,反正,除了理论,我敢打赌,有一种方式解析的东西做这样的经典SQL连接
例如,假设我们在我们的数据库3个表/班:人民,城市和国家,与示例值在括号
人物:
-name(马里奥·罗西)
- 年龄(23)
- 城市(博洛尼亚)城市:
-name(博洛尼亚)
-Postal code(40100)
-country(意大利)国家:
-name(意大利)
-Continent(欧洲)
由于这种结构,比方说,我想知道在哪个洲的人马里奥·罗西,23岁,家住。在传统的SQL方法这就好像很容易实现:
距离人民选择People.Name,People.Age,Countries.Continent
JOIN中的城市People.City = Cities.name
JOIN上City.Country = Countries.Name国家
凡People.Name ='马里奥·罗西
结果记录将是马里奥·罗西,23,欧洲。现在,如果我想和解析,或者更好,如果我想获得的相同的结果与解析,怎么我的结构看起来一样的东西?我应该使用指针?参考文献?我读到这些结构的,但我不知道他们是否适用于这种情况下,以及如何使用它们。
我正在开发在Android中,但由于这是一个基本的问题,我想我可以接受/基于其他平台也明白的答案。
感谢您!
Fosco建议后编辑:
公共类CustomAdapter扩展ParseQueryAdapter<&的parseObject GT;实现OnItemClickListener {
公共CustomAdapter(上下文的背景下){
超(背景下,新ParseQueryAdapter.QueryFactory<&的parseObject GT;(){
公共ParseQuery<&的parseObject GT;创建() {
ParseQuery<&的parseObject GT;查询= ParseQuery.getQuery(人物);
query.include(城市);
query.include(City.Country);
query.whereEqualTo(姓名,马里奥罗西);
返回查询;
}
});
} @覆盖
公共查看getItemView(的parseObject的parseObject,视图V的ViewGroup以及母公司){
如果(V == NULL){
V = View.inflate(的getContext(),R.layout.row_ppl为null);
} super.getItemView(的parseObject,V,父母);
城市的parseObject = parseobject.getParseObject(城市);
国家的parseObject = City.getParseObject(国家);
字符串大陆= country.getString(大陆); TextView的诺姆=(TextView中)v.findViewById(R.id.textView1);
nome.setText(parseobject.getString(姓名));
TextView的CONT =(TextView中)v.findViewById(R.id.textView2);
cont.setText(大陆); 返回伏; } @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
Log.d(rilevato,点击); }}
如果您创建国家和城市作为解析类,并且使用指针,你可以做到这一点很容易。
城市包含一个指向国家,列名国家
人们包含一个指向城市,列名城市
VAR的查询=新Parse.Query(人物);
query.include(城市);
query.include('city.country');
query.equalTo('名','马里奥罗西');
query.first(),然后(功能(的人){
的console.log(人);
},功能(错误){});
您将有完整的城市和放大器;当你拉国家记录的人的记录。
编辑添加的Android例如:
ParseQuery<&的parseObject GT;查询= ParseQuery.getQuery(人物);
query.include(城市);
query.include(city.country);
query.whereEqualTo(姓名,马里奥罗西);
query.getFirstInBackground(新GetCallback<&的parseObject GT;(){
公共无效做过(的parseObject人,ParseException的E){
如果(E == NULL){
城市的parseObject = object.get(城市);
国家的parseObject = city.get(国家);
字符串大陆= country.get(大陆);
}其他{
Log.d(人,错误:+ e.getMessage());
}
}
});
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());
}
}
});
这篇关于Parse.com相当于SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!