1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
Ruby on Rails
Find a file
Dylan Smith 1a2bf3df0e Test quoting integers when comparing a string column with integers.
An equality with a string column and integer like

  SELECT * FROM `users` WHERE `login_token` = 0 LIMIT 1;

will match match any string that doesn't start with a digit in certain
databases, like mysql. Make sure we quote the integer to avoid this
problem in a database independant way.
2014-04-28 03:10:47 -04:00
lib always add a space before keywords (it is easier) 2014-04-11 16:17:42 -07:00
test Test quoting integers when comparing a string column with integers. 2014-04-28 03:10:47 -04:00
.autotest Fisting arel specs -- still needs tree_manager and cleanup 2010-10-18 15:41:21 -07:00
.gemtest updating manifest and gemspec 2011-04-28 11:01:04 -07:00
.gitignore Ignore Gemfile.lock 2011-08-08 17:58:39 +01:00
.travis.yml test gem build in ci process, inspired by issue #256 2014-04-12 20:01:17 +03:00
arel.gemspec Fix typo in @brynary's name 2014-04-14 23:13:30 +02:00
Gemfile Bump minitest to 5.1. 2014-01-04 16:31:06 +05:30
History.txt Update history 2014-02-10 19:44:01 -02:00
Manifest.txt Fix manifest and gemspec 2014-04-12 15:01:35 -03:00
MIT-LICENSE.txt Arel is MIT licensed. 2010-09-24 16:50:48 -07:00
Rakefile Fix typo in @brynary's name 2014-04-14 23:13:30 +02:00
README.markdown fixed the readme per discussion w/ tenderlove 2014-02-18 14:45:40 -08:00

Arel Build Status Dependency Status

DESCRIPTION

Arel Really Exasperates Logicians

Arel is a SQL AST manager for Ruby. It

  1. Simplifies the generation of complex SQL queries
  2. Adapts to various RDBMSes

It is intended to be a framework framework; that is, you can build your own ORM with it, focusing on innovative object and collection modeling as opposed to database compatibility and query generation.

Status

For the moment, Arel uses Active Record's connection adapters to connect to the various engines, connection pooling, perform quoting, and do type conversion.

A Gentle Introduction

Generating a query with Arel is simple. For example, in order to produce

SELECT * FROM users

you construct a table relation and convert it to sql:

users = Arel::Table.new(:users)
query = users.project(Arel.sql('*'))
query.to_sql

More Sophisticated Queries

Here is a whirlwind tour through the most common SQL operators. These will probably cover 80% of all interaction with the database.

First is the 'restriction' operator, where:

users.where(users[:name].eq('amy'))
# => SELECT * FROM users WHERE users.name = 'amy'

What would, in SQL, be part of the SELECT clause is called in Arel a projection:

users.project(users[:id])
# => SELECT users.id FROM users

Joins resemble SQL strongly:

users.join(photos).on(users[:id].eq(photos[:user_id]))
# => SELECT * FROM users INNER JOIN photos ON users.id = photos.user_id

What are called LIMIT and OFFSET in SQL are called take and skip in Arel:

users.take(5) # => SELECT * FROM users LIMIT 5
users.skip(4) # => SELECT * FROM users OFFSET 4

GROUP BY is called group:

users.project(users[:name]).group(users[:name])
# => SELECT users.name FROM users GROUP BY users.name

The best property of arel is its "composability", or closure under all operations. For example, to restrict AND project, just "chain" the method invocations:

users                                 \
  .where(users[:name].eq('amy'))      \
  .project(users[:id])                \
# => SELECT users.id FROM users WHERE users.name = 'amy'

All operators are chainable in this way, and they are chainable any number of times, in any order.

users.where(users[:name].eq('bob')).where(users[:age].lt(25))

The OR operator works like this:

users.where(users[:name].eq('bob').or(users[:age].lt(25)))

The AND operator behaves similarly.

The Crazy Features

The examples above are fairly simple and other libraries match or come close to matching the expressiveness of Arel (e.g., Sequel in Ruby).

Inline math operations

Suppose we have a table products with prices in different currencies. And we have a table currency_rates, of constantly changing currency rates. In Arel:

products = Arel::Table.new(:products)
# Attributes: [:id, :name, :price, :currency_id]

currency_rates = Arel::Table.new(:currency_rates)
# Attributes: [:from_id, :to_id, :date, :rate]

Now, to order products by price in user preferred currency simply call:

products.
  join(:currency_rates).on(products[:currency_id].eq(currency_rates[:from_id])).
  where(currency_rates[:to_id].eq(user_preferred_currency), currency_rates[:date].eq(Date.today)).
  order(products[:price] * currency_rates[:rate])

Complex Joins

Where Arel really shines in its ability to handle complex joins and aggregations. As a first example, let's consider an "adjacency list", a tree represented in a table. Suppose we have a table comments, representing a threaded discussion:

comments = Arel::Table.new(:comments)

And this table has the following attributes:

# [:id, :body, :parent_id]

The parent_id column is a foreign key from the comments table to itself. Now, joining a table to itself requires aliasing in SQL. In fact, you may alias in Arel as well:

replies = comments.alias
comments_with_replies = \
  comments.join(replies).on(replies[:parent_id].eq(comments[:id]))
# => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments_2.parent_id = comments.id

This will return the first comment's reply's body.

License

Arel is released under the MIT License.