r2403 - in trunk: topia-persistence/src/main/java/org/nuiton/topia/framework topia-tutorial
Author: tchemit Date: 2012-02-01 14:07:33 +0100 (Wed, 01 Feb 2012) New Revision: 2403 Url: http://nuiton.org/repositories/revision/topia/2403 Log: Evolution #1931: Improve TopiaQuery Anomalie #1930: dao.countByquery does not work in some cases Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java trunk/topia-tutorial/ 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 2012-01-18 16:25:55 UTC (rev 2402) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaQuery.java 2012-02-01 13:07:33 UTC (rev 2403) @@ -242,20 +242,31 @@ /** Params for HQL query * */ protected List<Object> params; - /** Select part of the query * */ - protected StringBuilder select; +// /** Select part of the query * */ +// protected StringBuilder select; + /** + * To keep select part of the query filled by user. + * + * @since 2.6.7 + */ + protected List<String> userSelects; + protected boolean distinct; + //TODO tchemit-2012-02-01 Remove this (see Evol #1931) /** From part of the query * */ protected StringBuilder from; + //TODO tchemit-2012-02-01 Remove this (see Evol #1931) /** Where part of the query * */ protected StringBuilder where; + //TODO tchemit-2012-02-01 Remove this (see Evol #1931) /** Order By part of the query * */ protected StringBuilder orderBy; + //TODO tchemit-2012-02-01 Remove this (see Evol #1931) /** Group By part of the query * */ protected StringBuilder groupBy; @@ -273,23 +284,23 @@ /** Enum to simmplify using operation in query */ public enum Op { - /** EQUALS * */ + /** EQUALS */ EQ("="), - /** GREATER THAN * */ + /** GREATER THAN */ GT(">"), - /** GREATER OR EQUALS * */ + /** GREATER OR EQUALS */ GE(">="), - /** LIKE for String manipulation * */ + /** LIKE for String manipulation */ LIKE("LIKE"), - /** LESS THAN * */ + /** LESS THAN */ LT("<"), - /** LESS OR EQUALS * */ + /** LESS OR EQUALS */ LE("<="), - /** IS NOT NULL * */ + /** IS NOT NULL */ NOT_NULL("IS NOT NULL"), - /** IS NULL * */ + /** IS NULL */ NULL("IS NULL"), - /** NOT EQUAL * */ + /** NOT EQUAL */ NEQ("!="); protected String value; @@ -388,6 +399,7 @@ * the query or for join as specific {@code separator}. * * @param property the property to add + * @param alias alias of the property to add in form part of the query * @param separator The separator to use before adding the element (if null * the {@link #FROM_SEPARATOR_DEFAULT} will be used). * @return the TopiaQuery @@ -485,15 +497,26 @@ if (distinct) { selectStatement.append("DISTINCT "); } - if (select != null) { - result.append(selectStatement).append(select); + if (CollectionUtils.isNotEmpty(userSelects)) { + + result.append(selectStatement).append(StringUtils.join(userSelects, ',')); + } else if (StringUtils.contains(from.toString(), ',') && + StringUtils.isNotEmpty(mainAlias)) { // 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); } + +// if (select != null) { +// 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); +// } result.append(from); if (where != null) { result.append(where); @@ -603,7 +626,10 @@ } // Init select for single result - if (select == null) { +// if (select == null) { +// setSelect(mainAlias); +// } + if (userSelects == null) { setSelect(mainAlias); } @@ -1025,21 +1051,42 @@ * @return the TopiaQuery */ public TopiaQuery addSelect(String... select) { - String str = convertStringArray(select); - // if select is the mainAlias, do nothing - if (mainAlias != null && str.equals(mainAlias)) { - return this; + +// if (mainAlias != null && +// CollectionUtils.isNotEmpty(userSelects) && +// userSelects.contains(mainAlias)) { +// // if select is the mainAlias, do nothing +// return this; +// } +// String str = convertStringArray(select); +// // if select is the mainAlias, do nothing +// if (mainAlias != null && str.equals(mainAlias)) { +// return this; +// } + + if (userSelects == null) { + + userSelects = new ArrayList<String>(); + + if (mainAlias != null) { + + // if mainAlias is not null, add it before adding the select in argument + userSelects.add(mainAlias); + } } - // if select is not null, add the new element to the select - if (this.select != null) { - this.select.append(", "); - // if mainAlias is not null, add it before adding the select in argument - } else if (mainAlias != null) { - this.select = new StringBuilder(mainAlias).append(", "); - } else { - this.select = new StringBuilder(); - } - this.select.append(convertStringArray(select)); + + userSelects.addAll(Arrays.asList(select)); + +// // if select is not null, add the new element to the select +// if (this.select != null) { +// this.select.append(", "); +// // if mainAlias is not null, add it before adding the select in argument +// } else if (mainAlias != null) { +// this.select = new StringBuilder(mainAlias).append(", "); +// } else { +// this.select = new StringBuilder(); +// } +// this.select.append(convertStringArray(select)); return this; } @@ -1051,7 +1098,8 @@ * @return the TopiaQuery */ public TopiaQuery setSelect(String... select) { - this.select = new StringBuilder(convertStringArray(select)); + userSelects = new ArrayList<String>(Arrays.asList(select)); +// this.select = new StringBuilder(convertStringArray(select)); return this; } @@ -1404,7 +1452,8 @@ */ public Object executeToObject(TopiaContext transaction, String select) throws TopiaException { - StringBuilder oldValue = this.select; + List<String> oldValue = this.userSelects; +// StringBuilder oldValue = this.select; if (!StringUtils.isEmpty(select)) { setSelect(select); } @@ -1414,7 +1463,8 @@ if (!results.isEmpty()) { result = results.get(0); } - this.select = oldValue; +// this.select = oldValue; + userSelects = oldValue; resetLimit(); return result; } @@ -1470,9 +1520,40 @@ // Ano #560 : manage distinct case when the alias is set (otherwise // no need the distinct keyword because the entity type in query is // unique) - if (distinct && StringUtils.isNotEmpty(mainAlias)) { - count.append("DISTINCT ").append(mainAlias); - // When distinct is not set, use <pre>*</pre> + + //Note Ano #1930 tchemit 2012-02-01 this code does not work if a setSelect was done + +// if (distinct && StringUtils.isNotEmpty(mainAlias) && +// isUserSelectEqualsMainAlias()) { +// count.append("DISTINCT ").append(mainAlias); +// // When distinct is not set, use <pre>*</pre> +// } else { +// count.append('*'); +// } + + String mainSelect = null; + + if (distinct) { + + if (CollectionUtils.isNotEmpty(userSelects)) { + + if (userSelects.size() > 1) { + + // can not count this on multiple select... + throw new TopiaException( + "To count, can not have more than one select, but found " + + userSelects); + } + mainSelect = userSelects.get(0); + } else if (StringUtils.isNotEmpty(mainAlias)) { + + // use main alias + mainSelect = mainAlias; + } + } + + if (mainSelect != null) { + count.append("DISTINCT ").append(mainSelect); } else { count.append('*'); } @@ -1481,6 +1562,13 @@ orderBy = oldOrder; return result; } + + protected boolean isUserSelectEqualsMainAlias() { + boolean result = !CollectionUtils.isEmpty(userSelects) && + userSelects.size()==1 && + userSelects.get(0).equals(mainAlias); + return result; + } /** * Load all properties for the entity. @@ -1615,7 +1703,7 @@ @Override protected void finalize() throws Throwable { // Clean StringBuilder statements - select = null; +// select = null; from = null; where = null; orderBy = null; Property changes on: trunk/topia-tutorial ___________________________________________________________________ Modified: svn:ignore - target .settings .project + target .settings .project $.ipr *.iws *.iml
participants (1)
-
tchemit@users.nuiton.org