+ my($self, @constraints)=@_;
+ if(!@constraints) # /ALL
+ {
+ my $sql="SELECT DISTINCT name FROM artists;";
+ my $tags=$self->cmd_rows($sql);
+ return(map { $_->[0]; } @$tags);
+ }
+ my @file_ids=();
+ my @tag_ids=();
+ my $main_sql_start=("SELECT artists.name\n" .
+ "\tFROM (SELECT files_id FROM tags\n" .
+ "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+ "\t\tWHERE tags.id in\n\t\t\t(");
+ my $main_sql_end=(")\n\t\t) AS subselect\n" .
+ "\tINNER JOIN files_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+ "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+ "\n\tGROUP BY artists.name;");
+ while(my $constraint=shift @constraints)
+ {
+ print "CONSTRAINT: $constraint->{name}\n";
+ my $cid=$constraint->{id};
+ push(@tag_ids, $cid);
+ }
+ @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+ my $tagstr=join(", ", @tag_ids);
+ my $sql = ($main_sql_start . $tagstr .
+ $main_sql_end);
+ 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 @file_ids=(); # FIXME: needed? what about in artists()
+ my @tag_ids=();
+ # FIXME: rework PathElements
+ if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+ {
+ return $self->artist_albums($constraints[$#constraints]->{id});
+ }
+ return(); # FIXME
+ my $main_sql_start=("SELECT artists.name\n" .
+ "\tFROM (SELECT files_id FROM tags\n" .
+ "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+ "\t\tWHERE tags.id in\n\t\t\t(");
+ my $main_sql_end=(")\n\t\t) AS subselect\n" .
+ "\tINNER JOIN files_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+ "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+ "\n\tGROUP BY artists.name;");
+ while(my $constraint=shift @constraints)
+ {
+ print "CONSTRAINT: $constraint->{name}\n";
+ my $cid=$constraint->{id};
+ push(@tag_ids, $cid);
+ }
+ @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+ my $tagstr=join(", ", @tag_ids);
+ my $sql = ($main_sql_start . $tagstr .
+ $main_sql_end);
+ print "SQL: $sql\n";
+ my $result=$self->cmd_rows($sql);
+ my @tagnames=map { $_->[0]; } @$result;
+ print "ARTISTS: ", join(', ', @tagnames), "\n";
+ return(@tagnames);
+}
+
+sub artist_albums
+{
+ my($self, $artist_id)=@_;
+ my $sql=("SELECT albums.name FROM artists\n\t" .
+ "INNER JOIN artists_x_albums ON artists.id=artists_x_albums.artists_id\n\t" .
+ "INNER JOIN albums ON albums.id=artists_x_albums.albums_id\n\t" .
+ "WHERE artists.id=?\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)=@_;
+ my $sql=("SELECT files.name FROM artists\n\t" .
+ "INNER JOIN artists_x_files ON artists.id=files_x_artists.artists_id\n\t" .
+ "INNER JOIN files ON files.id=files_x_artists.files_id\n\t" .
+ "WHERE artists.id=?\n\t" .
+ "GROUP BY files.name\n");
+ print "ARTIST_TRACKS SQL: $sql\n";
+ my $result=$self->cmd_rows($sql, $artist_id);
+ my @albums=map { $_->[0]; } @$result;
+ print "ALBUMS: ", join(', ', @albums), "\n";
+ return(@albums);
+}
+
+sub album_tracks
+{
+ # FIXME: need albums_x_files table
+ my($self, $album_id)=@_;
+ my $sql=("SELECT files.name FROM albums\n\t" .
+ "INNER JOIN files_x_albums ON albums.id=files_x_albums.albums_id\n\t" .
+ "INNER JOIN albums ON albums.id=files_x_albums.albums_id\n\t" .
+ "WHERE albums.id=?\n\t" .
+ "GROUP BY files.name\n");
+ print "ALBUM_TRACKS SQL: $sql\n";
+ my $result=$self->cmd_rows($sql, $album_id);
+ my @tracks=map { $_->[0]; } @$result;
+ print "TRACKS: ", join(', ', @tracks), "\n";
+ return(@tracks);
+}
+
+sub tracks
+{
+ my($self, @constraints)=@_;
+ # FIXME: rework PathElements
+ if(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Artist")
+ {
+ return $self->artist_tracks($constraints[$#constraints]->{id});
+ }
+ elsif(ref($constraints[$#constraints]) eq "ID3FS::PathElement::Album")
+ {
+ # FIXME
+ return(());
+# return $self->album_tracks($constraints[$#constraints]->{id});
+ }
+ return(); # FIXME
+ my $main_sql_start=("SELECT artists.name\n" .
+ "\tFROM (SELECT files_id FROM tags\n" .
+ "\t\tINNER JOIN files_x_tags ON tags.id=files_x_tags.tags_id\n" .
+ "\t\tWHERE tags.id in\n\t\t\t(");
+ my $main_sql_end=(")\n\t\t) AS subselect\n" .
+ "\tINNER JOIN files_x_artists ON subselect.files_id=files_x_artists.files_id\n" .
+ "\tINNER JOIN artists ON artists.id=files_x_artists.artists_id\n" .
+ "\n\tGROUP BY artists.name;");
+ my @tag_ids;
+ while(my $constraint=shift @constraints)
+ {
+ print "CONSTRAINT: $constraint->{name}\n";
+ my $cid=$constraint->{id};
+ push(@tag_ids, $cid);
+ }
+ @tag_ids = map( { "\"$_\""; } @tag_ids) unless($self->{postgres});
+ my $tagstr=join(", ", @tag_ids);
+ my $sql = ($main_sql_start . $tagstr .
+ $main_sql_end);
+ print "SQL: $sql\n";
+ my $result=$self->cmd_rows($sql);
+ my @tagnames=map { $_->[0]; } @$result;
+ print "ARTISTS: ", join(', ', @tagnames), "\n";
+ return(@tagnames);
+}
+
+sub id
+{
+ my($self, $type, $val)=@_;
+ my $sql="SELECT id FROM $type WHERE name=?";
+ my ($id)=$self->cmd_onerow($sql, $val);