Class: Sequel::SQL::StringExpression

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

Overview

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

Constant Summary

LIKE_MAP =

Map of [regexp, case_insenstive] to ComplexExpression operator symbol

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

Constants inherited from ComplexExpression

ComplexExpression::BITWISE_OPERATORS, ComplexExpression::BOOLEAN_OPERATOR_METHODS, ComplexExpression::CONSTANT_INVERSIONS, ComplexExpression::CUSTOM_EXPRESSIONS, ComplexExpression::EQUALITY_OPERATORS, ComplexExpression::INEQUALITY_OPERATORS, ComplexExpression::IN_OPERATORS, ComplexExpression::IS_OPERATORS, ComplexExpression::LIKE_OPERATORS, ComplexExpression::MATHEMATICAL_OPERATORS, ComplexExpression::N_ARITY_OPERATORS, ComplexExpression::ONE_ARITY_OPERATORS, ComplexExpression::OPERTATOR_INVERSIONS, ComplexExpression::REGEXP_OPERATORS, ComplexExpression::TWO_ARITY_OPERATORS

Instance Attribute Summary

Attributes inherited from ComplexExpression

#args, #op

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StringConcatenationMethods

#+

Methods included from StringMethods

#ilike, #like

Methods inherited from ComplexExpression

#initialize, #sql_boolean, #sql_number

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

#==, attr_reader, #eql?, #hash, inherited, #inspect, #lit, #sql_literal

Constructor Details

This class inherits a constructor from Sequel::SQL::ComplexExpression

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).

If a regular expression is used as a pattern, an SQL regular expression will be used, which is currently only supported on MySQL and PostgreSQL. Be aware that MySQL and PostgreSQL regular expression syntax is similar to ruby regular expression syntax, but it not exactly the same, especially for advanced regular expression features. Sequel just uses the source of the ruby regular expression verbatim as the SQL regular expression string.

If any other object is used as a regular expression, the SQL LIKE operator will be used, and should be supported by most databases.

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.

StringExpression.like(:a, 'a%') # "a" LIKE 'a%'
StringExpression.like(:a, 'a%', :case_insensitive=>true) # "a" ILIKE 'a%'
StringExpression.like(:a, 'a%', /^a/i) # "a" LIKE 'a%' OR "a" ~* '^a'


1490
1491
1492
1493
1494
1495
1496
1497
1498
# File 'lib/sequel/sql.rb', line 1490

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

.like_element(re) ⇒ Object

Returns a three element array, made up of:

  • The object to use

  • Whether it is a regular expression

  • Whether it is case insensitive



1504
1505
1506
1507
1508
1509
1510
# File 'lib/sequel/sql.rb', line 1504

def self.like_element(re) # :nodoc:
  if re.is_a?(Regexp)
    [re.source, true, re.casefold?]
  else
    [re, false, false]
  end
end

Instance Method Details

#sql_stringObject

Return self instead of creating a new object to save on memory.



1514
1515
1516
# File 'lib/sequel/sql.rb', line 1514

def sql_string
  self
end