sql formatting tweaks
[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         $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'";
83         if($not && !$hasvals)
84         {
85             $str = "(" . $str . " AND fxt" . scalar(@joins) . ".files_id IS NULL)";
86         }
87         return ($str, @outjoins);
88     }
89
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);
96     my $op=$self->name();
97     print "op: $op type: ", $self->type(), " not: $not\n";
98     if(defined($op))
99     {
100         # if we are ANDing, add an inner join
101         # also if we are NOTing, but we are looking for a tag *value*
102         if($op eq "AND")
103         {
104             print "AND\n";
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")
108             {
109                 push(@joins, "INNER");
110                 push(@outjoins, "INNER");
111             }
112         }
113         elsif($op eq "NOT")
114         {
115             print "NOT (was $not)\n";
116             $not=1;
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")
120             {
121                 if($hasvals)
122                 {
123                     push(@joins, "INNER");
124                     push(@outjoins, "INNER");
125                 }
126                 else
127                 {
128                     push(@joins, "LEFT");
129                     push(@outjoins, "LEFT");
130                 }
131             }
132         }
133         elsif($op eq "OR")
134         {
135             print "OR\n";
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"))
140             {
141                 push(@joins, "INNER");
142                 push(@outjoins, "INNER");
143             }
144         }
145     }
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";
150     $str=$leftstr;
151     $str .= " $op " if($op && !$not);
152     $str .= $rightstr;
153     if($op && $left && $right)
154     {
155         $str="(" . $str . ")";
156     }
157 #    print "STR: $str\n";
158 #    my @all=(@joins, @rightjoins);
159 #    print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n";
160     return($str, @outjoins);
161 }
162
163 sub used_tags
164 {
165     my($self)=@_;
166     if($self->type() == $TYPE_BOOL)
167     {
168         my @used=();
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);
172     }
173     return $self->id();
174 }
175
176 1;