arel 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. data/.gitmodules +3 -0
  2. data/History.txt +11 -0
  3. data/README.markdown +26 -26
  4. data/Rakefile +34 -46
  5. data/Thorfile +124 -0
  6. data/arel.gemspec +216 -201
  7. data/lib/arel/algebra/{primitives/attribute.rb → attribute.rb} +7 -7
  8. data/lib/arel/algebra/{extensions → core_extensions}/class.rb +2 -2
  9. data/lib/arel/algebra/{extensions → core_extensions}/hash.rb +1 -1
  10. data/lib/arel/algebra/{extensions → core_extensions}/object.rb +1 -1
  11. data/lib/arel/algebra/{extensions → core_extensions}/symbol.rb +1 -1
  12. data/lib/arel/algebra/core_extensions.rb +4 -0
  13. data/lib/arel/algebra/{primitives/ordering.rb → ordering.rb} +1 -1
  14. data/lib/arel/algebra/predicates.rb +32 -30
  15. data/lib/arel/algebra/relations/operations/from.rb +6 -0
  16. data/lib/arel/algebra/relations/operations/having.rb +13 -0
  17. data/lib/arel/algebra/relations/operations/join.rb +4 -0
  18. data/lib/arel/algebra/relations/operations/lock.rb +12 -0
  19. data/lib/arel/algebra/relations/operations/project.rb +1 -1
  20. data/lib/arel/algebra/relations/relation.rb +9 -2
  21. data/lib/arel/algebra/relations/utilities/compound.rb +2 -2
  22. data/lib/arel/algebra/relations.rb +3 -0
  23. data/lib/arel/algebra.rb +7 -2
  24. data/lib/arel/engines/memory/predicates.rb +26 -24
  25. data/lib/arel/engines/memory/relations/operations.rb +6 -0
  26. data/lib/arel/engines/sql/christener.rb +2 -1
  27. data/lib/arel/engines/sql/{extensions → core_extensions}/array.rb +6 -2
  28. data/lib/arel/engines/sql/{extensions → core_extensions}/nil_class.rb +1 -1
  29. data/lib/arel/engines/sql/{extensions → core_extensions}/object.rb +1 -1
  30. data/lib/arel/engines/sql/{extensions → core_extensions}/range.rb +1 -1
  31. data/lib/arel/engines/sql/core_extensions.rb +4 -0
  32. data/lib/arel/engines/sql/engine.rb +4 -0
  33. data/lib/arel/engines/sql/formatters.rb +12 -3
  34. data/lib/arel/engines/sql/predicates.rb +37 -35
  35. data/lib/arel/engines/sql/primitives.rb +10 -2
  36. data/lib/arel/engines/sql/relations/operations/join.rb +2 -2
  37. data/lib/arel/engines/sql/relations/relation.rb +46 -10
  38. data/lib/arel/engines/sql/relations/table.rb +18 -5
  39. data/lib/arel/engines/sql/relations/utilities/compound.rb +1 -1
  40. data/lib/arel/engines/sql/relations/writes.rb +58 -11
  41. data/lib/arel/engines/sql.rb +1 -1
  42. data/lib/arel/session.rb +9 -6
  43. data/lib/arel.rb +9 -8
  44. data/spec/arel/algebra/unit/predicates/binary_spec.rb +23 -21
  45. data/spec/arel/algebra/unit/predicates/equality_spec.rb +20 -18
  46. data/spec/arel/algebra/unit/predicates/in_spec.rb +7 -5
  47. data/spec/arel/algebra/unit/primitives/attribute_spec.rb +10 -12
  48. data/spec/arel/algebra/unit/primitives/expression_spec.rb +1 -1
  49. data/spec/arel/algebra/unit/primitives/value_spec.rb +1 -1
  50. data/spec/arel/algebra/unit/relations/alias_spec.rb +2 -2
  51. data/spec/arel/algebra/unit/relations/delete_spec.rb +1 -1
  52. data/spec/arel/algebra/unit/relations/group_spec.rb +1 -1
  53. data/spec/arel/algebra/unit/relations/insert_spec.rb +1 -1
  54. data/spec/arel/algebra/unit/relations/join_spec.rb +1 -1
  55. data/spec/arel/algebra/unit/relations/order_spec.rb +2 -2
  56. data/spec/arel/algebra/unit/relations/project_spec.rb +1 -1
  57. data/spec/arel/algebra/unit/relations/relation_spec.rb +9 -9
  58. data/spec/arel/algebra/unit/relations/skip_spec.rb +1 -1
  59. data/spec/arel/algebra/unit/relations/table_spec.rb +2 -2
  60. data/spec/arel/algebra/unit/relations/take_spec.rb +1 -1
  61. data/spec/arel/algebra/unit/relations/update_spec.rb +1 -1
  62. data/spec/arel/algebra/unit/relations/where_spec.rb +1 -1
  63. data/spec/arel/algebra/unit/session/session_spec.rb +6 -6
  64. data/spec/arel/engines/memory/integration/joins/cross_engine_spec.rb +3 -4
  65. data/spec/arel/engines/memory/unit/relations/array_spec.rb +1 -1
  66. data/spec/arel/engines/memory/unit/relations/insert_spec.rb +8 -8
  67. data/spec/arel/engines/memory/unit/relations/join_spec.rb +1 -1
  68. data/spec/arel/engines/memory/unit/relations/order_spec.rb +1 -1
  69. data/spec/arel/engines/memory/unit/relations/project_spec.rb +1 -1
  70. data/spec/arel/engines/memory/unit/relations/skip_spec.rb +1 -1
  71. data/spec/arel/engines/memory/unit/relations/take_spec.rb +1 -1
  72. data/spec/arel/engines/memory/unit/relations/where_spec.rb +1 -1
  73. data/spec/arel/engines/sql/integration/joins/with_adjacency_spec.rb +1 -1
  74. data/spec/arel/engines/sql/integration/joins/with_aggregations_spec.rb +1 -1
  75. data/spec/arel/engines/sql/integration/joins/with_compounds_spec.rb +1 -1
  76. data/spec/arel/engines/sql/unit/engine_spec.rb +1 -1
  77. data/spec/arel/engines/sql/unit/predicates/binary_spec.rb +73 -70
  78. data/spec/arel/engines/sql/unit/predicates/equality_spec.rb +45 -28
  79. data/spec/arel/engines/sql/unit/predicates/in_spec.rb +75 -54
  80. data/spec/arel/engines/sql/unit/predicates/predicates_spec.rb +48 -46
  81. data/spec/arel/engines/sql/unit/primitives/attribute_spec.rb +1 -1
  82. data/spec/arel/engines/sql/unit/primitives/expression_spec.rb +1 -1
  83. data/spec/arel/engines/sql/unit/primitives/literal_spec.rb +13 -1
  84. data/spec/arel/engines/sql/unit/primitives/value_spec.rb +3 -3
  85. data/spec/arel/engines/sql/unit/relations/alias_spec.rb +1 -1
  86. data/spec/arel/engines/sql/unit/relations/delete_spec.rb +1 -1
  87. data/spec/arel/engines/sql/unit/relations/from_spec.rb +50 -0
  88. data/spec/arel/engines/sql/unit/relations/group_spec.rb +1 -1
  89. data/spec/arel/engines/sql/unit/relations/having_spec.rb +36 -0
  90. data/spec/arel/engines/sql/unit/relations/insert_spec.rb +1 -1
  91. data/spec/arel/engines/sql/unit/relations/join_spec.rb +85 -4
  92. data/spec/arel/engines/sql/unit/relations/lock_spec.rb +61 -0
  93. data/spec/arel/engines/sql/unit/relations/order_spec.rb +1 -1
  94. data/spec/arel/engines/sql/unit/relations/project_spec.rb +1 -1
  95. data/spec/arel/engines/sql/unit/relations/skip_spec.rb +1 -1
  96. data/spec/arel/engines/sql/unit/relations/table_spec.rb +42 -3
  97. data/spec/arel/engines/sql/unit/relations/take_spec.rb +1 -1
  98. data/spec/arel/engines/sql/unit/relations/update_spec.rb +13 -7
  99. data/spec/arel/engines/sql/unit/relations/where_spec.rb +1 -1
  100. data/spec/connections/mysql_connection.rb +3 -3
  101. data/spec/connections/postgresql_connection.rb +2 -2
  102. data/spec/connections/sqlite3_connection.rb +3 -2
  103. data/spec/doubles/hash.rb +4 -0
  104. data/spec/schemas/mysql_schema.rb +7 -0
  105. data/spec/schemas/postgresql_schema.rb +7 -0
  106. data/spec/schemas/sqlite3_schema.rb +7 -0
  107. data/spec/spec.opts +3 -0
  108. data/spec/spec_helper.rb +9 -1
  109. metadata +50 -25
  110. data/VERSION +0 -1
  111. data/lib/arel/.DS_Store +0 -0
  112. data/lib/arel/algebra/extensions.rb +0 -4
  113. data/lib/arel/algebra/primitives.rb +0 -5
  114. data/lib/arel/engines/sql/extensions.rb +0 -4
  115. /data/lib/arel/algebra/{primitives/expression.rb → expression.rb} +0 -0
  116. /data/lib/arel/algebra/{primitives/value.rb → value.rb} +0 -0
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Insert do
@@ -14,13 +14,13 @@ module Arel
14
14
  it "manufactures an array of hashes of attributes to values" do
