Class: ActiveRecord::Refined::AST::Sql

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

Overview

SQL as written: sql("length(name) > ?", 10). The ? and :name placeholders take quoted values through sanitize_sql_array, which needs the connection, so the binds wait here until the model is known. Without binds the statement passes untouched -- which is what leaves PostgreSQL's ? operators writable, since only the positional-bind rewrite reads ? as a placeholder.

As an operand the statement is parenthesized: its precedence is whatever was written inside. The top of a select list gets it bare, through field_arel, where parentheses would refuse an alias written into the string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Methods inherited from Node

#as, #asc, #collate, #desc

Constructor Details

#initialize(statement, binds) ⇒ Sql

Returns a new instance of Sql.



699
700
701
702
703
704
705
706
# File 'lib/active_record/refined/ast.rb', line 699

def initialize(statement, binds)
  unless statement.is_a?(::String)
    raise ArgumentError,
      "sql takes the statement as a string, not #{statement.inspect}"
  end
  @statement = statement
  @binds = binds
end

Instance Attribute Details

#bindsObject (readonly)

Returns the value of attribute binds.



697
698
699
# File 'lib/active_record/refined/ast.rb', line 697

def binds
  @binds
end

#statementObject (readonly)

Returns the value of attribute statement.



697
698
699
# File 'lib/active_record/refined/ast.rb', line 697

def statement
  @statement
end

Instance Method Details

#field_arel(model) ⇒ Object



712
713
714
715
716
# File 'lib/active_record/refined/ast.rb', line 712

def field_arel(model)
  return Arel.sql(statement) if binds.empty?

  Arel.sql(model.sanitize_sql_array([statement, *binds]))
end

#to_arel(_table, model) ⇒ Object



708
709
710
# File 'lib/active_record/refined/ast.rb', line 708

def to_arel(_table, model)
  Arel::Nodes::Grouping.new(field_arel(model))
end