[Dbix-class] DBIx-Class-ResultSet-AdvancedWebSearch

Marc Mims marc at questright.com
Thu Sep 25 16:48:51 BST 2008


* Zbigniew Lukasiak <zzbbyy at gmail.com> [080925 06:50]:
> I've assembled a package out of the code in my last year's Advent
> article: http://www.catalystframework.org/calendar/2007/16
> 
> I am not quite satisfied with it - too much interesting code goes into
> the tests (like searching by tags) instead of the main code.
> 
> I am waiting for suggestions on how to improve it.

Here's a simplification of the parse_column logic.  It's a non-recursive
version.  Unless I misread the code, parse_column, in practice, always
returns its input parameter (the full column name) and the join
condition.  I dispensed with the full column name, since we already have
it, and just return the join condition.  So, I renamed the function to
parse_join and changed the calling code accordingly.

All tests pass with the patch applied.

	-Marc


diff --git a/lib/DBIx/Class/ResultSet/AdvancedWebSearch.pm b/lib/DBIx/Class/ResultSet/AdvancedWebSearch.pm
index 8dcc2d2..e5715ed 100644
--- a/lib/DBIx/Class/ResultSet/AdvancedWebSearch.pm
+++ b/lib/DBIx/Class/ResultSet/AdvancedWebSearch.pm
@@ -17,31 +17,16 @@ sub advanced_search {
             $self = $self->$search( $params );
             next; 
         }
-        my ( $full_name, $relation ) = $self->parse_column( $column );
-        $self = $self->search({}, { join => $relation });
-        $columns->{$full_name} = $params->{$column};
+        $self = $self->search({}, { join => $self->parse_join($column) });
+        $columns->{$column} = $params->{$column};
     }
     return $self->search( $columns, $attrs );
 }
 
-sub parse_column {
-    my ( $self, $field)  = @_;
-    if( $field =~ /(.*?)\.(.*)/ ){
-        my $first = $1;
-        my $rest  = $2;
-        my( $column, $join ) = $self->parse_column( $rest );
-        if ( $join ) {
-            return $column, { $first => $join };
-        }else{
-            return $first . '.' . $column, $first;
-        }
-    }
-    elsif( $field ){ 
-        return $field;
-    }
-    else{
-        return;
-    }
+sub parse_join {
+    my (undef, $join, @rest) = reverse split /\./, $_[1];
+    $join = { $_ => $join } for @rest;
+    return $join;
 }



More information about the DBIx-Class mailing list