class ArelExtensions::Nodes::Function

Constants

MBSTRING

Support multibyte string if they are available.

RETURN_TYPE

Public Instance Methods

!=(other) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 47
def !=(other)
  Arel::Nodes::NotEqual.new self, Arel::Nodes.build_quoted(other, self)
end
==(other) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 43
def ==(other)
  Arel::Nodes::Equality.new self, Arel::Nodes.build_quoted(other, self)
end
as(other) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 23
def as other
  res = Arel::Nodes::As.new(self.clone, Arel.sql(other))
  if Gem::Version.new(Arel::VERSION) >= Gem::Version.new("9.0.0")
    self.alias = Arel.sql(other)
  end
  res
end
convert_to_date_node(object) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 121
def convert_to_date_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when DateTime, Time
    Arel::Nodes.build_quoted(Date.new(object.year, object.month, object.day), self)
  when MBSTRING, String
    Arel::Nodes.build_quoted(Date.parse(object.to_s), self)
  when Date
    Arel::Nodes.build_quoted(object, self)
  else
    raise(ArgumentError, "#{object.class} cannot be converted to Date")
  end
end
convert_to_datetime_node(object) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 136
def convert_to_datetime_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when DateTime, Time
    Arel::Nodes.build_quoted(object, self)
  when MBSTRING, String
    Arel::Nodes.build_quoted(Time.parse(object.to_s), self)
  when Date
    Arel::Nodes.build_quoted(Time.utc(object.year, object.month, object.day, 0, 0, 0), self)
  else
    raise(ArgumentError, "#{object.class} cannot be converted to Datetime")
  end
end
convert_to_node(object) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 66
def convert_to_node(object)
  case object
  when Arel::Attributes::Attribute, Arel::Nodes::Node, Integer
    object
  when DateTime
    Arel::Nodes.build_quoted(object, self)
  when Time
    Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
  when MBSTRING, String, Symbol
    Arel::Nodes.build_quoted(object.to_s)
  when Date
    Arel::Nodes.build_quoted(object.to_s, self)
  when NilClass
    Arel.sql('NULL')
  when ActiveSupport::Duration
    Arel.sql(object.to_i)
  when Array
    Arel::Nodes::Grouping.new(object.map{|e| convert_to_node(e)})
  else
    raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
  end
end
convert_to_number(object) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 151
def convert_to_number(object)
  case object
  when ArelExtensions::Nodes::Duration
    object.with_interval = true
    object
  when Arel::Attributes::Attribute, Arel::Nodes::Node
    object
  when Integer
    object.to_i.abs
  when DateTime, Date, Time, String, ActiveSupport::Duration
    object.to_i.abs
  when NilClass
    0
  else
    raise(ArgumentError, "#{object.class} cannot be converted to NUMBER arg")
  end
end
convert_to_string_node(object) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 89
def convert_to_string_node(object)
  case object
  when Arel::Nodes::Node
    object
  when Integer
    Arel::Nodes.build_quoted(object.to_s)
  when Arel::Attributes::Attribute
    case self.type_of_attribute(object)
    when :date
      ArelExtensions::Nodes::Format.new [object, 'yyyy-mm-dd']
    when :time
      ArelExtensions::Nodes::Format.new [object, '%H:%M:%S']
    else
      object
    end
  when DateTime
    Arel::Nodes.build_quoted(object, self)
  when Time
    Arel::Nodes.build_quoted(object.strftime('%H:%M:%S'), self)
  when MBSTRING, String
    Arel::Nodes.build_quoted(object.to_s)
  when Date
    Arel::Nodes.build_quoted(object, self)
  when NilClass
    Arel.sql(nil)
  when ActiveSupport::Duration
    Arel::Nodes.build_quoted(object.to_i.to_s)
  else
    raise(ArgumentError, "#{object.class} cannot be converted to CONCAT arg")
  end
end
expr() click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 31
def expr
   @expressions.first
end
left() click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 35
def left
  @expressions.first
end
return_type() click to toggle source

overrides as to make new Node like AliasPredication

# File lib/arel_extensions/nodes/function.rb, line 19
def return_type
  self.class.const_get(:RETURN_TYPE)
end
right() click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 39
def right
  @expressions[1]
end
type_of_attribute(att) click to toggle source
# File lib/arel_extensions/nodes/function.rb, line 51
def type_of_attribute(att)
  case att
  when Arel::Attributes::Attribute
    begin
      Arel::Table.engine.connection.schema_cache.columns_hash(att.relation.table_name)[att.name.to_s].type
    rescue
      att
    end
  when ArelExtensions::Nodes::Function
    att.return_type
    #        else
    #          nil
  end
end