From: Ian Beckwith Date: Mon, 18 Oct 2010 14:45:19 +0000 (+0100) Subject: Node: merge to_sql and node_to_sql X-Git-Tag: debian/1.0-1~57 X-Git-Url: http://erislabs.net/gitweb/?p=id3fs.git;a=commitdiff_plain;h=e24d1a1f6189d3088c8610472c5aff9fc8cd1be6 Node: merge to_sql and node_to_sql --- diff --git a/lib/ID3FS/Path.pm b/lib/ID3FS/Path.pm index e3aac80..e81f585 100644 --- a/lib/ID3FS/Path.pm +++ b/lib/ID3FS/Path.pm @@ -678,7 +678,10 @@ sub tags_subselect return "\tSELECT id FROM files AS files_id\n"; } my $tree=$self->{tagtree}; + # FIXME print "UNDEF!!\n" unless($self->{tagtree}); + use Data::Dumper; + print Dumper $self->{tagtree}; my $parent=$self->trailing_tag_parent(); # print "ELEMENTS: ", join('/', map { $_->{name}; } @{$self->{elements}}), "\n"; @@ -691,7 +694,7 @@ sub tags_subselect } my ($sqlclause, @joins)=(undef, ()); ($sqlclause, @joins) = $tree->to_sql($hasvals) if($tree); -# print "SQL(" . scalar(@joins) .": $sqlclause\n"; + print "SQL(" . scalar(@joins) .": $sqlclause\n"; my $sql="\tSELECT fxt1.files_id FROM tags t1"; my @crosses=(); my @inners=(); diff --git a/lib/ID3FS/Path/Node.pm b/lib/ID3FS/Path/Node.pm index dd650e4..a08a5f8 100644 --- a/lib/ID3FS/Path/Node.pm +++ b/lib/ID3FS/Path/Node.pm @@ -71,69 +71,68 @@ sub to_sql my($self, $hasvals, $not, @joins)=@_; $not=0 unless(defined($not)); my @outjoins=(); + my $str=''; # init unless(@joins) { @outjoins = @joins = ("INNER"); } - my $left=$self->left(); - my $right=$self->right(); - return ("", @outjoins) unless($left || $right); - my ($leftstr, @leftjoins) = $self->node_to_sql($left, $hasvals, $not, @joins); - push(@joins, @leftjoins); - push(@outjoins, @leftjoins); - my $op=$self->name(); - if(defined($op) && $self->type() eq "boolean") + if($self->type() ne "boolean") { - # 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") || ($hasvals && ($op eq "NOT"))) + my $cnt=scalar(@joins)+1; + $str .= "t" . scalar(@joins) . ".id='" . $self->{id} . "'"; + if($not && !$hasvals) { - # 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") + $str .= " AND fxt" . scalar(@joins) . ".files_id IS NULL"; + } + return ($str, @outjoins); + } + else + { + my $left=$self->left(); + my $right=$self->right(); + 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(); + if(defined($op) && $self->type() eq "boolean") + { + # 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") || ($hasvals && ($op eq "NOT"))) + { + # 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") { - push(@joins, "INNER"); - push(@outjoins, "INNER"); + $not=1; + push(@joins, "LEFT"); + push(@outjoins, "LEFT"); +# print("LEFT: ", $left->print(), "\n") if ($left); +# print("RIGHT: ", $right->print(), "\n") if($right); } } - elsif($op eq "NOT") + 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 " if($op && !$not); + $str .= $rightstr; + if($op || ($left && $right)) { - $not=1; - push(@joins, "LEFT"); - push(@outjoins, "LEFT"); -# print("LEFT: ", $left->print(), "\n") if ($left); -# print("RIGHT: ", $right->print(), "\n") if($right); + $str="(" . $str . ")"; } +# print "STR: $str\n"; + return($str, @outjoins); } - my ($rightstr, @rightjoins) = $self->node_to_sql($right, $hasvals, $not, @joins); - push(@outjoins, @rightjoins); -# print "LEFT (", scalar(@leftjoins), "): $leftstr\n"; -# print "RIGHT (", scalar(@rightjoins), "): $rightstr\n"; - my $str=$leftstr; - $str .= " $op " if($op && !$not); - $str .= $rightstr; - if($op || ($left && $right)) - { - $str="(" . $str . ")"; - } -# print "STR: $str\n"; - return($str, @outjoins); -} - -sub node_to_sql -{ - my($self, $node, $hasvals, $not, @joins)=@_; - return ("", ()) unless(defined($node)); - return $node->to_sql($hasvals, $not, @joins) if($node->type() eq "boolean"); - my $sql; - my $cnt=scalar(@joins)+1; - $sql .= "t" . scalar(@joins) . ".id='" . $node->{id} . "'"; - if($not && !$hasvals) - { - $sql .= " AND fxt" . scalar(@joins) . ".files_id IS NULL"; - } - return ($sql, ()); } sub used_tags