ActiveRecord Extensions, PostgreSQL Support
on November 23, 2006 @ 04:09 AM

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

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?


  1. Xavier Lange 11.24.06 / 08AM

    I really like where you’re going with all these exentensions/optimizations. I came here for the good looking import system and stayed for the extended finder stuff. The regexp stuff looks really helpful, I’m looking forward to giving it a whirl.

    Keep up the great work!