pass path to db methods, not just elements
[id3fs.git] / lib / ID3FS / DB.pm
index 6424e8c..50d9043 100644 (file)
@@ -180,7 +180,8 @@ sub cmd_sth
 
 sub tags
 {
-    my($self, @constraints)=@_;
+    my($self, $path)=@_;
+    my @constraints=@{$path->{elements}};
     if(!@constraints) # /
     {
        # FIXME: add ALL?
@@ -245,7 +246,8 @@ sub tag_values
 
 sub artists
 {
-    my($self, @constraints)=@_;
+    my($self, $path)=@_;
+    my @constraints=@{$path->{elements}};
     if(!@constraints) # /ALL
     {
        my $sql="SELECT DISTINCT name FROM artists;";
@@ -268,12 +270,13 @@ sub artists
 
 sub albums
 {
-    my($self, @constraints)=@_;
+    my($self, $path)=@_;
+    my @constraints=@{$path->{elements}};
     my @ids=();
     # FIXME: rework PathElements
     if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
     {
-       return $self->artist_albums($constraints[$#constraints]->{id}, @constraints);
+       return $self->artist_albums($constraints[$#constraints]->{id}, $path);
     }
     my $sql=("SELECT albums.name\n" .
             "\tFROM (\n" .
@@ -291,7 +294,8 @@ sub albums
 
 sub artist_albums
 {
-    my($self, $artist_id, @constraints)=@_;
+    my($self, $artist_id, $path)=@_;
+    my @constraints=@{$path->{elements}};
     my $sql=("SELECT albums.name FROM (\n" .
             $self->tags_subselect(@constraints) .
             "\t) AS subselect\n" .
@@ -309,7 +313,8 @@ sub artist_albums
 
 sub artist_tracks
 {
-    my($self, $artist_id, @constraints)=@_;
+    my($self, $artist_id, $path)=@_;
+    my @constraints=@{$path->{elements}};
     my $sql=("SELECT files.name FROM (\n" .
             $self->tags_subselect(@constraints) .
             "\t) AS subselect\n" .
@@ -342,7 +347,8 @@ sub album_tracks
 
 sub tracks
 {
-    my($self, @constraints)=@_;
+    my($self, $path)=@_;
+    my @constraints=@{$path->{elements}};
     # FIXME: rework PathElements
     if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
     {
@@ -375,7 +381,8 @@ sub tracks
 
 sub filename
 {
-    my($self, $mountpoint, @constraints)=@_;
+    my($self, $mountpoint, $path)=@_;
+    my @constraints=@{$path->{elements}};
     if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
     {
        my $id=$constraints[$#constraints]->{id};
@@ -396,6 +403,42 @@ sub tags_subselect
     return shift->tags_subselect_and(@_);
 }
 
+
+sub tags_subselect_and_not
+{
+    my($self,@constraints)=@_;
+    my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
+    my @tags=@$tags;
+    my @tags_vals=@$tags_vals;;
+    my $cnt=1;
+    my @andclauses=();
+    my $sql='';
+    for my $tag (@tags)
+    {
+       if($cnt == 1)
+       {
+           $sql="\tSELECT fxt" . scalar(@tags) . ".files_id FROM files_x_tags fxt1\n";
+           push(@andclauses, "\t\tfxt${cnt}.tags_id=$tag");
+       }
+       else
+       {
+           $sql .= ("\tLEFT JOIN files_x_tags fxt$cnt ON fxt" .
+                ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
+           push(@andclauses, "\t\tfxt${cnt}.tags_id IS NULL");
+       }
+       print "AND: @andclauses\n";
+       $cnt++;
+    }
+    if(@andclauses)
+    {
+       $sql .= "\tWHERE\n\t\t";
+       $sql .= join(" AND\n\t\t", @andclauses) . "\n";
+    }
+    $sql .= "\tGROUP BY fxt". scalar(@tags).".files_id\n";
+    return $sql;
+}
+
+
 sub tags_subselect_and
 {
     my($self,@constraints)=@_;
@@ -776,6 +819,7 @@ sub remove_unused
        LEFT JOIN files_x_tags ON files_x_tags.tags_id=tags.id
        WHERE files_x_tags.files_id IS NULL);
 
+    VACUUM
 EOT
     print "SQL: $sql\n";
     my @sql=split(/\n\n/, $sql);