Class: Sequel::SQL::StringExpression

Inherits:
ComplexExpression show all
Includes:
InequalityMethods, NoBooleanInputMethods, StringConcatenationMethods, StringMethods
Defined in:
lib/sequel/lib/sequel/sql.rb

Overview

Subclass of ComplexExpression where the expression results in a text/string/varchar value in SQL.

Constant Summary collapse

LIKE_MAP =

Map of [regexp, case_insenstive] to ComplexExpression operator

{[true, true]=>:'~*', [true, false]=>:~, [false, true]=>:ILIKE, [false, false]=>:LIKE}

Constants inherited from ComplexExpression

ComplexExpression::BITWISE_OPERATORS, ComplexExpression::BOOLEAN_OPERATOR_METHODS, ComplexExpression::INEQUALITY_OPERATORS, ComplexExpression::IS_OPERATORS, ComplexExpression::MATHEMATICAL_OPERATORS, ComplexExpression::N_ARITY_OPERATORS, ComplexExpression::ONE_ARITY_OPERATORS, ComplexExpression::OPERTATOR_INVERSIONS, ComplexExpression::TWO_ARITY_OPERATORS

Instance Attribute Summary

Attributes inherited from ComplexExpression

#args, #op

Class Method Summary collapse

Methods included from NoBooleanInputMethods

#initialize

Methods included from StringConcatenationMethods

#+

Methods included from StringMethods

#ilike, #like

Methods inherited from ComplexExpression

#eql?, #initialize

Methods included from SubscriptMethods

#sql_subscript

Methods included from OrderMethods

#asc, #desc

Methods included from CastMethods

#cast, #cast_numeric, #cast_string

Methods included from AliasMethods

#as

Methods inherited from Expression

#lit

Class Method Details

.like(l, *ces) ⇒ Object

Creates a SQL pattern match exprssion. left (l) is the SQL string we are matching against, and ces are the patterns we are matching. The match succeeds if any of the patterns match (SQL OR). Patterns can be given as strings or regular expressions. Strings will cause the SQL LIKE operator to be used, and should be supported by most databases. Regular expressions will probably only work on MySQL and PostgreSQL, and SQL regular expression syntax is not fully compatible with ruby regular expression syntax, so be careful if using regular expressions.

The pattern match will be case insensitive if the last argument is a hash with a key of :case_insensitive that is not false or nil. Also, if a case insensitive regular expression is used (//i), that particular pattern which will always be case insensitive.


756
757
758
759
760
761
762
763
764
# File 'lib/sequel/lib/sequel/sql.rb', line 756

def self.like(l, *ces)
  l, lre, lci = like_element(l)
  lci = (ces.last.is_a?(Hash) ? ces.pop : {})[:case_insensitive] ? true : lci
  ces.collect! do |ce|
    r, rre, rci = like_element(ce)
    BooleanExpression.new(LIKE_MAP[[lre||rre, lci||rci]], l, r)
  end
  ces.length == 1 ? ces.at(0) : BooleanExpression.new(:OR, *ces)
end