15
15
  @relation \
16
16
  .insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \
17
- .let do |relation|
18
- relation.call.should == [
19
- Row.new(relation, [1, 'duck']),
20
- Row.new(relation, [2, 'duck']),
21
- Row.new(relation, [3, 'goose']),
22
- Row.new(relation, [4, 'guinea fowl'])
23
- ]
17
+ do |relation|
18
+ relation.should == [
19
+ Row.new(relation, [1, 'duck']),
20
+ Row.new(relation, [2, 'duck']),
21
+ Row.new(relation, [3, 'goose']),
22
+ Row.new(relation, [4, 'guinea fowl'])
23
+ ]
24
24
  end
25
25
  end
26
26
  end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Join do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Order do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Project do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Skip do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Take do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Where do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Join do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Join do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Join do
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Sql::Engine do
@@ -1,113 +1,116 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
- describe Binary do
5
- before do
6
- @relation = Table.new(:users)
7
- @attribute1 = @relation[:id]
8
- @attribute2 = @relation[:name]
4
+ module Predicates
5
+ describe Binary do
9
6
  class ConcreteBinary < Binary
10
7
  def predicate_sql
11
8
  "<=>"
12
9
  end
13
10
  end
14
- end
15
11
 
16
- describe "with compound predicates" do
17
12
  before do
