ActiveRecord Extensions, PostgreSQL Support 23 Nov 2006
PostgreSQL support for ActiveRecord::Extensions is moving along nicely for the better finder support. It includes everything that the better finder support already supported (including Ranges, Regular Expressions and custom made query objects) except for Full Index support.

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 "#{caller.table_name}.#{fieldname} " +
             "#{match_data ? '!~ ':'~'} ?", val )

  register RegexpPostgreSQL, :adapters=>[ :postgresql ]


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