Class: Sequel::SQL::DateAdd

Inherits:
GenericExpression show all
Defined in:
lib/sequel/extensions/date_arithmetic.rb

Overview

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

Defined Under Namespace

Modules: DatasetMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Postgres::HStoreOpMethods

#hstore

Methods included from Postgres::RangeOpMethods

#pg_range

Methods included from Postgres::ArrayOpMethods

#pg_array

Methods included from Postgres::JSONOpMethods

#pg_json, #pg_jsonb

Methods included from Postgres::InetOpMethods

#pg_inet

Methods included from Postgres::PGRowOp::ExpressionMethods

#pg_row

Methods included from SubscriptMethods

#sql_subscript

Methods included from StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from PatternMatchMethods

#!~, #=~

Methods included from OrderMethods

#asc, #desc

Methods included from NumericMethods

#+, #coerce

Methods included from ComplexExpressionMethods

#extract, #sql_boolean, #sql_number, #sql_string

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from BooleanMethods

#~

Methods included from AliasMethods

#as

Methods inherited from Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

#initialize(expr, interval, opts = OPTS) ⇒ DateAdd

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.


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sequel/extensions/date_arithmetic.rb', line 187

def initialize(expr, interval, opts=OPTS)
  @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
    Hash[interval]
  else
    h = Hash.new(0)
    interval.parts.each{|unit, value| h[unit] += value}
    Hash[h]
  end

  @interval.freeze
  @cast_type = opts[:cast] if opts[:cast]
  freeze
end

Instance Attribute Details

#cast_typeObject (readonly)

The type to cast the expression to. nil if not overridden, in which cast the generic timestamp type for the database will be used.


182
183
184
# File 'lib/sequel/extensions/date_arithmetic.rb', line 182

def cast_type
  @cast_type
end

#exprObject (readonly)

The expression that the interval is being added to.


174
175
176
# File 'lib/sequel/extensions/date_arithmetic.rb', line 174

def expr
  @expr
end

#intervalObject (readonly)

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


178
179
180
# File 'lib/sequel/extensions/date_arithmetic.rb', line 178

def interval
  @interval
end