Skip to content

Instantly share code, notes, and snippets.

@kabakiyo
Created August 1, 2012 12:52
Show Gist options
  • Save kabakiyo/3226618 to your computer and use it in GitHub Desktop.
Save kabakiyo/3226618 to your computer and use it in GitHub Desktop.
Case insensitive like (ilike) in Datamapper with Postgresql
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
@Speljohan
Copy link

Know of a solution like this for PostgreSQL's SIMILAR TO condition (regex)? That'd be very much appreciated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment