Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Holek/306d96c4427ab693c56d to your computer and use it in GitHub Desktop.
Arel 3.0.3 Visitor for MSSQL UPDATE with JOIN monkey patch
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
@Holek
Copy link
Author

Holek commented May 1, 2014

This overloads Arel::Visitors::ToSql method of the same name, which Arel::Visitors::MSSQL is a descendant of.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment