Author: fdesbois Date: 2010-05-26 16:17:56 +0200 (Wed, 26 May 2010) New Revision: 1979 Url: http://nuiton.org/repositories/revision/topia/1979 Log: Change some method names in TopiaQuery Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 11:49:20 UTC (rev 1978) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2010-05-26 14:17:56 UTC (rev 1979) @@ -227,8 +227,8 @@ * @version $Revision$ * @since 2.3.0 * <p/> - * Mise a jour: $Date$ - * par : $Author$ + * Mise a jour: $Date$ par + * : $Author$ */ public class TopiaQuery { @@ -471,6 +471,7 @@ result.append(selectStatement).append(select); // Set default select if there is more than one table in from // part and main alias is defined + // Note : maybe a problem with using new {@link #addFrom(String, String) with other than ',' separator } else if (StringUtils.contains(from.toString(), ',') && StringUtils.isNotEmpty(mainAlias)) { result.append(selectStatement).append(mainAlias); @@ -518,66 +519,6 @@ return this; } - /** - * Method used to add a subquery in an existing query. The params will be - * automatically checked and copied from the subquery to the current one. - * This method is used to inject {@code subquery} in WHERE part of the - * query. The {@code queryPart} is the element in the query to bind with the - * {@code subquery}. The ? character is used to inject the subquery into the - * {@code queryPart}. Ex : - * <pre> - * // Add a SUB_ELMT = (subquery) into the query - * query.addSubQuery("SUB_ELMT = (?)", subquery, false); - * </pre> - * - * @param queryPart part of the query where subquery need to be injected - * @param subquery existing topiaQuery as subquery - * @return the TopiaQuery - * @see TopiaQuery#getValueName(String) - * @since 2.4 - */ - public TopiaQuery addSubQuery(String queryPart, - TopiaQuery subquery) { - - List<Object> subqueryParams = subquery.getParams(); - String subqueryString = subquery.fullQuery(); - - // If no params is still defined, use those from subquery. - if (CollectionUtils.isEmpty(params)) { - addParams(subqueryParams); - } else { - for (int i = 0; i < subqueryParams.size(); i += 2) { - - String paramName = (String) subqueryParams.get(i); - Object paramValue = subqueryParams.get(i + 1); - - // Check existence of paramName - int index = params.indexOf(paramName); - - // If already defined - if (index != -1) { - Object existingValue = params.get(index + 1); - - // Only change paramName in not equals case - if (!ObjectUtils.equals(existingValue, paramValue)) { - String newParamName = getValueName(paramName); - // Replace old paramName in subquery - subqueryString = - subqueryString.replace(":" + paramName, - ":" + newParamName); - - // Add the param to the current query - addParam(newParamName, paramValue); - } - } - } - } - - // Replace ? injection by the subquery - String result = queryPart.replace("?", subqueryString); - return add(result); - } - public List<Object> getParams() { if (params == null) { params = new ArrayList<Object>(); @@ -624,13 +565,24 @@ } /** + * @param where + * @return TopiaQuery + * @deprecated since 2.4, use {@link #addWhere(String)} instead + */ + @Deprecated + public TopiaQuery add(String where) { + return addWhere(where); + } + + /** * Add a where element to the Query. Could be anything. Parentheses are * added automatically (even if there are not needed). * * @param where element to add * @return the TopiaQuery + * @since 2.4 */ - public TopiaQuery add(String where) { + public TopiaQuery addWhere(String where) { if (StringUtils.isEmpty(where)) { return this; } @@ -652,20 +604,35 @@ } /** + * @param paramName + * @param constraint + * @param paramValue + * @return TopiaQuery + * @deprecated since 2.4, use {@link #addWhere(String, Op, Object)} instead + */ + @Deprecated + public TopiaQuery add(String paramName, Op constraint, Object paramValue) { + return addWhere(paramName, constraint, paramValue); + } + + /** * Add an element to the query. The parameter will be automatically added. - * The constraint is needed to determine what type of operation it is. Ex : - * add("boat", Op.EQ, boat) means -> boat = :boat. If the paramValue is - * Null, the paramName will be added to the query with the constraint null - * (IS NULL). + * The {@code operator} is needed to determine what type of operation it is. + * Ex : add("boat", Op.EQ, boat) means -> boat = :boat. Also if the paramValue + * is Null, the paramName will be added to the query with the constraint + * null (IS NULL). + * <p/> + * TODO-fdesbois-2010-05-26 : maybe manage more than one paramValue with Object... Depends on operator * * @param paramName the name of the parameter in the query (attribute of * the entity) - * @param constraint the operation concerned + * @param operator the operation concerned * @param paramValue the value of the parameter (an other entity, a String, * ...) * @return the TopiaQuery + * @since 2.4 */ - public TopiaQuery add(String paramName, Op constraint, Object paramValue) { + public TopiaQuery addWhere(String paramName, Op operator, Object paramValue) { StringBuilder result = new StringBuilder(paramName).append(' '); if (log.isDebugEnabled()) { log.debug("paramValue = " + paramValue); @@ -674,34 +641,12 @@ result.append(Op.NULL); } else { String valueName = getValueName(paramName); - result.append(constraint).append(" :").append(valueName); + result.append(operator).append(" :").append(valueName); addParam(valueName, paramValue); } parentheses = false; - return add(result.toString()); - } + return addWhere(result.toString()); - /** - * Add an element to the query. The nullity is tested or a constraint is - * added for that element. Ex : addNullOr("begin", Op.GT, new Date()) means - * begin IS NULL OR begin > :begin (where :begin = new Date()). - * - * @param paramName the name of the parameter in the query (attribute of - * the entity) - * @param constraint the operation concerned by the or - * @param paramValue the value of the parameter (an other entity, a String, - * ...) - * @return the TopiaQuery - */ - public TopiaQuery addNullOr(String paramName, Op constraint, - Object paramValue) { - String valueName = getValueName(paramName); - StringBuilder result = - new StringBuilder(paramName).append(' ').append(Op.NULL). - append(" OR ").append(paramName).append(constraint). - append(" :").append(valueName); - addParam(valueName, paramValue); - return add(result.toString()); } protected String getValueName(String paramName) { @@ -718,15 +663,14 @@ } /** - * Add an element to the query with the constraint Not null. - * - * @param paramName name of the parameter in the query - * @return the TopiaQuery + * @param paramName + * @param paramValue + * @return TopiaQuery + * @since 2.3.1 + * @deprecated since 2.4, use {@link #addEquals(String, Object...)} instead */ - public TopiaQuery addNotNull(String paramName) { - StringBuilder result = - new StringBuilder(paramName).append(' ').append(Op.NOT_NULL); - return add(result.toString()); + public TopiaQuery add(String paramName, Object... paramValue) { + return addEquals(paramName, paramValue); } /** @@ -736,16 +680,19 @@ * statement IN will be used. You can also have a null value in the {@code * paramValue} list (except if it's the only one value, it's ambiguous). * Note : this method do nothing if the {@code paramValue} is not defined. - * If you want to add the constraint null on the {@code paramName} use - * {@link #add(String, Op, Object)} as : add("param", Op.EQ, null); + * You can also set {@code paramValue} to null if you want the {@code + * paramName} to be null in the query. * * @param paramName name of the parameter in the query * @param paramValue values of the parameter * @return the TopiaQuery - * @see TopiaQuery#add(String, Op, Object) - * @since 2.3.1 + * @see TopiaQuery#addWhere(String, Op, Object) + * @since 2.4 */ - public TopiaQuery add(String paramName, Object... paramValue) { + public TopiaQuery addEquals(String paramName, Object... paramValue) { + if (paramValue == null) { + return addWhere(paramName, Op.EQ, null); + } int length = paramValue.length; // Do nothing if there is no value defined if (length == 0) { @@ -756,7 +703,7 @@ if (log.isDebugEnabled()) { log.debug("Only one value " + Arrays.toString(paramValue)); } - return add(paramName, Op.EQ, paramValue[0]); + return addWhere(paramName, Op.EQ, paramValue[0]); } // Multiple values is defined StringBuilder values = new StringBuilder(); @@ -779,7 +726,7 @@ } // Create buffer for IN statement with values StringBuilder buffer = new StringBuilder(); - buffer.append(paramName).append(" IN(").append(values).append(")"); + buffer.append(paramName).append(" IN (").append(values).append(")"); // Add the OR statement for null value if needed if (addNull) { buffer.append(" OR "). @@ -788,10 +735,20 @@ // no parentheses needed in this case (no OR statement) parentheses = false; } - return add(buffer.toString()); + return addWhere(buffer.toString()); } /** + * @param properties + * @return TopiaQuery + * @deprecated since 2.4 use {@link #addEquals(Map)} + */ + @Deprecated + public TopiaQuery add(Map<String, Object> properties) { + return addEquals(properties); + } + + /** * Add a map of properties to the where clause of the query. Each property * will be added to the query with Op.EQ operation, the key in the map is * the property name, and the value is the value of the parameter in the @@ -799,15 +756,51 @@ * * @param properties to add to the query * @return the TopiaQuery + * @since 2.4 */ - public TopiaQuery add(Map<String, Object> properties) { + public TopiaQuery addEquals(Map<String, Object> properties) { for (String key : properties.keySet()) { - add(key, properties.get(key)); + addEquals(key, properties.get(key)); } return this; } /** + * Add an element to the query with the constraint Not null. + * + * @param paramName name of the parameter in the query + * @return the TopiaQuery + */ + public TopiaQuery addNotNull(String paramName) { + StringBuilder result = + new StringBuilder(paramName).append(' ').append(Op.NOT_NULL); + return addWhere(result.toString()); + } + + /** + * Add an element to the query. The nullity is tested or a constraint is + * added for that element. Ex : addNullOr("begin", Op.GT, new Date()) means + * begin IS NULL OR begin > :begin (where :begin = new Date()). + * + * @param paramName the name of the parameter in the query (attribute of + * the entity) + * @param constraint the operation concerned by the or + * @param paramValue the value of the parameter (an other entity, a String, + * ...) + * @return the TopiaQuery + */ + public TopiaQuery addNullOr(String paramName, Op constraint, + Object paramValue) { + String valueName = getValueName(paramName); + StringBuilder result = + new StringBuilder(paramName).append(' ').append(Op.NULL). + append(" OR ").append(paramName).append(constraint). + append(" :").append(valueName); + addParam(valueName, paramValue); + return addWhere(result.toString()); + } + + /** * Add link constraint between two properties. {@code propertyA} is in * elements of {@code propertyB} which is a collection with same type than * {@code propertyA}. (HQL : A IN elements (B)) @@ -819,16 +812,74 @@ */ public TopiaQuery addInElements(String propertyA, String propertyB) { StringBuilder builder = new StringBuilder(propertyA). - append(" IN elements ("). - append(propertyB). - append(')'); + append(" IN elements(").append(propertyB).append(')'); parentheses = false; - add(builder.toString()); + addWhere(builder.toString()); parentheses = true; return this; } /** + * Method used to add a subquery in an existing query. The params will be + * automatically checked and copied from the subquery to the current one. + * This method is used to inject {@code subquery} in WHERE part of the + * query. The {@code queryPart} is the element in the query to bind with the + * {@code subquery}. The ? character is used to inject the subquery into the + * {@code queryPart}. Ex : + * <pre> + * // Add a SUB_ELMT = (subquery) into the query + * query.addSubQuery("SUB_ELMT = (?)", subquery, false); + * </pre> + * + * @param queryPart part of the query where subquery need to be injected + * @param subquery existing topiaQuery as subquery + * @return the TopiaQuery + * @see TopiaQuery#getValueName(String) + * @since 2.4 + */ + public TopiaQuery addSubQuery(String queryPart, + TopiaQuery subquery) { + + List<Object> subqueryParams = subquery.getParams(); + String subqueryString = subquery.fullQuery(); + + // If no params is still defined, use those from subquery. + if (CollectionUtils.isEmpty(params)) { + addParams(subqueryParams); + } else { + for (int i = 0; i < subqueryParams.size(); i += 2) { + + String paramName = (String) subqueryParams.get(i); + Object paramValue = subqueryParams.get(i + 1); + + // Check existence of paramName + int index = params.indexOf(paramName); + + // If already defined + if (index != -1) { + Object existingValue = params.get(index + 1); + + // Only change paramName in not equals case + if (!ObjectUtils.equals(existingValue, paramValue)) { + String newParamName = getValueName(paramName); + // Replace old paramName in subquery + subqueryString = + subqueryString.replace(":" + paramName, + ":" + newParamName); + + // Add the param to the current query + addParam(newParamName, paramValue); + } + } + } + } + + // Replace ? injection by the subquery + String result = queryPart.replace("?", subqueryString); + return addWhere(result); + } + + /** * Add an element to the select in the query. Depends on the result wanted * in execute methods. The main entity will be automatically added only if * an alias is initialize from constructor. If you want only this select Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-26 11:49:20 UTC (rev 1978) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAOImpl.java 2010-05-26 14:17:56 UTC (rev 1979) @@ -469,7 +469,7 @@ @Override public E findByTopiaId(String id) throws TopiaException { - TopiaQuery query = createQuery().add(TopiaEntity.TOPIA_ID, id); + TopiaQuery query = createQuery().addEquals(TopiaEntity.TOPIA_ID, id); E result = findByQuery(query); return result; } @@ -495,7 +495,7 @@ @Override public E findByProperties(Map<String, Object> properties) throws TopiaException { - TopiaQuery query = createQuery().add(properties); + TopiaQuery query = createQuery().addEquals(properties); E result = findByQuery(query); return result; } @@ -540,7 +540,7 @@ @Override public List<E> findAllByProperties(Map<String, Object> properties) throws TopiaException { - TopiaQuery query = createQuery().add(properties); + TopiaQuery query = createQuery().addEquals(properties); List<E> results = findAllByQuery(query); return results; } @@ -588,7 +588,7 @@ Object... others) throws TopiaException { Map<String, Object> properties = convertPropertiesArrayToMap(propertyName, propertyValue, others); - TopiaQuery query = createQuery().add(properties); + TopiaQuery query = createQuery().addEquals(properties); boolean result = existByQuery(query); return result; } @@ -789,28 +789,6 @@ "l'obbjet " + e, eee); } } -// } catch (IllegalAccessException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } catch (InvocationTargetException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } catch (NoSuchMethodException eee) { -// ok = false; -// if (log.isWarnEnabled()) { -// log.warn( -// "Impossible d'acceder a la methode demandé pour l'obbjet " -// + e, eee); -// } -// } if (ok) { result.add(e); } Modified: trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 11:49:20 UTC (rev 1978) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/framework/TopiaQueryTest.java 2010-05-26 14:17:56 UTC (rev 1979) @@ -42,16 +42,16 @@ // Test with one paramValue String value = "topia"; TopiaQuery query = new TopiaQuery(QueriedEntity.class); - query.add(QueriedEntity.TEST_ADD, value); + query.addEquals(QueriedEntity.TEST_ADD, value); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + "WHERE testAdd = :testAdd", query.fullQuery()); // Test with null paramValue - String nullValue = null; + //String nullValue = null; query = new TopiaQuery(QueriedEntity.class); - query.add(QueriedEntity.TEST_ADD, nullValue); + query.addEquals(QueriedEntity.TEST_ADD, null); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + "WHERE testAdd IS NULL", @@ -60,18 +60,18 @@ // Test with two paramValues String value2 = "eugene"; query = new TopiaQuery(QueriedEntity.class); - query.add(QueriedEntity.TEST_ADD, value, value2); + query.addEquals(QueriedEntity.TEST_ADD, value, value2); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE testAdd IN(:testAdd1, :testAdd2)", + "WHERE testAdd IN (:testAdd1, :testAdd2)", query.fullQuery()); // Test with two paramValues + null query = new TopiaQuery(QueriedEntity.class); - query.add(QueriedEntity.TEST_ADD, value, value2, null); + query.addEquals(QueriedEntity.TEST_ADD, value, value2, null); Assert.assertEquals( "FROM org.nuiton.topiatest.QueriedEntity " + - "WHERE (testAdd IN(:testAdd1, :testAdd2) OR testAdd IS NULL)", + "WHERE (testAdd IN (:testAdd1, :testAdd2) OR testAdd IS NULL)", query.fullQuery()); } @@ -80,12 +80,12 @@ // Test 1 : Subquery with two params with different values TopiaQuery query = new TopiaQuery(QueriedEntity.class). - add(QueriedEntity.TEST_ADD, "value1"); + addEquals(QueriedEntity.TEST_ADD, "value1"); // Exist 2 params Assert.assertEquals(2, query.getParams().size()); TopiaQuery subquery = new TopiaQuery(QueriedEntity.class). - add(QueriedEntity.TEST_ADD, "value2"); + addEquals(QueriedEntity.TEST_ADD, "value2"); query.addSubQuery("Q1 = (?)", subquery); log.debug(query); @@ -96,12 +96,12 @@ // Test 2 : Subquery with two params with different values // one of them is null query = new TopiaQuery(QueriedEntity.class). - add(QueriedEntity.TEST_ADD, TopiaQuery.Op.EQ, null); + addWhere(QueriedEntity.TEST_ADD, TopiaQuery.Op.EQ, null); // Exist 0 param (null value) Assert.assertEquals(0, query.getParams().size()); subquery = new TopiaQuery(QueriedEntity.class). - add(QueriedEntity.TEST_ADD, "value1"); + addEquals(QueriedEntity.TEST_ADD, "value1"); query.addSubQuery("Q1 = (?)", subquery); log.debug(query); @@ -111,12 +111,12 @@ // Test 3 : Subquery with two params with same value query = new TopiaQuery(QueriedEntity.class, "Q1"). - add(QueriedEntity.TEST_ADD, "value1"); + addEquals(QueriedEntity.TEST_ADD, "value1"); // Exist 2 params Assert.assertEquals(2, query.getParams().size()); subquery = new TopiaQuery(QueriedEntity.class, "Q2"). - add(QueriedEntity.TEST_ADD, "value1"); + addEquals(QueriedEntity.TEST_ADD, "value1"); query.addSubQuery("Q1 = (?)", subquery); log.debug(query);