1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add database specific string concatenation

This commit is contained in:
Keenan Brock 2015-12-31 20:22:07 -05:00
parent dfd22638c2
commit 83c47c1962
8 changed files with 47 additions and 3 deletions

View file

@ -40,5 +40,10 @@ module Arel
end
end
class Concat < InfixOperation
def initialize left, right
super('||', left, right)
end
end
end
end

View file

@ -202,6 +202,10 @@ Passing a range to `#not_in` is deprecated. Call `#not_between`, instead.
Nodes::Case.new(self).when quoted_node(right)
end
def concat other
Nodes::Concat.new self, other
end
private
def grouping_any method_id, others, *extras

View file

@ -72,6 +72,7 @@ module Arel
alias :visit_Arel_Nodes_As :binary
alias :visit_Arel_Nodes_Assignment :binary
alias :visit_Arel_Nodes_Between :binary
alias :visit_Arel_Nodes_Concat :binary
alias :visit_Arel_Nodes_DeleteStatement :binary
alias :visit_Arel_Nodes_DoesNotMatch :binary
alias :visit_Arel_Nodes_Equality :binary

View file

@ -176,6 +176,7 @@ module Arel
alias :visit_Arel_Nodes_As :binary
alias :visit_Arel_Nodes_Assignment :binary
alias :visit_Arel_Nodes_Between :binary
alias :visit_Arel_Nodes_Concat :binary
alias :visit_Arel_Nodes_DoesNotMatch :binary
alias :visit_Arel_Nodes_Equality :binary
alias :visit_Arel_Nodes_GreaterThan :binary

View file

@ -72,6 +72,14 @@ module Arel
maybe_visit o.limit, collector
end
def visit_Arel_Nodes_Concat o, collector
collector << " CONCAT("
visit o.left, collector
collector << ", "
visit o.right, collector
collector << ") "
collector
end
end
end
end

View file

@ -103,6 +103,7 @@ module Arel
[
Arel::Nodes::Assignment,
Arel::Nodes::Between,
Arel::Nodes::Concat,
Arel::Nodes::DoesNotMatch,
Arel::Nodes::Equality,
Arel::Nodes::GreaterThan,

View file

@ -55,6 +55,24 @@ module Arel
compile(node).must_be_like "LOCK IN SHARE MODE"
end
end
describe "concat" do
it "concats columns" do
@table = Table.new(:users)
query = @table[:name].concat(@table[:name])
compile(query).must_be_like %{
CONCAT("users"."name", "users"."name")
}
end
it "concats a string" do
@table = Table.new(:users)
query = @table[:name].concat(Nodes.build_quoted('abc'))
compile(query).must_be_like %{
CONCAT("users"."name", 'abc')
}
end
end
end
end
end

View file

@ -469,13 +469,19 @@ module Arel
compile(node).must_equal %(("products"."price" - 7))
end
it "should handle Concatination" do
table = Table.new(:users)
node = table[:name].concat(table[:name])
compile(node).must_equal %("users"."name" || "users"."name")
end
it "should handle arbitrary operators" do
node = Arel::Nodes::InfixOperation.new(
'||',
'&&',
Arel::Attributes::String.new(Table.new(:products), :name),
Arel::Attributes::String.new(Table.new(:products), :name)
)
compile(node).must_equal %("products"."name" || "products"."name")
compile(node).must_equal %("products"."name" && "products"."name")
end
end