到底是什么阿雷尔在Rails的3.0? [英] What exactly is Arel in Rails 3.0?

查看:113
本文介绍了到底是什么阿雷尔在Rails的3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这是一个替代的ActiveRecord,并且它使用的对象,而不是查询。

但是......

这是为什么好?

将对象/查询是容易创造?

这将导致更有效的SQL查询?

会是与所有主要的DB兼容吗? - 我认为它会

将是更容易/更难使用存储过程使用?

解决方案
  

到底是什么阿雷尔在Rails的3.0?

这对关系查询操作的代数的对象模型。

  

据我所知,这是一个替代的ActiveRecord

没有,事实并非如此。这是一个替换字符串中手工编写SQL查询。它是 underlies 的ActiveRecord的一种通用查询层,但它也可以用来作为一种支撑为的DataMapper,例如

如果这是一个替代任何东西,这是一个替代野心。或者,你可以把它看成是一个Ruby版本的LINQ标准查询运算符或Python的SQLAlchem​​y的。 (事实上​​,笔者明确列举了两种LINQ和SQLAlchem​​y的为灵感。)

或者,你可以把它看成是替代 named_scope 秒。事实上,阿雷尔是pretty的想法的许多认识到,每个查询是 named_scope 。而且,whaddayaknow:无论是出自同一个人

  

和它使用的,而不是查询对象。

没有,它使用对象的的查询。

  

这是为什么好?

Ruby是一种面向对象的语言,而不是面向字符串的语言。出于这个原因的单独的,是有意义的重新present查询的对象,而不是字符串。构建一个合适的对象模型进行查询,而不是使用字符串的一切给你pretty的大致相同的收益,建立了会计系统,而不是使用字符串的一切合适的对象模型给你。

另外一大优势是,阿雷尔执行查询操作,实际的代数的。换句话说,AREL知道用于构造和构成查询的数学规则。如果您连接两个字符串,其中每个都包含一个有效的SQL查询,结果可能不会是有效的SQL查询。或者,更糟的是,它的的一个有效的SQL查询,而是一个没有任何意义,或者说做一些事情,从你所想象的那样完全不同。这可的永远的发生与阿雷尔。 (这是我的文章链接到下面的意思与封闭下的作文。)

  

将对象/查询是容易创造?

是的。例如,正如我上面提到的,这是很容易从简单的部分构建更复杂的查询。

  

这将导致更有效的SQL查询?

是的。这阿雷尔具有查询合适的对象模型的事实,意味着它可以长期在这些查询进行优化之前,它曾经产生实际的SQL查询。

  

会是与所有主要的DB兼容吗? - 我认为它会

是的。事实上,我总是谈论的SQL以上,但实际上关系查询代数可以产生pretty的每样东西查询。再次,请参见LINQ和雄心的例子:既可以查询SQL,LDAP,的ActiveResource,CouchDB的,亚马逊,谷歌,…都具有相同的语法。

作为也许最好的讨论,什么阿雷尔是,为什么尼克·凯伦写的是适当命名的文章的 为什么阿雷尔?的由尼克·凯伦的自己。注:该文章包含一些轻微的数学和计算机科学术语,但正是这一点:阿雷尔在数学和计算机科学一些有实力的基金会,这些基金会是什么给它以其强大的性能

I understand that it is a replacement for ActiveRecord and that it uses objects instead of queries.

But...

why is this better?

will objects/queries be "easier" to create?

will it lead to more efficient SQL queries?

will it be compatible with all major DBs? - I assume it will.

will it be easier/harder to use with stored procedures?

解决方案

What exactly is Arel in Rails 3.0?

It's an object model for an algebra of relational query operators.

I understand that it is a replacement for ActiveRecord

No, it isn't. It's a replacement for hand-crafting SQL queries in strings. It is a common query layer that underlies ActiveRecord, but it can also be used as an underpinning for DataMapper, for example.

If it is a replacement for anything, it's a replacement for Ambition. Or, you can think of it as a Ruby version of the LINQ standard query operators or Python's SQLAlchemy. (In fact, the author explicitly cites both LINQ and SQLAlchemy as inspirations.)

Or, you can see it as a replacement for named_scopes. In fact, ARel is pretty much the realization of the idea that "every query is a named_scope". And, whaddayaknow: both were written by the same guy.

and that it uses objects instead of queries.

No, it uses objects as queries.

why is this better?

Ruby is an object-oriented language, not a string-oriented language. For that reason alone, it makes sense to represent queries as objects instead of strings. Building a proper object model for queries instead of using strings for everything gives you pretty much the same benefits that building a proper object model for an accounting system instead of using strings for everything gives you.

Another big advantage is that ARel implements an actual algebra of query operators. In other words, ARel knows about the mathematical rules for constructing and composing queries. If you concatenate two strings, each of which contains a valid SQL query, the result is probably not going to be a valid SQL query. Or, even worse, it is a valid SQL query, but one that doesn't make sense, or that does something totally different from what you think it does. This can never happen with ARel. (This is what the article I link to below means with "closed under composition".)

will objects/queries be "easier" to create?

Yes. For example, as I mentioned above, it is much easier to construct more complex queries from simpler parts.

will it lead to more efficient SQL queries?

Yes. The fact that ARel has a proper object model for the queries means that it can perform optimizations on those queries long before it ever generates an actual SQL query.

will it be compatible with all major DBs? - I assume it will.

Yes. In fact, I always talked about SQL above, but actually a relational query algebra can generate queries for pretty much everything. Again, see LINQ or Ambition as examples: both can query SQL, LDAP, ActiveResource, CouchDB, Amazon, Google, … all with the same syntax.

Perhaps the best discussion as to what ARel is and why Nick Kallen wrote is the aptly named article Why Arel? by Nick Kallen himself. Note: the article contains some mild mathematical and computer science jargon, but that is exactly the point: ARel has some strong foundations in mathematics and computer science, those foundations are what give it its powerful properties.

这篇关于到底是什么阿雷尔在Rails的3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