Node::to_sql: NOT: only return an extra join, don't pass it to $right->to_sql()
[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 require Exporter;
23 use vars qw(@ISA @EXPORT $TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE);
24 @ISA=qw(Exporter);
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);
27
28 sub new
29 {
30     my $proto=shift;
31     my $class=ref($proto) || $proto;
32     my $self={};
33     bless($self,$class);
34
35     my $db=shift;
36     $self->{type}=shift;
37     $self->{name}=shift;
38     $self->{parents_id}=shift;
39     if($self->{type} != $TYPE_BOOL)
40     {
41         my $table='';
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}));
48     }
49     return $self;
50 }
51
52 sub set
53 {
54     my($self, $name, $val)=@_;
55     if(defined($val))
56     {
57         $self->{$name}=$val;
58     }
59     return $self->{$name};
60 }
61
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); }
68
69 sub to_sql
70 {
71     my($self, $hasvals, $not, @joins)=@_;
72     $not=0 unless(defined($not));
73     my @outjoins=();
74     unless(@joins)
75     {
76         @outjoins = @joins = ("INNER");
77     }
78     my $str='';
79
80     if($self->type() != $TYPE_BOOL)
81     {
82         print $self->{id}, " = ", $self->{name}, "\n";
83         $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
84         if($not && !$hasvals)
85         {
86             $str = "(" . $str . " AND fxt" . scalar(@joins) . ".files_id IS NULL)";
87         }
88         return ($str, @outjoins);
89     }
90
91     my $left=$self->left();
92     my $right=$self->right();
93     return ("", @outjoins) unless($left || $right);
94
95     my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left);
96     push(@joins, @leftjoins);
97     push(@outjoins, @leftjoins);
98
99     my $op=$self->name();
100     if(defined($op))
101     {
102         # if we are ANDing add an inner join
103         if($op eq "AND")
104         {
105             # 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")
108 #           {
109                 push(@joins, "INNER");
110                 push(@outjoins, "INNER");
111 #           }
112         }
113         elsif($op eq "NOT")
114         {
115             $not=1;
116             # return an extra join, but don't pass it down to $right->to_sql
117             # if we are looking for a tag *value*, use INNER join rather than LEFT
118             push(@outjoins, ($hasvals ? "INNER" : "LEFT"));
119         }
120     }
121     my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right);
122     push(@outjoins, @rightjoins);
123     $str = $leftstr;
124     $str .= " $op " if($op && !$not);
125     $str .= $rightstr;
126     $str=("(" . $str . ")") if($op && $left && $right);
127 #    print "LEFT (", scalar(@$leftjoins), "): ";
128 #    print $leftstr if $leftstr;
129 #    print "\n";
130 #    print "OP: $op\n" if $op;
131 #    print "RIGHT (", scalar(@$rightjoins), "): ";
132 #    print $rightstr if $rightstr;
133 #    print "\n";
134     print "OUT(", scalar(@outjoins), "): $str\n";
135     return($str, @outjoins);
136 }
137
138 sub used_tags
139 {
140     my($self)=@_;
141     if($self->type() == $TYPE_BOOL)
142     {
143         my @used=();
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);
147     }
148     return $self->id();
149 }
150
151 # does the bottom right-most expression end in a NOT?
152 sub right_ends_in_not
153 {
154     my($self, $node)=@_;
155     return 0 unless($node);
156     my $right=$node->right();
157     if($right && $right->type() == $TYPE_BOOL)
158     {
159         return $self->right_ends_in_not($right);
160     }
161     my $op=$node->name();
162     return 0 unless($op);
163     return 1 if($op eq "NOT");
164     return 0;
165 }
166
167 1;