2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-20 08:43:11 -04:00
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module DateTime
|
|
|
|
# Find the first of the `end_time_attrs` that isn't `NULL`. Subtract from it
|
|
|
|
# the first of the `start_time_attrs` that isn't NULL. `SELECT` the resulting interval
|
|
|
|
# along with an alias specified by the `as` parameter.
|
|
|
|
#
|
2019-06-13 09:12:28 -04:00
|
|
|
# Note: the interval is returned as an INTERVAL type.
|
2016-10-20 04:08:53 -04:00
|
|
|
def subtract_datetimes(query_so_far, start_time_attrs, end_time_attrs, as)
|
2016-10-21 03:44:04 -04:00
|
|
|
diff_fn = subtract_datetimes_diff(query_so_far, start_time_attrs, end_time_attrs)
|
2016-09-20 08:43:11 -04:00
|
|
|
|
|
|
|
query_so_far.project(diff_fn.as(as))
|
|
|
|
end
|
2016-10-14 11:33:21 -04:00
|
|
|
|
2016-10-20 04:08:53 -04:00
|
|
|
def subtract_datetimes_diff(query_so_far, start_time_attrs, end_time_attrs)
|
2019-06-13 09:12:28 -04:00
|
|
|
Arel::Nodes::Subtraction.new(
|
|
|
|
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
|
|
|
|
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs))
|
|
|
|
)
|
2016-10-14 11:33:21 -04:00
|
|
|
end
|
2016-09-20 08:43:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|