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
|
2018-09-30 05:30:47 -04:00
|
|
|
include TreeManager::StatementMethods
|
|
|
|
|
2021-03-01 06:47:23 -05:00
|
|
|
def initialize(table = nil)
|
2021-03-04 12:37:54 -05:00
|
|
|
@ast = Nodes::UpdateStatement.new(table)
|
2010-08-14 21:29:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
# 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 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
|
2021-10-18 18:28:48 -04:00
|
|
|
|
|
|
|
def group(columns)
|
|
|
|
columns.each do |column|
|
|
|
|
column = Nodes::SqlLiteral.new(column) if String === column
|
|
|
|
column = Nodes::SqlLiteral.new(column.to_s) if Symbol === column
|
|
|
|
|
|
|
|
@ast.groups.push Nodes::Group.new column
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def having(expr)
|
|
|
|
@ast.havings << expr
|
|
|
|
self
|
|
|
|
end
|
2010-08-14 21:29:32 -04:00
|
|
|
end
|
|
|
|
end
|