Class: ArelExtensions::Nodes::DateAdd

Inherits:
Function
  • Object
show all
Defined in:
lib/arel_extensions/nodes/date_diff.rb

Constant Summary collapse

RETURN_TYPE =
:date

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#!=, #==, #as, #convert_to_date_node, #convert_to_datetime_node, #convert_to_node, #convert_to_number, #convert_to_string_node, #expr, #left, #return_type, #right, #type_of_attribute

Methods included from Predications

#cast, #convert_to_node, #imatches, #in, #matches, #not_in, #when

Constructor Details

#initialize(expr) ⇒ DateAdd

Returns a new instance of DateAdd.



40
41
42
43
44
45
46
47
# File 'lib/arel_extensions/nodes/date_diff.rb', line 40

def initialize expr
  col = expr.first
  @date_type = type_of_attribute(col)
  tab = expr.map do |arg|
    convert(arg)
  end
  return super(tab)
end

Instance Attribute Details

#date_typeObject

Returns the value of attribute date_type.



38
39
40
# File 'lib/arel_extensions/nodes/date_diff.rb', line 38

def date_type
  @date_type
end

Instance Method Details

#mssql_datepart(v = nil) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/arel_extensions/nodes/date_diff.rb', line 127

def mssql_datepart(v = nil)
  v ||= self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :date
      Arel.sql('day')
    elsif @date_type == :datetime
      Arel.sql('second')
    end
  else
    if ArelExtensions::Nodes::Duration === v
      v.with_interval = true
      case v.left
      when  'd','m','y'
        Arel.sql('day')
      when 'h','mn','s'
        Arel.sql('second')
      when /i\z/
        Arel.sql(Arel::Visitors::MSSQL::DATE_MAPPING[v.left[0..-2]])
      else
        Arel.sql(Arel::Visitors::MSSQL::DATE_MAPPING[v.left])
      end
    else
      nil
    end
  end
end

#mssql_value(v = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/arel_extensions/nodes/date_diff.rb', line 114

def mssql_value(v = nil)
  v ||= self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :date
      v.to_i / (24*3600)
    elsif @date_type == :datetime
      v.to_i
    end
  else
    v
  end
end

#mysql_value(v = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/arel_extensions/nodes/date_diff.rb', line 62

def mysql_value(v = nil)
  v ||= self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :date || @date_type == :datetime
      Arel.sql('INTERVAL %s' % v.inspect.sub(/s\Z/, ''))
    end
  else
    if ArelExtensions::Nodes::Duration === v
      v.with_interval = true
      v
    else
      v
    end
  end
end

#oracle_value(v = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/arel_extensions/nodes/date_diff.rb', line 96

def oracle_value(v = nil)
  v ||= self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :ruby_date
      Arel.sql("(INTERVAL '1' DAY) * %s" % v.inspect.to_i )
    else
      Arel.sql("(INTERVAL '1' SECOND) * %s" % v.to_i)
    end
  else
    if ArelExtensions::Nodes::Duration === v
      v.with_interval = true
      v
    else
      v
    end
  end
end

#postgresql_value(v = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/arel_extensions/nodes/date_diff.rb', line 78

def postgresql_value(v = nil)
  v ||= self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :date
      Arel.sql("INTERVAL '%s'" % v.inspect.sub(/s\Z/, '').upcase)
    elsif @date_type == :datetime
      Arel.sql("INTERVAL '%s'" % v.inspect.sub(/s\Z/, '').upcase)
    end
  else
    if ArelExtensions::Nodes::Duration === v
      v.with_interval = true
      v
    else
      v
    end
  end
end

#sqlite_valueObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/arel_extensions/nodes/date_diff.rb', line 49

def sqlite_value
  v = self.expressions.last
  if defined?(ActiveSupport::Duration) && ActiveSupport::Duration === v
    if @date_type == :date
      return Arel::Nodes.build_quoted((v.value >= 0 ? '+' : '-') + v.inspect)
    elsif @date_type == :datetime
      return Arel::Nodes.build_quoted((v.value >= 0 ? '+' : '-') + v.inspect)
    end
  else
    return v
  end
end