Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support table aliases on associated relations #429

Open
wants to merge 2 commits into
base: release-3.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/rom/sql/relation/reading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1086,13 +1086,16 @@ def coerce_conditions(conditions)
# @api private
def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &block)
if other.is_a?(Symbol) || other.is_a?(ROM::Relation::Name)
if join_cond.equal?(EMPTY_HASH) && !block
assoc = associations[other]
assoc = associations.key?(other) && associations[other]

if join_cond.eql?(EMPTY_HASH) && !block
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if join_cond.eql?(EMPTY_HASH) && !block
if (join_cond.nil? || join_cond.empty?) && !block

I'd rather do this. It'd allow nil or {} or [] as join_cond, which are all valid IMO.

I'm not sure why this was .equal?, since that does object equality (so even passing in {} would be false since it's a different though identical object).

assoc.join(type, self)
elsif block
__join__(type, other, JoinDSL.new(schema).(&block), opts)
elsif assoc
new(dataset.__send__(type, assoc.target.name.to_sym, join_cond, opts, &block))
else
new(dataset.__send__(type, other.to_sym, join_cond, opts, &block))
new(dataset.__send__(type, other, join_cond, opts, &block))
end
elsif other.is_a?(Sequel::SQL::AliasedExpression)
new(dataset.__send__(type, other, join_cond, opts, &block))
Expand All @@ -1107,6 +1110,8 @@ def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &block)
end

new(dataset.__send__(type, other.name.dataset.to_sym, join_cond, join_opts))
elsif opts[:table_alias]
new(dataset.__send__( type, other.name.key, join_cond, opts))
else
associations[other.name.key].join(type, self, other)
end
Expand Down
41 changes: 40 additions & 1 deletion spec/unit/relation/inner_join_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
])
end

it 'allows specifying table_aliases' do
it 'allows specifying table_alias with table name' do
relation.insert id: 3, name: 'Jade'

result = relation
Expand Down Expand Up @@ -193,6 +193,45 @@ def name

expect(result.to_a).to eql([name: 'Jane', text: 'solved by Jane'])
end

it 'allows specifying table_alias with association name' do
result = relation
.inner_join(:tasks, { user_id: :id }, table_alias: :t1)
.select(:name, tasks[:title].qualified(:t1))

expect(result.schema.map(&:name)).to eql(%i[name title])

expect(result.to_a).to eql([
{ name: 'Jane', title: "Jane's task" },
{ name: 'Joe', title: "Joe's task" }
])
end

it 'allows specifying table_alias with relation' do
result = relation
.inner_join(tasks, { user_id: :id }, table_alias: :requirements)
.select(:name, tasks[:title].qualified(:requirements))

expect(result.schema.map(&:name)).to eql(%i[name title])

expect(result.to_a).to eql([
{ name: 'Jane', title: "Jane's task" },
{ name: 'Joe', title: "Joe's task" }
])
end

it 'allows specifying table_alias with association name different from relation name' do
result = relation
.inner_join(:todos, {user_id: :id}, table_alias: :requirements)
.select(:name, tasks[:title].qualified(:requirements))

expect(result.schema.map(&:name)).to eql(%i[name title])

expect(result.to_a).to eql([
{ name: 'Jane', title: "Jane's task" },
{ name: 'Joe', title: "Joe's task" }
])
end
end
end
end