18
- @operand1 = ConcreteBinary.new(@attribute1, 1)
19
- @operand2 = ConcreteBinary.new(@attribute2, "name")
13
+ @relation = Arel::Table.new(:users)
14
+ @attribute1 = @relation[:id]
15
+ @attribute2 = @relation[:name]
20
16
  end
21
17
 
22
- describe Or do
23
- describe "#to_sql" do
24
- it "manufactures sql with an OR operation" do
25
- sql = Or.new(@operand1, @operand2).to_sql
18
+ describe "with compound predicates" do
19
+ before do
20
+ @operand1 = ConcreteBinary.new(@attribute1, 1)
21
+ @operand2 = ConcreteBinary.new(@attribute2, "name")
22
+ end
26
23
 
27
- adapter_is :mysql do
28
- sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')})
29
- end
24
+ describe Or do
25
+ describe "#to_sql" do
26
+ it "manufactures sql with an OR operation" do
27
+ sql = Or.new(@operand1, @operand2).to_sql
30
28
 
31
- adapter_is :postgresql do
32
- sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')})
33
- end
29
+ adapter_is :mysql do
30
+ sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')})
31
+ end
32
+
33
+ adapter_is :postgresql do
34
+ sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')})
35
+ end
34
36
 
35
- adapter_is :sqlite3 do
36
- sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')})
37
+ adapter_is :sqlite3 do
38
+ sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')})
39
+ end
37
40
  end
38
41
  end
39
42
  end
40
- end
41
43
 
