X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath%2FNode.pm;h=eb703c2374632292c590ee40563967534d0f33e4;hb=13e50dce4c98b33208f8d5b191e940c46433f61a;hp=472fe417a87a83438b1eb8bac2cd5203934c10b2;hpb=29d3cf8e8ed7ef37f1e746dedeb2c97299ad36f6;p=id3fs.git diff --git a/lib/ID3FS/Path/Node.pm b/lib/ID3FS/Path/Node.pm index 472fe41..eb703c2 100644 --- a/lib/ID3FS/Path/Node.pm +++ b/lib/ID3FS/Path/Node.pm @@ -1,8 +1,30 @@ +# id3fs - a FUSE-based filesystem for browsing audio metadata +# Copyright (C) 2010 Ian Beckwith +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + package ID3FS::Path::Node; use strict; use warnings; +require Exporter; +use vars qw(@ISA @EXPORT $TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE); +@ISA=qw(Exporter); +@EXPORT=qw($TYPE_BOOL $TYPE_TAG $TYPE_ARTIST $TYPE_ALBUM $TYPE_FILE); +($TYPE_BOOL, $TYPE_TAG, $TYPE_ARTIST, $TYPE_ALBUM, $TYPE_FILE)=(1..5); + sub new { my $proto=shift; @@ -10,10 +32,20 @@ sub new my $self={}; bless($self,$class); - $self->left(shift); - $self->op(shift); - $self->right(shift); - + my $db=shift; + $self->{type}=shift; + $self->{name}=shift; + $self->{parents_id}=shift; + if($self->{type} != $TYPE_BOOL) + { + my $table=''; + if ($self->{type} == $TYPE_TAG) { $table="tags"; } + elsif($self->{type} == $TYPE_ARTIST) { $table="artists"; } + elsif($self->{type} == $TYPE_ALBUM) { $table="albums"; } + elsif($self->{type} == $TYPE_FILE) { $table="files"; } + $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id}); + return undef unless(defined($self->{id})); + } return $self; } @@ -27,133 +59,120 @@ sub set return $self->{$name}; } -sub left { return shift->set("left", shift); } -sub right { return shift->set("right", shift); } -sub op { return shift->set("op", shift); } - -sub print -{ - my($self)=@_; - my $op=$self->op(); - my $left=$self->left(); - my $right=$self->right(); - return "" unless($left || $right); - my $str .= $self->print_node($left); - $str .= (" " . $op->{name} . " ") if($op); - $str .= $self->print_node($right); - if($op || ($left && $right)) - { - $str="(" . $str . ")"; - } - return $str; -} - -sub print_node -{ - my($self, $node)=@_; - return "" unless(defined($node)); - return $node->print() if(ref($node) eq "ID3FS::Path::Node"); - return $node->{name}; -} +sub left { return shift->set("left", shift); } +sub right { return shift->set("right", shift); } +sub name { return shift->set("name", shift); } +sub type { return shift->set("type", shift); } +sub id { return shift->set("id", shift); } +sub parents_id { return shift->set("parents_id", shift); } sub to_sql { - my($self, $not, @joins)=@_; + my($self, $hasvals, $not, @joins)=@_; $not=0 unless(defined($not)); my @outjoins=(); + my $str=''; # init unless(@joins) { @outjoins = @joins = ("INNER"); } + + if($self->type() != $TYPE_BOOL) + { + my $cnt=scalar(@joins)+1; + $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'"; + if($not && !$hasvals) + { + $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL"; + } + return ($str, @outjoins); + } + my $left=$self->left(); my $right=$self->right(); return ("", @outjoins) unless($left || $right); - my ($leftstr, @leftjoins) = $self->node_to_sql($left, $not, @joins); + my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left); push(@joins, @leftjoins); push(@outjoins, @leftjoins); - my $op=$self->op(); + my $op=$self->name(); + print "op: $op type: ", $self->type(), " not: $not\n"; if(defined($op)) { - if($op->{name} eq "AND") + # if we are ANDing, add an inner join + # also if we are NOTing, but we are looking for a tag *value* + if($op eq "AND") { - push(@joins, "INNER"); - push(@outjoins, "INNER"); + print "AND\n"; + # hack - if right child is a NOT, we don't need extra join/brackets + # NOT will do the same and we will end up with an extra one + unless($right && $right->name() && $right->name() eq "NOT") + { + push(@joins, "INNER"); + push(@outjoins, "INNER"); + } } - elsif($op->{name} eq "NOT") + elsif($op eq "NOT") { + print "NOT (was $not)\n"; $not=1; - push(@joins, "LEFT"); - push(@outjoins, "LEFT"); -# print("LEFT: ", $left->print(), "\n") if ($left); -# print("RIGHT: ", $right->print(), "\n") if($right); + # as above - if right child is a NOT, we don't need extra join/brackets + # NOT will do the same and we will end up with an extra one + unless($right && $right->name() && $right->name() eq "NOT") + { + if($hasvals) + { + push(@joins, "INNER"); + push(@outjoins, "INNER"); + } + else + { + push(@joins, "LEFT"); + push(@outjoins, "LEFT"); + } + } + } + elsif($op eq "OR") + { + print "OR\n"; + # if left child is a NOT, we need an extra (inner) join + # unless right child is also a NOT + if(($left && $left->name() && $left->name() eq "NOT") && + !($right && $right->name() && $right->name() eq "NOT")) + { + push(@joins, "INNER"); + push(@outjoins, "INNER"); + } } } - my ($rightstr, @rightjoins) = $self->node_to_sql($right, $not, @joins); + my ($rightstr, @rightjoins) = $right->to_sql($hasvals, $not, @joins) if($right); push(@outjoins, @rightjoins); # print "LEFT (", scalar(@leftjoins), "): $leftstr\n"; # print "RIGHT (", scalar(@rightjoins), "): $rightstr\n"; - my $str=$leftstr; - $str .= (" " . $op->{name} . " ") if($op && !$not); + $str=$leftstr; + $str .= " $op " if($op && !$not); $str .= $rightstr; if($op || ($left && $right)) { $str="(" . $str . ")"; } # print "STR: $str\n"; +# my @all=(@joins, @rightjoins); +# print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n"; return($str, @outjoins); } -sub node_to_sql -{ - my($self, $node, $not, @joins)=@_; - return ("", ()) unless(defined($node)); - return $node->to_sql($not, @joins) if(ref($node) eq "ID3FS::Path::Node"); - my $sql; - my $cnt=scalar(@joins)+1; - if(defined($node->{parents_id})) - { - $sql= "(t" . scalar(@joins) . ".parents_id='$node->{parents_id}'"; - $sql .= " AND t" . scalar(@joins) . ".id='" . $node->{id} . "'"; - } - else - { - $sql= "(t" . scalar(@joins) .".parents_id=''"; - $sql .= " AND t" . scalar(@joins) . ".id='" . $node->{id} . "'"; - } - if($not) - { - $sql .= " AND fxt" . scalar(@joins) . ".files_id IS NULL"; - } - $sql .= ")"; - return ($sql, ()); -} - sub used_tags { my($self)=@_; - my @used=(grep { defined; } ($self->node_used_tags($self->left()), - $self->node_used_tags($self->right()))); - return(@used); -} - -sub node_used_tags -{ - my($self, $node)=@_; - return (undef) unless(defined($node)); - return $node->used_tags() if(ref($node) eq "ID3FS::Path::Node"); - if(defined($node->{parents_id})) + if($self->type() == $TYPE_BOOL) { - return([ $node->{parents_id}, $node->{id} ]); + my @used=(); + push(@used, $self->left()->used_tags()) if($self->left()); + push(@used, $self->right()->used_tags()) if($self->right()); + return(grep { defined; } @used); } - return $node->{id}; -} - - -sub max -{ - my($self, $a, $b)=@_; - return(($a > $b) ? $a : $b); + return $self->id(); } 1;