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, #desc

Constructor Details

#initialize(statement, binds) ⇒ Sql

Returns a new instance of Sql.



521
522
523
524
525
526
527
528
# File 'lib/active_record/refined/ast.rb', line 521

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.



519
520
521
# File 'lib/active_record/refined/ast.rb', line 519

def binds
  @binds
end

#statementObject (readonly)

Returns the value of attribute statement.



519
520
521
# File 'lib/active_record/refined/ast.rb', line 519

def statement
  @statement
end

Instance Method Details

#field_arel(model) ⇒ Object



534
535
536
537
538
# File 'lib/active_record/refined/ast.rb', line 534

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



530
531
532
# File 'lib/active_record/refined/ast.rb', line 530

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