From 9864303a741c81eea7cc8cbfc19c4d12e78d680b Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Fri, 18 Dec 2009 05:38:49 +0000 Subject: [PATCH] Detect duplicate relations during parse, by making use of a map. In addition, store this map for later use. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@892143 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/org/ofbiz/entity/sql/EntityPlanner.java | 2 +- framework/sql/src/org/ofbiz/sql/Parser.jj | 13 ++++++++----- framework/sql/src/org/ofbiz/sql/Relation.java | 6 ++++++ framework/sql/src/org/ofbiz/sql/SQLSelect.java | 8 ++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java b/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java index f9032644ad..9f91766119 100644 --- a/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java +++ b/framework/entity/src/org/ofbiz/entity/sql/EntityPlanner.java @@ -85,7 +85,7 @@ public class EntityPlanner extends Planner groupBy = selectStatement.getGroupBy(); diff --git a/framework/sql/src/org/ofbiz/sql/Parser.jj b/framework/sql/src/org/ofbiz/sql/Parser.jj index 4a8b362388..112e4d7a98 100644 --- a/framework/sql/src/org/ofbiz/sql/Parser.jj +++ b/framework/sql/src/org/ofbiz/sql/Parser.jj @@ -278,8 +278,7 @@ private SQLSelect Select(): Map fieldDefs = FastMap.newInstance(); List fieldAlls = FastList.newInstance(); Table table; - List relations = FastList.newInstance(); - Relation relation; + Map relations = FastMap.newInstance(); Condition whereCondition = null, havingCondition = null; int offset = -1, limit = -1; } @@ -289,7 +288,7 @@ private SQLSelect Select(): ( FieldDef(fieldDefs, fieldAlls) )* ) table=Table() - ( relation=Relation() { relations.add(relation); } )* + ( Relation(relations) )* ( whereCondition=ConditionExpression() )? ( havingCondition=ConditionExpression() )? ( groupBy=FieldList() )? @@ -299,7 +298,7 @@ private SQLSelect Select(): { return new SQLSelect(fieldAlls, fieldDefs, table, relations, whereCondition, havingCondition, groupBy, orderBy, offset, limit); } } -private Relation Relation(): +private void Relation(Map relations): { String type = null, title = null, entityName; List keyMaps; @@ -309,7 +308,11 @@ private Relation Relation(): ( title=NamePart() )? <NAME> { entityName = getToken(0).image; } keyMaps=KeyMaps("cur", "other") - { return new Relation(type, title, entityName, keyMaps); } + { + Relation relation = new Relation(type, title, entityName, keyMaps); + if (relations.containsKey(relation.getName())) throw new IllegalArgumentException("Duplicate relation: " + relation); + relations.put(relation.getName(), relation); + } } diff --git a/framework/sql/src/org/ofbiz/sql/Relation.java b/framework/sql/src/org/ofbiz/sql/Relation.java index b43063d9e3..cbd8112888 100644 --- a/framework/sql/src/org/ofbiz/sql/Relation.java +++ b/framework/sql/src/org/ofbiz/sql/Relation.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; public final class Relation extends Atom implements Iterable<KeyMap> { + private final String name; private final String type; private final String title; private final String entityName; @@ -32,6 +33,11 @@ public final class Relation extends Atom implements Iterable<KeyMap> { this.title = title; this.entityName = entityName; this.keyMaps = keyMaps; + this.name = title == null ? entityName : title + entityName; + } + + public String getName() { + return name; } public String getType() { diff --git a/framework/sql/src/org/ofbiz/sql/SQLSelect.java b/framework/sql/src/org/ofbiz/sql/SQLSelect.java index d5b1a0b1a2..1f3c542bb9 100644 --- a/framework/sql/src/org/ofbiz/sql/SQLSelect.java +++ b/framework/sql/src/org/ofbiz/sql/SQLSelect.java @@ -30,7 +30,7 @@ public final class SQLSelect extends SQLStatement<SQLSelect> implements InsertSo private final List<FieldAll> fieldAlls; private final Map<String, FieldDef> fieldDefs; private final Table table; - private final List<Relation> relations; + private final Map<String, Relation> relations; private final Condition whereCondition; private final Condition havingCondition; private final int offset; @@ -38,7 +38,7 @@ public final class SQLSelect extends SQLStatement<SQLSelect> implements InsertSo private final List<OrderByItem> orderBy; private final List<String> groupBy; - public SQLSelect(List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, List<Relation> relations, Condition whereCondition, Condition havingCondition, List<String> groupBy, List<OrderByItem> orderBy, int offset, int limit) { + public SQLSelect(List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, Map<String, Relation> relations, Condition whereCondition, Condition havingCondition, List<String> groupBy, List<OrderByItem> orderBy, int offset, int limit) { this.fieldAlls = fieldAlls; this.fieldDefs = fieldDefs; this.table = table; @@ -63,7 +63,7 @@ public final class SQLSelect extends SQLStatement<SQLSelect> implements InsertSo return table; } - public List<Relation> getRelations() { + public Map<String, Relation> getRelations() { return relations; } @@ -101,7 +101,7 @@ public final class SQLSelect extends SQLStatement<SQLSelect> implements InsertSo sb.append(" FROM "); table.appendTo(sb); if (!relations.isEmpty()) { - StringUtil.appendTo(sb, relations, " ", null, ","); + StringUtil.appendTo(sb, relations.values(), " ", null, ","); } if (whereCondition != null) { sb.append(" WHERE ");