-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, @constraints)=@_;
- if(!@constraints) # /
- {
- # FIXME: add ALL?
- my $sql="SELECT DISTINCT name FROM tags WHERE parents_id='';";
- my $tags=$self->cmd_rows($sql);
- return(map { $_->[0]; } @$tags);
- }
- my @ids=();
-
- my $sql=("SELECT t2.name FROM (\n" .
- $self->tags_subselect(@constraints) .
- ") AS subselect\n" .
- "INNER JOIN files_x_tags ON subselect.files_id=files_x_tags.files_id\n" .
- "INNER JOIN tags t2 ON files_x_tags.tags_id=t2.id\n");
- my ($tags, $tags_vals, $parent)=$self->constraints_tag_list(@constraints);
- my @tags=@$tags;
- my @tags_vals=@$tags_vals;;
- my @orclauses=();
- my @andclauses=();
- use Data::Dumper;
- print "TAGS: ", Dumper \@tags;
- print "VALS: ", Dumper \@tags_vals;
-
- push(@andclauses, "( t2.parents_id=" . (defined($parent) ? $parent : "''") . " )");
- if(@tags)
- {
- push(@orclauses, "( t2.id NOT IN ( " . join(', ', @tags) ." ) )");
- }
- for my $pair (@tags_vals)
- {
- my($tag, $val)=@$pair;
-# push(@orclauses, "( NOT ( t2.parents_id=$tag AND t2.id=$val ) )");
- push(@andclauses, "( NOT ( t2.id=$tag ) )");
- }
- if(@orclauses)
- {
- push(@andclauses, join("\n\tOR ", @orclauses));
- }
- if(@andclauses)
- {
- $sql .= "\tWHERE\n\t\t";
- $sql .= join("\n\tAND ", @andclauses) . "\n";
- }
- $sql .= "GROUP BY t2.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, @constraints)=@_;
- 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(@constraints) .
- ") 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, @constraints)=@_;
- my @ids=();
- # FIXME: rework PathElements
- if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
- {
- return $self->artist_albums($constraints[$#constraints]->{id}, @constraints);
- }
- my $sql=("SELECT albums.name\n" .
- "\tFROM (\n" .
- $self->tags_subselect(@constraints) .
- "\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;");
- 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, @constraints)=@_;
- my $sql=("SELECT albums.name FROM (\n" .
- $self->tags_subselect(@constraints) .
- "\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, @constraints)=@_;
- my $sql=("SELECT files.name FROM (\n" .
- $self->tags_subselect(@constraints) .
- "\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