Class: MysqlFramework::SqlCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_framework/sql_condition.rb

Overview

This class is used to represent a Sql Condition for a column.

Direct Known Subclasses

InCondition

Constant Summary collapse

NIL_COMPARISONS =
['IS NULL', 'IS NOT NULL'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column:, comparison:, value: nil) ⇒ SqlCondition

Creates a new SqlCondition using the given parameters.

Parameters:

  • column (String)
    • the name of the column to use in the comparison

  • comparison (String)
    • the MySQL comparison operator to use

  • value (Object) (defaults to: nil)
    • the value to use in the comparison (default nil)

Raises:

  • ArgumentError if comparison is ‘IS NULL’ and value is not nil

  • ArgumentError if comparison is ‘IS NOT NULL’ and value is not nil

  • ArgumentError if comparison is neither ‘IS NULL’ or ‘IS NOT NULL’ and value is nil



20
21
22
23
24
25
26
# File 'lib/mysql_framework/sql_condition.rb', line 20

def initialize(column:, comparison:, value: nil)
  @column = column
  @comparison = comparison

  validate(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

This method is called to get the value of this condition for prepared statements.



9
10
11
# File 'lib/mysql_framework/sql_condition.rb', line 9

def value
  @value
end

Instance Method Details

#to_sString

This method is called to get the condition as a string for a sql prepared statement

Returns:

  • (String)


31
32
33
34
35
# File 'lib/mysql_framework/sql_condition.rb', line 31

def to_s
  return "#{@column} #{@comparison.upcase}" if nil_comparison?

  "#{@column} #{@comparison} ?"
end