aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--TODO5
-rw-r--r--lib/active_relation/primitives/attribute.rb14
-rw-r--r--lib/active_relation/relations/relation.rb12
-rw-r--r--lib/active_relation/sql.rb11
-rw-r--r--spec/active_relation/unit/primitives/attribute_spec.rb14
-rw-r--r--spec/active_relation/unit/relations/order_spec.rb42
-rw-r--r--spec/active_relation/unit/relations/selection_spec.rb45
7 files changed, 96 insertions, 47 deletions
diff --git a/TODO b/TODO
index 4e7701917d..c58a6d091f 100644
--- a/TODO
+++ b/TODO
@@ -11,9 +11,6 @@ todo:
{:conditions=>{"topics.approved"=>false}}
{:conditions=>{:address=>#<Address:0x3489b3c @street="Funny Street", @country="Loony Land", @city="Scary Town">, "customers.name"=>"David1"}}
-- need to_sql for ranges
- {:conditions=>{:id=>2..3}}
-
- orders need string pass through
:order=>"developers.name desc, developers.id desc",
@@ -69,3 +66,5 @@ done:
- #formatter is now on value, attribute and relation; you must admit it's name is confusing given that e.g., relation already has a formatter (Sql::Relation) ... should it be called predicate formatter? operand1.to_sql(operand2.predicate) maybe prefer operand1.cast(operand2) or project or in light of
- renamed to #format: operand1.format(operand2)
- rename sql strategies
+- need to_sql for ranges
+ - {:conditions=>{:id=>2..3}} \ No newline at end of file
diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb
index ddf5ef5e07..d78e940ffc 100644
--- a/lib/active_relation/primitives/attribute.rb
+++ b/lib/active_relation/primitives/attribute.rb
@@ -63,31 +63,31 @@ module ActiveRelation
module Predications
def eq(other)
- Equality.new(self, other)
+ Equality.new(self, other.bind(relation))
end
def lt(other)
- LessThan.new(self, other)
+ LessThan.new(self, other.bind(relation))
end
def lteq(other)
- LessThanOrEqualTo.new(self, other)
+ LessThanOrEqualTo.new(self, other.bind(relation))
end
def gt(other)
- GreaterThan.new(self, other)
+ GreaterThan.new(self, other.bind(relation))
end
def gteq(other)
- GreaterThanOrEqualTo.new(self, other)
+ GreaterThanOrEqualTo.new(self, other.bind(relation))
end
def matches(regexp)
- Match.new(self, regexp)
+ Match.new(self, regexp.bind(relation))
end
def in(array)
- In.new(self, array)
+ In.new(self, array.bind(relation))
end
end
include Predications
diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb
index 1364911f0c..fa66bd039b 100644
--- a/lib/active_relation/relations/relation.rb
+++ b/lib/active_relation/relations/relation.rb
@@ -105,12 +105,12 @@ module ActiveRelation
formatter.select [
"SELECT #{attributes.collect { |a| a.to_sql(Sql::SelectExpression.new(engine)) }.join(', ')}",
"FROM #{table_sql}",
- (joins unless joins.blank? ),
- ("WHERE #{selects.collect {|s| s.to_sql(Sql::WhereClause.new(engine))}.join("\n\tAND ")}" unless selects.blank? ),
- ("ORDER BY #{orders.collect(&:to_sql)}" unless orders.blank? ),
- ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ),
- ("LIMIT #{limit}" unless limit.blank? ),
- ("OFFSET #{offset}" unless offset.blank? )
+ (joins unless joins.blank? ),
+ ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(engine)) }.join("\n\tAND ")}" unless selects.blank? ),
+ ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(engine)) }.join(', ')}" unless orders.blank? ),
+ ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ),
+ ("LIMIT #{limit}" unless limit.blank? ),
+ ("OFFSET #{offset}" unless offset.blank? )
].compact.join("\n"), self.alias
end
alias_method :to_s, :to_sql
diff --git a/lib/active_relation/sql.rb b/lib/active_relation/sql.rb
index 027356d4d7..3a773fdf6e 100644
--- a/lib/active_relation/sql.rb
+++ b/lib/active_relation/sql.rb
@@ -24,12 +24,21 @@ module ActiveRelation
end
end
- class WhereClause < Formatter
+ class PassThrough < Formatter
def value(value)
value
end
end
+ class WhereClause < PassThrough
+ end
+
+ class OrderClause < PassThrough
+ def attribute(relation_name, attribute_name, aliaz)
+ "#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}"
+ end
+ end
+
class WhereCondition < Formatter
def attribute(relation_name, attribute_name, aliaz)
"#{quote_table_name(relation_name)}.#{quote_column_name(attribute_name)}"
diff --git a/spec/active_relation/unit/primitives/attribute_spec.rb b/spec/active_relation/unit/primitives/attribute_spec.rb
index 2806d26330..0f6e5e289d 100644
--- a/spec/active_relation/unit/primitives/attribute_spec.rb
+++ b/spec/active_relation/unit/primitives/attribute_spec.rb
@@ -97,43 +97,43 @@ module ActiveRelation
describe '#eq' do
it "manufactures an equality predicate" do
- @attribute.eq('name').should == Equality.new(@attribute, 'name')
+ @attribute.eq('name').should == Equality.new(@attribute, 'name'.bind(@relation))
end
end
describe '#lt' do
it "manufactures a less-than predicate" do
- @attribute.lt(10).should == LessThan.new(@attribute, 10)
+ @attribute.lt(10).should == LessThan.new(@attribute, 10.bind(@relation))
end
end
describe '#lteq' do
it "manufactures a less-than or equal-to predicate" do
- @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10)
+ @attribute.lteq(10).should == LessThanOrEqualTo.new(@attribute, 10.bind(@relation))
end
end
describe '#gt' do
it "manufactures a greater-than predicate" do
- @attribute.gt(10).should == GreaterThan.new(@attribute, 10)
+ @attribute.gt(10).should == GreaterThan.new(@attribute, 10.bind(@relation))
end
end
describe '#gteq' do
it "manufactures a greater-than or equal-to predicate" do
- @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10)
+ @attribute.gteq(10).should == GreaterThanOrEqualTo.new(@attribute, 10.bind(@relation))
end
end
describe '#matches' do
it "manufactures a match predicate" do
- @attribute.matches(/.*/).should == Match.new(@attribute, /.*/)
+ @attribute.matches(/.*/).should == Match.new(@attribute, /.*/.bind(@relation))
end
end
describe '#in' do
it "manufactures an in predicate" do
- @attribute.in(1..30).should == In.new(@attribute, 1..30)
+ @attribute.in(1..30).should == In.new(@attribute, (1..30).bind(@relation))
end
end
end
diff --git a/spec/active_relation/unit/relations/order_spec.rb b/spec/active_relation/unit/relations/order_spec.rb
index 6a9ce07024..f37a9f26e9 100644
--- a/spec/active_relation/unit/relations/order_spec.rb
+++ b/spec/active_relation/unit/relations/order_spec.rb
@@ -22,12 +22,42 @@ module ActiveRelation
end
describe '#to_sql' do
- it "manufactures sql with an order clause" do
- Order.new(@relation, @attribute).to_sql.should be_like("
- SELECT `users`.`id`, `users`.`name`
- FROM `users`
- ORDER BY `users`.`id`
- ")
+ describe "when given an attribute" do
+ it "manufactures sql with an order clause populated by the attribute" do
+ Order.new(@relation, @attribute).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ ORDER BY `users`.`id`
+ ")
+ end
+ end
+
+ describe "when given multiple attributes" do
+ before do
+ @another_attribute = @relation[:name]
+ end
+
+ it "manufactures sql with an order clause populated by comma-separated attributes" do
+ Order.new(@relation, @attribute, @another_attribute).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ ORDER BY `users`.`id`, `users`.`name`
+ ")
+ end
+ end
+
+ describe "when given a string" do
+ before do
+ @string = "asdf".bind(@relation)
+ end
+
+ it "passes the string through to the order clause" do
+ Order.new(@relation, @string).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ ORDER BY asdf
+ ")
+ end
end
end
end
diff --git a/spec/active_relation/unit/relations/selection_spec.rb b/spec/active_relation/unit/relations/selection_spec.rb
index 4bb3817bf5..001c38c370 100644
--- a/spec/active_relation/unit/relations/selection_spec.rb
+++ b/spec/active_relation/unit/relations/selection_spec.rb
@@ -4,14 +4,17 @@ module ActiveRelation
describe Selection do
before do
@relation = Table.new(:users)
- @predicate = Equality.new(@relation[:id], 1.bind(@relation))
+ @predicate = @relation[:id].eq(1)
end
describe '#initialize' do
+ before do
+ @another_predicate = @relation[:name].lt(2)
+ end
+
it "manufactures nested selection relations if multiple predicates are provided" do
- @predicate2 = LessThan.new(@relation[:age], 2.bind(@relation))
- Selection.new(@relation, @predicate, @predicate2). \
- should == Selection.new(Selection.new(@relation, @predicate2), @predicate)
+ Selection.new(@relation, @predicate, @another_predicate). \
+ should == Selection.new(Selection.new(@relation, @another_predicate), @predicate)
end
end
@@ -30,20 +33,28 @@ module ActiveRelation
end
describe '#to_sql' do
- it "manufactures sql with where clause conditions" do
- Selection.new(@relation, @predicate).to_sql.should be_like("
- SELECT `users`.`id`, `users`.`name`
- FROM `users`
- WHERE `users`.`id` = 1
- ")
+ describe 'when given a predicate' do
+ it "manufactures sql with where clause conditions" do
+ Selection.new(@relation, @predicate).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ WHERE `users`.`id` = 1
+ ")
+ end
end
-
- it "allows arbitrary sql" do
- Selection.new(@relation, "asdf".bind(@relation)).to_sql.should be_like("
- SELECT `users`.`id`, `users`.`name`
- FROM `users`
- WHERE asdf
- ")
+
+ describe 'when given a string' do
+ before do
+ @string = "asdf".bind(@relation)
+ end
+
+ it "passes the string through to the where clause" do
+ Selection.new(@relation, @string).to_sql.should be_like("
+ SELECT `users`.`id`, `users`.`name`
+ FROM `users`
+ WHERE asdf
+ ")
+ end
end
end
end