X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2FID3FS%2FPath%2FNode.pm;h=3775d384d4a0472d7a06d011ebd52aa0273fdf93;hb=e4fb6406212b7adb92d5963ae1bc156f0d43e46d;hp=9fc4570a91a1ff3dde4df154a647595dcc9653e9;hpb=8c6c420ca9104a94e92f470ef7cca8b4ef242ab5;p=id3fs.git diff --git a/lib/ID3FS/Path/Node.pm b/lib/ID3FS/Path/Node.pm index 9fc4570..3775d38 100644 --- a/lib/ID3FS/Path/Node.pm +++ b/lib/ID3FS/Path/Node.pm @@ -1,3 +1,19 @@ +# 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; @@ -10,10 +26,17 @@ 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} ne "boolean") + { + my $table=ucfirst($self->{type}); + $table .= "s" unless($table=~/s$/); + $self->{id}=$db->lookup_id($table, $self->{name}, $self->{parents_id}); + return undef unless(defined($self->{id})); + } return $self; } @@ -27,94 +50,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 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 print +sub to_sql { - 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)) + my($self, $hasvals, $not, @joins)=@_; + $not=0 unless(defined($not)); + my @outjoins=(); + my $str=''; + # init + unless(@joins) { - $str="(" . $str . ")"; + @outjoins = @joins = ("INNER"); } - 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}; -} + if($self->type() ne "boolean") + { + 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); + } -sub to_sql -{ - my($self, $andlevel, $parent_is_tag)=@_; - $andlevel=1 unless(defined($andlevel)); - $parent_is_tag=0 unless(defined($parent_is_tag)); - my ($leftandlevel, $rightandlevel); - my ($leftstr, $rightstr); - my $op=$self->op(); my $left=$self->left(); my $right=$self->right(); - return ("", $andlevel) unless($left || $right); - ($leftstr, $leftandlevel) = $self->node_to_sql($left, $andlevel); - $andlevel=$self->max($andlevel, $leftandlevel); - if(defined($op) && (($op->{name} eq "AND") || ($op->{name} eq "NOT"))) + return ("", @outjoins) unless($left || $right); + my ($leftstr, @leftjoins) = $left->to_sql($hasvals, $not, @joins) if($left); + push(@joins, @leftjoins); + push(@outjoins, @leftjoins); + my $op=$self->name(); + print "op: $op type: ", $self->type(), " not: $not\n"; + if(defined($op)) { - $andlevel++; + # 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") + { + 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 eq "NOT") + { + print "NOT (was $not)\n"; + $not=1; + # 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"); + } + } } - ($rightstr, $rightandlevel) = $self->node_to_sql($right, $andlevel); - my $str=$leftstr; - $str .= (" " . $op->{name} . " ") if($op); + 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"; + $str=$leftstr; + $str .= " $op " if($op && !$not); $str .= $rightstr; if($op || ($left && $right)) { $str="(" . $str . ")"; } - return($str, $self->max($leftandlevel, $rightandlevel)); -} - -sub node_to_sql -{ - my($self, $node, $andlevel)=@_; - return ("", $andlevel) unless(defined($node)); - return $node->to_sql($andlevel) if(ref($node) eq "ID3FS::Path::Node"); - return( ( "fxt${andlevel}.tags_id=\"" . $node->{id} . "\""), $andlevel); +# print "STR: $str\n"; +# my @all=(@joins, @rightjoins); +# print "JOINS: RETURN ", scalar(@outjoins), " ALL ", scalar(@all), "\n"; + return($str, @outjoins); } sub used_tags { my($self)=@_; - my @used=(grep { defined; } ($self->node_used_tags($self->left()), - $self->node_used_tags($self->right()))); - print "used_tags: ", join(", ", @used), "\n"; - 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"); - print $node->{id}, "\n"; - return($node->{id}); -} - - -sub max -{ - my($self, $a, $b)=@_; - return(($a > $b) ? $a : $b); + if($self->type() eq "boolean") + { + 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 $self->id(); } 1;