class Sequel::SQL::DateAdd

  1. lib/sequel/extensions/date_arithmetic.rb
Parent: SQL

The DateAdd class represents the addition of an interval to a date/timestamp expression.

Methods

Public Class

  1. new

Public Instance

  1. expr
  2. interval

Attributes

expr [R]

The expression that the interval is being added to.

interval [R]

The interval added to the expression, as a hash with symbol keys.

Public Class methods

new (expr, interval)

Supports two types of intervals:

Hash

Used directly, but values cannot be plain strings.

ActiveSupport::Duration

Converted to a hash using the interval’s parts.

[show source]
# File lib/sequel/extensions/date_arithmetic.rb, line 163
def initialize(expr, interval)
  @expr = expr
  @interval = if interval.is_a?(Hash)
    interval.each_value do |v|
       # Attempt to prevent SQL injection by users who pass untrusted strings
       # as interval values. 
       if v.is_a?(String) && !v.is_a?(LiteralString)
         raise Sequel::InvalidValue, "cannot provide String value as interval part: #{v.inspect}"
       end
    end
    interval
  else
    h = Hash.new(0)
    interval.parts.each{|unit, value| h[unit] += value}
    {}.merge(h)
  end
end