From: David ‘Bombe’ Roden Date: Tue, 31 Jul 2012 05:01:17 +0000 (+0200) Subject: Add method that loads properties of several objects at once. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b2b9708445bf8b67def72383a63da3285c026339;p=demoscenemusic.git Add method that loads properties of several objects at once. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java index f06a48b..5972bd8 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/data/DataManager.java @@ -21,10 +21,12 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Collection; import java.util.EnumMap; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.UUID; import java.util.concurrent.Callable; @@ -37,6 +39,7 @@ import net.pterodactylus.util.database.Join; import net.pterodactylus.util.database.Join.JoinType; import net.pterodactylus.util.database.ObjectCreator; import net.pterodactylus.util.database.ObjectCreator.StringCreator; +import net.pterodactylus.util.database.OrWhereClause; import net.pterodactylus.util.database.OrderField; import net.pterodactylus.util.database.Parameter.IntegerParameter; import net.pterodactylus.util.database.Parameter.StringParameter; @@ -950,6 +953,53 @@ public class DataManager { } /** + * Loads the properties for the given objects. + * + * @param + * The type of the object + * @param objects + * The objects + * @param table + * The table to load the properties from + * @param type + * The type of the object (“ARTIST,” “TRACK,” etc.) + * @return The object with its properties loaded + * @throws DatabaseException + * if a database error occurs + */ + private List loadProperties(List objects, final String table, final String type) throws DatabaseException { + final Map objectMap = new HashMap(); + final Set firstObjects = new HashSet(); + for (T object : objects) { + objectMap.put(object.getId(), object); + firstObjects.add(object.getId()); + } + Query query = new Query(Type.SELECT, table); + query.addField(new Field(table + "." + type)); + query.addField(new Field(table + ".PROPERTY")); + query.addField(new Field(table + ".VALUE")); + OrWhereClause whereClause = new OrWhereClause(); + for (T object : objects) { + whereClause.add(new ValueFieldWhereClause(new ValueField(type, new StringParameter(object.getId())))); + } + query.addWhereClause(whereClause); + database.process(query, new ResultProcessor() { + + @Override + public void processResult(ResultSet resultSet) throws SQLException { + String id = resultSet.getString(table + "." + type); + T object = objectMap.get(id); + if (firstObjects.remove(id)) { + object.getProperties().removeAll(); + } + object.getProperties().set(resultSet.getString(table + ".PROPERTY"), resultSet.getString(table + ".VALUE")); + } + + }); + return objects; + } + + /** * {@link Artist} implementation that retrieves some attributes (such as * {@link #getGroups()}, and {@link #getTracks()}) from the * {@link DataManager} on demand.