Index: topia/src/java/org/codelutin/topia/persistence/TopiaCollectionQueryHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaCollectionQueryHelper.java:1.3 topia/src/java/org/codelutin/topia/persistence/TopiaCollectionQueryHelper.java:1.4 --- topia/src/java/org/codelutin/topia/persistence/TopiaCollectionQueryHelper.java:1.3 Fri Jun 17 14:15:06 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaCollectionQueryHelper.java Tue Jun 21 17:00:43 2005 @@ -24,7 +24,7 @@ * Created: 13 juin 2005 * * @author Arnaud Thimel -* @version $Revision: 1.3 $ +* @version $Revision: 1.4 $ */ @@ -35,6 +35,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -97,7 +98,8 @@ throw new QueryHelperException("Please use setQuery(...) " + "before execute()"); - if (queryBuilder == null) { //Préparation de la requete pour BSH + // Préparation de la requete pour BSH + if (queryBuilder == null) { queryBuilder = new TopiaCollectionQueryBuilder(); getQuery().visit(queryBuilder); inter = new Interpreter(); @@ -111,6 +113,7 @@ generateMethodsForBSH(inter); } + // Filter List result = new ArrayList(); for (Iterator it = datas.iterator(); it.hasNext(); ) { Object o = it.next(); @@ -118,8 +121,9 @@ result.add(o); } + // FieldPath if (queryBuilder.getFieldName() != null) { - List resultAccordingToFieldPath = new ArrayList(); + List resultAccordingToFieldPath = new ArrayList(result.size()); for (Iterator it = result.iterator(); it.hasNext(); ) { Object o = eval(it.next(), queryBuilder.getFieldName(), inter); if (o != null) @@ -128,6 +132,18 @@ result = resultAccordingToFieldPath; } + // Distinct + if (queryBuilder.isDistinct()) { + List resultAfterDistinct = new ArrayList(result.size()); + for (Iterator it = result.iterator(); it.hasNext(); ) { + Object o = it.next(); + if (!resultAfterDistinct.contains(o)) + resultAfterDistinct.add(o); + } + result = resultAfterDistinct; + } + + // OrderBy Collections.sort(result, new TopiaCollectionQueryComparator( queryBuilder.getOrderBy(), inter)); @@ -197,7 +213,9 @@ } - + /** + * Implémente un comparateur pour le orderBy utilisant un interprêteur BSH + */ public class TopiaCollectionQueryComparator implements Comparator { Map orderBy; @@ -241,6 +259,20 @@ public class TopiaCollectionQueryBuilder extends DefaultQueryVisitorUnsupportedOperation { + protected String className; + protected String fieldName; + protected String from; + protected String filter = ""; + //LinkedHashMap instead of Map to keep order between fields + protected Map orderBy = new LinkedHashMap(); + protected boolean distinct = false; + + protected Collection argCollection = new LinkedList(); + + protected int argCount = 1; + + protected Query query; + public String getClassName() { return className; } @@ -259,19 +291,10 @@ public Map getOrderBy() { return orderBy; } + public boolean isDistinct() { + return distinct; + } - protected String className; - protected String fieldName; - protected String from; - protected String filter = ""; - //LinkedHashMap instead of Map to keep order between fields - protected Map orderBy = new LinkedHashMap(); - - protected Collection argCollection = new LinkedList(); - - protected int argCount = 1; - - protected Query query; public void visitFrom(From from) { } @@ -309,8 +332,7 @@ System.out.println("\"selectCount\" " + select); } public void visitSelectDistinct(Select select) { - // TODO Arno : Auto-generated method stub - System.out.println("\"selectDistinct\" " + select); + distinct = true; } public void visitSelectField(Select select, FieldPath field, String alias) {