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;
25 my $class=ref($proto) || $proto;
32 $self->{parents_id}=shift;
33 if($self->{type} ne "boolean")
35 my $table=ucfirst($self->{type});
36 $table .= "s" unless($table=~/s$/);
37 $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id});
38 return undef unless(defined($self->{id}));
45 my($self, $name, $val)=@_;
50 return $self->{$name};
53 sub left { return shift->set("left", shift); }
54 sub right { return shift->set("right", shift); }
55 sub name { return shift->set("name", shift); }
56 sub type { return shift->set("type", shift); }
57 sub id { return shift->set("id", shift); }
58 sub parents_id { return shift->set("parents_id", shift); }
64 my $left=$self->left();
65 my $right=$self->right();
66 return "" unless($left || $right);
67 my $str .= $self->print_node($left);
68 $str .= (" " . $op . " ") if($op);
69 $str .= $self->print_node($right);
70 if($op || ($left && $right))
72 $str="(" . $str . ")";
80 return "" unless(defined($node));
81 return $node->print() if(ref($node) eq "ID3FS::Path::Node");
87 my($self, $hasvals, $not, @joins)=@_;
88 $not=0 unless(defined($not));
94 @outjoins = @joins = ("INNER");
96 if($self->type() ne "boolean")
98 my $cnt=scalar(@joins)+1;
99 $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
100 if($not && !$hasvals)
102 $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
104 return ($str, @outjoins);
108 my $left=$self->left();
109 my $right=$self->right();
110 return ("", @outjoins) unless($left || $right);
111 my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
112 push(@joins, @leftjoins);
113 push(@outjoins, @leftjoins);
114 my $op=$self->name();
115 if(defined($op) && $self->type() eq "boolean")
117 # if we are ANDing, add an inner join
118 # also if we are NOTing, but we are looking for a tag *value*
119 if( ($op eq "AND") || ($hasvals && ($op eq "NOT")))
121 # hack - if right child is a NOT, we don't need extra join/brackets
122 # NOT will do the same and we will end up with an extra one
123 unless($right && $right->name() && $right->name() eq "NOT")
125 push(@joins, "INNER");
126 push(@outjoins, "INNER");
132 push(@joins, "LEFT");
133 push(@outjoins, "LEFT");
134 # print("LEFT: ", $left->print(), "\n") if ($left);
135 # print("RIGHT: ", $right->print(), "\n") if($right);
138 my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
139 push(@outjoins, @rightjoins);
140 # print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
141 # print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
143 $str .= " $op " if($op && !$not);
145 if($op || ($left && $right))
147 $str="(" . $str . ")";
149 # print "STR: $str\n";
150 return($str, @outjoins);
157 if($self->type() eq "boolean")
160 push(@used, $self->left()->used_tags()) if($self->left());
161 push(@used, $self->right()->used_tags()) if($self->right());
162 return(grep { defined; } @used);