From 5b98d82f58d22623e2d0643f6e3e990fcac08ff7 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Sat, 17 May 2014 23:53:23 +1200 Subject: [PATCH] No effective change - reformat --- .../server/query/SqlTreeAlias.java | 366 +++++++++--------- 1 file changed, 179 insertions(+), 187 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeAlias.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeAlias.java index 1f8cd1d3b..4d12f48e0 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeAlias.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeAlias.java @@ -18,206 +18,198 @@ import com.avaje.ebeaninternal.server.el.ElPropertyDeploy; */ public class SqlTreeAlias { - private int counter; - - private int manyWhereCounter; - - private TreeSet joinProps = new TreeSet(); + private int counter; - private HashSet embeddedPropertyJoins; + private int manyWhereCounter; - private TreeSet manyWhereJoinProps = new TreeSet(); + private TreeSet joinProps = new TreeSet(); - private HashMap aliasMap = new HashMap(); + private HashSet embeddedPropertyJoins; - private HashMap manyWhereAliasMap = new HashMap(); + private TreeSet manyWhereJoinProps = new TreeSet(); - private final String rootTableAlias; - - public SqlTreeAlias(String rootTableAlias) { - this.rootTableAlias = rootTableAlias; - } + private HashMap aliasMap = new HashMap(); - /** - * Add joins to support where predicates - * @param manyWhereJoins - */ - public void addManyWhereJoins(Set manyWhereJoins) { - if (manyWhereJoins != null){ - for (String include : manyWhereJoins) { - addPropertyJoin(include, manyWhereJoinProps); - } - } + private HashMap manyWhereAliasMap = new HashMap(); + + private final String rootTableAlias; + + public SqlTreeAlias(String rootTableAlias) { + this.rootTableAlias = rootTableAlias; + } + + /** + * Add joins to support where predicates + */ + public void addManyWhereJoins(Set manyWhereJoins) { + if (manyWhereJoins != null) { + for (String include : manyWhereJoins) { + addPropertyJoin(include, manyWhereJoinProps); + } } - - private void addEmbeddedPropertyJoin(String embProp){ - if (embeddedPropertyJoins == null){ - embeddedPropertyJoins = new HashSet(); - } - embeddedPropertyJoins.add(embProp); + } + + private void addEmbeddedPropertyJoin(String embProp) { + if (embeddedPropertyJoins == null) { + embeddedPropertyJoins = new HashSet(); } - - /** - * Add joins. - */ - public void addJoin(Set propJoins, BeanDescriptor desc) { - if (propJoins != null){ - for (String propJoin : propJoins) { - ElPropertyDeploy elProp = desc.getElPropertyDeploy(propJoin); - if (elProp != null && elProp.getBeanProperty().isEmbedded()) { - //String[] split = SplitName.split(propJoin); - //addPropertyJoin(split[0], joinProps); - addEmbeddedPropertyJoin(propJoin); - - } else { - addPropertyJoin(propJoin, joinProps); - } - } - } - } - + embeddedPropertyJoins.add(embProp); + } - - private void addPropertyJoin(String include, TreeSet set){ - if (set.add(include)) { - String[] split = SplitName.split(include); - if (split[0] != null){ - addPropertyJoin(split[0], set); - } - } - } - - /** - * Build a set of table alias for the given bean and fetch - * joined properties. - */ - public void buildAlias() { - - Iterator i = joinProps.iterator(); - while (i.hasNext()) { - calcAlias(i.next()); - } + /** + * Add joins. + */ + public void addJoin(Set propJoins, BeanDescriptor desc) { + if (propJoins != null) { + for (String propJoin : propJoins) { + ElPropertyDeploy elProp = desc.getElPropertyDeploy(propJoin); + if (elProp != null && elProp.getBeanProperty().isEmbedded()) { + addEmbeddedPropertyJoin(propJoin); - i = manyWhereJoinProps.iterator(); - while (i.hasNext()) { - calcAliasManyWhere(i.next()); - } - - mapEmbeddedPropertyAlias(); - } - - private void mapEmbeddedPropertyAlias() { - if (embeddedPropertyJoins != null){ - for (String propJoin : embeddedPropertyJoins) { - String[] split = SplitName.split(propJoin); - // the table alias of the parent path - String alias = getTableAlias(split[0]); - aliasMap.put(propJoin, alias); - } - } - } - - private String calcAlias(String prefix) { - - String alias = nextTableAlias(); - aliasMap.put(prefix, alias); - return alias; - } - - private String calcAliasManyWhere(String prefix) { - - String alias = nextManyWhereTableAlias(); - manyWhereAliasMap.put(prefix, alias); - return alias; - } - - /** - * Return the table alias for a given property name. - */ - public String getTableAlias(String prefix){ - if (prefix == null){ - return rootTableAlias; - } else { - String s = aliasMap.get(prefix); - if (s == null){ - return calcAlias(prefix); - } - return s; - } - } - - /** - * Return an alias using "Many where joins". - */ - public String getTableAliasManyWhere(String prefix) { - if (prefix == null){ - return rootTableAlias; - } - String s = manyWhereAliasMap.get(prefix); - if (s == null){ - s = aliasMap.get(prefix); - } - if (s == null) { - String msg = "Could not determine table alias for [" + prefix + "] manyMap[" - + manyWhereAliasMap + "] aliasMap[" + aliasMap + "]"; - throw new RuntimeException(msg); - } - return s; - } - - /** - * Parse for where clauses that uses "Many where joins" - */ - public String parseWhere(String clause) { - clause = parseRootAlias(clause); - clause = parseAliasMap(clause, manyWhereAliasMap); - return parseAliasMap(clause, aliasMap); - } - - /** - * Parse without using any extra "Many where joins". - */ - public String parse(String clause) { - clause = parseRootAlias(clause); - return parseAliasMap(clause, aliasMap); - } - - /** - * Parse the clause replacing the table alias place holders. - */ - private String parseRootAlias(String clause) { - - if (rootTableAlias == null){ - return clause.replace("${}", ""); } else { - return clause.replace("${}", rootTableAlias+"."); + addPropertyJoin(propJoin, joinProps); } + } } - - /** - * Parse the clause replacing the table alias place holders. - */ - private String parseAliasMap(String clause, HashMap parseAliasMap) { + } - Iterator> i = parseAliasMap.entrySet().iterator(); - while (i.hasNext()) { - Map.Entry e = i.next(); - String k = "${"+e.getKey()+"}"; - clause = clause.replace(k, e.getValue()+"."); - } - - return clause; - } - - - /** - * Return the next valid table alias given the preferred table alias. - */ - private String nextTableAlias() { - return "t"+(++counter); - } - - private String nextManyWhereTableAlias() { - return "u"+(++manyWhereCounter); + private void addPropertyJoin(String include, TreeSet set) { + if (set.add(include)) { + String[] split = SplitName.split(include); + if (split[0] != null) { + addPropertyJoin(split[0], set); + } } + } + + /** + * Build a set of table alias for the given bean and fetch joined properties. + */ + public void buildAlias() { + + Iterator i = joinProps.iterator(); + while (i.hasNext()) { + calcAlias(i.next()); + } + + i = manyWhereJoinProps.iterator(); + while (i.hasNext()) { + calcAliasManyWhere(i.next()); + } + + mapEmbeddedPropertyAlias(); + } + + private void mapEmbeddedPropertyAlias() { + if (embeddedPropertyJoins != null) { + for (String propJoin : embeddedPropertyJoins) { + String[] split = SplitName.split(propJoin); + // the table alias of the parent path + String alias = getTableAlias(split[0]); + aliasMap.put(propJoin, alias); + } + } + } + + private String calcAlias(String prefix) { + + String alias = nextTableAlias(); + aliasMap.put(prefix, alias); + return alias; + } + + private String calcAliasManyWhere(String prefix) { + + String alias = nextManyWhereTableAlias(); + manyWhereAliasMap.put(prefix, alias); + return alias; + } + + /** + * Return the table alias for a given property name. + */ + public String getTableAlias(String prefix) { + if (prefix == null) { + return rootTableAlias; + } else { + String s = aliasMap.get(prefix); + if (s == null) { + return calcAlias(prefix); + } + return s; + } + } + + /** + * Return an alias using "Many where joins". + */ + public String getTableAliasManyWhere(String prefix) { + if (prefix == null) { + return rootTableAlias; + } + String s = manyWhereAliasMap.get(prefix); + if (s == null) { + s = aliasMap.get(prefix); + } + if (s == null) { + String msg = "Could not determine table alias for [" + prefix + "] manyMap[" + manyWhereAliasMap + "] aliasMap["+ aliasMap + "]"; + throw new RuntimeException(msg); + } + return s; + } + + /** + * Parse for where clauses that uses "Many where joins" + */ + public String parseWhere(String clause) { + clause = parseRootAlias(clause); + clause = parseAliasMap(clause, manyWhereAliasMap); + return parseAliasMap(clause, aliasMap); + } + + /** + * Parse without using any extra "Many where joins". + */ + public String parse(String clause) { + clause = parseRootAlias(clause); + return parseAliasMap(clause, aliasMap); + } + + /** + * Parse the clause replacing the table alias place holders. + */ + private String parseRootAlias(String clause) { + + if (rootTableAlias == null) { + return clause.replace("${}", ""); + } else { + return clause.replace("${}", rootTableAlias + "."); + } + } + + /** + * Parse the clause replacing the table alias place holders. + */ + private String parseAliasMap(String clause, HashMap parseAliasMap) { + + Iterator> i = parseAliasMap.entrySet().iterator(); + while (i.hasNext()) { + Map.Entry e = i.next(); + String k = "${" + e.getKey() + "}"; + clause = clause.replace(k, e.getValue() + "."); + } + + return clause; + } + + /** + * Return the next valid table alias given the preferred table alias. + */ + private String nextTableAlias() { + return "t" + (++counter); + } + + private String nextManyWhereTableAlias() { + return "u" + (++manyWhereCounter); + } }