Created
May 1, 2014 09:35
-
-
Save Holek/306d96c4427ab693c56d to your computer and use it in GitHub Desktop.
Arel 3.0.3 Visitor for MSSQL UPDATE with JOIN monkey patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Arel | |
module Visitors | |
class MSSQL | |
private | |
def visit_Arel_Nodes_UpdateStatement o | |
if o.orders.empty? && o.limit.nil? | |
wheres = o.wheres | |
else | |
key = o.key | |
unless key | |
warn(<<-eowarn) if $VERBOSE | |
(#{caller.first}) Using UpdateManager without setting UpdateManager#key is | |
deprecated and support will be removed in ARel 4.0.0. Please set the primary | |
key on UpdateManager using UpdateManager#key= | |
eowarn | |
key = o.relation.primary_key | |
end | |
wheres = [Nodes::In.new(key, [build_subselect(key, o)])] | |
end | |
[ | |
"UPDATE #{visit(o.relation.respond_to?(:left) ? o.relation.left : o.relation)}", | |
("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), | |
"FROM #{visit o.relation}", | |
("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?), | |
].compact.join ' ' | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This overloads
Arel::Visitors::ToSql
method of the same name, whichArel::Visitors::MSSQL
is a descendant of.