fix /NOT
authorIan Beckwith <ianb@erislabs.net>
Tue, 12 Oct 2010 08:21:03 +0000 (09:21 +0100)
committerIan Beckwith <ianb@erislabs.net>
Tue, 12 Oct 2010 08:21:03 +0000 (09:21 +0100)
lib/ID3FS/Path.pm
lib/ID3FS/Path/Node.pm

index ea22065..0f3241c 100644 (file)
@@ -416,8 +416,7 @@ sub sort_elements
 sub used_tags
 {
     my($self)=@_;
-    print "TAGTREE UNDEF\n" unless(defined($self->{tagtree}));
-    return undef unless(defined($self->{tagtree}));
+    return() unless(defined($self->{tagtree}));
     return($self->{tagtree}->used_tags());
 }
 
@@ -471,7 +470,7 @@ sub tail_parent
 sub tags
 {
     my($self)=@_;
-    if(!@{$self->{elements}}) # /
+    if(!$self->{tagtree}) # / or /NOT
     {
        my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
        return($self->{db}->cmd_firstcol($sql));
@@ -486,7 +485,7 @@ sub tags
             ") AS subselect\n" .
             "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
             "INNER JOIN tags ON files_x_tags.tags_id=tags.id\n");
-    my (@allused)=$self->used_tags();
+    my @allused=$self->used_tags();
     my @used=grep { ref($_) ne "ARRAY"; } @allused;
     my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused;
 #    print "tags(): USED: ", join(", ", @used), "\n";
@@ -722,7 +721,7 @@ sub tags_subselect
 #      print "Trailing id: $tag\n";
     }
     my ($sqlclause, @joins)=(undef, ());
-    ($sqlclause, @joins) = $tree->to_sql($tag) if($tree);
+    ($sqlclause, @joins) = $tree->to_sql() if($tree);
 #    print "SQL(" . scalar(@joins) .": $sqlclause\n";
     my $sql="\tSELECT fxt1.files_id FROM tags t1";
     my @crosses=();
index 11e2de3..68515e3 100644 (file)
@@ -58,7 +58,8 @@ sub print_node
 
 sub to_sql
 {
-    my($self, $parent, @joins)=@_;
+    my($self, $not, @joins)=@_;
+    $not=0 unless(defined($not));
     my @outjoins=();
     # init
     unless(@joins)
@@ -68,7 +69,7 @@ sub to_sql
     my $left=$self->left();
     my $right=$self->right();
     return ("", @outjoins) unless($left || $right);
-    my ($leftstr, @leftjoins) = $self->node_to_sql($left, $parent, @joins);
+    my ($leftstr, @leftjoins) = $self->node_to_sql($left, $not, @joins);
     push(@joins, @leftjoins);
     push(@outjoins, @leftjoins);
     my $op=$self->op();
@@ -81,16 +82,19 @@ sub to_sql
        }
        elsif($op->{name} eq "NOT")
        {
+           $not=1;
            push(@joins, "LEFT");
            push(@outjoins, "LEFT");
+           print("LEFT: ", $left->print(), "\n") if ($left);
+           print("RIGHT: ", $right->print(), "\n") if($right);
        }
     }
-    my ($rightstr, @rightjoins) = $self->node_to_sql($right, $parent, @joins);
+    my ($rightstr, @rightjoins) = $self->node_to_sql($right, $not, @joins);
     push(@outjoins, @rightjoins);
 #    print "LEFT (", scalar(@leftjoins), "): $leftstr\n";
 #    print "RIGHT (", scalar(@rightjoins), "): $rightstr\n";
     my $str=$leftstr;
-    $str .= (" " . $op->{name} . " ") if($op);
+    $str .= (" " . $op->{name} . " ") if($op && !$not);
     $str .= $rightstr;
     if($op || ($left && $right))
     {
@@ -102,21 +106,26 @@ sub to_sql
 
 sub node_to_sql
 {
-    my($self, $node, $parent, @joins)=@_;
+    my($self, $node, $not, @joins)=@_;
     return ("", ()) unless(defined($node));
-    return $node->to_sql($parent, @joins) if(ref($node) eq "ID3FS::Path::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 fxt" . scalar(@joins) . ".tags_id='" . $node->{id} . "')";
+       $sql .= " AND fxt" . scalar(@joins) . ".tags_id='" . $node->{id} . "'";
     }
     else
     {
        $sql= "(t" . scalar(@joins) .".parents_id=''";
-       $sql .= " AND fxt" . scalar(@joins) . ".tags_id='" . $node->{id} . "')";
+       $sql .= " AND fxt" . scalar(@joins) . ".tags_id='" . $node->{id} . "'";
+    }
+    if($not)
+    {
+       $sql .= " AND fxt" . scalar(@joins) . ".files_id IS NULL";
     }
+    $sql .= ")";
     return ($sql, ());
 }