42
- describe And do
43
- describe "#to_sql" do
44
- it "manufactures sql with an AND operation" do
45
- sql = And.new(@operand1, @operand2).to_sql
44
+ describe And do
45
+ describe "#to_sql" do
46
+ it "manufactures sql with an AND operation" do
47
+ sql = And.new(@operand1, @operand2).to_sql
46
48
 
47
- adapter_is :mysql do
48
- sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')})
49
- end
49
+ adapter_is :mysql do
50
+ sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')})
51
+ end
50
52
 
51
- adapter_is :sqlite3 do
52
- sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')})
53
- end
53
+ adapter_is :sqlite3 do
54
+ sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')})
55
+ end
54
56
 
55
- adapter_is :postgresql do
56
- sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')})
57
+ adapter_is :postgresql do
58
+ sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')})
59
+ end
57
60
  end
58
61
  end
59
62
  end
60
63
  end
61
- end
62
64
 
63
- describe '#to_sql' do
64
- describe 'when relating two attributes' do
65
- it 'manufactures sql with a binary operation' do
66
- sql = ConcreteBinary.new(@attribute1, @attribute2).to_sql
65
+ describe '#to_sql' do
66
+ describe 'when relating two attributes' do
67
+ it 'manufactures sql with a binary operation' do
68
+ sql = ConcreteBinary.new(@attribute1, @attribute2).to_sql
67
69
 
68
- adapter_is :mysql do
69
- sql.should be_like(%Q{`users`.`id` <=> `users`.`name`})
70
- end
70
+ adapter_is :mysql do
71
+ sql.should be_like(%Q{`users`.`id` <=> `users`.`name`})
72
+ end
71
73
 
72
- adapter_is_not :mysql do
73
- sql.should be_like(%Q{"users"."id" <=> "users"."name"})
74
+ adapter_is_not :mysql do
75
+ sql.should be_like(%Q{"users"."id" <=> "users"."name"})
76
+ end
74
77
  end
75
78
  end
76
- end
77
79
 
78
- describe 'when relating an attribute and a value' do
79
- before do
80
- @value = "1-asdf"
81
- end
80
+ describe 'when relating an attribute and a value' do
81
+ before do
82
+ @value = "1-asdf"
83
+ end
82
84
 
83
- describe 'when relating to an integer attribute' do
84
- it 'formats values as integers' do
85
- sql = ConcreteBinary.new(@attribute1, @value).to_sql
85
+ describe 'when relating to an integer attribute' do
86
+ it 'formats values as integers' do
87
+ sql = ConcreteBinary.new(@attribute1, @value).to_sql
86
88
 
87
- adapter_is :mysql do
88
- sql.should be_like(%Q{`users`.`id` <=> 1})
89
- end
89
+ adapter_is :mysql do
90
+ sql.should be_like(%Q{`users`.`id` <=> 1})
91
+ end
90
92
 
91
- adapter_is_not :mysql do
92
- sql.should be_like(%Q{"users"."id" <=> 1})
93
+ adapter_is_not :mysql do
94
+ sql.should be_like(%Q{"users"."id" <=> 1})
95
+ end
93
96
  end
94
97
  end
95
- end
96
98
 
97
- describe 'when relating to a string attribute' do
98
- it 'formats values as strings' do
99
- sql = ConcreteBinary.new(@attribute2, @value).to_sql
99
+ describe 'when relating to a string attribute' do
100
+ it 'formats values as strings' do
101
+ sql = ConcreteBinary.new(@attribute2, @value).to_sql
100
102
 
101
- adapter_is :mysql do
102
- sql.should be_like(%Q{`users`.`name` <=> '1-asdf'})
103
- end
103
+ adapter_is :mysql do
104
+ sql.should be_like(%Q{`users`.`name` <=> '1-asdf'})
105
+ end
104
106
 
105
- adapter_is :sqlite3 do
106
- sql.should be_like(%Q{"users"."name" <=> '1-asdf'})
107
- end
107
+ adapter_is :sqlite3 do
108
+ sql.should be_like(%Q{"users"."name" <=> '1-asdf'})
109
+ end
108
110
 
109
- adapter_is :postgresql do
110
- sql.should be_like(%Q{"users"."name" <=> E'1-asdf'})
111
+ adapter_is :postgresql do
112
+ sql.should be_like(%Q{"users"."name" <=> E'1-asdf'})
113
+ end
111
114
  end
