1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010 Ian Beckwith <ianb@erislabs.net>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package ID3FS::Path::Node;
23 use vars qw(@ISA @EXPORT $TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE);
25 @EXPORT=qw($TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE);
26 ($TYPE_BOOL, $TYPE_TAG, $TYPE_ARTIST, $TYPE_ALBUM, $TYPE_FILE)=(1..5);
31 my $class=ref($proto) || $proto;
38 $self->{parents_id}=shift;
39 if($self->{type} != $TYPE_BOOL)
42 if ($self->{type} == $TYPE_TAG) { $table="tags"; }
43 elsif($self->{type} == $TYPE_ARTIST) { $table="artists"; }
44 elsif($self->{type} == $TYPE_ALBUM) { $table="albums"; }
45 elsif($self->{type} == $TYPE_FILE) { $table="files"; }
46 $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id});
47 return undef unless(defined($self->{id}));
54 my($self, $name, $val)=@_;
59 return $self->{$name};
62 sub left { return shift->set("left", shift); }
63 sub right { return shift->set("right", shift); }
64 sub name { return shift->set("name", shift); }
65 sub type { return shift->set("type", shift); }
66 sub id { return shift->set("id", shift); }
67 sub parents_id { return shift->set("parents_id", shift); }
71 my($self, $hasvals, $not, @joins)=@_;
72 $not=0 unless(defined($not));
76 @outjoins = @joins = ("INNER");
80 if($self->type() != $TYPE_BOOL)
82 $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
85 $str = "(" . $str . " AND fxt" . scalar(@joins) . ".files_id IS NULL)";
87 return ($str, @outjoins);
90 my $left=$self->left();
91 my $right=$self->right();
92 return ("", @outjoins) unless($left || $right);
94 my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
95 push(@joins, @leftjoins);
96 push(@outjoins, @leftjoins);
102 # if we are ANDing add an inner join
103 # also if we are NOTing, but we are looking for a tag *value*
106 # if right child is a NOT, we don't need extra join/brackets
107 # NOT will do the same and we will end up with an extra one
108 $join= "INNER" unless($right && $right->name() && $right->name() eq "NOT");
113 $join = ($hasvals ? "INNER" : "LEFT");
117 # if the rightmost part of the left sub-expression ends in
118 # NOT, then we need an extra join. This doesn't apply if
119 # (as above) the righthand expression is a NOT.
120 $join="INNER" if(($self->right_ends_in_not($left)) &&
121 !($right && $right->name() && $right->name() eq "NOT"));
126 push(@outjoins, $join);
129 my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
130 push(@outjoins, @rightjoins);
132 $str .= " $op " if($op && !$not);
134 $str=("(" . $str . ")") if($op && $left && $right);
135 return($str, @outjoins);
141 if($self->type() == $TYPE_BOOL)
144 push(@used, $self->left()->used_tags()) if($self->left());
145 push(@used, $self->right()->used_tags()) if($self->right());
146 return(grep { defined; } @used);
151 sub right_ends_in_not
154 return 0 unless($node);
155 my $right=$node->right();
156 if($right && $right->type() == $TYPE_BOOL)
158 return $self->right_ends_in_not($right);
160 my $op=$node->name();
161 return 0 unless($op);
162 return 1 if($op eq "NOT");