tidy code
[id3fs.git] / lib / ID3FS / Path / Node.pm
index 681a66c..52888a6 100644 (file)
@@ -1,8 +1,30 @@
+# 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;
 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;
@@ -14,10 +36,15 @@ sub new
     $self->{type}=shift;
     $self->{name}=shift;
     $self->{parents_id}=shift;
-    if($self->{type} ne "boolean")
+    $self->{table}=1;
+    $self->{hasvals}=undef;
+    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}));
     }
@@ -40,126 +67,50 @@ sub name       { return shift->set("name",       shift); }
 sub type       { return shift->set("type",       shift); }
 sub id         { return shift->set("id",         shift); }
 sub parents_id { return shift->set("parents_id", shift); }
-
-sub print
-{
-    my($self)=@_;
-    my $op=$self->name();
-    my $left=$self->left();
-    my $right=$self->right();
-    return "" unless($left || $right);
-    my $str .= $self->print_node($left);
-    $str .= (" " . $op . " ") if($op);
-    $str .= $self->print_node($right);
-    if($op || ($left && $right))
-    {
-       $str="(" . $str . ")";
-    }
-    return $str;
-}
-
-sub print_node
-{
-    my($self, $node)=@_;
-    return "" unless(defined($node));
-    return $node->print() if(ref($node) eq "ID3FS::Path::Node");
-    return $node->{name};
-}
+sub table      { return shift->set("table",      shift); }
+sub hasvals    { return shift->set("hasvals",    shift); }
 
 sub to_sql
 {
-    my($self, $hasvals, $not, @joins)=@_;
+    my($self, $not)=@_;
     $not=0 unless(defined($not));
-    my @outjoins=();
-    # init
-    unless(@joins)
+    my $str='';
+
+    if($self->type() != $TYPE_BOOL)
     {
-       @outjoins = @joins = ("INNER");
+       $str .= "t" . $self->table() . ".id='" . $self->{id} . "'";
+       if($not && !$self->hasvals())
+       {
+           $str = "(" . $str . " AND fxt" . $self->table() . ".files_id IS NULL)";
+       }
+       return ($str);
     }
+
     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);
+    return ("") unless($left || $right);
+
+    my $leftstr = $left->to_sql($not) if($left);
     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")))
-       {
-           # 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);
-       }
-    }
-    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;
+    $not=1 if(defined($op) && ($op eq "NOT"));
+    my $rightstr = $right->to_sql($not) if($right);
+    $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;
-    if(defined($node->{parents_id}))
-    {
-       $sql= "(t" . scalar(@joins) . ".parents_id='$node->{parents_id}'";
-       $sql .= " AND t" . scalar(@joins) . ".id='" . $node->{id} . "'";
-    }
-    else
-    {
-       $sql= "(t" . scalar(@joins) .".parents_id=''";
-       $sql .= " AND t" . scalar(@joins) . ".id='" . $node->{id} . "'";
-    }
-    if($not && !$hasvals)
-    {
-       $sql .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
-    }
-    $sql .= ")";
-    return ($sql, ());
+    $str=("(" . $str . ")") if($op && $left && $right);
+    return($str);
 }
 
 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());
        push(@used, $self->right()->used_tags()) if($self->right());
        return(grep { defined; } @used);
     }
-    elsif($self->parents_id())
-    {
-       return([ $self->parents_id(), $self->id() ]);
-    }
     return $self->id();
 }