2017-02-13 13:58:58 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-24 01:45:50 -05:00
|
|
|
|
2018-02-24 02:41:47 -05:00
|
|
|
module Arel # :nodoc: all
|
2010-08-14 21:29:32 -04:00
|
|
|
class UpdateManager < Arel::TreeManager
|
2014-11-29 19:22:17 -05:00
|
|
|
def initialize
|
2010-08-14 21:29:32 -04:00
|
|
|
super
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast = Nodes::UpdateStatement.new
|
2010-12-07 13:46:30 -05:00
|
|
|
@ctx = @ast
|
2010-08-14 21:29:32 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def take(limit)
|
2014-03-24 19:26:09 -04:00
|
|
|
@ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit
|
2010-09-21 12:20:14 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def key=(key)
|
2014-03-24 19:26:09 -04:00
|
|
|
@ast.key = Nodes.build_quoted(key)
|
2010-12-24 17:48:33 -05:00
|
|
|
end
|
|
|
|
|
2011-08-11 03:14:51 -04:00
|
|
|
def key
|
|
|
|
@ast.key
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def order(*expr)
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.orders = expr
|
2010-09-21 12:20:14 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2010-08-14 21:29:32 -04:00
|
|
|
###
|
|
|
|
# UPDATE +table+
|
2018-02-24 01:45:50 -05:00
|
|
|
def table(table)
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.relation = table
|
2010-08-14 21:29:32 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def wheres=(exprs)
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.wheres = exprs
|
2010-08-14 22:48:33 -04:00
|
|
|
end
|
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def where(expr)
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.wheres << expr
|
2010-08-14 21:29:32 -04:00
|
|
|
self
|
|
|
|
end
|
2010-08-14 22:12:52 -04:00
|
|
|
|
2018-02-24 01:45:50 -05:00
|
|
|
def set(values)
|
2010-08-18 12:52:16 -04:00
|
|
|
if String === values
|
2010-11-05 17:09:09 -04:00
|
|
|
@ast.values = [values]
|
2010-08-18 12:52:16 -04:00
|
|
|
else
|
2018-02-24 01:45:50 -05:00
|
|
|
@ast.values = values.map { |column, value|
|
2010-09-10 16:36:42 -04:00
|
|
|
Nodes::Assignment.new(
|
2010-08-18 12:52:16 -04:00
|
|
|
Nodes::UnqualifiedColumn.new(column),
|
|
|
|
value
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
2010-08-14 22:12:52 -04:00
|
|
|
self
|
|
|
|
end
|
2010-08-14 21:29:32 -04:00
|
|
|
end
|
|
|
|
end
|