handle /NOT/foo/OR/bar
[id3fs.git] / lib / ID3FS / Path / Node.pm
index dd650e4..478adb3 100644 (file)
@@ -1,3 +1,19 @@
+# id3fs - a FUSE-based filesystem for browsing audio metadata
+# Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 package ID3FS::Path::Node;
 
 use strict;
@@ -71,69 +87,79 @@ sub to_sql
     my($self, $hasvals, $not, @joins)=@_;
     $not=0 unless(defined($not));
     my @outjoins=();
+    my $str='';
     # init
     unless(@joins)
     {
        @outjoins = @joins = ("INNER");
     }
-    my $left=$self->left();
-    my $right=$self->right();
-    return ("", @outjoins) unless($left || $right);
-    my ($leftstr, @leftjoins) = $self->node_to_sql($left, $hasvals, $not, @joins);
-    push(@joins, @leftjoins);
-    push(@outjoins, @leftjoins);
-    my $op=$self->name();
-    if(defined($op) && $self->type() eq "boolean")
+    if($self->type() ne "boolean")
     {
-       # if we are ANDing, add an inner join
-       # also if we are NOTing, but we are looking for a tag *value*
-       if( ($op eq "AND") || ($hasvals && ($op eq "NOT")))
+       my $cnt=scalar(@joins)+1;
+       $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
+       if($not && !$hasvals)
        {
-           # hack - if right child is a NOT, we don't need extra join/brackets
-           # NOT will do the same and we will end up with an extra one
-           unless($right && $right->name() && $right->name() eq "NOT")
+           $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
+       }
+       return ($str, @outjoins);
+    }
+    else
+    {
+       my $left=$self->left();
+       my $right=$self->right();
+       return ("", @outjoins) unless($left || $right);
+       my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
+       push(@joins, @leftjoins);
+       push(@outjoins, @leftjoins);
+       my $op=$self->name();
+       if(defined($op) && $self->type() eq "boolean")
+       {
+           # if we are ANDing, add an inner join
+           # also if we are NOTing, but we are looking for a tag *value*
+           if( ($op eq "AND") || ($hasvals && ($op eq "NOT")))
            {
-               push(@joins, "INNER");
-               push(@outjoins, "INNER");
+               # hack - if right child is a NOT, we don't need extra join/brackets
+               # NOT will do the same and we will end up with an extra one
+               unless($right && $right->name() && $right->name() eq "NOT")
+               {
+                   push(@joins, "INNER");
+                   push(@outjoins, "INNER");
+               }
+           }
+           elsif($op eq "NOT")
+           {
+               $not=1;
+               push(@joins, "LEFT");
+               push(@outjoins, "LEFT");
+#              print("LEFT: ", $left->print(), "\n") if ($left);
+#              print("RIGHT: ", $right->print(), "\n") if($right);
+           }
+           elsif($op eq "OR")
+           {
+               # if left child is a not, we need an extra (inner) join
+               if($left && $left->name() && $left->name() eq "NOT")
+               {
+                   push(@joins, "INNER");
+                   push(@outjoins, "INNER");
+               }
            }
        }
-       elsif($op eq "NOT")
+       my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
+       push(@outjoins, @rightjoins);
+#      print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
+#      print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
+       my $str=$leftstr;
+       $str .= " $op " if($op && !$not);
+       $str .= $rightstr;
+       if($op || ($left && $right))
        {
-           $not=1;
-           push(@joins, "LEFT");
-           push(@outjoins, "LEFT");
-#          print("LEFT: ", $left->print(), "\n") if ($left);
-#          print("RIGHT: ", $right->print(), "\n") if($right);
+           $str="(" . $str . ")";
        }
+#      print "STR: $str\n";
+#      my @all=(@joins, @rightjoins);
+#      print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
+       return($str, @outjoins);
     }
-    my ($rightstr, @rightjoins) = $self->node_to_sql($right, $hasvals, $not, @joins);
-    push(@outjoins, @rightjoins);
-#    print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
-#    print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
-    my $str=$leftstr;
-    $str .= " $op " if($op && !$not);
-    $str .= $rightstr;
-    if($op || ($left && $right))
-    {
-       $str="(" . $str . ")";
-    }
-#    print "STR: $str\n";
-    return($str, @outjoins);
-}
-
-sub node_to_sql
-{
-    my($self, $node, $hasvals, $not, @joins)=@_;
-    return ("", ()) unless(defined($node));
-    return $node->to_sql($hasvals, $not, @joins) if($node->type() eq "boolean");
-    my $sql;
-    my $cnt=scalar(@joins)+1;
-    $sql .= "t" . scalar(@joins) . ".id='" . $node->{id} . "'";
-    if($not && !$hasvals)
-    {
-       $sql .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
-    }
-    return ($sql, ());
 }
 
 sub used_tags