Class: ArelExtensions::Nodes::DateAdd

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

Constant Summary collapse

RETURN_TYPE =
:date

Constants inherited from Function

Function::MBSTRING

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

Methods inherited from Arel::Nodes::Function

#as

Methods included from ArelExtensions::NullFunctions

#coalesce, #coalesce_blank, #if_present, #is_not_null, #is_null

Methods included from BooleanFunctions

#and, #or, #then, #⋀, #⋁

Methods included from StringFunctions

#&, #[], #ai_collate, #ai_imatches, #ai_matches, #blank, #byte_length, #char_length, #ci_collate, #collate, #concat, #downcase, #edit_distance, #group_concat, #idoes_not_match, #idoes_not_match_all, #idoes_not_match_any, #imatches, #imatches_all, #imatches_any, #length, #levenshtein_distance, #locate, #ltrim, #md5, #not_blank, #regexp_replace, #repeat, #replace, #rtrim, #smatches, #soundex, #substring, #trim, #upcase

Methods included from MathFunctions

#*, #/, #abs, #ceil, #floor, #format_number, #log10, #pow, #power, #round, #std, #sum, #variance

Methods included from DateDuration

#day, #format, #format_date, #hour, #minute, #month, #second, #wday, #week, #year

Methods included from Comparators

#!~, #<, #<=, #=~, #>, #>=

Methods included from Math

#+, #-

Methods included from Aliases

#xas

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
  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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/arel_extensions/nodes/date_diff.rb', line 136

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
      res = if v.parts.size == 1
              #       first entry in the dict v.parts; one of [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
              #       |     the key
              #       |     |     convert symbol to string
              #       |     |     |   remove the plural suffix `s`
              #       v     v     v   v
              v.parts.first.first.to_s[0..-2]
            else
              'second'
            end
      Arel.sql(res)
    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(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left[0..-2]])
      else
        Arel.sql(ArelExtensions::Visitors::MSSQL::LOADED_VISITOR::DATE_MAPPING[v.left])
      end
    end
  end
end

#mssql_value(v = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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
      if v.parts.size == 1
        #       first entry in the dict v.parts; one of [:years, :months, :weeks, :days, :hours, :minutes, :seconds]
        #       |     the value
        #       |     |
        #       |     |
        #       v     v
        v.parts.first.second
      else
        v.to_i
      end
    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
      Arel.quoted((v.value >= 0 ? '+' : '-') + v.inspect)
    elsif @date_type == :datetime
      Arel.quoted((v.value >= 0 ? '+' : '-') + v.inspect)
    end
  else
    v
  end
end