From: David ‘Bombe’ Roden Date: Wed, 18 Apr 2012 05:38:05 +0000 (+0200) Subject: Add first stab at database interface. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4afa9c06e910ed4d8b005fda571d0e7c62822317;p=demoscenemusic.git Add first stab at database interface. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java new file mode 100644 index 0000000..56d5cea --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -0,0 +1,122 @@ +/* + * DemosceneMusic - DataManager.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.demoscenemusic.data; + +import java.sql.ResultSet; +import java.sql.SQLException; + +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.Parameter.StringParameter; +import net.pterodactylus.util.database.Query; +import net.pterodactylus.util.database.Query.Type; +import net.pterodactylus.util.database.ValueField; +import net.pterodactylus.util.database.ValueFieldWhereClause; + +/** + * TODO + * + * @author David ‘Bombe’ Roden + */ +public class DataManager { + + 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("ARTISTS.*")); + 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); + } + + private class TrackLoader implements ObjectCreator { + + /** + * {@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 ArtistLoader implements ObjectCreator { + + /** + * {@inheritDoc} + */ + public Artist createObject(ResultSet resultSet) throws SQLException { + return new AbstractArtist(resultSet.getString("ARTISTS.ID")) { + + public void save() { + if (!dirty()) { + return; + } + } + + }.name(resultSet.getString("ARTISTS.NAME")); + } + + } + + private class StyleLoader implements ObjectCreator