arel 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE.txt +20 -0
- data/Manifest.txt +105 -0
- data/README.markdown +12 -32
- data/Rakefile +17 -0
- data/arel.gemspec +39 -0
- data/lib/arel.rb +30 -9
- data/lib/arel/attributes.rb +20 -0
- data/lib/arel/attributes/attribute.rb +190 -0
- data/lib/arel/compatibility/wheres.rb +33 -0
- data/lib/arel/crud.rb +37 -0
- data/lib/arel/delete_manager.rb +22 -0
- data/lib/arel/deprecated.rb +4 -0
- data/lib/arel/expression.rb +4 -0
- data/lib/arel/expressions.rb +23 -0
- data/lib/arel/insert_manager.rb +34 -0
- data/lib/arel/nodes.rb +44 -0
- data/lib/arel/nodes/and.rb +6 -0
- data/lib/arel/nodes/assignment.rb +6 -0
- data/lib/arel/nodes/avg.rb +6 -0
- data/lib/arel/nodes/between.rb +6 -0
- data/lib/arel/nodes/binary.rb +12 -0
- data/lib/arel/nodes/count.rb +13 -0
- data/lib/arel/nodes/delete_statement.rb +17 -0
- data/lib/arel/nodes/does_not_match.rb +6 -0
- data/lib/arel/nodes/equality.rb +9 -0
- data/lib/arel/nodes/exists.rb +11 -0
- data/lib/arel/nodes/function.rb +18 -0
- data/lib/arel/nodes/greater_than.rb +6 -0
- data/lib/arel/nodes/greater_than_or_equal.rb +6 -0
- data/lib/arel/nodes/group.rb +11 -0
- data/lib/arel/nodes/grouping.rb +11 -0
- data/lib/arel/nodes/having.rb +11 -0
- data/lib/arel/nodes/in.rb +6 -0
- data/lib/arel/nodes/inner_join.rb +6 -0
- data/lib/arel/nodes/insert_statement.rb +19 -0
- data/lib/arel/nodes/join.rb +13 -0
- data/lib/arel/nodes/less_than.rb +6 -0
- data/lib/arel/nodes/less_than_or_equal.rb +6 -0
- data/lib/arel/nodes/lock.rb +6 -0
- data/lib/arel/nodes/matches.rb +6 -0
- data/lib/arel/nodes/max.rb +6 -0
- data/lib/arel/nodes/min.rb +6 -0
- data/lib/arel/nodes/node.rb +30 -0
- data/lib/arel/nodes/not_equal.rb +6 -0
- data/lib/arel/nodes/not_in.rb +6 -0
- data/lib/arel/nodes/offset.rb +11 -0
- data/lib/arel/nodes/on.rb +11 -0
- data/lib/arel/nodes/or.rb +6 -0
- data/lib/arel/nodes/ordering.rb +19 -0
- data/lib/arel/nodes/outer_join.rb +6 -0
- data/lib/arel/nodes/select_core.rb +25 -0
- data/lib/arel/nodes/select_statement.rb +22 -0
- data/lib/arel/nodes/sql_literal.rb +7 -0
- data/lib/arel/nodes/string_join.rb +11 -0
- data/lib/arel/nodes/sum.rb +6 -0
- data/lib/arel/nodes/table_alias.rb +21 -0
- data/lib/arel/nodes/unqualified_column.rb +19 -0
- data/lib/arel/nodes/update_statement.rb +21 -0
- data/lib/arel/nodes/values.rb +12 -0
- data/lib/arel/relation.rb +6 -0
- data/lib/arel/select_manager.rb +203 -0
- data/lib/arel/sql/engine.rb +10 -0
- data/lib/arel/sql_literal.rb +1 -10
- data/lib/arel/table.rb +126 -0
- data/lib/arel/tree_manager.rb +26 -0
- data/lib/arel/update_manager.rb +48 -0
- data/lib/arel/visitors.rb +30 -0
- data/lib/arel/visitors/dot.rb +233 -0
- data/lib/arel/visitors/join_sql.rb +38 -0
- data/lib/arel/visitors/mysql.rb +16 -0
- data/lib/arel/visitors/oracle.rb +69 -0
- data/lib/arel/visitors/order_clauses.rb +9 -0
- data/lib/arel/visitors/postgresql.rb +54 -0
- data/lib/arel/visitors/to_sql.rb +301 -0
- data/lib/arel/visitors/where_sql.rb +9 -0
- data/spec/activerecord_compat_spec.rb +18 -0
- data/spec/attributes/attribute_spec.rb +648 -0
- data/spec/attributes_spec.rb +33 -6
- data/spec/crud_spec.rb +69 -0
- data/spec/delete_manager_spec.rb +53 -0
- data/spec/insert_manager_spec.rb +141 -0
- data/spec/nodes/count_spec.rb +18 -0
- data/spec/nodes/delete_statement_spec.rb +15 -0
- data/spec/nodes/equality_spec.rb +72 -0
- data/spec/nodes/insert_statement_spec.rb +18 -0
- data/spec/nodes/or_spec.rb +20 -0
- data/spec/nodes/select_core_spec.rb +21 -0
- data/spec/nodes/select_statement_spec.rb +14 -0
- data/spec/nodes/sql_literal_spec.rb +26 -0
- data/spec/nodes/sum_spec.rb +12 -0
- data/spec/nodes/update_statement_spec.rb +18 -0
- data/spec/select_manager_spec.rb +581 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +6 -21
- data/spec/support/fake_record.rb +89 -0
- data/spec/support/shared/tree_manager_shared.rb +9 -0
- data/spec/table_spec.rb +176 -0
- data/spec/update_manager_spec.rb +89 -0
- data/spec/visitors/join_sql_spec.rb +35 -0
- data/spec/visitors/oracle_spec.rb +111 -0
- data/spec/visitors/to_sql_spec.rb +134 -0
- metadata +160 -260
- data/lib/arel/algebra.rb +0 -10
- data/lib/arel/algebra/attributes.rb +0 -7
- data/lib/arel/algebra/attributes/attribute.rb +0 -304
- data/lib/arel/algebra/attributes/boolean.rb +0 -21
- data/lib/arel/algebra/attributes/decimal.rb +0 -9
- data/lib/arel/algebra/attributes/float.rb +0 -9
- data/lib/arel/algebra/attributes/integer.rb +0 -10
- data/lib/arel/algebra/attributes/string.rb +0 -10
- data/lib/arel/algebra/attributes/time.rb +0 -6
- data/lib/arel/algebra/core_extensions.rb +0 -3
- data/lib/arel/algebra/core_extensions/hash.rb +0 -7
- data/lib/arel/algebra/core_extensions/object.rb +0 -13
- data/lib/arel/algebra/core_extensions/symbol.rb +0 -9
- data/lib/arel/algebra/expression.rb +0 -56
- data/lib/arel/algebra/header.rb +0 -66
- data/lib/arel/algebra/ordering.rb +0 -31
- data/lib/arel/algebra/predicates.rb +0 -306
- data/lib/arel/algebra/relations.rb +0 -16
- data/lib/arel/algebra/relations/operations/from.rb +0 -14
- data/lib/arel/algebra/relations/operations/group.rb +0 -14
- data/lib/arel/algebra/relations/operations/having.rb +0 -14
- data/lib/arel/algebra/relations/operations/join.rb +0 -103
- data/lib/arel/algebra/relations/operations/lock.rb +0 -10
- data/lib/arel/algebra/relations/operations/order.rb +0 -23
- data/lib/arel/algebra/relations/operations/project.rb +0 -20
- data/lib/arel/algebra/relations/operations/skip.rb +0 -14
- data/lib/arel/algebra/relations/operations/take.rb +0 -18
- data/lib/arel/algebra/relations/operations/where.rb +0 -24
- data/lib/arel/algebra/relations/relation.rb +0 -205
- data/lib/arel/algebra/relations/row.rb +0 -29
- data/lib/arel/algebra/relations/utilities/compound.rb +0 -55
- data/lib/arel/algebra/relations/utilities/externalization.rb +0 -26
- data/lib/arel/algebra/relations/utilities/nil.rb +0 -7
- data/lib/arel/algebra/relations/writes.rb +0 -47
- data/lib/arel/algebra/value.rb +0 -53
- data/lib/arel/engines.rb +0 -2
- data/lib/arel/engines/memory.rb +0 -2
- data/lib/arel/engines/memory/engine.rb +0 -10
- data/lib/arel/engines/memory/relations.rb +0 -2
- data/lib/arel/engines/memory/relations/array.rb +0 -37
- data/lib/arel/engines/memory/relations/operations.rb +0 -9
- data/lib/arel/engines/sql.rb +0 -6
- data/lib/arel/engines/sql/attributes.rb +0 -45
- data/lib/arel/engines/sql/christener.rb +0 -20
- data/lib/arel/engines/sql/compilers/ibm_db_compiler.rb +0 -48
- data/lib/arel/engines/sql/compilers/mysql_compiler.rb +0 -11
- data/lib/arel/engines/sql/compilers/oracle_compiler.rb +0 -106
- data/lib/arel/engines/sql/compilers/postgresql_compiler.rb +0 -50
- data/lib/arel/engines/sql/compilers/sqlite_compiler.rb +0 -9
- data/lib/arel/engines/sql/core_extensions.rb +0 -4
- data/lib/arel/engines/sql/core_extensions/array.rb +0 -24
- data/lib/arel/engines/sql/core_extensions/nil_class.rb +0 -15
- data/lib/arel/engines/sql/core_extensions/object.rb +0 -19
- data/lib/arel/engines/sql/core_extensions/range.rb +0 -19
- data/lib/arel/engines/sql/engine.rb +0 -47
- data/lib/arel/engines/sql/formatters.rb +0 -138
- data/lib/arel/engines/sql/relations.rb +0 -3
- data/lib/arel/engines/sql/relations/compiler.rb +0 -153
- data/lib/arel/engines/sql/relations/table.rb +0 -100
- data/lib/arel/engines/sql/relations/utilities/nil.rb +0 -6
- data/lib/arel/recursion/base_case.rb +0 -13
- data/lib/arel/session.rb +0 -35
- data/lib/arel/version.rb +0 -3
- data/spec/algebra/unit/predicates/binary_spec.rb +0 -35
- data/spec/algebra/unit/predicates/equality_spec.rb +0 -29
- data/spec/algebra/unit/predicates/in_spec.rb +0 -12
- data/spec/algebra/unit/predicates/inequality_spec.rb +0 -32
- data/spec/algebra/unit/predicates/predicate_spec.rb +0 -22
- data/spec/algebra/unit/primitives/attribute_spec.rb +0 -175
- data/spec/algebra/unit/primitives/expression_spec.rb +0 -39
- data/spec/algebra/unit/primitives/value_spec.rb +0 -15
- data/spec/algebra/unit/relations/alias_spec.rb +0 -16
- data/spec/algebra/unit/relations/delete_spec.rb +0 -9
- data/spec/algebra/unit/relations/group_spec.rb +0 -10
- data/spec/algebra/unit/relations/insert_spec.rb +0 -9
- data/spec/algebra/unit/relations/join_spec.rb +0 -18
- data/spec/algebra/unit/relations/order_spec.rb +0 -21
- data/spec/algebra/unit/relations/project_spec.rb +0 -34
- data/spec/algebra/unit/relations/relation_spec.rb +0 -241
- data/spec/algebra/unit/relations/skip_spec.rb +0 -10
- data/spec/algebra/unit/relations/table_spec.rb +0 -38
- data/spec/algebra/unit/relations/take_spec.rb +0 -10
- data/spec/algebra/unit/relations/update_spec.rb +0 -9
- data/spec/algebra/unit/relations/where_spec.rb +0 -19
- data/spec/algebra/unit/session/session_spec.rb +0 -84
- data/spec/attributes/boolean_spec.rb +0 -57
- data/spec/attributes/float_spec.rb +0 -119
- data/spec/attributes/header_spec.rb +0 -42
- data/spec/attributes/integer_spec.rb +0 -119
- data/spec/attributes/string_spec.rb +0 -43
- data/spec/attributes/time_spec.rb +0 -24
- data/spec/engines/memory/integration/joins/cross_engine_spec.rb +0 -61
- data/spec/engines/memory/unit/relations/array_spec.rb +0 -33
- data/spec/engines/memory/unit/relations/insert_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/join_spec.rb +0 -32
- data/spec/engines/memory/unit/relations/order_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/project_spec.rb +0 -27
- data/spec/engines/memory/unit/relations/skip_spec.rb +0 -31
- data/spec/engines/memory/unit/relations/take_spec.rb +0 -28
- data/spec/engines/memory/unit/relations/where_spec.rb +0 -43
- data/spec/engines/sql/integration/joins/with_adjacency_spec.rb +0 -258
- data/spec/engines/sql/integration/joins/with_aggregations_spec.rb +0 -221
- data/spec/engines/sql/integration/joins/with_compounds_spec.rb +0 -137
- data/spec/engines/sql/unit/engine_spec.rb +0 -65
- data/spec/engines/sql/unit/predicates/binary_spec.rb +0 -140
- data/spec/engines/sql/unit/predicates/equality_spec.rb +0 -75
- data/spec/engines/sql/unit/predicates/in_spec.rb +0 -179
- data/spec/engines/sql/unit/predicates/noteq_spec.rb +0 -75
- data/spec/engines/sql/unit/predicates/predicates_spec.rb +0 -79
- data/spec/engines/sql/unit/primitives/attribute_spec.rb +0 -36
- data/spec/engines/sql/unit/primitives/expression_spec.rb +0 -28
- data/spec/engines/sql/unit/primitives/literal_spec.rb +0 -43
- data/spec/engines/sql/unit/primitives/value_spec.rb +0 -29
- data/spec/engines/sql/unit/relations/alias_spec.rb +0 -53
- data/spec/engines/sql/unit/relations/delete_spec.rb +0 -83
- data/spec/engines/sql/unit/relations/from_spec.rb +0 -64
- data/spec/engines/sql/unit/relations/group_spec.rb +0 -72
- data/spec/engines/sql/unit/relations/having_spec.rb +0 -78
- data/spec/engines/sql/unit/relations/insert_spec.rb +0 -143
- data/spec/engines/sql/unit/relations/join_spec.rb +0 -180
- data/spec/engines/sql/unit/relations/lock_spec.rb +0 -86
- data/spec/engines/sql/unit/relations/order_spec.rb +0 -161
- data/spec/engines/sql/unit/relations/project_spec.rb +0 -143
- data/spec/engines/sql/unit/relations/skip_spec.rb +0 -41
- data/spec/engines/sql/unit/relations/table_spec.rb +0 -122
- data/spec/engines/sql/unit/relations/take_spec.rb +0 -75
- data/spec/engines/sql/unit/relations/update_spec.rb +0 -203
- data/spec/engines/sql/unit/relations/where_spec.rb +0 -72
- data/spec/relations/join_spec.rb +0 -42
- data/spec/relations/relation_spec.rb +0 -31
- data/spec/shared/relation_spec.rb +0 -255
- data/spec/sql/christener_spec.rb +0 -70
- data/spec/support/connections/mysql_connection.rb +0 -14
- data/spec/support/connections/oracle_connection.rb +0 -17
- data/spec/support/connections/postgresql_connection.rb +0 -13
- data/spec/support/connections/sqlite3_connection.rb +0 -24
- data/spec/support/guards.rb +0 -28
- data/spec/support/matchers/disambiguate_attributes.rb +0 -28
- data/spec/support/matchers/hash_the_same_as.rb +0 -26
- data/spec/support/matchers/have_rows.rb +0 -18
- data/spec/support/model.rb +0 -67
- data/spec/support/schemas/mysql_schema.rb +0 -26
- data/spec/support/schemas/oracle_schema.rb +0 -20
- data/spec/support/schemas/postgresql_schema.rb +0 -26
- data/spec/support/schemas/sqlite3_schema.rb +0 -26
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'bigdecimal'
|
3
|
-
|
4
|
-
module Arel
|
5
|
-
describe "Attributes::String" do
|
6
|
-
|
7
|
-
before :all do
|
8
|
-
@relation = Model.build do |r|
|
9
|
-
r.engine Testing::Engine.new
|
10
|
-
r.attribute :name, Attributes::String
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def type_cast(val)
|
15
|
-
@relation[:name].type_cast(val)
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#type_cast" do
|
19
|
-
it "returns same value if passed a String" do
|
20
|
-
val = "hell"
|
21
|
-
type_cast(val).should eql(val)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "returns nil if passed nil" do
|
25
|
-
type_cast(nil).should be_nil
|
26
|
-
end
|
27
|
-
|
28
|
-
it "returns String representation of Symbol" do
|
29
|
-
type_cast(:hello).should == "hello"
|
30
|
-
end
|
31
|
-
|
32
|
-
it "returns string representation of Integer" do
|
33
|
-
type_cast(1).should == '1'
|
34
|
-
end
|
35
|
-
|
36
|
-
it "calls #to_s on arbitrary objects" do
|
37
|
-
obj = Object.new
|
38
|
-
obj.extend Module.new { def to_s ; 'hello' ; end }
|
39
|
-
type_cast(obj).should == 'hello'
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'bigdecimal'
|
3
|
-
|
4
|
-
module Arel
|
5
|
-
describe "Attributes::Time" do
|
6
|
-
|
7
|
-
before :all do
|
8
|
-
@relation = Model.build do |r|
|
9
|
-
r.engine Testing::Engine.new
|
10
|
-
r.attribute :created_at, Attributes::Time
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def type_cast(val)
|
15
|
-
@relation[:created_at].type_cast(val)
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "#type_cast" do
|
19
|
-
it "works" do
|
20
|
-
pending
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Join do
|
5
|
-
before do
|
6
|
-
@users = Array.new([
|
7
|
-
[1, 'bryan' ],
|
8
|
-
[2, 'emilio' ],
|
9
|
-
[3, 'nick']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
@photos = Table.new(:photos)
|
12
|
-
@photos.delete
|
13
|
-
@photos.insert(@photos[:id] => 1, @photos[:user_id] => 1, @photos[:camera_id] => 6)
|
14
|
-
@photos.insert(@photos[:id] => 2, @photos[:user_id] => 2, @photos[:camera_id] => 42)
|
15
|
-
# Oracle adapter returns database integers as Ruby integers and not strings
|
16
|
-
# So does the FFI sqlite library
|
17
|
-
db_int_return = @photos.project(@photos[:camera_id]).first.tuple.first
|
18
|
-
@adapter_returns_integer = db_int_return.is_a?(String) ? false : true
|
19
|
-
end
|
20
|
-
|
21
|
-
describe 'when the in memory relation is on the left' do
|
22
|
-
it 'joins across engines' do
|
23
|
-
@users \
|
24
|
-
.join(@photos) \
|
25
|
-
.on(@users[:id].eq(@photos[:user_id])) \
|
26
|
-
.project(@users[:name], @photos[:camera_id]) \
|
27
|
-
.tap do |relation|
|
28
|
-
rows = relation.call
|
29
|
-
rows.length.should == 2
|
30
|
-
[
|
31
|
-
['bryan', @adapter_returns_integer ? 6 : '6'],
|
32
|
-
['emilio', @adapter_returns_integer ? 42 : '42']
|
33
|
-
].zip(rows).each do |tuple, row|
|
34
|
-
row.relation.should == relation
|
35
|
-
row.tuple.should == tuple
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe 'when the in memory relation is on the right' do
|
42
|
-
it 'joins across engines' do
|
43
|
-
@photos \
|
44
|
-
.join(@users) \
|
45
|
-
.on(@users[:id].eq(@photos[:user_id])) \
|
46
|
-
.project(@users[:name], @photos[:camera_id]) \
|
47
|
-
.tap do |relation|
|
48
|
-
rows = relation.call
|
49
|
-
rows.length.should == 2
|
50
|
-
[
|
51
|
-
['bryan', @adapter_returns_integer ? 6 : '6'],
|
52
|
-
['emilio', @adapter_returns_integer ? 42 : '42']
|
53
|
-
].zip(rows).each do |tuple, row|
|
54
|
-
row.relation.should == relation
|
55
|
-
row.tuple.should == tuple
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Array do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#attributes' do
|
14
|
-
it 'manufactures attributes corresponding to the names given on construction' do
|
15
|
-
@relation.attributes.should == [
|
16
|
-
Attribute.new(@relation, :id),
|
17
|
-
Attribute.new(@relation, :name)
|
18
|
-
]
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#call' do
|
23
|
-
it "manufactures an array of hashes of attributes to values" do
|
24
|
-
rows = @relation.call
|
25
|
-
rows.length.should == 3
|
26
|
-
@relation.array.zip(rows).each do |tuple, row|
|
27
|
-
row.relation.should == @relation
|
28
|
-
row.tuple.should == tuple
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Insert do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it "manufactures an array of hashes of attributes to values" do
|
15
|
-
@relation \
|
16
|
-
.insert(@relation[:id] => 4, @relation[:name] => 'guinea fowl') \
|
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
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Join do
|
5
|
-
before do
|
6
|
-
@relation1 = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
@relation2 = @relation1.alias
|
12
|
-
end
|
13
|
-
|
14
|
-
describe InnerJoin do
|
15
|
-
describe '#call' do
|
16
|
-
it 'combines the two tables where the predicate obtains' do
|
17
|
-
@relation1 \
|
18
|
-
.join(@relation2) \
|
19
|
-
.on(@relation1[:id].eq(@relation2[:id])) \
|
20
|
-
.tap do |relation|
|
21
|
-
rows = relation.call
|
22
|
-
rows.length.should == 3
|
23
|
-
@relation1.array.zip(rows).each do |tuple, row|
|
24
|
-
row.relation.should == relation
|
25
|
-
row.tuple.should == (tuple * 2)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Order do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it 'sorts the relation with the provided ordering' do
|
15
|
-
@relation \
|
16
|
-
.order(@relation[:id].desc) \
|
17
|
-
.tap do |relation|
|
18
|
-
rows = relation.call
|
19
|
-
rows.length.should == 3
|
20
|
-
@relation.array.reverse.zip(rows) do |tuple, row|
|
21
|
-
row.relation.should == relation
|
22
|
-
row.tuple.should == tuple
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Project do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it 'retains only the attributes that are provided' do
|
15
|
-
@relation \
|
16
|
-
.project(@relation[:id]) \
|
17
|
-
.tap do |relation|
|
18
|
-
rows = relation.call
|
19
|
-
@relation.array.zip(rows) do |tuple, row|
|
20
|
-
row.relation.should == relation
|
21
|
-
row.tuple.should == [tuple.first]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Skip do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it 'removes the first n rows' do
|
15
|
-
@relation \
|
16
|
-
.skip(1) \
|
17
|
-
.tap do |relation|
|
18
|
-
rows = relation.call
|
19
|
-
rows.length.should == 2
|
20
|
-
one, two = *rows
|
21
|
-
|
22
|
-
one.relation.should == relation
|
23
|
-
one.tuple.should == [2, 'duck']
|
24
|
-
|
25
|
-
two.relation.should == relation
|
26
|
-
two.tuple.should == [3, 'goose']
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Take do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it 'removes the rows after the first n' do
|
15
|
-
@relation \
|
16
|
-
.take(2) \
|
17
|
-
.tap do |relation|
|
18
|
-
rows = relation.call
|
19
|
-
rows.length.should == 2
|
20
|
-
rows.each_with_index do |row, i|
|
21
|
-
row.relation.should == relation
|
22
|
-
row.tuple.should == [i + 1, 'duck']
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Where do
|
5
|
-
before do
|
6
|
-
@relation = Array.new([
|
7
|
-
[1, 'duck' ],
|
8
|
-
[2, 'duck' ],
|
9
|
-
[3, 'goose']
|
10
|
-
], [[:id, Attributes::Integer], [:name, Attributes::String]])
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#call' do
|
14
|
-
it 'filters the relation with the provided predicate' do
|
15
|
-
@relation \
|
16
|
-
.where(@relation[:id].lt(3)) \
|
17
|
-
.tap do |relation|
|
18
|
-
rows = relation.call
|
19
|
-
rows.length.should == 2
|
20
|
-
rows.each_with_index do |row, i|
|
21
|
-
row.relation.should == relation
|
22
|
-
row.tuple.should == [i + 1, 'duck']
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 'when filtering a where relation' do
|
28
|
-
it 'further filters the already-filtered relation with the provided predicate' do
|
29
|
-
@relation \
|
30
|
-
.where(@relation[:id].gt(1)) \
|
31
|
-
.where(@relation[:id].lt(3)) \
|
32
|
-
.tap do |relation|
|
33
|
-
rows = relation.call
|
34
|
-
rows.length.should == 1
|
35
|
-
row = rows.first
|
36
|
-
row.relation.should == relation
|
37
|
-
row.tuple.should == [2, 'duck']
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,258 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Arel
|
4
|
-
describe Join do
|
5
|
-
before do
|
6
|
-
@relation1 = Table(:users)
|
7
|
-
@relation2 = @relation1.alias
|
8
|
-
@predicate = @relation1[:id].eq(@relation2[:id])
|
9
|
-
end
|
10
|
-
|
11
|
-
describe 'when joining a relation to itself' do
|
12
|
-
describe '#to_sql' do
|
13
|
-
it 'manufactures sql aliasing the table and attributes properly in the join predicate and the where clause' do
|
14
|
-
sql = @relation1.join(@relation2).on(@predicate).to_sql
|
15
|
-
|
16
|
-
adapter_is :mysql do
|
17
|
-
sql.should be_like(%Q{
|
18
|
-
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
|
19
|
-
FROM `users`
|
20
|
-
INNER JOIN `users` `users_2`
|
21
|
-
ON `users`.`id` = `users_2`.`id`
|
22
|
-
})
|
23
|
-
end
|
24
|
-
|
25
|
-
adapter_is :oracle do
|
26
|
-
sql.should be_like(%Q{
|
27
|
-
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
|
28
|
-
FROM "USERS"
|
29
|
-
INNER JOIN "USERS" "USERS_2"
|
30
|
-
ON "USERS"."ID" = "USERS_2"."ID"
|
31
|
-
})
|
32
|
-
end
|
33
|
-
|
34
|
-
adapter_is_not :mysql, :oracle do
|
35
|
-
sql.should be_like(%Q{
|
36
|
-
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
|
37
|
-
FROM "users"
|
38
|
-
INNER JOIN "users" "users_2"
|
39
|
-
ON "users"."id" = "users_2"."id"
|
40
|
-
})
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe 'when joining with a where on the same relation' do
|
45
|
-
it 'manufactures sql aliasing the tables properly' do
|
46
|
-
sql = @relation1 \
|
47
|
-
.join(@relation2.where(@relation2[:id].eq(1))) \
|
48
|
-
.on(@predicate) \
|
49
|
-
.to_sql
|
50
|
-
|
51
|
-
adapter_is :mysql do
|
52
|
-
sql.should be_like(%Q{
|
53
|
-
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
|
54
|
-
FROM `users`
|
55
|
-
INNER JOIN `users` `users_2`
|
56
|
-
ON `users`.`id` = `users_2`.`id` AND `users_2`.`id` = 1
|
57
|
-
})
|
58
|
-
end
|
59
|
-
|
60
|
-
adapter_is :oracle do
|
61
|
-
sql.should be_like(%Q{
|
62
|
-
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
|
63
|
-
FROM "USERS"
|
64
|
-
INNER JOIN "USERS" "USERS_2"
|
65
|
-
ON "USERS"."ID" = "USERS_2"."ID" AND "USERS_2"."ID" = 1
|
66
|
-
})
|
67
|
-
end
|
68
|
-
|
69
|
-
adapter_is_not :mysql, :oracle do
|
70
|
-
sql.should be_like(%Q{
|
71
|
-
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
|
72
|
-
FROM "users"
|
73
|
-
INNER JOIN "users" "users_2"
|
74
|
-
ON "users"."id" = "users_2"."id" AND "users_2"."id" = 1
|
75
|
-
})
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe 'when the where occurs before the alias' do
|
80
|
-
it 'manufactures sql aliasing the predicates properly' do
|
81
|
-
relation2 = @relation1.where(@relation1[:id].eq(1)).alias
|
82
|
-
|
83
|
-
sql = @relation1 \
|
84
|
-
.join(relation2) \
|
85
|
-
.on(relation2[:id].eq(@relation1[:id])) \
|
86
|
-
.to_sql
|
87
|
-
|
88
|
-
adapter_is :mysql do
|
89
|
-
sql.should be_like(%Q{
|
90
|
-
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`
|
91
|
-
FROM `users`
|
92
|
-
INNER JOIN `users` `users_2`
|
93
|
-
ON `users_2`.`id` = `users`.`id` AND `users_2`.`id` = 1
|
94
|
-
})
|
95
|
-
end
|
96
|
-
|
97
|
-
adapter_is :oracle do
|
98
|
-
sql.should be_like(%Q{
|
99
|
-
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME"
|
100
|
-
FROM "USERS"
|
101
|
-
INNER JOIN "USERS" "USERS_2"
|
102
|
-
ON "USERS_2"."ID" = "USERS"."ID" AND "USERS_2"."ID" = 1
|
103
|
-
})
|
104
|
-
end
|
105
|
-
|
106
|
-
adapter_is_not :mysql, :oracle do
|
107
|
-
sql.should be_like(%Q{
|
108
|
-
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name"
|
109
|
-
FROM "users"
|
110
|
-
INNER JOIN "users" "users_2"
|
111
|
-
ON "users_2"."id" = "users"."id" AND "users_2"."id" = 1
|
112
|
-
})
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
describe 'when joining the relation to itself multiple times' do
|
119
|
-
before do
|
120
|
-
@relation3 = @relation1.alias
|
121
|
-
end
|
122
|
-
|
123
|
-
describe 'when joining left-associatively' do
|
124
|
-
it 'manufactures sql aliasing the tables properly' do
|
125
|
-
sql = @relation1 \
|
126
|
-
.join(@relation2 \
|
127
|
-
.join(@relation3) \
|
128
|
-
.on(@relation2[:id].eq(@relation3[:id]))) \
|
129
|
-
.on(@relation1[:id].eq(@relation2[:id])) \
|
130
|
-
.to_sql
|
131
|
-
|
132
|
-
adapter_is :mysql do
|
133
|
-
sql.should be_like(%Q{
|
134
|
-
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
|
135
|
-
FROM `users`
|
136
|
-
INNER JOIN `users` `users_2`
|
137
|
-
ON `users`.`id` = `users_2`.`id`
|
138
|
-
INNER JOIN `users` `users_3`
|
139
|
-
ON `users_2`.`id` = `users_3`.`id`
|
140
|
-
})
|
141
|
-
end
|
142
|
-
|
143
|
-
adapter_is :oracle do
|
144
|
-
sql.should be_like(%Q{
|
145
|
-
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME", "USERS_3"."ID", "USERS_3"."NAME"
|
146
|
-
FROM "USERS"
|
147
|
-
INNER JOIN "USERS" "USERS_2"
|
148
|
-
ON "USERS"."ID" = "USERS_2"."ID"
|
149
|
-
INNER JOIN "USERS" "USERS_3"
|
150
|
-
ON "USERS_2"."ID" = "USERS_3"."ID"
|
151
|
-
})
|
152
|
-
end
|
153
|
-
|
154
|
-
adapter_is_not :mysql, :oracle do
|
155
|
-
sql.should be_like(%Q{
|
156
|
-
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
|
157
|
-
FROM "users"
|
158
|
-
INNER JOIN "users" "users_2"
|
159
|
-
ON "users"."id" = "users_2"."id"
|
160
|
-
INNER JOIN "users" "users_3"
|
161
|
-
ON "users_2"."id" = "users_3"."id"
|
162
|
-
})
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
describe 'when joining right-associatively' do
|
168
|
-
it 'manufactures sql aliasing the tables properly' do
|
169
|
-
sql = @relation1 \
|
170
|
-
.join(@relation2).on(@relation1[:id].eq(@relation2[:id])) \
|
171
|
-
.join(@relation3).on(@relation2[:id].eq(@relation3[:id])) \
|
172
|
-
.to_sql
|
173
|
-
|
174
|
-
adapter_is :mysql do
|
175
|
-
sql.should be_like(%Q{
|
176
|
-
SELECT `users`.`id`, `users`.`name`, `users_2`.`id`, `users_2`.`name`, `users_3`.`id`, `users_3`.`name`
|
177
|
-
FROM `users`
|
178
|
-
INNER JOIN `users` `users_2`
|
179
|
-
ON `users`.`id` = `users_2`.`id`
|
180
|
-
INNER JOIN `users` `users_3`
|
181
|
-
ON `users_2`.`id` = `users_3`.`id`
|
182
|
-
})
|
183
|
-
end
|
184
|
-
|
185
|
-
adapter_is :oracle do
|
186
|
-
sql.should be_like(%Q{
|
187
|
-
SELECT "USERS"."ID", "USERS"."NAME", "USERS_2"."ID", "USERS_2"."NAME", "USERS_3"."ID", "USERS_3"."NAME"
|
188
|
-
FROM "USERS"
|
189
|
-
INNER JOIN "USERS" "USERS_2"
|
190
|
-
ON "USERS"."ID" = "USERS_2"."ID"
|
191
|
-
INNER JOIN "USERS" "USERS_3"
|
192
|
-
ON "USERS_2"."ID" = "USERS_3"."ID"
|
193
|
-
})
|
194
|
-
end
|
195
|
-
|
196
|
-
adapter_is_not :mysql, :oracle do
|
197
|
-
sql.should be_like(%Q{
|
198
|
-
SELECT "users"."id", "users"."name", "users_2"."id", "users_2"."name", "users_3"."id", "users_3"."name"
|
199
|
-
FROM "users"
|
200
|
-
INNER JOIN "users" "users_2"
|
201
|
-
ON "users"."id" = "users_2"."id"
|
202
|
-
INNER JOIN "users" "users_3"
|
203
|
-
ON "users_2"."id" = "users_3"."id"
|
204
|
-
})
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe '[]' do
|
212
|
-
describe 'when given an attribute belonging to both sub-relations' do
|
213
|
-
it 'disambiguates the relation that serves as the ancestor to the attribute' do
|
214
|
-
@relation1 \
|
215
|
-
.join(@relation2) \
|
216
|
-
.on(@predicate) \
|
217
|
-
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
|
218
|
-
end
|
219
|
-
|
220
|
-
describe 'when both relations are compound and only one is an alias' do
|
221
|
-
it 'disambiguates the relation that serves as the ancestor to the attribute' do
|
222
|
-
compound1 = @relation1.where(@predicate)
|
223
|
-
compound2 = compound1.alias
|
224
|
-
compound1 \
|
225
|
-
.join(compound2) \
|
226
|
-
.on(@predicate) \
|
227
|
-
.should disambiguate_attributes(compound1[:id], compound2[:id])
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
describe 'when the left relation is extremely compound' do
|
232
|
-
it 'disambiguates the relation that serves as the ancestor to the attribute' do
|
233
|
-
@relation1 \
|
234
|
-
.where(@predicate) \
|
235
|
-
.where(@predicate) \
|
236
|
-
.join(@relation2) \
|
237
|
-
.on(@predicate) \
|
238
|
-
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
|
239
|
-
end
|
240
|
-
end
|
241
|
-
|
242
|
-
describe 'when the right relation is extremely compound' do
|
243
|
-
it 'disambiguates the relation that serves as the ancestor to the attribute' do
|
244
|
-
@relation1 \
|
245
|
-
.join( \
|
246
|
-
@relation2 \
|
247
|
-
.where(@predicate) \
|
248
|
-
.where(@predicate) \
|
249
|
-
.where(@predicate)) \
|
250
|
-
.on(@predicate) \
|
251
|
-
.should disambiguate_attributes(@relation1[:id], @relation2[:id])
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|