To add regular expression support (only case-sensitive at this time, case-insensitivity will come later) the only code I had to write was the below:
module ActiveRecord::Extensions
# The below supports finder sql for regular expression
# support the PostgreSQL adapter:
# Example:
# Model.find :all, :conditions=>{ :title => /regex/ }
#
class RegexpPostgreSQL < AbstractExtension
NOT_EQUAL_RGX = /(.+)_(ne|not)/
def self.process( key, val, caller )
if val.is_a?( Regexp )
match_data = key.to_s.match( NOT_EQUAL_RGX )
key = match_data.captures\[0\] if match_data
fieldname = caller.connection.quote_column_name( key )
return Result.new( "#{caller.table_name}.#{fieldname} " +
"#{match_data ? '!~ ':'~'} ?", val )
end
nil
end
end
register RegexpPostgreSQL, :adapters=>[ :postgresql ]
end
I am really digging the pluggable extensions so far. What do you think of the above code set to add Regexp support for PostgreSQL?
blog comments powered by Disqus