Class: Sequel::SQL::DateAdd
- Inherits:
-
GenericExpression
- Object
- Expression
- GenericExpression
- Sequel::SQL::DateAdd
- 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
-
#cast_type ⇒ Object
readonly
The type to cast the expression to.
-
#expr ⇒ Object
readonly
The expression that the interval is being added to.
-
#interval ⇒ Object
readonly
The interval added to the expression, as a hash with symbol keys.
Instance Method Summary collapse
-
#initialize(expr, interval, opts = OPTS) ⇒ DateAdd
constructor
- Supports two types of intervals: Hash
-
Used directly, but values cannot be plain strings.
Methods included from IsDistinctFrom::Methods
Methods included from Sequel::SQLite::JSONOpMethods
#sqlite_json_op, #sqlite_jsonb_op
Methods included from Postgres::HStoreOpMethods
Methods included from Postgres::RangeOpMethods
Methods included from Postgres::ArrayOpMethods
Methods included from Postgres::JSONOpMethods
Methods included from Postgres::InetOpMethods
Methods included from Postgres::PGRowOp::ExpressionMethods
Methods included from SubscriptMethods
Methods included from StringMethods
#escaped_ilike, #escaped_like, #ilike, #like
Methods included from PatternMatchMethods
Methods included from OrderMethods
Methods included from NumericMethods
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
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.
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/sequel/extensions/date_arithmetic.rb', line 214 def initialize(expr, interval, opts=OPTS) @expr = expr h = Hash.new(0) interval = interval.parts unless interval.is_a?(Hash) interval.each do |unit, value| # skip nil values next unless value # Convert weeks to days, as ActiveSupport::Duration can use weeks, # but the database-specific literalizers only support days. if unit == :weeks unit = :days value *= 7 end unless DatasetMethods::DURATION_UNITS.include?(unit) raise Sequel::Error, "Invalid key used in DateAdd interval hash: #{unit.inspect}" end # Attempt to prevent SQL injection by users who pass untrusted strings # as interval values. It doesn't make sense to support literal strings, # due to the numeric adding below. if value.is_a?(String) raise Sequel::InvalidValue, "cannot provide String value as interval part: #{value.inspect}" end h[unit] += value end @interval = Hash[h].freeze @cast_type = opts[:cast] if opts[:cast] freeze end |
Instance Attribute Details
#cast_type ⇒ Object (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.
209 210 211 |
# File 'lib/sequel/extensions/date_arithmetic.rb', line 209 def cast_type @cast_type end |
#expr ⇒ Object (readonly)
The expression that the interval is being added to.
201 202 203 |
# File 'lib/sequel/extensions/date_arithmetic.rb', line 201 def expr @expr end |
#interval ⇒ Object (readonly)
The interval added to the expression, as a hash with symbol keys.
205 206 207 |
# File 'lib/sequel/extensions/date_arithmetic.rb', line 205 def interval @interval end |