import java.sql.ResultSet;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
import net.pterodactylus.util.database.Database;
import net.pterodactylus.util.database.DatabaseException;
import net.pterodactylus.util.database.Field;
import net.pterodactylus.util.database.Join;
import net.pterodactylus.util.database.Join.JoinType;
-import net.pterodactylus.util.database.ObjectCreator;
+import net.pterodactylus.util.database.OrderField;
+import net.pterodactylus.util.database.OrderField.Order;
import net.pterodactylus.util.database.Parameter.StringParameter;
import net.pterodactylus.util.database.Query;
import net.pterodactylus.util.database.Query.Type;
+import net.pterodactylus.util.database.ResultProcessor;
import net.pterodactylus.util.database.ValueField;
import net.pterodactylus.util.database.ValueFieldWhereClause;
private final Database database;
- private final TrackLoader trackLoader = new TrackLoader();
-
- private final ArtistLoader artistLoader = new ArtistLoader();
-
- private final StyleLoader styleLoader = new StyleLoader();
-
public DataManager(Database database) {
this.database = database;
}
public Track trackById(String id) throws DatabaseException {
Query query = new Query(Type.SELECT, "TRACKS");
query.addField(new Field("TRACKS.*"));
+ query.addField(new Field("TRACK_ARTISTS.*"));
query.addField(new Field("ARTISTS.*"));
+ query.addField(new Field("TRACK_STYLES.*"));
query.addField(new Field("STYLES.*"));
- query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACKS.ARTIST"), new Field("ARTISTS.ID")));
- query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACKS.STYLE"), new Field("STYLES.ID")));
- query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(id))));
- return database.getSingle(query, trackLoader);
+ query.addJoin(new Join(JoinType.INNER, "TRACK_ARTISTS", new Field("TRACKS.ID"), new Field("TRACK_ARTISTS.TRACK")));
+ query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACK_ARTISTS.ARTIST"), new Field("ARTISTS.ID")));
+ query.addJoin(new Join(JoinType.INNER, "TRACK_STYLES", new Field("TRACKS.ID"), new Field("TRACK_STYLES.TRACK")));
+ query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACK_STYLES.STYLE"), new Field("STYLES.ID")));
+ query.addWhereClause(new ValueFieldWhereClause(new ValueField("TRACKS.ID", new StringParameter(id))));
+ query.addOrderField(new OrderField(new Field("TRACKS.ID"), Order.ASCENDING));
+ query.addOrderField(new OrderField(new Field("TRACK_ARTISTS.DISPLAY_ORDER"), Order.ASCENDING));
+ TrackLoader trackLoader = new TrackLoader();
+ database.process(query, trackLoader);
+ return trackLoader.get();
}
- private class TrackLoader implements ObjectCreator<Track> {
-
- /**
- * {@inheritDoc}
- */
- public Track createObject(ResultSet resultSet) throws SQLException {
- return new AbstractTrack(resultSet.getString("TRACKS.ID")) {
-
- public void save() {
- if (!dirty()) {
- return;
- }
- }
-
- }.name(resultSet.getString("TRACKS.NAME")).style(styleLoader.createObject(resultSet)).artist(artistLoader.createObject(resultSet));
- }
-
- }
+ private class TrackLoader implements ResultProcessor {
- private class ArtistLoader implements ObjectCreator<Artist> {
+ private final List<Track> loadedTracks = new ArrayList<Track>();
+ private Track track;
/**
* {@inheritDoc}
*/
- public Artist createObject(ResultSet resultSet) throws SQLException {
- return new AbstractArtist(resultSet.getString("ARTISTS.ID")) {
+ public void processResult(ResultSet resultSet) throws SQLException {
+ String trackId = resultSet.getString("TRACKS.ID");
+ if ((track == null) || (!track.id().equals(trackId))) {
+ track = new AbstractTrack(trackId) {
+
+ public void save() {
+ if (!dirty()) {
+ return;
+ }
+ }
+ }.name(resultSet.getString("TRACKS.NAME")).artists(new ArrayList<Artist>()).styles(new HashSet<Style>());
+ loadedTracks.add(track);
+ }
+ Artist artist = new AbstractArtist(resultSet.getString("ARTISTS.ID")) {
public void save() {
- if (!dirty()) {
- return;
- }
}
}.name(resultSet.getString("ARTISTS.NAME"));
- }
-
- }
-
- private class StyleLoader implements ObjectCreator<Style> {
-
- /**
- * {@inheritDoc}
- */
- public Style createObject(ResultSet resultSet) throws SQLException {
- return new AbstractStyle(resultSet.getString("STYLES.ID")) {
+ if (!track.artists().contains(artist)) {
+ track.artists().add(artist);
+ }
+ Style style = new AbstractStyle(resultSet.getString("STYLES.ID")) {
public void save() {
- if (!dirty()) {
- return;
- }
}
}.name(resultSet.getString("STYLES.NAME"));
+ track.styles().add(style);
+ }
+
+ public Track get() {
+ return loadedTracks.isEmpty() ? null : loadedTracks.get(0);
}
}