+ my($self, $artist_id)=@_;
+ my $sql=("SELECT files.name FROM files\n\t" .
+ "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;
+ @names = map { s/.*\///; $_; } @names;
+ 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;
+ @names = map { s/.*\///; $_;} @names;
+ print "TRACKS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+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")
+ {
+ 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 $main_sql_start=("SELECT files.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 ON files.id=subselect.files_id" .
+ "\tGROUP BY files.name;");
+ my @ids;
+ while(my $constraint=shift @constraints)
+ {
+ print "CONSTRAINT: $constraint->{name}\n";
+ my $cid=$constraint->{id};
+ push(@ids, $cid);
+ }
+ @ids = map( { "\"$_\""; } @ids) unless($self->{postgres});
+ my $str=join(", ", @ids);
+ my $sql = ($main_sql_start . $str .
+ $main_sql_end);
+ print "SQL: $sql\n";
+ my $result=$self->cmd_rows($sql);
+ my @names=map { $_->[0]; } @$result;
+ @names = map { s/.*\///; $_; } @names;
+ print "TRACKS: ", join(', ', @names), "\n";
+ return(@names);
+}
+
+sub filename
+{
+ my($self, @constraints)=@_;
+ 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);
+ return($self->{absbase} . "/$path/$name");
+ }
+ die("DB::filename: unhandled case\n"); #FIXME
+}
+
+sub id
+{
+ my($self, $type, $val)=@_;
+ print "ID: $type $val\n";
+ my $sql="SELECT id FROM $type WHERE name=?";
+ my ($id)=$self->cmd_onerow($sql, $val);