Created
August 1, 2012 12:52
-
-
Save kabakiyo/3226618 to your computer and use it in GitHub Desktop.
Case insensitive like (ilike) in Datamapper with Postgresql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'dm-core' | |
require 'dm-do-adapter' | |
module DataMapper | |
class Query | |
module Conditions | |
class ILikeComparison < AbstractComparison | |
slug :ilike | |
private | |
# Overloads the +expected+ method in AbstractComparison | |
# | |
# Return a regular expression suitable for matching against the | |
# records value. | |
# | |
# @return [Regexp] | |
# | |
# @see AbtractComparison#expected | |
# | |
# @api semipublic | |
def expected | |
Regexp.new('\A' << super.gsub('%', '.*').tr('_', '.') << '\z') | |
end | |
# @return [String] | |
# | |
# @see AbstractComparison#to_s | |
# | |
# @api private | |
def comparator_string | |
'ILIKE' | |
end | |
end # class LikeComparison | |
end | |
end | |
module Adapters | |
class DataObjectsAdapter < AbstractAdapter | |
module SQL #:nodoc: | |
alias :old_comparison_operator :comparison_operator | |
def comparison_operator(comparison) | |
case comparison.slug | |
when :ilike then 'ILIKE' | |
else old_comparison_operator(comparison) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Know of a solution like this for PostgreSQL's SIMILAR TO condition (regex)? That'd be very much appreciated :)