GeezerVison is showing github.com — Update docu, fix some problems with the leading elements in join scen… · Xiermires/EntityMatcher@36cf79c
File treeExpand file treeCollapse file tree Open diff view settings test/java/org/matcher/name
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +93-8Lines changed: 93 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4,12 +4,19 @@ Enhance an EntityManager to create JPQL queries through matchers. | 4 | 4 |
| 5 | 5 | The matchers can be created both using the names of the Entities properties, or a Entity matching "mock" conveniently prepared to capture method invokations. | 6 | 6 |
| 7 | | -```final Person mtch = BeanBasedMatcher.matcher(Person.class);``` | | 7 | +This WHERE statement... | 8 | 8 |
| 9 | | -The following matchers are equivalent. | | 9 | +```sql WHERE person.name LIKE 'X%' OR person.surname LIKE 'Y%' OR person.eyes = 'blue'``` | 10 | 10 |
| 11 | | -```java matching("name", startsWith("X").or("Y")).or("eyes", eq("blue"))``` | 12 | | -```java matching(mtch.getName(), startsWith("X").or("Y")).or(mtch.getEyes(), eq("blue"))``` | | 11 | +is equivalent to the following generated matchers. | | 12 | + | | 13 | +```java matching(Person.class, "name", startsWith("X").or("surname", startsWith("Y")).or("eyes", eq("blue")))``` | | 14 | +```java | | 15 | +final Person mtch = BeanBasedMatcher.matcher(Person.class); | | 16 | +matching(mtch.getName(), startsWith("X").or(startsWith("Y")).or(mtch.getEyes(), eq("blue"))) | | 17 | +``` | | 18 | + | | 19 | +From now on, we will focus only in the using names approach, although examples can be easily switched to its bean alternative. | 13 | 20 |
| 14 | 21 | ### Step 0. Configuration | 15 | 22 |
| @@ -48,13 +55,91 @@ final EntityManager em = Persistence.createEntityManagerFactory("test").createEn | 48 | 55 | final EntityMatcher matcher = new EntityMatcher(em); | 49 | 56 | ``` | 50 | 57 |
| 51 | | -### Step 2. Using the matcher | | 58 | +### Step 2. Usage | 52 | 59 |
| 53 | | -Let's assume a SQL Table 'Person' with properties 'name', 'surname', 'height_cm', 'weight_kg' | | 60 | +Let's assume the following two SQL Tables | | 61 | + | | 62 | +1. 'Person' with properties 'name', 'height_cm', 'weight_kg', 'age' | | 63 | +2. 'Pet' with properties 'name', 'owner', 'species', 'age' | | 64 | + | | 65 | +#### The basics | 54 | 66 |
| 55 | 67 | + natural: find any person named Xiermires and who weights more than 70 kg | 56 | 68 | + JPQL: ```sql SELECT person FROM Person person WHERE person.name = 'Xiermires' AND person.weight_kg > 70``` | 57 | | -+ matcher: ```java matcher.findAny(Person.class, matching("name", eq("Xiermires")).and("weight_kg, gt(70)))``` | | 69 | ++ matcher: ```java matcher.findAny(Person.class, matching("name", eq("Xiermires")).and("weight_kg", gt(70)))``` | | 70 | + | | 71 | ++ natural: find person names which start by X and whose aged is between 25 and 50 | | 72 | ++ JPQL: ```sql SELECT person FROM Person person WHERE person.name LIKE 'X%' AND person.age BETWEEN 25 AND 50``` | | 73 | ++ matcher: ```java matcher.findAny(String.class, selection(Person.class, "name"), matching("name", startsWith("X")).and("age", between(25, 50)))``` | | 74 | + | | 75 | ++ natural: find any person which name is also a pet name | | 76 | ++ JPQL: ```sql SELECT person FROM Person person, Pet pet WHERE person.name = pet.name``` | | 77 | ++ matcher: ```java matcher.findAny(Person.class, matching("name", Pet.class)) | | 78 | + | | 79 | ++ natural: find any pet which name is also a pet name | | 80 | ++ JPQL: ```sql SELECT person FROM Person person, Pet pet WHERE person.name = pet.name``` | | 81 | ++ matcher: ```java matcher.findAny(Person.class, matching("name", Pet.class)) | | 82 | + | | 83 | +#### Parenthesis I | | 84 | + | | 85 | +Matchers use implicit precedence, where any AND or OR right side terms are automatically between parenthesis, the following expressions look alike but their their precedence and expected results are different. | | 86 | + | | 87 | ++ natural: find any person older than 20 and named either Xiermires or Serimreix | | 88 | ++ JPQL: ```sql SELECT person FROM Person person WHERE person.age > 20 AND ( person.name 'Xiermires' OR person.name = 'Serimreix' )``` | | 89 | ++ matcher: ```java matcher.findAny(Person.class, matching("age", gt(20)).and("name", eq("Xiermires").or("Serimreix")))``` | | 90 | + | | 91 | ++ natural: find any person older than 20 named Xiermires, or any other named Serimreix | | 92 | ++ JPQL: ```sql SELECT person FROM Person person WHERE person.age > 20 AND person.name = 'Xiermires' OR person.name = 'Serimreix'``` | | 93 | ++ matcher: ```java matcher.findAny(Person.class, matching("age", gt(20).and("name", eq("Xiermires").or("Serimreix"))))``` | | 94 | + | | 95 | +Parenthesis can be explicitly defined if desired using the closure() method. | | 96 | + | | 97 | ++ matcher: ```java matcher.findAny(Person.class, closure(matching("name", eq("Xiermires")).or("name", eq("Serimreix")))).and("age", gt(20)))``` | | 98 | + | | 99 | +#### Parenthesis II | | 100 | + | | 101 | +Parenthesis also determine the leading table or column of an expression. | | 102 | + | | 103 | +matching**(**Person.class, "name", eq("Xiermires")).and(matching**(**Pet.class, "name", eq("Fluffy"))**)**.and("age", gt(10)**)** | | 104 | + | | 105 | +> WHERE person.name = 'Xiermires' AND pet.name = 'Fluffy' AND person.age > 10 | | 106 | +
| | 107 | +matching**(**Person.class, "name", eq("Xiermires")).and(matching**(**Pet.class, "name", eq("Fluffy").and("age", gt(10))**))** | | 108 | + | | 109 | +> WHERE person.name = 'Xiermires' and pet.name = 'Fluffy' and pet.age > 10 | | 110 | +
| | 111 | +#### Not support | | 112 | + | | 113 | +Any expression, or collection of expressions, can be negated by using the not() method. | | 114 | + | | 115 | ++ natural: find any person not aged more than 25 | | 116 | ++ matcher: ```java matcher.findAny(Person.class, matching("age", not(gt(25))))``` | | 117 | ++ JPQL: ```sql SELECT person FROM Person person WHERE person.age < 25``` | | 118 | + | | 119 | +#### Group by, Order by, Having | | 120 | + | | 121 | +Group by, order by and having clauses are all supported. | | 122 | + | | 123 | +natural: find any person total owned pets | | 124 | +JPQL: ```sql SELECT COUNT(person.name) FROM Person person, Pet pet WHERE person.name = pet.owner GROUP BY person.name``` | | 125 | +matcher: ```java matcher.findAny(Long.class, count(Person.class, "name"), matching("name", matching(Pet.class, "owner")), groupBy("name"))``` | | 126 | + | | 127 | +natural: find all person order by name | | 128 | +JPQL: ```sql SELECT person FROM Person person ORDER BY person.name``` | | 129 | +matcher: ```java matcher.findAny(Person.class, orderBy("name"))``` | | 130 | + | | 131 | +natural: find person names of people owning more than 2 pets | | 132 | +JPQL: ```sql SELECT person.name FROM Person person, Pet pet WHERE person.name = pet.owner GROUP BY person.name HAVING COUNT(person.name) > 2``` | | 133 | +matcher: ```java matcher.findAny(selection(Person.class, "name"), matching("name", matching(Pet.class, "owner")), groupBy("name").having(count("name"), gt(2)))``` | | 134 | + | | 135 | +#### Functions | | 136 | + | | 137 | +The following functions are supported { DISTINCT, COUNT, MIN, MAX, SUM, AVG }. | 58 | 138 |
| 59 | | -In similar fashion we can use | | 139 | +natural: find the max age of any person | | 140 | +JPQL: ```sql SELECT MAX(person.age) FROM Person person``` | | 141 | +matcher: ```java matcher.find(Long.class, max(Person.class, "age"))``` | 60 | 142 |
| | 143 | +natural: find any person order by amount of owned pets | | 144 | +JPQL: ```sql SELECT person, person.name FROM Person person, Pet pet WHERE person.name = pet.owner GROUP BY person.name ORDER BY COUNT(person.name)``` | | 145 | +matcher: ```java matcher.findAny(Person.class, matching("name", matching(Pet.class, "owner")), groupBy("name").orderBy(count("name")))``` |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/bean/BeanBasedWhereBuilder.java +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ public class BeanBasedWhereBuilder extends WhereBuilder<BeanBasedWhereBuilder> { | 38 | 38 | private final NameBasedWhereBuilder delegate; | 39 | 39 |
| 40 | 40 | public BeanBasedWhereBuilder(NameBasedWhereBuilder wrapped) { | | 41 | + super(null, null); | 41 | 42 | this.delegate = wrapped; | 42 | 43 | } | 43 | 44 |
| @@ -59,7 +60,7 @@ protected NameBasedWhereBuilder getDelegate() { | 59 | 60 | public Set<Class<?>> getReferents() { | 60 | 61 | return delegate.getReferents(); | 61 | 62 | } | 62 | | - | | 63 | + | 63 | 64 | public <E> BeanBasedWhereBuilder or(E property, BeanBasedWhereBuilder other) { | 64 | 65 | final Capture lastCapture = InvokationCapturer.getLastCapture(); | 65 | 66 | delegate.or(getReferent(lastCapture), getPropertyName(lastCapture), other.delegate); | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/builder/ClauseBuilder.java +17-17Lines changed: 17 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,7 +114,8 @@ public Set<Class<?>> getReferents() { | 114 | 114 | * The other builder inherits any types defined in the this builder. | 115 | 115 | */ | 116 | 116 | public <T extends Builder> Builder or(T other) { | 117 | | - return merge(null, null, closureOnMerge ? closure(other) : other, getOrOperator()); | | 117 | + return merge(getLeadingReferent(), getLeadingProperty(), closureOnMerge ? closure(other) : other, | | 118 | + getOrOperator()); | 118 | 119 | } | 119 | 120 |
| 120 | 121 | /** | @@ -123,7 +124,8 @@ public <T extends Builder> Builder or(T other) { | 123 | 124 | * The other builder inherits any types defined in the this builder. | 124 | 125 | */ | 125 | 126 | public <T extends Builder> Builder and(T other) { | 126 | | - return merge(null, null, closureOnMerge ? closure(other) : other, getAndOperator()); | | 127 | + return merge(getLeadingReferent(), getLeadingProperty(), closureOnMerge ? closure(other) : other, | | 128 | + getAndOperator()); | 127 | 129 | } | 128 | 130 |
| 129 | 131 | protected String getOrOperator() { | @@ -183,14 +185,16 @@ protected void parseExpressions(StringBuilder appender, // | 183 | 185 | } | 184 | 186 |
| 185 | 187 | public void overwriteNullReferenceAndProperties(Class<?> referent, String property) { | 186 | | - if (previousClause != null) { | 187 | | - previousClause.overwriteNullReferenceAndProperties(referent, property); | 188 | | - } | 189 | | - if (nextClause != null) { | 190 | | - nextClause.overwriteNullReferenceAndProperties(referent, property); | 191 | | - } | 192 | | - for (Expression expression : getExpressions()) { | 193 | | - expression.overwriteNullReferenceAndProperties(referent, property); | | 188 | + if (referent != null || property != null) { | | 189 | + if (previousClause != null) { | | 190 | + previousClause.overwriteNullReferenceAndProperties(referent, property); | | 191 | + } | | 192 | + if (nextClause != null) { | | 193 | + nextClause.overwriteNullReferenceAndProperties(referent, property); | | 194 | + } | | 195 | + for (Expression expression : getExpressions()) { | | 196 | + expression.overwriteNullReferenceAndProperties(referent, property); | | 197 | + } | 194 | 198 | } | 195 | 199 | } | 196 | 200 |
| @@ -206,21 +210,17 @@ void initializeBindings() { | 206 | 210 | * <p> | 207 | 211 | * If an operator is specified, it is used to concatenate both this last and other's first expression. | 208 | 212 | */ | 209 | | - public <Other extends Builder> Builder merge(// | | 213 | + public <Other extends Builder> Builder merge( // | 210 | 214 | Class<?> referent, // | 211 | 215 | String property, // | 212 | 216 | Other other, // | 213 | 217 | String operator) { | 214 | 218 |
| | 219 | + other.overwriteNullReferenceAndProperties(referent, property); | | 220 | + | 215 | 221 | if (operator != null) { | 216 | 222 | getExpressions().addLast(new ConstantExpression(operator)); | 217 | 223 | } | 218 | | - if (referent != null) { | 219 | | - this.leadingReferent = referent; | 220 | | - } | 221 | | - if (property != null) { | 222 | | - this.leadingProperty = property; | 223 | | - } | 224 | 224 | getExpressions().addAll(other.getExpressions()); | 225 | 225 | return getThis(); | 226 | 226 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/builder/WhereBuilder.java +7-28Lines changed: 7 additions & 28 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | import static org.matcher.expression.Expressions.AND; | 25 | 25 | import static org.matcher.expression.Expressions.OR; | 26 | 26 |
| 27 | | -import java.util.ArrayList; | 28 | | -import java.util.List; | 29 | 27 | import java.util.Set; | 30 | 28 |
| 31 | 29 | import org.matcher.expression.Expression; | 32 | 30 | import org.matcher.expression.JoinQualifierExpression; | 33 | 31 |
| 34 | 32 | public abstract class WhereBuilder<T extends WhereBuilder<T>> extends ClauseBuilder<T> { | 35 | 33 |
| 36 | | - final List<JoinQualifierExpression> joins = new ArrayList<>(); | 37 | | - | 38 | | - protected WhereBuilder() { | 39 | | - super(null, null); | 40 | | - } | 41 | | - | 42 | | - protected WhereBuilder(Expression expression) { | 43 | | - super(expression.getReferent(), expression.getProperty()); | 44 | | - getExpressions().add(expression); | 45 | | - if (expression instanceof JoinQualifierExpression) { | 46 | | - joins.add((JoinQualifierExpression) expression); | 47 | | - } | | 34 | + protected WhereBuilder(Class<?> referent, String property) { | | 35 | + super(referent, property); | 48 | 36 | setClosureOnMerge(true); | 49 | 37 | } | 50 | 38 |
| 51 | 39 | @Override | 52 | 40 | public Set<Class<?>> getReferents() { | 53 | 41 | final Set<Class<?>> referents = super.getReferents(); | 54 | | - for (JoinQualifierExpression join : joins) { | 55 | | - referents.add(join.getOtherReferent()); | | 42 | + for (Expression expression : getExpressions()) { | | 43 | + if (expression instanceof JoinQualifierExpression) { | | 44 | + final JoinQualifierExpression joinExpression = (JoinQualifierExpression) expression; | | 45 | + referents.add(joinExpression.getOtherReferent()); | | 46 | + } | 56 | 47 | } | 57 | 48 | return referents; | 58 | 49 | } | @@ -76,16 +67,4 @@ protected String getOrOperator() { | 76 | 67 | protected String getAndOperator() { | 77 | 68 | return AND; | 78 | 69 | } | 79 | | - | 80 | | - @Override | 81 | | - public <E extends T> T merge(// | 82 | | - Class<?> referent, // | 83 | | - String property, // | 84 | | - E other, // | 85 | | - String operator) { | 86 | | - | 87 | | - super.merge(referent, property, other, operator); | 88 | | - joins.addAll(other.joins); | 89 | | - return getThis(); | 90 | | - } | 91 | 70 | } |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/expression/Expression.java +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,10 +95,10 @@ public String getProperty() { | 95 | 95 | } | 96 | 96 |
| 97 | 97 | public void overwriteNullReferenceAndProperties(Class<?> referent, String property) { | 98 | | - if (getReferent() == null) { | | 98 | + if (referent != null && getReferent() == null) { | 99 | 99 | setReferent(referent); | 100 | 100 | } | 101 | | - if (getProperty() == null) { | | 101 | + if (property != null && getProperty() == null) { | 102 | 102 | setProperty(property); | 103 | 103 | } | 104 | 104 | getChildren().forEach(node -> node.getData().overwriteNullReferenceAndProperties(referent, property)); | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/expression/Expressions.java +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ public class Expressions { | 31 | 31 |
| 32 | 32 | public static final String OR = " OR "; | 33 | 33 | public static final String AND = " AND "; | | 34 | + public static final String SPACE = " "; | 34 | 35 |
| 35 | 36 | public static final ConstantExpression COMMA = new ConstantExpression(", "); | 36 | 37 | public static final ConstantExpression OPEN = new ConstantExpression(" ( "); | |
Collapse file Copy file name to clipboardExpand all lines: src/main/java/org/matcher/name/NameBasedExpressions.java +22-7Lines changed: 22 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 |
| 24 | 24 | import java.util.Collection; | 25 | 25 |
| 26 | | -import org.matcher.builder.ClauseBuilder; | 27 | 26 | import org.matcher.builder.GroupByBuilder; | 28 | 27 | import org.matcher.builder.OrderByBuilder; | 29 | 28 | import org.matcher.expression.BetweenExpression; | @@ -148,23 +147,39 @@ public static <T> OrderByBuilder<T> orderBy(String property, String... others) { | 148 | 147 | */ | 149 | 148 | public static NameBasedWhereBuilder matching(Class<?> other, String otherProperty) { | 150 | 149 | final Expression expression = new JoinQualifierExpression(EQUALS, NOT_EQUALS, other, otherProperty); | 151 | | - final NameBasedWhereBuilder builder = new NameBasedWhereBuilder(expression); | 152 | | - return matching(null, otherProperty, builder); | | 150 | + final NameBasedWhereBuilder builder = new NameBasedWhereBuilder(null, null); | | 151 | + builder.getExpressions().add(expression); | | 152 | + return matching(null, null, builder); | | 153 | + } | | 154 | + | | 155 | + /** | | 156 | + * An inner join expression. | | 157 | + * <p> | | 158 | + * Equivalent to ?.property = other.property | | 159 | + */ | | 160 | + public static NameBasedWhereBuilder matching(String property, Class<?> firstOther, Class<?>... others) { | | 161 | + final NameBasedWhereBuilder matching = matching(property, matching(firstOther, property)); | | 162 | + for (Class<?> other : others) { | | 163 | + matching.merge(null, null, matching(property, matching(other, property)), AND); | | 164 | + } | | 165 | + return matching; | 153 | 166 | } | 154 | 167 |
| 155 | 168 | /** | 156 | 169 | * Typifies the {@code builder} and all its children with a column property. | 157 | 170 | */ | 158 | | - public static <T extends ClauseBuilder<T>> T matching(String property, T builder) { | | 171 | + public static NameBasedWhereBuilder matching(String property, NameBasedWhereBuilder builder) { | 159 | 172 | return matching(null, property, builder); | 160 | 173 | } | 161 | 174 |
| 162 | 175 | /** | 163 | 176 | * Typifies the {@code builder} and all its children with a referent table and a column property. | 164 | 177 | */ | 165 | | - public static <T extends ClauseBuilder<T>> T matching(Class<?> referent, String property, T builder) { | 166 | | - builder.overwriteNullReferenceAndProperties(referent, property); | 167 | | - return builder; | | 178 | + public static NameBasedWhereBuilder matching(Class<?> referent, String property, NameBasedWhereBuilder builder) { | | 179 | + final NameBasedWhereBuilder newBuilder = new NameBasedWhereBuilder(referent, property); | | 180 | + builder.overwriteNullReferenceAndProperties(builder.getLeadingReferent(), builder.getLeadingProperty()); | | 181 | + newBuilder.merge(referent, property, builder, null); | | 182 | + return newBuilder; | 168 | 183 | } | 169 | 184 |
| 170 | 185 | // expressions | |
|