class Sequel::SQL::EscapedLikeExpression

  1. lib/sequel/extensions/escaped_like.rb
Superclass: Expression

Represents an pattern match SQL expression, where the pattern can depend upon interpolated values in a database-dependent manner.

Methods

Public Class

  1. new

Public Instance

  1. to_s_append

Included modules

  1. AliasMethods
  2. BooleanMethods
  3. CastMethods
  4. OrderMethods

Public Class methods

new(expr, case_sensitive, placeholder_pattern, placeholder_values)

Initialize the expression. Arguments:

expr

Right hand site of LIKE/ILIKE operator, what you are matching against the pattern

case_insensitive

Whether the match is case sensitive

placeholder_pattern

The pattern to match against, with +?+ for the placeholders

placeholder_values

The string values for each +?+ in the placeholder pattern. Should be an array of strings, though it can be a single string if there is only a single placeholder.

[show source]
   # File lib/sequel/extensions/escaped_like.rb
37 def initialize(expr, case_sensitive, placeholder_pattern, placeholder_values)
38   @expr = expr
39   @method = case_sensitive ? :like : :ilike
40   @pattern = placeholder_pattern
41   unless placeholder_values.is_a?(Array)
42     placeholder_values = [placeholder_values].freeze
43   end
44   @values = placeholder_values
45   freeze
46 end

Public Instance methods

to_s_append(ds, sql)

Interpolate the pattern values into the placeholder pattern to get the final pattern, now that we have access to the dataset. Use the expression and final pattern and add an appropriate LIKE/ILIKE expression to the SQL being built.

[show source]
   # File lib/sequel/extensions/escaped_like.rb
51 def to_s_append(ds, sql)
52   i = -1
53   match_len = @values.length - 1
54   like_pattern = String.new
55   pattern = @pattern
56   while true
57     previous, q, pattern = pattern.partition('?')
58     like_pattern << previous
59 
60     unless q.empty?
61       if i == match_len
62         raise Error, "Mismatched number of placeholders (#{i+1}) and placeholder arguments (#{@values.length}) for escaped like expression: #{@pattern.inspect}"
63       end
64       like_pattern << ds.escape_like(@values.at(i+=1))
65     end
66 
67     if pattern.empty?
68       unless i == match_len
69         raise Error, "Mismatched number of placeholders (#{i+1}) and placeholder arguments (#{@values.length}) for escaped like expression: #{@pattern.inspect}"
70       end
71       break
72     end
73   end
74   ds.literal_append(sql, Sequel.send(@method, @expr, like_pattern))
75 end