Rails 3.0 中的 Arel 到底是什么? [英] What exactly is Arel in Rails 3.0?

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

问题描述

我知道它是 ActiveRecord 的替代品,并且它使用对象而不是查询.

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

可是……

为什么这样更好?

对象/查询会更容易"创建吗?

will objects/queries be "easier" to create?

它会导致更高效的 SQL 查询吗?

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?

推荐答案

Rails 3.0 中的 Arel 到底是什么?

What exactly is Arel in Rails 3.0?

它是关系查询运算符代数的对象模型.

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

我知道它是 ActiveRecord 的替代品

I understand that it is a replacement for ActiveRecord

不,它不是.它替代了在字符串中手工制作的 SQL 查询.它是一个常见的查询层,位于 ActiveRecord 的基础,但它也可以用作 DataMapper 的基础,例如.

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.

如果它是任何东西的替代品,那就是 Ambition 的替代品.或者,您可以将其视为 LINQ 标准查询运算符或 Python 的 SQLAlchemy 的 Ruby 版本.(事实上​​,作者明确引用了 LINQ 和 SQLAlchemy 作为灵感.)

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.)

或者,您可以将其视为 named_scope 的替代品.事实上,ARel 几乎是每个查询都是一个 named_scope"这一想法的实现.而且,whaddayaknow:两者都是同一个人写的.

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.

不,它使用对象作为查询.

为什么这更好?

Ruby 是一种面向对象的语言,而不是面向字符串的语言.出于这个原因,单独,将查询表示为对象而不是字符串是有意义的.为查询构建适当的对象模型而不是对所有内容使用字符串所带来的好处与为会计系统构建适当的对象模型而不是对所有内容使用字符串所带来的好处几乎相同.

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.

另一大优势是 ARel 实现了查询运算符的实际代数.换句话说,ARel 知道构建和编写查询的数学规则.如果连接两个字符串,每个字符串都包含一个有效的 SQL 查询,则结果可能不是有效的 SQL 查询.或者,更糟糕的是,它是一个有效的 SQL 查询,但它没有任何意义,或者它所做的事情与您认为的完全不同.ARel永远不会发生这种情况.(这就是我在下面链接到的文章的意思是在组合下关闭".)

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.

它会导致更高效的 SQL 查询吗?

will it lead to more efficient SQL queries?

是的.ARel 具有用于查询的适当对象模型这一事实意味着它可以在生成实际 SQL 查询之前很久就对这些查询执行优化.

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.

是的.事实上,我一直在上面讨论 SQL,但实际上关系查询代数可以为几乎所有内容生成查询.同样,以 LINQ 或 Ambition 为例:两者都可以查询 SQL、LDAP、ActiveResource、CouchDB、Amazon、Google ……所有这些都使用相同的语法.

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.

关于什么是 ARel 以及为什么 Nick Kallen 写了一篇恰当命名的文章,也许最好的讨论是 为什么是 Arel? Nick Kallen 本人.注意:这篇文章包含一些温和的数学和计算机科学术语,但这正是重点:ARel 在数学和计算机科学方面有一些扎实的基础,这些基础赋予了它强大的特性.

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 中的 Arel 到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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