class Sequel::SQL::StringExpression

  1. lib/sequel/sql.rb
Superclass: ComplexExpression

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

Methods

Public Class

  1. like

Public Instance

  1. sql_string

Constants

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

Map of [regexp, case_insenstive] to ComplexExpression operator symbol

Public Class methods

like (l, *ces)

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 some databases. Be aware that SQL 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%' ESCAPE '\')
StringExpression.like(:a, 'a%', case_insensitive: true) # ("a" ILIKE 'a%' ESCAPE '\')
StringExpression.like(:a, 'a%', /^a/i) # (("a" LIKE 'a%' ESCAPE '\') OR ("a" ~* '^a'))
[show source]
     # File lib/sequel/sql.rb
1754 def self.like(l, *ces)
1755   l, lre, lci = like_element(l)
1756   lci = (ces.last.is_a?(Hash) ? ces.pop : OPTS)[:case_insensitive] ? true : lci
1757   ces.map! do |ce|
1758     r, rre, rci = like_element(ce)
1759     BooleanExpression.new(LIKE_MAP[[lre||rre, lci||rci]], l, r)
1760   end
1761   ces.length == 1 ? ces[0] : BooleanExpression.new(:OR, *ces)
1762 end

Public Instance methods

sql_string ()

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

[show source]
     # File lib/sequel/sql.rb
1778 def sql_string
1779   self
1780 end