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);
93 my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
94 push(@joins, @leftjoins);
95 push(@outjoins, @leftjoins);
97 print "op: $op type: ", $self->type(), " not: $not\n";
100 # if we are ANDing, add an inner join
101 # also if we are NOTing, but we are looking for a tag *value*
105 # hack - if right child is a NOT, we don't need extra join/brackets
106 # NOT will do the same and we will end up with an extra one
107 unless($right && $right->name() && $right->name() eq "NOT")
109 push(@joins, "INNER");
110 push(@outjoins, "INNER");
115 print "NOT (was $not)\n";
117 # as above - if right child is a NOT, we don't need extra join/brackets
118 # NOT will do the same and we will end up with an extra one
119 unless($right && $right->name() && $right->name() eq "NOT")
123 push(@joins, "INNER");
124 push(@outjoins, "INNER");
128 push(@joins, "LEFT");
129 push(@outjoins, "LEFT");
136 # if left child is a NOT, we need an extra (inner) join
137 # unless right child is also a NOT
138 if(($left && $left->name() && $left->name() eq "NOT") &&
139 !($right && $right->name() && $right->name() eq "NOT"))
141 push(@joins, "INNER");
142 push(@outjoins, "INNER");
146 my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
147 push(@outjoins, @rightjoins);
148 # print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
149 # print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
151 $str .= " $op " if($op && !$not);
153 if($op && $left && $right)
155 $str="(" . $str . ")";
157 # print "STR: $str\n";
158 # my @all=(@joins, @rightjoins);
159 # print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
160 return($str, @outjoins);
166 if($self->type() == $TYPE_BOOL)
169 push(@used, $self->left()->used_tags()) if($self->left());
170 push(@used, $self->right()->used_tags()) if($self->right());
171 return(grep { defined; } @used);