112
115
  end
113
116
  end
@@ -1,43 +1,60 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
- describe Equality do
5
- before do
6
- @relation1 = Table.new(:users)
7
- @relation2 = Table.new(:photos)
8
- @attribute1 = @relation1[:id]
9
- @attribute2 = @relation2[:user_id]
10
- end
4
+ module Predicates
5
+ describe Equality do
6
+ before do
7
+ @relation1 = Arel::Table.new(:users)
8
+ @relation2 = Arel::Table.new(:photos)
9
+ @attribute1 = @relation1[:id]
10
+ @attribute2 = @relation2[:user_id]
11
+ end
11
12
 
12
- describe '#to_sql' do
13
- describe 'when relating to a non-nil value' do
14
- it "manufactures an equality predicate" do
15
- sql = Equality.new(@attribute1, @attribute2).to_sql
13
+ describe '#to_sql' do
14
+ describe 'when relating to a non-nil value' do
15
+ it "manufactures an equality predicate" do
16
+ sql = Equality.new(@attribute1, @attribute2).to_sql
16
17
 
17
- adapter_is :mysql do
18
- sql.should be_like(%Q{`users`.`id` = `photos`.`user_id`})
19
- end
18
+ adapter_is :mysql do
19
+ sql.should be_like(%Q{`users`.`id` = `photos`.`user_id`})
20
+ end
20
21
 
21
- adapter_is_not :mysql do
22
- sql.should be_like(%Q{"users"."id" = "photos"."user_id"})
22
+ adapter_is_not :mysql do
23
+ sql.should be_like(%Q{"users"."id" = "photos"."user_id"})
24
+ end
23
25
  end
24
26
  end
25
- end
26
27
 
27
- describe 'when relation to a nil value' do
28
- before do
29
- @nil = nil
30
- end
28
+ describe 'when relation to a nil value' do
29
+ before do
30
+ @nil = nil
31
+ end
32
+
33
+ it "manufactures an is null predicate" do
34
+ sql = Equality.new(@attribute1, @nil).to_sql
31
35
 
32
- it "manufactures an is null predicate" do
33
- sql = Equality.new(@attribute1, @nil).to_sql
36
+ adapter_is :mysql do
37
+ sql.should be_like(%Q{`users`.`id` IS NULL})
38
+ end
34
39
 
35
- adapter_is :mysql do
36
- sql.should be_like(%Q{`users`.`id` IS NULL})
40
+ adapter_is_not :mysql do
41
+ sql.should be_like(%Q{"users"."id" IS NULL})
42
+ end
37
43
  end
44
+ end
45
+
46
+ describe "when relating to a nil Value" do
47
+ it "manufactures an IS NULL predicate" do
48
+ value = nil.bind(@relation1)
49
+ sql = Equality.new(@attribute1, value).to_sql
50
+
51
+ adapter_is :mysql do
52
+ sql.should be_like(%Q{`users`.`id` IS NULL})
53
+ end
38
54
 
39
- adapter_is_not :mysql do
40
- sql.should be_like(%Q{"users"."id" IS NULL})
55
+ adapter_is_not :mysql do
56
+ sql.should be_like(%Q{"users"."id" IS NULL})
57
+ end
41
58
  end
42
59
  end
43
60
  end
@@ -1,83 +1,104 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
- describe In do
5
- before do
6
- @relation = Table.new(:users)
7
- @attribute = @relation[:id]
8
- end
4
+ module Predicates
5
+ describe In do
6
+ before do
7
+ @relation = Arel::Table.new(:users)
8
+ @attribute = @relation[:id]
9
+ end
9
10
 
10
- describe '#to_sql' do
11
- describe 'when relating to an array' do
12
- describe 'when the array\'s elements are the same type as the attribute' do
13
- before do
14
- @array = [1, 2, 3]
11
+ describe '#to_sql' do
12
+ describe 'when relating to an array' do
13
+ describe 'when the array\'s elements are the same type as the attribute' do
14
+ before do
15
+ @array = [1, 2, 3]
16
+ end
17
+
18
+ it 'manufactures sql with a comma separated list' do
19
+ sql = In.new(@attribute, @array).to_sql
20
+
21
+ adapter_is :mysql do
22
+ sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
23
+ end
24
+
25
+ adapter_is_not :mysql do
26
+ sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
27
+ end
28
+ end
15
29
  end
