Allow nesting of create_date and edit_date filters
authorMike Rylander <mrylander@gmail.com>
Tue, 12 Mar 2013 20:01:25 +0000 (16:01 -0400)
committerMike Rylander <mrylander@gmail.com>
Wed, 13 Mar 2013 16:07:45 +0000 (12:07 -0400)
As gently suggested by tsbere, this commit allows nesting of said
filters.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm

index 9d713e6..fb49bbe 100644 (file)
@@ -958,45 +958,6 @@ sub toSQL {
         $agg_record = 'CASE WHEN COUNT(DISTINCT m.source) = 1 THEN FIRST(m.source) ELSE NULL END AS record';
     }
 
-    # bre.create_date and bre.edit_date filtering
-    for my $datefilter ( qw/create_date edit_date/ ) {
-        my $cdate = $self->find_filter($datefilter);
-        if ($cdate && $cdate->args && scalar(@{$cdate->args}) > 0 && scalar(@{$cdate->args}) < 3) {
-            my ($cstart, $cend) = @{$cdate->args};
-
-            if (!$cstart and !$cend) {
-                # useless use of filter
-            } elsif (!$cstart or $cstart eq '-infinity') { # no start supplied
-                if ($cend eq 'infinity') {
-                    # useless use of filter
-                } else {
-                    # "before $cend"
-                    $cend = cleanse_ISO8601($cend);
-                    $limit_where .= <<"                    SQL";
-            AND bre.$datefilter <= \$_$$\$$cend\$_$$\$
-                    SQL
-                }
-    
-            } elsif (!$cend or $cend eq 'infinity') { # no end supplied
-                if ($cstart eq '-infinity') {
-                    # useless use of filter
-                } else { # "after $cstart"
-                    $cstart = cleanse_ISO8601($cstart);
-                    $limit_where .= <<"                    SQL";
-            AND bre.$datefilter >= \$_$$\$$cstart\$_$$\$
-                    SQL
-                }
-            } else { # both supplied
-                # "between $cstart and $cend"
-                $cstart = cleanse_ISO8601($cstart);
-                $cend = cleanse_ISO8601($cend);
-                $limit_where .= <<"                SQL";
-            AND bre.$datefilter BETWEEN \$_$$\$$cstart\$_$$\$ AND \$_$$\$$cend\$_$$\$
-                SQL
-            }
-        }
-    }
-
     my $sql = <<SQL;
 WITH
 $with
@@ -1310,6 +1271,42 @@ sub flatten {
                     $where .= $joiner if $where ne '';
                     $where .= "$key ${NOT}IN (" . join(',', map { $self->QueryParser->quote_value($_) } @{$filter->args}) . ')';
                 }
+
+            } elsif ($filter->name eq 'edit_date' or $filter->name eq 'create_date') {
+                # bre.create_date and bre.edit_date filtering
+                my $datefilter = $filter->name;
+
+                if ($filter && $filter->args && scalar(@{$filter->args}) > 0 && scalar(@{$filter->args}) < 3) {
+                    my ($cstart, $cend) = @{$filter->args};
+        
+                    if (!$cstart and !$cend) {
+                        # useless use of filter
+                    } elsif (!$cstart or $cstart eq '-infinity') { # no start supplied
+                        if ($cend eq 'infinity') {
+                            # useless use of filter
+                        } else {
+                            # "before $cend"
+                            $cend = cleanse_ISO8601($cend);
+                            $where .= $joiner if $where ne '';
+                            $where .= "bre.$datefilter <= \$_$$\$$cend\$_$$\$";
+                        }
+            
+                    } elsif (!$cend or $cend eq 'infinity') { # no end supplied
+                        if ($cstart eq '-infinity') {
+                            # useless use of filter
+                        } else { # "after $cstart"
+                            $cstart = cleanse_ISO8601($cstart);
+                            $where .= $joiner if $where ne '';
+                            $where .= "bre.$datefilter >= \$_$$\$$cstart\$_$$\$";
+                        }
+                    } else { # both supplied
+                        # "between $cstart and $cend"
+                        $cstart = cleanse_ISO8601($cstart);
+                        $cend = cleanse_ISO8601($cend);
+                        $where .= $joiner if $where ne '';
+                        $where .= "bre.$datefilter BETWEEN \$_$$\$$cstart\$_$$\$ AND \$_$$\$$cend\$_$$\$";
+                    }
+                }
             } elsif ($filter->name eq 'locations') {
                 if (@{$filter->args} > 0) {
                     my $spcdepth = $self->plan_level + 5;