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

Fix function qualification methods #391

Merged
merged 1 commit into from
Jan 6, 2021
Merged
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
17 changes: 11 additions & 6 deletions lib/rom/sql/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,23 @@ def name
#
# @api private
def qualified(table_alias = nil)
meta(
func: ::Sequel::SQL::Function.new(func.name, *func.args.map { |arg| arg.respond_to?(:qualified) ? arg.qualified(table_alias) : arg })
)
new { |arg|
arg.respond_to?(:qualified) ? arg.qualified(table_alias) : arg
}
end

# @see Attribute#qualified_projection
#
# @api private
def qualified_projection(table_alias = nil)
meta(
func: ::Sequel::SQL::Function.new(func.name, *func.args.map { |arg| arg.respond_to?(:qualified_projection) ? arg.qualified_projection(table_alias) : arg })
)
new { |arg|
arg.respond_to?(:qualified_projection) ? arg.qualified_projection(table_alias) : arg
}
end

# @api private
def new(&block)
meta(func: ::Sequel::SQL::Function.new!(func.name, func.args.map(&block), func.opts))
end

# @see Attribute#qualified?
Expand Down
5 changes: 5 additions & 0 deletions spec/unit/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
to eql('ROW_NUMBER() OVER (PARTITION BY "name")')
end

example 'with the PARTITION BY clause which is aliased' do
expect(func.row_number.over(partition: :name, order: :name).as(:row_numbers).sql_literal(ds)).
to eql('ROW_NUMBER() OVER (PARTITION BY "name" ORDER BY "name") AS "row_numbers"')
end

example 'with the frame clause' do
expect(func.row_number.over(frame: :all).sql_literal(ds)).
to eql('ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)')
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/relation/select_append_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,21 @@
expect(selected.schema.map(&:name)).to eql(%i[id title])
expect(selected.first).to eql(id: 1, title: "JOE'S TASK")
end

context 'functions' do
with_adapters(:postgres) do
example 'row_number' do
selected = relation.select(:id).select_append {
[integer::row_number().over(partition: title).as(:row_numbers)]
}

expect(selected.dataset.sql).to eql(<<~SQL.strip.gsub("\n", " "))
SELECT "tasks"."id", ROW_NUMBER() OVER (PARTITION BY "tasks"."title") AS "row_numbers"
FROM "tasks"
ORDER BY "tasks"."id"
SQL
end
end
end
end
end