16
30
 
17
- it 'manufactures sql with a comma separated list' do
18
- sql = In.new(@attribute, @array).to_sql
31
+ describe 'when the array\'s elements are not same type as the attribute' do
32
+ before do
33
+ @array = ['1-asdf', 2, 3]
34
+ end
19
35
 
20
- adapter_is :mysql do
21
- sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
36
+ it 'formats values in the array as the type of the attribute' do
37
+ sql = In.new(@attribute, @array).to_sql
38
+
39
+ adapter_is :mysql do
40
+ sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
41
+ end
42
+
43
+ adapter_is_not :mysql do
44
+ sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'when the array is empty' do
50
+ before do
51
+ @array = []
22
52
  end
23
53
 
24
- adapter_is_not :mysql do
25
- sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
54
+ it 'manufactures sql with a comma separated list' do
55
+ sql = In.new(@attribute, @array).to_sql
56
+
57
+ adapter_is :mysql do
58
+ sql.should be_like(%Q{`users`.`id` IN (NULL)})
59
+ end
60
+
61
+ adapter_is_not :mysql do
62
+ sql.should be_like(%Q{"users"."id" IN (NULL)})
63
+ end
26
64
  end
27
65
  end
66
+
28
67
  end
29
68
 
30
- describe 'when the array\'s elements are not same type as the attribute' do
69
+ describe 'when relating to a range' do
31
70
  before do
32
- @array = ['1-asdf', 2, 3]
71
+ @range = 1..2
33
72
  end
34
73
 
35
- it 'formats values in the array as the type of the attribute' do
36
- sql = In.new(@attribute, @array).to_sql
74
+ it 'manufactures sql with a between' do
75
+ sql = In.new(@attribute, @range).to_sql
37
76
 
38
77
  adapter_is :mysql do
39
- sql.should be_like(%Q{`users`.`id` IN (1, 2, 3)})
78
+ sql.should be_like(%Q{`users`.`id` BETWEEN 1 AND 2})
40
79
  end
41
80
 
42
81
  adapter_is_not :mysql do
43
- sql.should be_like(%Q{"users"."id" IN (1, 2, 3)})
82
+ sql.should be_like(%Q{"users"."id" BETWEEN 1 AND 2})
44
83
  end
45
84
  end
46
85
  end
47
- end
48
-
49
- describe 'when relating to a range' do
50
- before do
51
- @range = 1..2
52
- end
53
86
 
54
- it 'manufactures sql with a between' do
55
- sql = In.new(@attribute, @range).to_sql
87
+ describe 'when relating to a relation' do
88
+ it 'manufactures sql with a subselect' do
89
+ sql = In.new(@attribute, @relation).to_sql
56
90
 
57
- adapter_is :mysql do
58
- sql.should be_like(%Q{`users`.`id` BETWEEN 1 AND 2})
59
- end
60
-
61
- adapter_is_not :mysql do
62
- sql.should be_like(%Q{"users"."id" BETWEEN 1 AND 2})
63
- end
64
- end
65
- end
66
-
67
- describe 'when relating to a relation' do
68
- it 'manufactures sql with a subselect' do
69
- sql = In.new(@attribute, @relation).to_sql
70
-
71
- adapter_is :mysql do
72
- sql.should be_like(%Q{
73
- `users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`)
74
- })
75
- end
91
+ adapter_is :mysql do
92
+ sql.should be_like(%Q{
93
+ `users`.`id` IN (SELECT `users`.`id`, `users`.`name` FROM `users`)
94
+ })
95
+ end
76
96
 
77
- adapter_is_not :mysql do
78
- sql.should be_like(%Q{
79
- "users"."id" IN (SELECT "users"."id", "users"."name" FROM "users")
80
- })
97
+ adapter_is_not :mysql do
98
+ sql.should be_like(%Q{
99
+ "users"."id" IN (SELECT "users"."id", "users"."name" FROM "users")
100
+ })
101
+ end
81
102
  end
