Module: Babik::QuerySet::Update::Assignment

Defined in:
lib/babik/queryset/lib/update/assignment.rb

Overview

Field assignment module

Defined Under Namespace

Classes: Decrement, Divide, Function, Increment, Multiply, Operation

Class Method Summary collapse

Class Method Details

._escape(str) ⇒ String

Escape a value for database

Parameters:

  • str (String)

    original string value.

Returns:

  • (String)

    escaped string value.



40
41
42
# File 'lib/babik/queryset/lib/update/assignment.rb', line 40

def self._escape(str)
  Babik::Database.escape(str)
end

.sql_field(model, field) ⇒ String

Return the field prepared for the UPDATE operation. Used when rendering the SQL template

Parameters:

  • model (ActiveRecord::Base)

    model this field belongs to.

  • field (String)

    field to be updated.

Returns:

  • (String)

    Field prepared to be inserted in the left part of a SQL UPDATE assignment.



15
16
17
18
# File 'lib/babik/queryset/lib/update/assignment.rb', line 15

def self.sql_field(model, field)
  field = Babik::Table::Field.new(model, field)
  field.real_field
end

.sql_value(update_field_value) ⇒ String

Return the value prepared for the UPDATE operation. Used when rendering the SQL template

Parameters:

  • update_field_value (Operation, Function, String, ActiveRecord::BASE)

    field to be updated. if Operation, an arithmetic operation based on other field of the record will be applied (+, -, * …) if Function, a function will be called.

    The parameters of the function can ben any other field of the record.
    

    if String, a escaped version of the value will be returned. if ActiveRecord::Base, the id of the object will be returned. Otherwise, the value as-is will be returned.

Returns:

  • (String)

    Field prepared to be inserted in the left part of a SQL UPDATE assignment.



30
31
32
33
34
35
# File 'lib/babik/queryset/lib/update/assignment.rb', line 30

def self.sql_value(update_field_value)
  return update_field_value.sql_value if update_field_value.is_a?(Operation) || update_field_value.is_a?(Function)
  return _escape(update_field_value) if update_field_value.is_a?(String)
  return update_field_value.id if update_field_value.is_a?(ActiveRecord::Base)
  update_field_value
end