diff options
author | Bryan Helmkamp <bryan@brynary.com> | 2009-09-20 12:00:39 -0400 |
---|---|---|
committer | Bryan Helmkamp <bryan@brynary.com> | 2009-09-20 12:00:39 -0400 |
commit | 58920bc508a533d02140f4df3db25c3b9e1c7597 (patch) | |
tree | a27fcdd77290be44d495c4ff145df94fd130c9fa | |
parent | 724b48620c5cf21920717d66eea6e7cd4148b535 (diff) | |
parent | 8852db7087a8f4f98e5fd26fa33bac14a5400979 (diff) | |
download | rails-58920bc508a533d02140f4df3db25c3b9e1c7597.tar.gz rails-58920bc508a533d02140f4df3db25c3b9e1c7597.tar.bz2 rails-58920bc508a533d02140f4df3db25c3b9e1c7597.zip |
Merge remote branch 'miloops/master' into miloops
Conflicts:
spec/connections/mysql_connection.rb
spec/connections/postgresql_connection.rb
spec/connections/sqlite3_connection.rb
69 files changed, 208 insertions, 122 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..5f43dd3ae5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/rails"] + path = vendor/rails + url = git://github.com/rails/rails.git diff --git a/README.markdown b/README.markdown index 4d95234423..20d6e62b44 100644 --- a/README.markdown +++ b/README.markdown @@ -15,20 +15,20 @@ The long term goal, following both LINQ and DataMapper, is to have Arel adapt to 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 = Table(:users) users.to_sql - + In fact, you will probably never call `#to_sql`. Rather, you'll work with data from the table directly. You can iterate through all rows in the `users` table like this: users.each { |user| ... } - + In other words, Arel relations implement Ruby's Enumerable interface. Let's have a look at a concrete example: users.first # => { users[:id] => 1, users[:name] => 'bob' } - + As you can see, Arel converts the rows from the database into a hash, the values of which are sublimated to the appropriate Ruby primitive (integers, strings, and so forth). ### More Sophisticated <strike>Queries</strike> Relations ### @@ -43,7 +43,7 @@ First is the 'restriction' operator, `where`: 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])) @@ -53,7 +53,7 @@ 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.group(users[:name]) # => SELECT * FROM users GROUP BY name @@ -68,7 +68,7 @@ The best property of the Relational Algebra is its "composability", or closure u 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)) - + Of course, many of the operators take multiple arguments, so the last example can be written more tersely: users.where(users[:name].eq('bob'), users[:age].lt(25)) @@ -76,7 +76,7 @@ Of course, many of the operators take multiple arguments, so the last example ca The `OR` operator is not yet supported. It will work like this: users.where(users[:name].eq('bob').or(users[:age].lt(25))) - + The `AND` operator will behave similarly. Finally, most operations take a block form. For example: @@ -96,7 +96,7 @@ The examples above are fairly simple and other libraries match or come close to 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 = Table(:comments) - + And this table has the following attributes: comments.attributes # => [comments[:id], comments[:body], comments[:parent_id]] @@ -107,23 +107,23 @@ The `parent_id` column is a foreign key from the `comments` table to itself. Now 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 - + The call to `#alias` is actually optional: Arel will always produce a unique name for every table joined in the relation, and it will always do so deterministically to exploit query caching. Explicit aliasing is more common, however. When you want to extract specific slices of data, aliased tables are a necessity. For example to get just certain columns from the row, treat a row like a hash: comments_with_replies.first[replies[:body]] - + This will return the first comment's reply's body. If you don't need to extract the data later (for example, you're simply doing a join to find comments that have replies, you don't care what the content of the replies are), the block form may be preferable: comments.join(comments) { |comments, replies| replies[:parent_id].eq(comments[:id]) } # => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments_2.parent_id = comments.id - + Note that you do NOT want to do something like: comments.join(comments, comments[:parent_id].eq(comments[:id])) # => SELECT * FROM comments INNER JOIN comments AS comments_2 WHERE comments.parent_id = comments.id - + This does NOT have the same meaning as the previous query, since the comments[:parent_id] reference is effectively ambiguous. #### Complex Aggregations #### @@ -135,24 +135,24 @@ The easiest way to introduce this is in SQL. Your task is to get all users and t SELECT count(*) FROM photos GROUP BY user_id - + Now, we'd like to join this with the user table. Naively, you might try to do this: SELECT users.*, count(photos.id) FROM users LEFT OUTER JOIN photos - ON users.id = photos.id + ON users.id = photos.user_id GROUP BY photos.user_id - + Of course, this has a slightly different meaning than our intended query. This is actually a fairly advanced topic in SQL so let's see why this doesn't work *step by step*. Suppose we have these records in our `users` table: mysql> select * from users; +------+--------+ | id | name | +------+--------+ - | 1 | hai | - | 2 | bai | - | 3 | dumpty | + | 1 | hai | + | 2 | bai | + | 3 | dumpty | +------+--------+ And these in the photos table: @@ -161,19 +161,19 @@ And these in the photos table: +------+---------+-----------+ | id | user_id | camera_id | +------+---------+-----------+ - | 1 | 1 | 1 | - | 2 | 1 | 1 | - | 3 | 1 | 1 | + | 1 | 1 | 1 | + | 2 | 1 | 1 | + | 3 | 1 | 1 | +------+---------+-----------+ - + If we perform the above, incorrect query, we get the following: mysql> select users.*, count(photos.id) from users left outer join photos on users.id = photos.user_id limit 3 group by user_id; +------+------+------------------+ | id | name | count(photos.id) | +------+------+------------------+ - | 2 | bai | 0 | - | 1 | hai | 3 | + | 2 | bai | 0 | + | 1 | hai | 3 | +------+------+------------------+ As you can see, we're completely missing data for user with id 3. `dumpty` has no photos, neither does `bai`. But strangely `bai` appeared and `dumpty` didn't! The reason is that the `GROUP BY` clause is aggregating on both tables, not just the `photos` table. All users without photos have a `photos.id` of `null` (thanks to the left outer join). These are rolled up together and an arbitrary user wins. In this case, `bai` not `dumpty`. @@ -181,4 +181,4 @@ As you can see, we're completely missing data for user with id 3. `dumpty` has n SELECT users.*, photos_aggregation.cnt FROM users LEFT OUTER JOIN (SELECT user_id, count(*) as cnt FROM photos GROUP BY user_id) AS photos_aggregation - ON photos_aggregation.user_id = users.id
\ No newline at end of file + ON photos_aggregation.user_id = users.id @@ -40,6 +40,8 @@ namespace :spec do for adapter in %w[mysql sqlite3 postgresql] desc "Run specs with the #{adapter} database adapter" Spec::Rake::SpecTask.new(adapter) do |t| + t.libs << "#{File.dirname(__FILE__)}/vendor/rails/activerecord/lib" + t.libs << "#{File.dirname(__FILE__)}/spec" t.spec_files = ["spec/connections/#{adapter}_connection.rb"] + ["spec/schemas/#{adapter}_schema.rb"] + diff --git a/lib/arel.rb b/lib/arel.rb index fcca60758e..c4069aa78e 100644 --- a/lib/arel.rb +++ b/lib/arel.rb @@ -1,10 +1,11 @@ -$LOAD_PATH.unshift(File.dirname(__FILE__)) +require 'active_support/inflector' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/class/attribute_accessors' -require 'rubygems' -require 'activesupport' -require 'activerecord' +require 'active_record' require 'active_record/connection_adapters/abstract/quoting' +$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'arel/algebra' require 'arel/engines' require 'arel/session' diff --git a/lib/arel/.DS_Store b/lib/arel/.DS_Store Binary files differdeleted file mode 100644 index 9918127870..0000000000 --- a/lib/arel/.DS_Store +++ /dev/null diff --git a/lib/arel/algebra/relations/relation.rb b/lib/arel/algebra/relations/relation.rb index 9fdac26528..5403e40fae 100644 --- a/lib/arel/algebra/relations/relation.rb +++ b/lib/arel/algebra/relations/relation.rb @@ -110,7 +110,7 @@ module Arel private def matching_attributes(attribute) (@matching_attributes ||= attributes.inject({}) do |hash, a| - (hash[a.root] ||= []) << a + (hash[a.is_a?(Value) ? a.value : a.root] ||= []) << a hash end)[attribute.root] || [] end diff --git a/lib/arel/engines/sql/formatters.rb b/lib/arel/engines/sql/formatters.rb index ae80feb18e..626803a887 100644 --- a/lib/arel/engines/sql/formatters.rb +++ b/lib/arel/engines/sql/formatters.rb @@ -13,7 +13,6 @@ module Arel class SelectClause < Formatter def attribute(attribute) - # FIXME this should check that the column exists "#{quote_table_name(name_for(attribute.original_relation))}.#{quote_column_name(attribute.name)}" + (attribute.alias ? " AS #{quote(attribute.alias.to_s)}" : "") end @@ -92,8 +91,12 @@ module Arel end def table(table) - quote_table_name(table.name) + - (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') + if table.name =~ /\s/ + table.name + else + quote_table_name(table.name) + + (table.name != name_for(table) ? " AS " + quote_table_name(name_for(table)) : '') + end end end diff --git a/lib/arel/engines/sql/primitives.rb b/lib/arel/engines/sql/primitives.rb index 16e1abdf59..6cce46a441 100644 --- a/lib/arel/engines/sql/primitives.rb +++ b/lib/arel/engines/sql/primitives.rb @@ -7,6 +7,8 @@ module Arel def to_sql(formatter = nil) self end + + include Attribute::Expressions end class Attribute diff --git a/lib/arel/engines/sql/relations/operations/join.rb b/lib/arel/engines/sql/relations/operations/join.rb index 7c5e13510a..a3aaaa163b 100644 --- a/lib/arel/engines/sql/relations/operations/join.rb +++ b/lib/arel/engines/sql/relations/operations/join.rb @@ -26,8 +26,8 @@ module Arel end class StringJoin < Join - def joins(_, __ = nil) - relation2 + def joins(environment, formatter = Sql::TableReference.new(environment)) + [relation1.joins(environment), relation2].compact.join(" ") end end end diff --git a/lib/arel/engines/sql/relations/relation.rb b/lib/arel/engines/sql/relations/relation.rb index 4cfb83a601..ed7f19ddbd 100644 --- a/lib/arel/engines/sql/relations/relation.rb +++ b/lib/arel/engines/sql/relations/relation.rb @@ -5,6 +5,26 @@ module Arel end def select_sql + if engine.adapter_name == "PostgreSQL" && !orders.blank? && using_distinct_on? + # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this + # by wrapping the +sql+ string as a sub-select and ordering in that query. + order = order_clauses.join(', ').split(',').map { |s| s.strip }.reject(&:blank?) + order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{'DESC' if s =~ /\bdesc$/i}" }.join(', ') + + query = build_query \ + "SELECT #{select_clauses.to_s}", + "FROM #{table_sql(Sql::TableReference.new(self))}", + (joins(self) unless joins(self).blank? ), + ("WHERE #{where_clauses.join("\n\tAND ")}" unless wheres.blank? ), + ("GROUP BY #{group_clauses.join(', ')}" unless groupings.blank? ) + + build_query \ + "SELECT * FROM (#{query}) AS id_list", + "ORDER BY #{order}", + ("LIMIT #{taken}" unless taken.blank? ), + ("OFFSET #{skipped}" unless skipped.blank? ) + + else build_query \ "SELECT #{select_clauses.join(', ')}", "FROM #{table_sql(Sql::TableReference.new(self))}", @@ -14,6 +34,7 @@ module Arel ("ORDER BY #{order_clauses.join(', ')}" unless orders.blank? ), ("LIMIT #{taken}" unless taken.blank? ), ("OFFSET #{skipped}" unless skipped.blank? ) + end end def inclusion_predicate_sql @@ -46,5 +67,8 @@ module Arel orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) } end + def using_distinct_on? + select_clauses.any? { |x| x =~ /DISTINCT ON/ } + end end end diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb index f1a9bfd2ac..d648a54d91 100644 --- a/lib/arel/engines/sql/relations/writes.rb +++ b/lib/arel/engines/sql/relations/writes.rb @@ -11,11 +11,17 @@ module Arel class Insert < Compound def to_sql(formatter = nil) + insertion_attributes_values_sql = if record.is_a?(Value) + record.value + else + build_query "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", + "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + end + build_query \ "INSERT", "INTO #{table_sql}", - "(#{record.keys.collect { |key| engine.quote_column_name(key.name) }.join(', ')})", - "VALUES (#{record.collect { |key, value| key.format(value) }.join(', ')})" + insertion_attributes_values_sql end end @@ -24,16 +30,40 @@ module Arel build_query \ "UPDATE #{table_sql} SET", assignment_sql, - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ) + build_update_conditions_sql end protected - def assignment_sql - assignments.collect do |attribute, value| - "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" - end.join(",\n") + if assignments.respond_to?(:collect) + assignments.collect do |attribute, value| + "#{engine.quote_column_name(attribute.name)} = #{attribute.format(value)}" + end.join(",\n") + else + assignments.value + end + end + + def build_update_conditions_sql + conditions = "" + conditions << " WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? + conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank? + + unless taken.blank? + conditions << " LIMIT #{taken}" + + if engine.adapter_name != "MySQL" + begin + quote_primary_key = engine.quote_column_name(table.name.classify.constantize.primary_key) + rescue NameError + quote_primary_key = engine.quote_column_name("id") + end + + conditions = "WHERE #{quote_primary_key} IN (SELECT #{quote_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})" + end + end + + conditions end end end diff --git a/lib/arel/session.rb b/lib/arel/session.rb index 921ad0a7ac..cf04e8a93a 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -24,7 +24,6 @@ module Arel module CRUD def create(insert) insert.call - insert end def read(select) @@ -35,12 +34,10 @@ module Arel def update(update) update.call - update end def delete(delete) delete.call - delete end end include CRUD diff --git a/spec/arel/algebra/unit/predicates/binary_spec.rb b/spec/arel/algebra/unit/predicates/binary_spec.rb index 14fd7ab21b..97ef098e0e 100644 --- a/spec/arel/algebra/unit/predicates/binary_spec.rb +++ b/spec/arel/algebra/unit/predicates/binary_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Binary do diff --git a/spec/arel/algebra/unit/predicates/equality_spec.rb b/spec/arel/algebra/unit/predicates/equality_spec.rb index af91f8b51b..c917520158 100644 --- a/spec/arel/algebra/unit/predicates/equality_spec.rb +++ b/spec/arel/algebra/unit/predicates/equality_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Equality do diff --git a/spec/arel/algebra/unit/predicates/in_spec.rb b/spec/arel/algebra/unit/predicates/in_spec.rb index a8a15ce4e3..54a6d6c7da 100644 --- a/spec/arel/algebra/unit/predicates/in_spec.rb +++ b/spec/arel/algebra/unit/predicates/in_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe In do diff --git a/spec/arel/algebra/unit/primitives/attribute_spec.rb b/spec/arel/algebra/unit/primitives/attribute_spec.rb index 2ca63ba48e..cd484007e1 100644 --- a/spec/arel/algebra/unit/primitives/attribute_spec.rb +++ b/spec/arel/algebra/unit/primitives/attribute_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Attribute do diff --git a/spec/arel/algebra/unit/primitives/expression_spec.rb b/spec/arel/algebra/unit/primitives/expression_spec.rb index 768bb492a7..ac932ed139 100644 --- a/spec/arel/algebra/unit/primitives/expression_spec.rb +++ b/spec/arel/algebra/unit/primitives/expression_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Expression do diff --git a/spec/arel/algebra/unit/primitives/value_spec.rb b/spec/arel/algebra/unit/primitives/value_spec.rb index 45208e6c5d..8fed752d66 100644 --- a/spec/arel/algebra/unit/primitives/value_spec.rb +++ b/spec/arel/algebra/unit/primitives/value_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Value do diff --git a/spec/arel/algebra/unit/relations/alias_spec.rb b/spec/arel/algebra/unit/relations/alias_spec.rb index a5d716a638..2aa4d52d99 100644 --- a/spec/arel/algebra/unit/relations/alias_spec.rb +++ b/spec/arel/algebra/unit/relations/alias_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Alias do diff --git a/spec/arel/algebra/unit/relations/delete_spec.rb b/spec/arel/algebra/unit/relations/delete_spec.rb index 7578e12a3e..c244be8631 100644 --- a/spec/arel/algebra/unit/relations/delete_spec.rb +++ b/spec/arel/algebra/unit/relations/delete_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Deletion do diff --git a/spec/arel/algebra/unit/relations/group_spec.rb b/spec/arel/algebra/unit/relations/group_spec.rb index 58f9252356..48fc818682 100644 --- a/spec/arel/algebra/unit/relations/group_spec.rb +++ b/spec/arel/algebra/unit/relations/group_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Group do diff --git a/spec/arel/algebra/unit/relations/insert_spec.rb b/spec/arel/algebra/unit/relations/insert_spec.rb index feb1a5eae4..3141fa2fc4 100644 --- a/spec/arel/algebra/unit/relations/insert_spec.rb +++ b/spec/arel/algebra/unit/relations/insert_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Insert do diff --git a/spec/arel/algebra/unit/relations/join_spec.rb b/spec/arel/algebra/unit/relations/join_spec.rb index f5a8bd32aa..9c1422c571 100644 --- a/spec/arel/algebra/unit/relations/join_spec.rb +++ b/spec/arel/algebra/unit/relations/join_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/algebra/unit/relations/order_spec.rb b/spec/arel/algebra/unit/relations/order_spec.rb index 8b3c932fb9..4190901024 100644 --- a/spec/arel/algebra/unit/relations/order_spec.rb +++ b/spec/arel/algebra/unit/relations/order_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Order do diff --git a/spec/arel/algebra/unit/relations/project_spec.rb b/spec/arel/algebra/unit/relations/project_spec.rb index 9f4358ea54..8886e65a2e 100644 --- a/spec/arel/algebra/unit/relations/project_spec.rb +++ b/spec/arel/algebra/unit/relations/project_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Project do diff --git a/spec/arel/algebra/unit/relations/relation_spec.rb b/spec/arel/algebra/unit/relations/relation_spec.rb index adf82847ac..0a08deffb8 100644 --- a/spec/arel/algebra/unit/relations/relation_spec.rb +++ b/spec/arel/algebra/unit/relations/relation_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Relation do diff --git a/spec/arel/algebra/unit/relations/skip_spec.rb b/spec/arel/algebra/unit/relations/skip_spec.rb index a41913436e..e7dea6c1cf 100644 --- a/spec/arel/algebra/unit/relations/skip_spec.rb +++ b/spec/arel/algebra/unit/relations/skip_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Skip do diff --git a/spec/arel/algebra/unit/relations/table_spec.rb b/spec/arel/algebra/unit/relations/table_spec.rb index dfe457043c..e4c4e58b75 100644 --- a/spec/arel/algebra/unit/relations/table_spec.rb +++ b/spec/arel/algebra/unit/relations/table_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Table do diff --git a/spec/arel/algebra/unit/relations/take_spec.rb b/spec/arel/algebra/unit/relations/take_spec.rb index 2bc17db5a1..3ad8d269f1 100644 --- a/spec/arel/algebra/unit/relations/take_spec.rb +++ b/spec/arel/algebra/unit/relations/take_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Take do diff --git a/spec/arel/algebra/unit/relations/update_spec.rb b/spec/arel/algebra/unit/relations/update_spec.rb index e9642ffc99..9ed20a446b 100644 --- a/spec/arel/algebra/unit/relations/update_spec.rb +++ b/spec/arel/algebra/unit/relations/update_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Update do diff --git a/spec/arel/algebra/unit/relations/where_spec.rb b/spec/arel/algebra/unit/relations/where_spec.rb index 6c3074a3a5..96b95b5823 100644 --- a/spec/arel/algebra/unit/relations/where_spec.rb +++ b/spec/arel/algebra/unit/relations/where_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Where do diff --git a/spec/arel/algebra/unit/session/session_spec.rb b/spec/arel/algebra/unit/session/session_spec.rb index ca0a43f278..03aab6a03f 100644 --- a/spec/arel/algebra/unit/session/session_spec.rb +++ b/spec/arel/algebra/unit/session/session_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Session do diff --git a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb index bffecc9182..db754d9cec 100644 --- a/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +++ b/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do @@ -10,9 +10,8 @@ module Arel ], [:id, :name]) @photos = Table.new(:photos) @photos.delete - @photos \ - .insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) \ - .insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) + @photos.insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6) + @photos.insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42) end describe 'when the in memory relation is on the left' do diff --git a/spec/arel/engines/memory/unit/relations/array_spec.rb b/spec/arel/engines/memory/unit/relations/array_spec.rb index dd9da41569..9a834148b1 100644 --- a/spec/arel/engines/memory/unit/relations/array_spec.rb +++ b/spec/arel/engines/memory/unit/relations/array_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Array do diff --git a/spec/arel/engines/memory/unit/relations/insert_spec.rb b/spec/arel/engines/memory/unit/relations/insert_spec.rb index 59e43328a3..222e525c7b 100644 --- a/spec/arel/engines/memory/unit/relations/insert_spec.rb +++ b/spec/arel/engines/memory/unit/relations/insert_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Insert do @@ -14,13 +14,13 @@ module Arel it "manufactures an array of hashes of attributes to values" do @relation \ .insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \ - .let do |relation| - relation.call.should == [ - Row.new(relation, [1, 'duck']), - Row.new(relation, [2, 'duck']), - Row.new(relation, [3, 'goose']), - Row.new(relation, [4, 'guinea fowl']) - ] + do |relation| + relation.should == [ + Row.new(relation, [1, 'duck']), + Row.new(relation, [2, 'duck']), + Row.new(relation, [3, 'goose']), + Row.new(relation, [4, 'guinea fowl']) + ] end end end diff --git a/spec/arel/engines/memory/unit/relations/join_spec.rb b/spec/arel/engines/memory/unit/relations/join_spec.rb index 110fdb03b7..112434ae1d 100644 --- a/spec/arel/engines/memory/unit/relations/join_spec.rb +++ b/spec/arel/engines/memory/unit/relations/join_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/engines/memory/unit/relations/order_spec.rb b/spec/arel/engines/memory/unit/relations/order_spec.rb index 1e9690bbbf..21d77a2a24 100644 --- a/spec/arel/engines/memory/unit/relations/order_spec.rb +++ b/spec/arel/engines/memory/unit/relations/order_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Order do diff --git a/spec/arel/engines/memory/unit/relations/project_spec.rb b/spec/arel/engines/memory/unit/relations/project_spec.rb index 1690910026..e688b93a39 100644 --- a/spec/arel/engines/memory/unit/relations/project_spec.rb +++ b/spec/arel/engines/memory/unit/relations/project_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Project do diff --git a/spec/arel/engines/memory/unit/relations/skip_spec.rb b/spec/arel/engines/memory/unit/relations/skip_spec.rb index 3411c5493b..0c2077db80 100644 --- a/spec/arel/engines/memory/unit/relations/skip_spec.rb +++ b/spec/arel/engines/memory/unit/relations/skip_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Skip do diff --git a/spec/arel/engines/memory/unit/relations/take_spec.rb b/spec/arel/engines/memory/unit/relations/take_spec.rb index 5e7c4fb462..4b08a63d22 100644 --- a/spec/arel/engines/memory/unit/relations/take_spec.rb +++ b/spec/arel/engines/memory/unit/relations/take_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Take do diff --git a/spec/arel/engines/memory/unit/relations/where_spec.rb b/spec/arel/engines/memory/unit/relations/where_spec.rb index 1d2c2eb39c..8d0af4b52d 100644 --- a/spec/arel/engines/memory/unit/relations/where_spec.rb +++ b/spec/arel/engines/memory/unit/relations/where_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Where do diff --git a/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb index 50b0908441..37afb1a8f2 100644 --- a/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb +++ b/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb index 709ae9f8d1..5ed530508a 100644 --- a/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb +++ b/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb index 4bceef4975..5909716542 100644 --- a/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb +++ b/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/engines/sql/unit/engine_spec.rb b/spec/arel/engines/sql/unit/engine_spec.rb index c607abcfa1..f782f56938 100644 --- a/spec/arel/engines/sql/unit/engine_spec.rb +++ b/spec/arel/engines/sql/unit/engine_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Sql::Engine do diff --git a/spec/arel/engines/sql/unit/predicates/binary_spec.rb b/spec/arel/engines/sql/unit/predicates/binary_spec.rb index b1400e2588..ccf9d4ef73 100644 --- a/spec/arel/engines/sql/unit/predicates/binary_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/binary_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Binary do diff --git a/spec/arel/engines/sql/unit/predicates/equality_spec.rb b/spec/arel/engines/sql/unit/predicates/equality_spec.rb index e874d07bf3..c4dd2c0c31 100644 --- a/spec/arel/engines/sql/unit/predicates/equality_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/equality_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Equality do diff --git a/spec/arel/engines/sql/unit/predicates/in_spec.rb b/spec/arel/engines/sql/unit/predicates/in_spec.rb index d3e75cfb84..4a3eff79ec 100644 --- a/spec/arel/engines/sql/unit/predicates/in_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/in_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe In do diff --git a/spec/arel/engines/sql/unit/predicates/predicates_spec.rb b/spec/arel/engines/sql/unit/predicates/predicates_spec.rb index d55e178e43..fe0010a8cb 100644 --- a/spec/arel/engines/sql/unit/predicates/predicates_spec.rb +++ b/spec/arel/engines/sql/unit/predicates/predicates_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Predicate do diff --git a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb index 6cb72f3c19..d24b6a9d13 100644 --- a/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/attribute_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Attribute do diff --git a/spec/arel/engines/sql/unit/primitives/expression_spec.rb b/spec/arel/engines/sql/unit/primitives/expression_spec.rb index ee7f2c1461..d4df4f8d5a 100644 --- a/spec/arel/engines/sql/unit/primitives/expression_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/expression_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Expression do diff --git a/spec/arel/engines/sql/unit/primitives/literal_spec.rb b/spec/arel/engines/sql/unit/primitives/literal_spec.rb index c7ff1cf879..cf66a60be9 100644 --- a/spec/arel/engines/sql/unit/primitives/literal_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/literal_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe SqlLiteral do @@ -18,6 +18,18 @@ module Arel sql.should be_like(%Q{SELECT COUNT(*) AS count_id FROM "users"}) end end + + it "manufactures expressions on literal SQL fragment" do + sql = @relation.project(SqlLiteral.new("2 * credit_limit").sum).to_sql + + adapter_is :mysql do + sql.should be_like(%Q{SELECT SUM(2 * credit_limit) AS sum_id FROM `users`}) + end + + adapter_is_not :mysql do + sql.should be_like(%Q{SELECT SUM(2 * credit_limit) AS sum_id FROM "users"}) + end + end end end end diff --git a/spec/arel/engines/sql/unit/primitives/value_spec.rb b/spec/arel/engines/sql/unit/primitives/value_spec.rb index ff3533f6ef..c8f03fc270 100644 --- a/spec/arel/engines/sql/unit/primitives/value_spec.rb +++ b/spec/arel/engines/sql/unit/primitives/value_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Value do diff --git a/spec/arel/engines/sql/unit/relations/alias_spec.rb b/spec/arel/engines/sql/unit/relations/alias_spec.rb index b67a0bbc89..55d5a9b671 100644 --- a/spec/arel/engines/sql/unit/relations/alias_spec.rb +++ b/spec/arel/engines/sql/unit/relations/alias_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Alias do diff --git a/spec/arel/engines/sql/unit/relations/delete_spec.rb b/spec/arel/engines/sql/unit/relations/delete_spec.rb index 7a5e2b0088..0b1e2329b6 100644 --- a/spec/arel/engines/sql/unit/relations/delete_spec.rb +++ b/spec/arel/engines/sql/unit/relations/delete_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Deletion do diff --git a/spec/arel/engines/sql/unit/relations/group_spec.rb b/spec/arel/engines/sql/unit/relations/group_spec.rb index 5e0c675c8b..703dc35be5 100644 --- a/spec/arel/engines/sql/unit/relations/group_spec.rb +++ b/spec/arel/engines/sql/unit/relations/group_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Group do diff --git a/spec/arel/engines/sql/unit/relations/insert_spec.rb b/spec/arel/engines/sql/unit/relations/insert_spec.rb index 29a5e0bf42..1884e32a3d 100644 --- a/spec/arel/engines/sql/unit/relations/insert_spec.rb +++ b/spec/arel/engines/sql/unit/relations/insert_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Insert do diff --git a/spec/arel/engines/sql/unit/relations/join_spec.rb b/spec/arel/engines/sql/unit/relations/join_spec.rb index f904b61870..2820763a66 100644 --- a/spec/arel/engines/sql/unit/relations/join_spec.rb +++ b/spec/arel/engines/sql/unit/relations/join_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Join do diff --git a/spec/arel/engines/sql/unit/relations/order_spec.rb b/spec/arel/engines/sql/unit/relations/order_spec.rb index ce97a4dd5e..575e617021 100644 --- a/spec/arel/engines/sql/unit/relations/order_spec.rb +++ b/spec/arel/engines/sql/unit/relations/order_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Order do diff --git a/spec/arel/engines/sql/unit/relations/project_spec.rb b/spec/arel/engines/sql/unit/relations/project_spec.rb index 5e29124cfa..70f2dff70a 100644 --- a/spec/arel/engines/sql/unit/relations/project_spec.rb +++ b/spec/arel/engines/sql/unit/relations/project_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Project do diff --git a/spec/arel/engines/sql/unit/relations/skip_spec.rb b/spec/arel/engines/sql/unit/relations/skip_spec.rb index c14bd1ce95..2d606359ee 100644 --- a/spec/arel/engines/sql/unit/relations/skip_spec.rb +++ b/spec/arel/engines/sql/unit/relations/skip_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Skip do diff --git a/spec/arel/engines/sql/unit/relations/table_spec.rb b/spec/arel/engines/sql/unit/relations/table_spec.rb index 9797b38822..26b9364929 100644 --- a/spec/arel/engines/sql/unit/relations/table_spec.rb +++ b/spec/arel/engines/sql/unit/relations/table_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Table do diff --git a/spec/arel/engines/sql/unit/relations/take_spec.rb b/spec/arel/engines/sql/unit/relations/take_spec.rb index 8f1240fc17..2a8aea3d9d 100644 --- a/spec/arel/engines/sql/unit/relations/take_spec.rb +++ b/spec/arel/engines/sql/unit/relations/take_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Take do diff --git a/spec/arel/engines/sql/unit/relations/update_spec.rb b/spec/arel/engines/sql/unit/relations/update_spec.rb index 4d728eb241..fd444e27a8 100644 --- a/spec/arel/engines/sql/unit/relations/update_spec.rb +++ b/spec/arel/engines/sql/unit/relations/update_spec.rb @@ -1,4 +1,10 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' + +class User + def self.primary_key + "id" + end +end module Arel describe Update do @@ -45,17 +51,17 @@ module Arel adapter_is :sqlite3 do sql.should be_like(%Q{ - UPDATE "users" - SET "name" = 'nick' - LIMIT 1 + UPDATE "users" SET + "name" = 'nick' + WHERE "id" IN (SELECT "id" FROM "users" LIMIT 1) }) end adapter_is :postgresql do sql.should be_like(%Q{ - UPDATE "users" - SET "name" = E'nick' - LIMIT 1 + UPDATE "users" SET + "name" = E'nick' + WHERE "id" IN (SELECT "id" FROM "users" LIMIT 1) }) end end diff --git a/spec/arel/engines/sql/unit/relations/where_spec.rb b/spec/arel/engines/sql/unit/relations/where_spec.rb index 4f0cce1e01..8f9a8db4c7 100644 --- a/spec/arel/engines/sql/unit/relations/where_spec.rb +++ b/spec/arel/engines/sql/unit/relations/where_spec.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper') +require 'spec_helper' module Arel describe Where do diff --git a/spec/connections/mysql_connection.rb b/spec/connections/mysql_connection.rb index bcba2b3843..66a53b5037 100644 --- a/spec/connections/mysql_connection.rb +++ b/spec/connections/mysql_connection.rb @@ -1,6 +1,6 @@ -require "rubygems" -require "activerecord" puts "Using native MySQL" +require "active_record" +require 'logger' ActiveRecord::Base.logger = Logger.new("debug.log") diff --git a/spec/connections/postgresql_connection.rb b/spec/connections/postgresql_connection.rb index 01d65f09dc..0fb6dfe065 100644 --- a/spec/connections/postgresql_connection.rb +++ b/spec/connections/postgresql_connection.rb @@ -1,6 +1,6 @@ -require "rubygems" -require "activerecord" puts "Using native PostgreSQL" +require "active_record" +require 'logger' ActiveRecord::Base.logger = Logger.new("debug.log") diff --git a/spec/connections/sqlite3_connection.rb b/spec/connections/sqlite3_connection.rb index 9e9503e0ca..e8eeee17d0 100644 --- a/spec/connections/sqlite3_connection.rb +++ b/spec/connections/sqlite3_connection.rb @@ -1,6 +1,6 @@ -require "rubygems" -require "activerecord" puts "Using native SQLite3" +require "active_record" +require 'logger' ActiveRecord::Base.logger = Logger.new("debug.log") @@ -16,6 +16,7 @@ ActiveRecord::Base.configurations = { unless File.exist?(db_file) puts "SQLite3 database not found at #{db_file}. Rebuilding it." + require 'fileutils' FileUtils.mkdir_p(File.dirname(db_file)) sqlite_command = %Q{sqlite3 "#{db_file}" "create table a (a integer); drop table a;"} puts "Executing '#{sqlite_command}'" diff --git a/vendor/rails b/vendor/rails new file mode 160000 +Subproject 181cd109d9812d371e2d554a4846f0b2b25b169 |