Skip to content

Add database specific string concatenation #410

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

Merged
merged 1 commit into from
Jan 6, 2016
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
7 changes: 6 additions & 1 deletion lib/arel/nodes/infix_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ def initialize left, right
end
end

class Concat < InfixOperation
def initialize left, right
super('||', left, right)
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/arel/predications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def when right
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
Expand Down
1 change: 1 addition & 0 deletions lib/arel/visitors/depth_first.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def binary o
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
Expand Down
1 change: 1 addition & 0 deletions lib/arel/visitors/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def binary o
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
Expand Down
8 changes: 8 additions & 0 deletions lib/arel/visitors/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def visit_Arel_Nodes_UpdateStatement o, collector
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
1 change: 1 addition & 0 deletions test/visitors/test_depth_first.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def test_right_outer_join
[
Arel::Nodes::Assignment,
Arel::Nodes::Between,
Arel::Nodes::Concat,
Arel::Nodes::DoesNotMatch,
Arel::Nodes::Equality,
Arel::Nodes::GreaterThan,
Expand Down
18 changes: 18 additions & 0 deletions test/visitors/test_mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ def compile node
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
10 changes: 8 additions & 2 deletions test/visitors/test_to_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,19 @@ def quote value, column = nil
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

Expand Down