add copyright/license headers
[id3fs.git] / lib / ID3FS / Path / Node.pm
1 # id3fs - a FUSE-based filesystem for browsing audio metadata
2 # Copyright (C) 2010  Ian Beckwith <ianb@erislabs.net>
3 #
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.
8 #
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.
13 #
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/>.
16
17 package ID3FS::Path::Node;
18
19 use strict;
20 use warnings;
21
22 sub new
23 {
24     my $proto=shift;
25     my $class=ref($proto) || $proto;
26     my $self={};
27     bless($self,$class);
28
29     my $db=shift;
30     $self->{type}=shift;
31     $self->{name}=shift;
32     $self->{parents_id}=shift;
33     if($self->{type} ne "boolean")
34     {
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}));
39     }
40     return $self;
41 }
42
43 sub set
44 {
45     my($self, $name, $val)=@_;
46     if(defined($val))
47     {
48         $self->{$name}=$val;
49     }
50     return $self->{$name};
51 }
52
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); }
59
60 sub print
61 {
62     my($self)=@_;
63     my $op=$self->name();
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))
71     {
72         $str="(" . $str . ")";
73     }
74     return $str;
75 }
76
77 sub print_node
78 {
79     my($self, $node)=@_;
80     return "" unless(defined($node));
81     return $node->print() if(ref($node) eq "ID3FS::Path::Node");
82     return $node->{name};
83 }
84
85 sub to_sql
86 {
87     my($self, $hasvals, $not, @joins)=@_;
88     $not=0 unless(defined($not));
89     my @outjoins=();
90     my $str='';
91     # init
92     unless(@joins)
93     {
94         @outjoins = @joins = ("INNER");
95     }
96     if($self->type() ne "boolean")
97     {
98         my $cnt=scalar(@joins)+1;
99         $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
100         if($not && !$hasvals)
101         {
102             $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
103         }
104         return ($str, @outjoins);
105     }
106     else
107     {
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")
116         {
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")))
120             {
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")
124                 {
125                     push(@joins, "INNER");
126                     push(@outjoins, "INNER");
127                 }
128             }
129             elsif($op eq "NOT")
130             {
131                 $not=1;
132                 push(@joins, "LEFT");
133                 push(@outjoins, "LEFT");
134 #               print("LEFT: ", $left->print(), "\n") if ($left);
135 #               print("RIGHT: ", $right->print(), "\n") if($right);
136             }
137         }
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";
142         my $str=$leftstr;
143         $str .= " $op " if($op && !$not);
144         $str .= $rightstr;
145         if($op || ($left && $right))
146         {
147             $str="(" . $str . ")";
148         }
149 #       print "STR: $str\n";
150         return($str, @outjoins);
151     }
152 }
153
154 sub used_tags
155 {
156     my($self)=@_;
157     if($self->type() eq "boolean")
158     {
159         my @used=();
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);
163     }
164     return $self->id();
165 }
166
167 1;