join stack tweaking
[id3fs.git] / lib / ID3FS / Path / Node.pm
index 3775d38..b71406d 100644 (file)
@@ -19,6 +19,12 @@ package ID3FS::Path::Node;
 use strict;
 use warnings;
 
+require Exporter;
+use vars qw(@ISA @EXPORT $TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE);
+@ISA=qw(Exporter);
+@EXPORT=qw($TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE);
+($TYPE_BOOL, $TYPE_TAG, $TYPE_ARTIST, $TYPE_ALBUM, $TYPE_FILE)=(1..5);
+
 sub new
 {
     my $proto=shift;
@@ -30,10 +36,13 @@ sub new
     $self->{type}=shift;
     $self->{name}=shift;
     $self->{parents_id}=shift;
-    if($self->{type} ne "boolean")
+    if($self->{type} != $TYPE_BOOL)
     {
-       my $table=ucfirst($self->{type});
-       $table .= "s" unless($table=~/s$/);
+       my $table='';
+       if   ($self->{type} == $TYPE_TAG)    { $table="tags";    }
+       elsif($self->{type} == $TYPE_ARTIST) { $table="artists"; }
+       elsif($self->{type} == $TYPE_ALBUM)  { $table="albums";  }
+       elsif($self->{type} == $TYPE_FILE)   { $table="files";   }
        $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id});
        return undef unless(defined($self->{id}));
     }
@@ -62,20 +71,18 @@ sub to_sql
     my($self, $hasvals, $not, @joins)=@_;
     $not=0 unless(defined($not));
     my @outjoins=();
-    my $str='';
-    # init
     unless(@joins)
     {
        @outjoins = @joins = ("INNER");
     }
+    my $str='';
 
-    if($self->type() ne "boolean")
+    if($self->type() != $TYPE_BOOL)
     {
-       my $cnt=scalar(@joins)+1;
        $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
        if($not && !$hasvals)
        {
-           $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
+           $str = "(" . $str . " AND fxt" . scalar(@joins) . ".files_id IS NULL)";
        }
        return ($str, @outjoins);
     }
@@ -83,80 +90,54 @@ sub to_sql
     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();
-    print "op: $op type: ", $self->type(), " not: $not\n";
     if(defined($op))
     {
-       # 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")
+       my $join=undef;
+       # 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")
        {
-           print "AND\n";
-           # 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")
+           # if we are ANDing or ORing, add an inner join
+           # also if we are NOTing, but we are looking for a tag *value*
+           if($op eq "AND")
            {
-               push(@joins, "INNER");
-               push(@outjoins, "INNER");
+               $join= "INNER";
            }
-       }
-       elsif($op eq "NOT")
-       {
-           print "NOT (was $not)\n";
-           $not=1;
-           # as above - 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")
+           elsif($op eq "NOT")
            {
-               if($hasvals)
-               {
-                   push(@joins, "INNER");
-                   push(@outjoins, "INNER");
-               }
-               else
-               {
-                   push(@joins, "LEFT");
-                   push(@outjoins, "LEFT");
-               }
+               $not=1;
+               $join = ($hasvals ? "INNER" : "LEFT");
            }
-       }
-       elsif($op eq "OR")
-       {
-           print "OR\n";
-           # if left child is a NOT, we need an extra (inner) join
-           # unless right child is also a NOT
-           if(($left && $left->name() && $left->name() eq "NOT") &&
-              !($right && $right->name() && $right->name() eq "NOT"))
+           elsif($op eq "OR")
            {
-               push(@joins, "INNER");
-               push(@outjoins, "INNER");
+               $join="INNER" unless($left && $left->name() && $left->name() eq "NOT")
            }
        }
+       if($join)
+       {
+           push(@joins, $join);
+           push(@outjoins, $join);
+       }
     }
     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";
-    $str=$leftstr;
+    $str = $leftstr;
     $str .= " $op " if($op && !$not);
     $str .= $rightstr;
-    if($op || ($left && $right))
-    {
-       $str="(" . $str . ")";
-    }
-#    print "STR: $str\n";
-#    my @all=(@joins, @rightjoins);
-#    print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
+    $str=("(" . $str . ")") if($op && $left && $right);
     return($str, @outjoins);
 }
 
 sub used_tags
 {
     my($self)=@_;
-    if($self->type() eq "boolean")
+    if($self->type() == $TYPE_BOOL)
     {
        my @used=();
        push(@used, $self->left()->used_tags())  if($self->left());