public
Description: A Relational Algebra
Clone URL: git://github.com/nkallen/arel.git
Search Repo:
Click here to lend your support to: arel and make a donation at www.pledgie.com !
AND/OR support for predicates
brynary (author)
Tue May 27 18:56:38 -0700 2008
commit  aeb09afd73cf188c6aee22bf4dd0f1beb937ac22
tree    051780a6e22ca2e0d61e9361abc8037ae8e492c3
parent  191b2b7b7e6e2cf4fc5a24321bc9b1e593f96551
...
14
15
16
17
18
19
20
...
23
24
25
 
26
27
28
...
14
15
16
 
17
18
19
...
22
23
24
25
26
27
28
0
@@ -14,7 +14,6 @@ users.delete().where(
0
 
0
     SELECT users.*, (SELECT count(id) FROM addresses WHERE
0
     addresses.user_id=users.id) FROM users
0
-- and/or w/ predicates
0
 - blocks for all operations
0
 - result sets to attr correlation too
0
 - cache expiry on write
0
@@ -23,6 +22,7 @@ users.delete().where(
0
 - scoped writes
0
 
0
 done:
0
+- and/or w/ predicates
0
 - mock out database
0
 . Relation <=> Relation -> InnerJoinOperation
0
 . Relation << Relation -> LeftOuterJoinOperation
...
1
2
 
 
 
 
 
 
 
3
4
5
...
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
25
26
...
1
2
3
4
5
6
7
8
9
10
11
12
...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
0
@@ -1,5 +1,12 @@
0
 module Arel
0
   class Predicate
0
+ def or(other_predicate)
0
+ Or.new(self, other_predicate)
0
+ end
0
+
0
+ def and(other_predicate)
0
+ And.new(self, other_predicate)
0
+ end
0
   end
0
 
0
   class Binary < Predicate
0
@@ -21,6 +28,20 @@ module Arel
0
     end
0
     alias_method :to_s, :to_sql
0
   end
0
+
0
+ class CompoundPredicate < Binary
0
+ def to_sql(formatter = nil)
0
+ "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})"
0
+ end
0
+ end
0
+
0
+ class Or < CompoundPredicate
0
+ def predicate_sql; "OR" end
0
+ end
0
+
0
+ class And < CompoundPredicate
0
+ def predicate_sql; "AND" end
0
+ end
0
 
0
   class Equality < Binary
0
     def ==(other)
...
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
0
@@ -13,6 +13,33 @@ module Arel
0
       end
0
     end
0
 
0
+ describe "with compound predicates" do
0
+ before do
0
+ @operand1 = ConcreteBinary.new(@attribute1, 1)
0
+ @operand2 = ConcreteBinary.new(@attribute2, "name")
0
+ end
0
+
0
+ describe Or do
0
+ describe "#to_sql" do
0
+ it "manufactures sql with an OR operation" do
0
+ Or.new(@operand1, @operand2).to_sql.should be_like("
0
+ (`users`.`id` <=> 1 OR `users`.`name` <=> 'name')
0
+ ")
0
+ end
0
+ end
0
+ end
0
+
0
+ describe And do
0
+ describe "#to_sql" do
0
+ it "manufactures sql with an AND operation" do
0
+ And.new(@operand1, @operand2).to_sql.should be_like("
0
+ (`users`.`id` <=> 1 AND `users`.`name` <=> 'name')
0
+ ")
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
     describe '#to_sql' do
0
       describe 'when relating two attributes' do
0
         it 'manufactures sql with a binary operation' do

Comments

    No one has commented yet.