return $newval;
}
-sub cmd_sth
-{
- my($self, $sql, @params)=@_;
- my $sth=$self->{dbh}->prepare($sql);
- my $idx=1;
- for my $param (@params)
- {
- $param="" unless(defined($param));
- $sth->bind_param($idx++, $param);
- }
- $sth->execute();
- return $sth;
-}
-
-sub tags
-{
- my($self, $path)=@_;
- my @constraints=@{$path->{elements}};
- if(!@constraints) # /
- {
- my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
- my $tags=$self->cmd_rows($sql);
- return(map { $_->[0]; } @$tags);
- }
- my $hasvals=$path->tag_has_values();
- my $parent=$path->trailing_tag_parent();
- print "THASVALS: $hasvals\n";
- print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
- my @ids=();
- my $sql=("SELECT tags.name FROM (\n" .
- $self->tags_subselect($path) .
- ") 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)=$path->used_tags();
- my @used=grep { ref($_) ne "ARRAY"; } @allused;
- my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused;
- print "tags(): USED: ", join(", ", @used), "\n";
- print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n";
- my @orclauses=();
- my @andclauses=();
- my $id=$path->trailing_tag_id();
- if($hasvals)
- {
- print "HAS_VALUES\n";
- my @values=map { "'".$_->[1]."'"; } grep { $_->[0] == $id; } @used_with_vals;
- my $clause="(tags.parents_id='$id'";
- if(@values)
- {
- $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")";
- }
- $clause .= ")";
- push(@orclauses, $clause);
- }
- else
- {
- print "HASNT VALUES\n";;
- if(@used)
- {
- push(@orclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
- }
- for my $pair (@used_with_vals)
- {
- push(@orclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
- }
- }
-
- my $parentclause= "(tags.parents_id='";
- if($hasvals)
- {
- $parentclause .= $id;
- }
- elsif($parent)
- {
- $parentclause .= $parent;
- }
- $parentclause .= "')";
- push(@andclauses, $parentclause);
-
- if(@orclauses)
- {
- push(@andclauses, join(' OR ', @orclauses));
- }
- if(@andclauses)
- {
- $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
- }
- $sql .= "GROUP BY tags.name;";
- print "SQL: $sql\n";
- my $result=$self->cmd_rows($sql);
- my @tagnames=map { $_->[0]; } @$result;
- print "SUBNAMES: ", join(', ', @tagnames), "\n";
- return(@tagnames);
-}
-
-sub tag_values
-{
- my($self, $tagid)=@_;
- my $sql=("SELECT DISTINCT name FROM tags\n" .
- "WHERE parents_id=?");
- my $tags=$self->cmd_rows($sql, $tagid);
- my @tags=map { $_->[0]; } @$tags;
- @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
- return @tags;
-}
-
-sub artists
-{
- my($self, $path)=@_;
- my @constraints=@{$path->{elements}};
- if(!@constraints) # /ALL
- {
- my $sql="SELECT DISTINCT name FROM artists;";
- my $tags=$self->cmd_rows($sql);
- return(map { $_->[0]; } @$tags);
- }
- my @ids=();
- my $sql=("SELECT artists.name FROM (\n" .
- $self->tags_subselect($path) .
- ") AS subselect\n" .
- "INNER JOIN files ON subselect.files_id=files.id\n" .
- "INNER JOIN artists ON files.artists_id=artists.id\n" .
- "GROUP BY artists.name;");
- print "SQL: $sql\n";
- my $result=$self->cmd_rows($sql);
- my @tagnames=map { $_->[0]; } @$result;
- print "ARTISTS: ", join(', ', @tagnames), "\n";
- return(@tagnames);
-}
-
-sub albums
-{
- 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}, $path);
- }
- my $sql=("SELECT albums.name\n" .
- "\tFROM (\n" .
- $self->tags_subselect($path) .
- "\t) AS subselect\n" .
- "INNER JOIN files ON subselect.files_id=files.id\n" .
- "INNER JOIN albums ON files.albums_id=albums.id\n" .
- "GROUP BY albums.name;");
- print "SQL(ALBUMS): \n$sql\n";
- my $result=$self->cmd_rows($sql);
- my @names=map { $_->[0]; } @$result;
- print "ALBUMS: ", join(', ', @names), "\n";
- return(@names);
-}
-
-sub artist_albums
-{
- my($self, $artist_id, $path)=@_;
- my @constraints=@{$path->{elements}};
- my $sql=("SELECT albums.name FROM (\n" .
- $self->tags_subselect($path) .
- "\t) AS subselect\n" .
- "INNER JOIN files ON subselect.files_id=files.id\n" .
- "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
- "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
- "WHERE artists.id=? and albums.name <> ''\n\t" .
- "GROUP BY albums.name\n");
- print "ARTIST_ALBUMS SQL: $sql\n";
- my $result=$self->cmd_rows($sql, $artist_id);
- my @albums=map { $_->[0]; } @$result;
- print "ALBUMS: ", join(', ', @albums), "\n";
- return(@albums);
-}
-
-sub artist_tracks
-{
- my($self, $artist_id, $path)=@_;
- my $sql=("SELECT files.name FROM (\n" .
- $self->tags_subselect($path) .
- "\t) AS subselect\n" .
- "INNER JOIN files ON subselect.files_id=files.id\n" .
- "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
- "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
- "WHERE artists.id=? AND albums.name=''\n\t" .
- "GROUP BY files.name\n");
- print "ARTIST_TRACKS SQL: $sql\n";
- my $result=$self->cmd_rows($sql, $artist_id);
- my @names=map { $_->[0]; } @$result;
- print "ARTISTTRACKS: ", join(', ', @names), "\n";
- return(@names);
-}
-
-sub album_tracks
-{
- my($self, $artist_id, $album_id)=@_;
- my $sql=("SELECT files.name FROM files\n\t" .
- "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
- "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
- "WHERE artists.id=? AND albums.id=?\n\t" .
- "GROUP BY files.name\n");
- print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n";
- my $result=$self->cmd_rows($sql, $artist_id, $album_id);
- my @names=map { $_->[0]; } @$result;
- print "TRACKS: ", join(', ', @names), "\n";
- return(@names);
-}
-
-sub tracks
-{
- my($self, $path)=@_;
- my @constraints=@{$path->{elements}};
- # FIXME: rework PathElements
- if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
- {
- my $artist_id=0;
- my $artist=$constraints[$#constraints];
- if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
- {
- # should always happen
- $artist_id=$artist->{id};
- }
- return $self->artist_tracks($artist_id, $path);
- }
- elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
- {
- my $artist_id=0;
- my $artist=$constraints[($#constraints)-1];
- if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
- {
- # should always happen
- $artist_id=$artist->{id};
- }
- return $self->album_tracks($artist_id, $constraints[$#constraints]->{id});
- }
-
- my $sql=("SELECT files.name\n" .
- "\tFROM (\n" .
- $self->tags_subselect($path) .
- "\t) AS subselect\n" .
- "INNER JOIN files ON files.id=subselect.files_id\n" .
- "GROUP BY files.name;");
- print "SQL: $sql\n";
- my $result=$self->cmd_rows($sql);
- my @names=map { $_->[0]; } @$result;
- print "TRACKS: ", join(', ', @names), "\n";
- return(@names);
-}
-
-sub filename
-{
- my($self, $mountpoint, $path)=@_;
- my @constraints=@{$path->{elements}};
- if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
- {
- my $id=$constraints[$#constraints]->{id};
- my $sql=("SELECT paths.name, files.name FROM files\n" .
- "INNER JOIN paths ON files.paths_id=paths.id\n" .
- "WHERE files.id=?\n" .
- "GROUP BY paths.name, files.name");
- print "FILENAME SQL: $sql\n";
- my ($path, $name)=$self->cmd_onerow($sql, $id);
- my $id3fs_path=join('/', map { $_->{name}; } @constraints);
- return($self->relativise($path, $name, $mountpoint, $id3fs_path));
- }
- die("DB::filename: unhandled case\n"); #FIXME
-}
-
-sub tags_subselect
-{
- my($self, $path)=@_;
- my $tree=$path->{tagtree};
- my $hasvals=$path->tag_has_values();
- my $parent=$path->trailing_tag_parent();
-
- my $tag=undef;
- if($hasvals)
- {
- $tag=$path->trailing_tag_id();
- print "Trailing id: $tag\n";
- }
- my ($sqlclause, $joinsneeded)=(undef, 1);
- ($sqlclause, $joinsneeded) = $tree->to_sql($tag) if($tree);
- print "SQL($joinsneeded): $sqlclause\n";
- my $sql="\tSELECT fxt1.files_id FROM tags t1";
- my @crosses=();
- my @inners=();
-# $joinsneeded++ if($tag);
- for(my $i=1; $i <= $joinsneeded; $i++)
- {
- if($i > 1)
- {
- push(@crosses, "CROSS JOIN tags t$i");
- }
- my $inner=("\tINNER JOIN files_x_tags fxt$i ON " .
- "t${i}.id=fxt${i}.tags_id");
- if($i>2)
- {
- $inner .= " AND fxt1.files_id=fxt${i}.files_id";
- }
- push(@inners, $inner);
- }
- $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
- $sql .= ("\n" . join("\n", @inners)) if(@inners);
- $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
-# if($tag)
-# {
-# $sql .= " AND t${joinsneeded}.parents_id='$tag'";
-# }
- $sql .= "\n\tGROUP BY fxt1.files_id\n";
- return $sql;
-}
-
-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)=@_;
- 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";
- }
- else
- {
- $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" .
- ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
- }
- push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag");
- 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_or
+sub id
{
- my($self,@constraints)=@_;
- my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
- my @tags=@$tags;
- my @tags_vals=@$tags_vals;;
-
- my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" .
- "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n");
- my @orclauses=();
- my @andclauses=();
- # FIXME: and / or?
- if(@tags)
- {
- push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
- push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
- }
- for my $pair (@tags_vals)
- {
- my($tag, $val)=@$pair;
- push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )");
- }
-# push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
- if(@orclauses)
- {
- push(@andclauses, join("\n\t\tOR ", @orclauses));
- }
- if(@andclauses)
- {
- $sql .= "\tWHERE\n\t\t";
- $sql .= join("\n\t\tAND ", @andclauses) . "\n";
- }
- $sql .= "\tGROUP BY files_x_tags.files_id\n";
- return $sql;
+ my($self, $type, $val)=@_;
+ my $sql="SELECT id FROM $type WHERE name=?";
+ my ($id)=$self->cmd_onerow($sql, $val);
+ return($id);
}
-sub constraints_tag_list
+sub tag_has_values
{
- my($self, @constraints)=@_;
- my $lasttag=undef;
- my @tags=();
- my @tags_vals=();
- for my $constraint (@constraints)
- {
-# print ref($constraint), ": ", $constraint->{name}, "\n";
- if(ref($constraint) eq "ID3FS::PathElement::Tag")
- {
- if(defined($lasttag))
- {
-# print "TAGVAL\n";
- push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id});
- $lasttag=undef;
- }
- elsif($self->tag_has_values($constraint->{id}))
- {
-# print "HASVALUES\n";
- $lasttag=$constraint->{id} if defined($constraint->{id});
- }
- else
- {
-# print "NOVALUES\n";
- push(@tags, $constraint->{id}) if(defined($constraint->{id}));
- }
- }
- }
- unless($self->{postgres})
- {
- @tags=map{ "\"$_\""; } @tags;
- @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
- $lasttag="\"$lasttag\"" if defined($lasttag);
- }
- return(\@tags, \@tags_vals, $lasttag);
+ my($self, $id)=@_;
+ my $sql=("SELECT COUNT(*) FROM tags\n\t" .
+ "WHERE tags.parents_id=?\n");
+ my ($rows)=$self->cmd_onerow($sql, $id);
+ return $rows;
}
-
sub relativise
{
my($self, $path, $name, $mountpoint, $id3fs_path)=@_;
return $result;
}
-sub bare_tags
-{
- my($self)=@_;
- my $sql=("SELECT tags.name FROM tags\n" .
- "WHERE tags.parents_id=''\n" .
- "GROUP BY tags.name\n");
- my $result=$self->cmd_rows($sql);
- my @names=map { $_->[0]; } @$result;
- return (@names);
-}
-
-sub tags_with_values
-{
- # FIXME: only shows one level of tag depth
- my($self)=@_;
- my $sql=("SELECT p.name, t.name FROM tags t\n" .
- "INNER JOIN tags p ON t.parents_id=p.id\n" .
- "GROUP BY p.name, t.name\n");
- print "SQL: $sql\n";
- my $result=$self->cmd_rows($sql);
- my $tags={};
- for my $pair (@$result)
- {
- push(@{$tags->{$pair->[0]}}, $pair->[1]);
- }
- return $tags;
-}
-
-sub id
-{
- my($self, $type, $val)=@_;
- my $sql="SELECT id FROM $type WHERE name=?";
- my ($id)=$self->cmd_onerow($sql, $val);
- return($id);
-}
-
sub add
{
my($self,$path)=@_;
$self->cmd($sql, map { $fields->{$_}; } sort keys(%$fields));
}
-sub lookup_id
-{
- my($self, $table, $name)=@_;
- my($id)=$self->cmd_onerow("SELECT id FROM $table where name=?", $name);
- return $id;
-}
-
-sub tag_has_values
-{
- my($self, $id)=@_;
- my $sql=("SELECT COUNT(*) FROM tags\n\t" .
- "WHERE tags.parents_id=?\n");
- my ($rows)=$self->cmd_onerow($sql, $id);
- return $rows;
-}
-
sub files_in
{
my ($self, $dir)=@_;
return(defined($thing) && length($thing) && $thing =~ /\S+/);
}
+sub cmd_sth
+{
+ my($self, $sql, @params)=@_;
+ my $sth=$self->{dbh}->prepare($sql);
+ my $idx=1;
+ for my $param (@params)
+ {
+ $param="" unless(defined($param));
+ $sth->bind_param($idx++, $param);
+ }
+ $sth->execute();
+ return $sth;
+}
+
sub cmd
{
my ($self, @args)=@_;
return $sth->fetchall_arrayref();
}
+sub cmd_firstcol
+{
+ my ($self, @args)=@_;
+ return(map { $_->[0] } @{$self->cmd_rows(@args)});
+}
+
sub cmd_id
{
my ($self, @args)=@_;
my($self, $mountpoint)=@_;
if($self->state() == $STATE_FILE)
{
- return $self->{db}->filename($mountpoint, $self);
+ return $self->filename($mountpoint, $self);
}
return "ERROR"; #should never happen?
}
ref($tag) eq "ID3FS::PathElement::Tag" &&
$self->{db}->tag_has_values($tag->{id}))
{
- @dents=$self->{db}->tags($self);
+ @dents=$self->tags();
}
else
{
@dents=(qw(AND OR TRACKS NOARTIST),
- $self->{db}->artists($self));
+ $self->artists());
}
}
elsif($state==$STATE_BOOLEAN)
{
push(@dents, "NOT");
}
- push(@dents, $self->{db}->tags($self));
+ push(@dents, $self->tags());
}
elsif($state==$STATE_ROOT)
{
- @dents=(qw(ALL NOT), $self->{db}->tags($self));
+ @dents=(qw(ALL NOT), $self->tags());
}
elsif($state==$STATE_ALBUMS)
{
- @dents=(qw(TRACKS NOALBUM),$self->{db}->albums($self));
+ @dents=(qw(TRACKS NOALBUM),$self->albums());
}
elsif($state==$STATE_TRACKLIST)
{
- @dents=$self->{db}->tracks($self);
+ @dents=$self->tracks();
}
else
{
return undef;
}
+######################################################################
+
+sub tags
+{
+ my($self)=@_;
+ my @constraints=@{$self->{elements}};
+ if(!@constraints) # /
+ {
+ my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
+ return($self->{db}->cmd_firstcol($sql));
+ }
+ my $hasvals=$self->tag_has_values();
+ my $parent=$self->trailing_tag_parent();
+ print "THASVALS: $hasvals\n";
+ print "TPARENT: ", (defined($parent)? $parent : "NO"), "\n";
+ my @ids=();
+ my $sql=("SELECT tags.name FROM (\n" .
+ $self->tags_subselect() .
+ ") 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 @used=grep { ref($_) ne "ARRAY"; } @allused;
+ my @used_with_vals=grep { ref($_) eq "ARRAY"; } @allused;
+ print "tags(): USED: ", join(", ", @used), "\n";
+ print "tags(): USED_WITH_VALS: ", join(", ", map { "[".$_->[0]. ", ".$_->[1]."]";} @used_with_vals), "\n";
+ my @orclauses=();
+ my @andclauses=();
+ my $id=$self->trailing_tag_id();
+ if($hasvals)
+ {
+ print "HAS_VALUES\n";
+ my @values=map { "'".$_->[1]."'"; } grep { $_->[0] == $id; } @used_with_vals;
+ my $clause="(tags.parents_id='$id'";
+ if(@values)
+ {
+ $clause .= " AND tags.id NOT IN (" . join(', ', @values) . ")";
+ }
+ $clause .= ")";
+ push(@orclauses, $clause);
+ }
+ else
+ {
+ print "HASNT VALUES\n";;
+ if(@used)
+ {
+ push(@orclauses, "(NOT (tags.parents_id='' AND tags.id IN (" . join(', ', @used) . ")))");
+ }
+ for my $pair (@used_with_vals)
+ {
+ push(@orclauses, "(NOT (tags.parents_id='" . $pair->[0] . "' AND tags.id='" . $pair->[1] . "'))");
+ }
+ }
+
+ my $parentclause= "(tags.parents_id='";
+ if($hasvals)
+ {
+ $parentclause .= $id;
+ }
+ elsif($parent)
+ {
+ $parentclause .= $parent;
+ }
+ $parentclause .= "')";
+ push(@andclauses, $parentclause);
+
+ if(@orclauses)
+ {
+ push(@andclauses, join(' OR ', @orclauses));
+ }
+ if(@andclauses)
+ {
+ $sql .= "WHERE " . join(' AND ', @andclauses) . "\n";
+ }
+ $sql .= "GROUP BY tags.name;";
+ print "SQL: $sql\n";
+ my @tagnames=$self->{db}->cmd_firstcol($sql);
+ print "SUBNAMES: ", join(', ', @tagnames), "\n";
+ return(@tagnames);
+}
+
+sub tag_values
+{
+ my($self, $tagid)=@_;
+ my $sql=("SELECT DISTINCT name FROM tags\n" .
+ "WHERE parents_id=?");
+ my @tags=$self->{db}->cmd_firstcol($sql, $tagid);
+ @tags=map { length($_) ? $_ : "NOVALUE"; } @tags;
+ return @tags;
+}
+
+sub artists
+{
+ my($self)=@_;
+ my @constraints=@{$self->{elements}};
+ if(!@constraints) # /ALL
+ {
+ my $sql="SELECT DISTINCT name FROM artists;";
+ return($self->{db}->cmd_firstcol($sql));
+ }
+ my @ids=();
+ my $sql=("SELECT artists.name FROM (\n" .
+ $self->tags_subselect() .
+ ") AS subselect\n" .
+ "INNER JOIN files ON subselect.files_id=files.id\n" .
+ "INNER JOIN artists ON files.artists_id=artists.id\n" .
+ "GROUP BY artists.name;");
+ print "SQL: $sql\n";
+ my @tagnames=$self->{db}->cmd_firstcol($sql);
+ print "ARTISTS: ", join(', ', @tagnames), "\n";
+ return(@tagnames);
+}
+
+sub albums
+{
+ my($self)=@_;
+ my @constraints=@{$self->{elements}};
+ my @ids=();
+ # FIXME: rework PathElements
+ if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+ {
+ return $self->artist_albums($constraints[$#constraints]->{id});
+ }
+ my $sql=("SELECT albums.name\n" .
+ "\tFROM (\n" .
+ $self->tags_subselect() .
+ "\t) AS subselect\n" .
+ "INNER JOIN files ON subselect.files_id=files.id\n" .
+ "INNER JOIN albums ON files.albums_id=albums.id\n" .
+ "GROUP BY albums.name;");
+ print "SQL(ALBUMS): \n$sql\n";
+ my @names=$self->{db}->cmd_firstcol($sql);
+ print "ALBUMS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+sub artist_albums
+{
+ my($self, $artist_id)=@_;
+ my @constraints=@{$self->{elements}};
+ my $sql=("SELECT albums.name FROM (\n" .
+ $self->tags_subselect() .
+ "\t) AS subselect\n" .
+ "INNER JOIN files ON subselect.files_id=files.id\n" .
+ "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
+ "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+ "WHERE artists.id=? and albums.name <> ''\n\t" .
+ "GROUP BY albums.name\n");
+ print "ARTIST_ALBUMS SQL: $sql\n";
+ my @albums=$self->{db}->cmd_firstcol($sql, $artist_id);
+ print "ALBUMS: ", join(', ', @albums), "\n";
+ return(@albums);
+}
+
+sub artist_tracks
+{
+ my($self, $artist_id)=@_;
+ my $sql=("SELECT files.name FROM (\n" .
+ $self->tags_subselect() .
+ "\t) AS subselect\n" .
+ "INNER JOIN files ON subselect.files_id=files.id\n" .
+ "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+ "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
+ "WHERE artists.id=? AND albums.name=''\n\t" .
+ "GROUP BY files.name\n");
+ print "ARTIST_TRACKS SQL: $sql\n";
+ my @names=$self->{db}->cmd_firstcol($sql, $artist_id);
+ print "ARTISTTRACKS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+sub album_tracks
+{
+ my($self, $artist_id, $album_id)=@_;
+ my $sql=("SELECT files.name FROM files\n\t" .
+ "INNER JOIN albums ON albums.id=files.albums_id\n\t" .
+ "INNER JOIN artists ON artists.id=files.artists_id\n\t" .
+ "WHERE artists.id=? AND albums.id=?\n\t" .
+ "GROUP BY files.name\n");
+ print "ALBUM_TRACKS SQL($artist_id, $album_id): $sql\n";
+ my @names=$self->{db}->cmd_firstcol($sql, $artist_id, $album_id);
+ print "TRACKS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+sub tracks
+{
+ my($self)=@_;
+ my @constraints=@{$self->{elements}};
+ # FIXME: rework PathElements
+ if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+ {
+ my $artist_id=0;
+ my $artist=$constraints[$#constraints];
+ if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
+ {
+ # should always happen
+ $artist_id=$artist->{id};
+ }
+ return $self->artist_tracks($artist_id);
+ }
+ elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
+ {
+ my $artist_id=0;
+ my $artist=$constraints[($#constraints)-1];
+ if(defined($artist) && (ref($artist) eq "ID3FS::PathElement::Artist"))
+ {
+ # should always happen
+ $artist_id=$artist->{id};
+ }
+ return $self->album_tracks($artist_id, $constraints[$#constraints]->{id});
+ }
+
+ my $sql=("SELECT files.name\n" .
+ "\tFROM (\n" .
+ $self->tags_subselect() .
+ "\t) AS subselect\n" .
+ "INNER JOIN files ON files.id=subselect.files_id\n" .
+ "GROUP BY files.name;");
+ print "SQL: $sql\n";
+ my @names=$self->{db}->cmd_firstcol($sql);
+ print "TRACKS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+sub filename
+{
+ my($self, $mountpoint)=@_;
+ my @constraints=@{$self->{elements}};
+ if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::File")
+ {
+ my $id=$constraints[$#constraints]->{id};
+ my $sql=("SELECT paths.name, files.name FROM files\n" .
+ "INNER JOIN paths ON files.paths_id=paths.id\n" .
+ "WHERE files.id=?\n" .
+ "GROUP BY paths.name, files.name");
+ print "FILENAME SQL: $sql\n";
+ my ($path, $name)=$self->{db}->cmd_onerow($sql, $id);
+ my $id3fs_path=join('/', map { $_->{name}; } @constraints);
+ return($self->{db}->relativise($path, $name, $mountpoint, $id3fs_path));
+ }
+ die("DB::filename: unhandled case\n"); #FIXME
+}
+
+sub tags_subselect
+{
+ my($self)=@_;
+ my $tree=$self->{tagtree};
+ my $hasvals=$self->tag_has_values();
+ my $parent=$self->trailing_tag_parent();
+
+ my $tag=undef;
+ if($hasvals)
+ {
+ $tag=$self->trailing_tag_id();
+ print "Trailing id: $tag\n";
+ }
+ my ($sqlclause, $joinsneeded)=(undef, 1);
+ ($sqlclause, $joinsneeded) = $tree->to_sql($tag) if($tree);
+ print "SQL($joinsneeded): $sqlclause\n";
+ my $sql="\tSELECT fxt1.files_id FROM tags t1";
+ my @crosses=();
+ my @inners=();
+# $joinsneeded++ if($tag);
+ for(my $i=1; $i <= $joinsneeded; $i++)
+ {
+ if($i > 1)
+ {
+ push(@crosses, "CROSS JOIN tags t$i");
+ }
+ my $inner=("\tINNER JOIN files_x_tags fxt$i ON " .
+ "t${i}.id=fxt${i}.tags_id");
+ if($i>2)
+ {
+ $inner .= " AND fxt1.files_id=fxt${i}.files_id";
+ }
+ push(@inners, $inner);
+ }
+ $sql .= ("\n\t" . join(" ", @crosses)) if(@crosses);
+ $sql .= ("\n" . join("\n", @inners)) if(@inners);
+ $sql .= "\n\tWHERE $sqlclause" if($sqlclause);
+# if($tag)
+# {
+# $sql .= " AND t${joinsneeded}.parents_id='$tag'";
+# }
+ $sql .= "\n\tGROUP BY fxt1.files_id\n";
+ return $sql;
+}
+
+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)=@_;
+ 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";
+ }
+ else
+ {
+ $sql .= ("\tINNER JOIN files_x_tags fxt$cnt ON fxt" .
+ ($cnt-1) . ".files_id=fxt${cnt}.files_id\n");
+ }
+ push(@andclauses, "\t\tfxt${cnt}.tags_id = $tag");
+ 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_or
+{
+ my($self,@constraints)=@_;
+ my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
+ my @tags=@$tags;
+ my @tags_vals=@$tags_vals;;
+
+ my $sql=("\tSELECT files_x_tags.files_id FROM tags t1\n" .
+ "\tINNER JOIN files_x_tags ON t1.id=files_x_tags.tags_id\n");
+ my @orclauses=();
+ my @andclauses=();
+ # FIXME: and / or?
+ if(@tags)
+ {
+ push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
+ push(@andclauses, "( t1.id IN ( " . join(', ', @tags) ." ) )");
+ }
+ for my $pair (@tags_vals)
+ {
+ my($tag, $val)=@$pair;
+ push(@orclauses, "( t1.parents_id=$tag AND t1.id=$val )");
+ }
+# push(@andclauses, "( t1.parents_id=" . (defined($parent) ? $parent : "''") . " )");
+ if(@orclauses)
+ {
+ push(@andclauses, join("\n\t\tOR ", @orclauses));
+ }
+ if(@andclauses)
+ {
+ $sql .= "\tWHERE\n\t\t";
+ $sql .= join("\n\t\tAND ", @andclauses) . "\n";
+ }
+ $sql .= "\tGROUP BY files_x_tags.files_id\n";
+ return $sql;
+}
+
+sub constraints_tag_list
+{
+ my($self, @constraints)=@_;
+ my $lasttag=undef;
+ my @tags=();
+ my @tags_vals=();
+ for my $constraint (@constraints)
+ {
+# print ref($constraint), ": ", $constraint->{name}, "\n";
+ if(ref($constraint) eq "ID3FS::PathElement::Tag")
+ {
+ if(defined($lasttag))
+ {
+# print "TAGVAL\n";
+ push(@tags_vals, [$lasttag, $constraint->{id}]) if defined($constraint->{id});
+ $lasttag=undef;
+ }
+ elsif($self->tag_has_values($constraint->{id}))
+ {
+# print "HASVALUES\n";
+ $lasttag=$constraint->{id} if defined($constraint->{id});
+ }
+ else
+ {
+# print "NOVALUES\n";
+ push(@tags, $constraint->{id}) if(defined($constraint->{id}));
+ }
+ }
+ }
+ unless($self->{db}->{postgres})
+ {
+ @tags=map{ "\"$_\""; } @tags;
+ @tags_vals=map( { [ map({ "\"$_\""; } @$_ ) ] } @tags_vals);
+ $lasttag="\"$lasttag\"" if defined($lasttag);
+ }
+ return(\@tags, \@tags_vals, $lasttag);
+}
+
+
+sub bare_tags
+{
+ my($self)=@_;
+ my $sql=("SELECT tags.name FROM tags\n" .
+ "WHERE tags.parents_id=''\n" .
+ "GROUP BY tags.name\n");
+ my @names=$self->{db}->cmd_firstcol($sql);
+ return (@names);
+}
+
+sub tags_with_values
+{
+ # FIXME: only shows one level of tag depth
+ my($self)=@_;
+ my $sql=("SELECT p.name, t.name FROM tags t\n" .
+ "INNER JOIN tags p ON t.parents_id=p.id\n" .
+ "GROUP BY p.name, t.name\n");
+ print "SQL: $sql\n";
+ my $result=$self->{db}->cmd_rows($sql);
+ my $tags={};
+ for my $pair (@$result)
+ {
+ push(@{$tags->{$pair->[0]}}, $pair->[1]);
+ }
+ return $tags;
+}
+
+sub lookup_id
+{
+ my($self, $table, $name)=@_;
+ my($id)=$self->{db}->cmd_onerow("SELECT id FROM $table where name=?", $name);
+ return $id;
+}
1;