82
103
  end
83
104
  end
@@ -1,62 +1,64 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
- describe Predicate do
5
- before do
6
- @relation = Table.new(:users)
7
- @attribute1 = @relation[:id]
8
- @attribute2 = @relation[:name]
9
- @operand1 = Equality.new(@attribute1, 1)
10
- @operand2 = Equality.new(@attribute2, "name")
11
- end
4
+ module Predicates
5
+ describe Predicate do
6
+ before do
7
+ @relation = Arel::Table.new(:users)
8
+ @attribute1 = @relation[:id]
9
+ @attribute2 = @relation[:name]
10
+ @operand1 = Arel::Predicates::Equality.new(@attribute1, 1)
11
+ @operand2 = Arel::Predicates::Equality.new(@attribute2, "name")
12
+ end
12
13
 
13
- describe "when being combined with another predicate with AND logic" do
14
- describe "#to_sql" do
15
- it "manufactures sql with an AND operation" do
16
- sql = @operand1.and(@operand2).to_sql
14
+ describe "when being combined with another predicate with AND logic" do
15
+ describe "#to_sql" do
16
+ it "manufactures sql with an AND operation" do
17
+ sql = @operand1.and(@operand2).to_sql
17
18
 
18
- adapter_is :mysql do
19
- sql.should be_like(%Q{
20
- (`users`.`id` = 1 AND `users`.`name` = 'name')
21
- })
22
- end
19
+ adapter_is :mysql do
20
+ sql.should be_like(%Q{
21
+ (`users`.`id` = 1 AND `users`.`name` = 'name')
22
+ })
23
+ end
23
24
 
24
- adapter_is :sqlite3 do
25
- sql.should be_like(%Q{
26
- ("users"."id" = 1 AND "users"."name" = 'name')
27
- })
28
- end
25
+ adapter_is :sqlite3 do
26
+ sql.should be_like(%Q{
27
+ ("users"."id" = 1 AND "users"."name" = 'name')
28
+ })
29
+ end
29
30
 
30
- adapter_is :postgresql do
31
- sql.should be_like(%Q{
32
- ("users"."id" = 1 AND "users"."name" = E'name')
33
- })
31
+ adapter_is :postgresql do
32
+ sql.should be_like(%Q{
33
+ ("users"."id" = 1 AND "users"."name" = E'name')
34
+ })
35
+ end
34
36
  end
35
37
  end
36
38
  end
37
- end
38
39
 
39
- describe "when being combined with another predicate with OR logic" do
40
- describe "#to_sql" do
41
- it "manufactures sql with an OR operation" do
42
- sql = @operand1.or(@operand2).to_sql
40
+ describe "when being combined with another predicate with OR logic" do
41
+ describe "#to_sql" do
42
+ it "manufactures sql with an OR operation" do
43
+ sql = @operand1.or(@operand2).to_sql
43
44
 
44
- adapter_is :mysql do
45
- sql.should be_like(%Q{
46
- (`users`.`id` = 1 OR `users`.`name` = 'name')
47
- })
48
- end
45
+ adapter_is :mysql do
46
+ sql.should be_like(%Q{
47
+ (`users`.`id` = 1 OR `users`.`name` = 'name')
48
+ })
49
+ end
49
50
 
50
- adapter_is :sqlite3 do
51
- sql.should be_like(%Q{
52
- ("users"."id" = 1 OR "users"."name" = 'name')
53
- })
54
- end
51
+ adapter_is :sqlite3 do
52
+ sql.should be_like(%Q{
53
+ ("users"."id" = 1 OR "users"."name" = 'name')
54
+ })
55
+ end
55
56
 
56
- adapter_is :postgresql do
57
- sql.should be_like(%Q{
58
- ("users"."id" = 1 OR "users"."name" = E'name')
59
- })
57
+ adapter_is :postgresql do
58
+ sql.should be_like(%Q{
59
+ ("users"."id" = 1 OR "users"."name" = E'name')
60
+ })
61
+ end
60
62
  end
61
63
  end
62
64
  end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', '..', '..', '..', 'spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  module Arel
4
4
  describe Attribute do