b34309008ca9fb12e43009a4fd81dea209df1be9
[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     $self->{table}=1;
40     $self->{hasvals}=undef;
41     if($self->{type} != $TYPE_BOOL)
42     {
43         my $table='';
44         if   ($self->{type} == $TYPE_TAG)    { $table="tags";    }
45         elsif($self->{type} == $TYPE_ARTIST) { $table="artists"; }
46         elsif($self->{type} == $TYPE_ALBUM)  { $table="albums";  }
47         elsif($self->{type} == $TYPE_FILE)   { $table="files";   }
48         $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id});
49         return undef unless(defined($self->{id}));
50     }
51     return $self;
52 }
53
54 sub set
55 {
56     my($self, $name, $val)=@_;
57     if(defined($val))
58     {
59         $self->{$name}=$val;
60     }
61     return $self->{$name};
62 }
63
64 sub left       { return shift->set("left",       shift); }
65 sub right      { return shift->set("right",      shift); }
66 sub name       { return shift->set("name",       shift); }
67 sub type       { return shift->set("type",       shift); }
68 sub id         { return shift->set("id",         shift); }
69 sub parents_id { return shift->set("parents_id", shift); }
70 sub table      { return shift->set("table",      shift); }
71 sub hasvals    { return shift->set("hasvals",    shift); }
72
73 sub to_sql
74 {
75     my($self, $not)=@_;
76     $not=0 unless(defined($not));
77     my $str='';
78
79     if($self->type() != $TYPE_BOOL)
80     {
81         $str .= "t" . $self->table() . ".id='" . $self->{id} . "'";
82         if($not && !$self->hasvals())
83         {
84             $str = "(" . $str . " AND fxt" . $self->table() . ".files_id IS NULL)";
85         }
86         return ($str);
87     }
88
89     my $left=$self->left();
90     my $right=$self->right();
91     return ("") unless($left || $right);
92
93     my $leftstr = $left->to_sql($not) if($left);
94     my $op=$self->name();
95     $not=1 if(defined($op) && ($op eq "NOT"));
96     my $rightstr = $right->to_sql($not) if($right);
97     $str = $leftstr;
98     $str .= " $op " if($op && !$not);
99     $str .= $rightstr;
100     $str=("(" . $str . ")") if($op && $left && $right);
101     return($str);
102 }
103
104 sub used_tags
105 {
106     my($self)=@_;
107     if($self->type() == $TYPE_BOOL)
108     {
109         my @used=();
110         push(@used, $self->left()->used_tags())  if($self->left());
111         push(@used, $self->right()->used_tags()) if($self->right());
112         return(grep { defined; } @used);
113     }
114     return $self->id();
115 }
116
117 # does the bottom right-most expression end in a NOT?
118 sub right_ends_in_not
119 {
120     my($self, $node)=@_;
121     return 0 unless($node);
122     my $right=$node->right();
123     if($right && $right->type() == $TYPE_BOOL)
124     {
125         return $self->right_ends_in_not($right);
126     }
127     my $op=$node->name();
128     return 0 unless($op);
129     return 1 if($op eq "NOT");
130     return 0;
131 }
132
133 1;