Class: LegacyMigrations::Squirrel::Query::Condition
- Inherits:
-
Object
- Object
- LegacyMigrations::Squirrel::Query::Condition
- Defined in:
- lib/legacy_migrations/squirrel/squirrel.rb
Overview
Handles comparisons in the query. This class is analagous to the columns in the database. When comparing the Condition to a value, the operators are used as follows:
-
, === : Straight-up Equals. Can also be used as the “IN” operator if the operand is an Array.
Additionally, when the oprand is nil
, the comparison is correctly generates as “IS NULL”.“
-
~ : The LIKE and REGEXP operators. If the operand is a String, it will generate a LIKE
comparison. If it is a Regexp, the REGEXP operator will be used. NOTE: MySQL regular expressions are NOT the same as Ruby regular expressions. Also NOTE: No wildcards are inserted into the LIKE comparison, so you may add them where you wish.
-
<=> : Performs a BETWEEN comparison, as long as the operand responds to both #first and #last,
which both Ranges and Arrays do.
-
> : A simple greater-than comparison.
-
>= : Greater-than or equal-to.
-
< : A simple less-than comparison.
-
<= : Less-than or equal-to.
-
contains? : Like =~, except automatically surrounds the operand in %s, which =~ does not do.
-
nil? : Works exactly like “column == nil”, but in a nicer syntax, which is what Squirrel is all about.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
Instance Method Summary collapse
-
#-@ ⇒ Object
(also: #not, #desc)
:nodoc:.
-
#assign_join(association = nil) ⇒ Object
Gets the name of the table that this Condition refers to by taking it out of the association object.
-
#contains?(val) ⇒ Boolean
:nodoc:.
-
#full_name ⇒ Object
Returns the full name of the column, including any assigned table alias.
-
#initialize(name) ⇒ Condition
constructor
Creates and Condition with the given name.
-
#negative? ⇒ Boolean
Returns true if this Condition has been negated, which means it will be prefixed with “NOT”.
-
#nil? ⇒ Boolean
:nodoc:.
-
#to_find_conditions(join_association = {}) ⇒ Object
Generates the :condition parameter for this Condition, in [“sql”, args] format.].
Constructor Details
#initialize(name) ⇒ Condition
Creates and Condition with the given name.
447 448 449 450 451 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 447 def initialize name @name = name @sql = nil @negative = false end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
444 445 446 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 444 def name @name end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
444 445 446 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 444 def operand @operand end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
444 445 446 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 444 def operator @operator end |
Instance Method Details
#-@ ⇒ Object Also known as: not, desc
:nodoc:
473 474 475 476 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 473 def -@ #:nodoc: @negative = !@negative self end |
#assign_join(association = nil) ⇒ Object
Gets the name of the table that this Condition refers to by taking it out of the association object.
488 489 490 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 488 def assign_join association = nil @table_alias = association ? "#{association.aliased_table_name}." : "" end |
#contains?(val) ⇒ Boolean
:nodoc:
461 462 463 464 465 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 461 def contains? val #:nodoc: @operator = :contains @operand = val self end |
#full_name ⇒ Object
Returns the full name of the column, including any assigned table alias.
493 494 495 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 493 def full_name "#{@table_alias}#{name}" end |
#negative? ⇒ Boolean
Returns true if this Condition has been negated, which means it will be prefixed with “NOT”
482 483 484 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 482 def negative? @negative end |
#nil? ⇒ Boolean
:nodoc:
467 468 469 470 471 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 467 def nil? #:nodoc: @operator = :== @operand = nil self end |
#to_find_conditions(join_association = {}) ⇒ Object
Generates the :condition parameter for this Condition, in [“sql”, args] format.]
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 498 def to_find_conditions(join_association = {}) return nil if operator.nil? op, arg_format, values = operator, "?", [operand] op, arg_format, values = case operator when :<=> then [ "BETWEEN", "? AND ?", [ operand.first, operand.last ] ] when :=~ then case operand when String then [ "LIKE", arg_format, values ] when Regexp then [ "REGEXP", arg_format, values.map(&:source) ] end when :==, :=== then case operand when Array then [ "IN", "(?)", values ] when Range then [ "IN", "(?)", values ] when Condition then [ "=", operand.full_name, [] ] when nil then [ "IS", "NULL", [] ] else [ "=", arg_format, values ] end when :contains then [ "LIKE", arg_format, values.map{|v| "%#{v}%" } ] else case operand when Condition then [ op, oprand.full_name, [] ] else [ op, arg_format, values ] end end sql = "#{full_name} #{op} #{arg_format}" sql = "NOT (#{sql})" if @negative [ sql, *values ] end |