class Sequel::SQL::DateAdd
The DateAdd
class represents the addition of an interval to a date/timestamp expression.
Attributes
The type to cast the expression to. nil if not overridden, in which cast the generic timestamp type for the database will be used.
The expression that the interval is being added to.
The interval added to the expression, as a hash with symbol keys.
Public Class Methods
Source
# File lib/sequel/extensions/date_arithmetic.rb 214 def initialize(expr, interval, opts=OPTS) 215 @expr = expr 216 217 h = Hash.new(0) 218 interval = interval.parts unless interval.is_a?(Hash) 219 interval.each do |unit, value| 220 # skip nil values 221 next unless value 222 223 # Convert weeks to days, as ActiveSupport::Duration can use weeks, 224 # but the database-specific literalizers only support days. 225 if unit == :weeks 226 unit = :days 227 value *= 7 228 end 229 230 unless DatasetMethods::DURATION_UNITS.include?(unit) 231 raise Sequel::Error, "Invalid key used in DateAdd interval hash: #{unit.inspect}" 232 end 233 234 # Attempt to prevent SQL injection by users who pass untrusted strings 235 # as interval values. It doesn't make sense to support literal strings, 236 # due to the numeric adding below. 237 if value.is_a?(String) 238 raise Sequel::InvalidValue, "cannot provide String value as interval part: #{value.inspect}" 239 end 240 241 h[unit] += value 242 end 243 244 @interval = Hash[h].freeze 245 @cast_type = opts[:cast] if opts[:cast] 246 freeze 247 end
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.