2 * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.demoscenemusic.data;
20 import java.sql.ResultSet;
21 import java.sql.SQLException;
23 import net.pterodactylus.util.database.Database;
24 import net.pterodactylus.util.database.DatabaseException;
25 import net.pterodactylus.util.database.Field;
26 import net.pterodactylus.util.database.Join;
27 import net.pterodactylus.util.database.Join.JoinType;
28 import net.pterodactylus.util.database.ObjectCreator;
29 import net.pterodactylus.util.database.Parameter.StringParameter;
30 import net.pterodactylus.util.database.Query;
31 import net.pterodactylus.util.database.Query.Type;
32 import net.pterodactylus.util.database.ValueField;
33 import net.pterodactylus.util.database.ValueFieldWhereClause;
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class DataManager {
42 private final Database database;
44 private final TrackLoader trackLoader = new TrackLoader();
46 private final ArtistLoader artistLoader = new ArtistLoader();
48 private final StyleLoader styleLoader = new StyleLoader();
50 public DataManager(Database database) {
51 this.database = database;
54 public Track trackById(String id) throws DatabaseException {
55 Query query = new Query(Type.SELECT, "TRACKS");
56 query.addField(new Field("TRACKS.*"));
57 query.addField(new Field("ARTISTS.*"));
58 query.addField(new Field("STYLES.*"));
59 query.addJoin(new Join(JoinType.INNER, "ARTISTS", new Field("TRACKS.ARTIST"), new Field("ARTISTS.ID")));
60 query.addJoin(new Join(JoinType.INNER, "STYLES", new Field("TRACKS.STYLE"), new Field("STYLES.ID")));
61 query.addWhereClause(new ValueFieldWhereClause(new ValueField("ID", new StringParameter(id))));
62 return database.getSingle(query, trackLoader);
65 private class TrackLoader implements ObjectCreator<Track> {
70 public Track createObject(ResultSet resultSet) throws SQLException {
71 return new AbstractTrack(resultSet.getString("TRACKS.ID")) {
79 }.name(resultSet.getString("TRACKS.NAME")).style(styleLoader.createObject(resultSet)).artist(artistLoader.createObject(resultSet));
84 private class ArtistLoader implements ObjectCreator<Artist> {
89 public Artist createObject(ResultSet resultSet) throws SQLException {
90 return new AbstractArtist(resultSet.getString("ARTISTS.ID")) {
98 }.name(resultSet.getString("ARTISTS.NAME"));
103 private class StyleLoader implements ObjectCreator<Style> {
108 public Style createObject(ResultSet resultSet) throws SQLException {
109 return new AbstractStyle(resultSet.getString("STYLES.ID")) {
117 }.name(resultSet.getString("STYLES.NAME"));