Class: ActiveRecord::Refined::AST::Cast

Inherits:
Node
  • Object
show all
Includes:
Arithmetics, Predications
Defined in:
lib/active_record/refined/ast.rb

Overview

CAST(expr AS type). The type is grammar too, written into the SQL as given -- it is the adapter's own name for the type, and whether it exists is the database's to say -- so it has to look like one: a plain name, at most parenthesized with lengths.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arithmetics

#*, #+, #-, #/

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #casecmp?, #distinct_from?, #end_with?, #ilike?, #in?, #include?, #intersect?, #like?, #member?, #not_between?, #not_distinct_from?, #not_ilike?, #not_in?, #not_like?, #not_null?, #null?, #start_with?, #subset?, #superset?

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(operand, sql_type) ⇒ Cast

Returns a new instance of Cast.



447
448
449
450
# File 'lib/active_record/refined/ast.rb', line 447

def initialize(operand, sql_type)
  @operand = operand
  @sql_type = AST.check_name(sql_type, TYPE_NAME, "SQL type")
end

Instance Attribute Details

#operandObject (readonly)

Returns the value of attribute operand.



445
446
447
# File 'lib/active_record/refined/ast.rb', line 445

def operand
  @operand
end

#sql_typeObject (readonly)

Returns the value of attribute sql_type.



445
446
447
# File 'lib/active_record/refined/ast.rb', line 445

def sql_type
  @sql_type
end

Instance Method Details

#to_arel(table) ⇒ Object



452
453
454
455
456
457
# File 'lib/active_record/refined/ast.rb', line 452

def to_arel(table)
  Arel::Nodes::NamedFunction.new(
    "CAST",
    [Arel::Nodes::As.new(to_arel_argument(operand, table),
                         Arel::Nodes::SqlLiteral.new(sql_type.to_s))])
end