import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashSet;
import java.util.List;
this.database = database;
}
+ public Collection<Artist> getAllArtists() throws DatabaseException {
+ Query query = new Query(Type.SELECT, "ARTISTS");
+ query.addField(new Field("ARTISTS.*"));
+ query.addField(new Field("GROUP_ARTISTS.*"));
+ query.addField(new Field("GROUPS.*"));
+ query.addJoin(new Join(JoinType.INNER, "GROUP_ARTISTS", new Field("ARTISTS.ID"), new Field("GROUP_ARTISTS.ARTIST")));
+ query.addJoin(new Join(JoinType.INNER, "GROUPS", new Field("GROUP_ARTISTS.GROUP_"), new Field("GROUPS.ID")));
+ query.addOrderField(new OrderField(new Field("ARTISTS.ID")));
+ ArtistLoader artistLoader = new ArtistLoader();
+ database.process(query, artistLoader);
+ return artistLoader.all();
+ }
+
public Track trackById(String id) throws DatabaseException {
Query query = new Query(Type.SELECT, "TRACKS");
query.addField(new Field("TRACKS.*"));
}
+ private class ArtistLoader implements ResultProcessor {
+
+ private final List<Artist> artists = new ArrayList<Artist>();
+ private Artist artist;
+
+ public List<Artist> all() {
+ return artists;
+ }
+
+ //
+ // RESULTPROCESSOR METHODS
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void processResult(ResultSet resultSet) throws SQLException {
+ String artistId = resultSet.getString("ARTISTS.ID");
+ if ((artist == null) || (!artist.id().equals(artistId))) {
+ artist = new AbstractArtist(artistId) {
+
+ public void save() {
+ }
+
+ }.name(resultSet.getString("ARTISTS.NAME")).groups(new HashSet<Group>());
+ artists.add(artist);
+ }
+
+ Group group = new AbstractGroup(resultSet.getString("GROUPS.ID")) {
+
+ public void save() {
+ }
+
+ }.name(resultSet.getString("GROUPS.NAME")).url(resultSet.getString("GROUPS.URL"));
+ artist.groups().add(group);
+ }
+
+ }
+
}