debian-mirror-gitlab/lib/gitlab/database/date_time.rb

26 lines
939 B
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
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-10-12 21:52:04 +05:30
# Note: the interval is returned as an INTERVAL type.
2017-08-17 22:00:37 +05:30
def subtract_datetimes(query_so_far, start_time_attrs, end_time_attrs, as)
diff_fn = subtract_datetimes_diff(query_so_far, start_time_attrs, end_time_attrs)
2016-09-29 09:46:39 +05:30
query_so_far.project(diff_fn.as(as))
end
2017-08-17 22:00:37 +05:30
def subtract_datetimes_diff(query_so_far, start_time_attrs, end_time_attrs)
2019-10-12 21:52:04 +05:30
Arel::Nodes::Subtraction.new(
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(end_time_attrs)),
Arel::Nodes::NamedFunction.new("COALESCE", Array.wrap(start_time_attrs))
)
2017-08-17 22:00:37 +05:30
end
2016-09-29 09:46:39 +05:30
end
end
end