-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