3775d384d4a0472d7a06d011ebd52aa0273fdf93
[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 to_sql
61 {
62     my($self, $hasvals, $not, @joins)=@_;
63     $not=0 unless(defined($not));
64     my @outjoins=();
65     my $str='';
66     # init
67     unless(@joins)
68     {
69         @outjoins = @joins = ("INNER");
70     }
71
72     if($self->type() ne "boolean")
73     {
74         my $cnt=scalar(@joins)+1;
75         $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
76         if($not && !$hasvals)
77         {
78             $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
79         }
80         return ($str, @outjoins);
81     }
82
83     my $left=$self->left();
84     my $right=$self->right();
85     return ("", @outjoins) unless($left || $right);
86     my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
87     push(@joins, @leftjoins);
88     push(@outjoins, @leftjoins);
89     my $op=$self->name();
90     print "op: $op type: ", $self->type(), " not: $not\n";
91     if(defined($op))
92     {
93         # if we are ANDing, add an inner join
94         # also if we are NOTing, but we are looking for a tag *value*
95         if($op eq "AND")
96         {
97             print "AND\n";
98             # hack - if right child is a NOT, we don't need extra join/brackets
99             # NOT will do the same and we will end up with an extra one
100             unless($right && $right->name() && $right->name() eq "NOT")
101             {
102                 push(@joins, "INNER");
103                 push(@outjoins, "INNER");
104             }
105         }
106         elsif($op eq "NOT")
107         {
108             print "NOT (was $not)\n";
109             $not=1;
110             # as above - if right child is a NOT, we don't need extra join/brackets
111             # NOT will do the same and we will end up with an extra one
112             unless($right && $right->name() && $right->name() eq "NOT")
113             {
114                 if($hasvals)
115                 {
116                     push(@joins, "INNER");
117                     push(@outjoins, "INNER");
118                 }
119                 else
120                 {
121                     push(@joins, "LEFT");
122                     push(@outjoins, "LEFT");
123                 }
124             }
125         }
126         elsif($op eq "OR")
127         {
128             print "OR\n";
129             # if left child is a NOT, we need an extra (inner) join
130             # unless right child is also a NOT
131             if(($left && $left->name() && $left->name() eq "NOT") &&
132                !($right && $right->name() && $right->name() eq "NOT"))
133             {
134                 push(@joins, "INNER");
135                 push(@outjoins, "INNER");
136             }
137         }
138     }
139     my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
140     push(@outjoins, @rightjoins);
141 #    print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
142 #    print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
143     $str=$leftstr;
144     $str .= " $op " if($op && !$not);
145     $str .= $rightstr;
146     if($op || ($left && $right))
147     {
148         $str="(" . $str . ")";
149     }
150 #    print "STR: $str\n";
151 #    my @all=(@joins, @rightjoins);
152 #    print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
153     return($str, @outjoins);
154 }
155
156 sub used_tags
157 {
158     my($self)=@_;
159     if($self->type() eq "boolean")
160     {
161         my @used=();
162         push(@used, $self->left()->used_tags())  if($self->left());
163         push(@used, $self->right()->used_tags()) if($self->right());
164         return(grep { defined; } @used);
165     }
166     return $self->id();
167 }
168
169 1;