GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Update docu, fix some problems with the leading elements in join scen… · Xiermires/EntityMatcher@36cf79c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • src
    • main/java/org/matcher
      • bean
        • BeanBasedWhereBuilder.java
      • builder
        • ClauseBuilder.java
        • WhereBuilder.java
      • expression
        • Expression.java
        • Expressions.java
      • name
        • NameBasedExpressions.java
        • NameBasedWhereBuilder.java
    • test/java/org/matcher/name
      • NameBasedEntityMatcherTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ Enhance an EntityManager to create JPQL queries through matchers.
44

55
The matchers can be created both using the names of the Entities properties, or a Entity matching "mock" conveniently prepared to capture method invokations.
66

7-
```final Person mtch = BeanBasedMatcher.matcher(Person.class);```
7+
This WHERE statement...
88

9-
The following matchers are equivalent.
9+
```sql WHERE person.name LIKE 'X%' OR person.surname LIKE 'Y%' OR person.eyes = 'blue'```
1010

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.
1320

1421
### Step 0. Configuration
1522

@@ -48,13 +55,91 @@ final EntityManager em = Persistence.createEntityManagerFactory("test").createEn
4855
final EntityMatcher matcher = new EntityMatcher(em);
4956
```
5057

51-
### Step 2. Using the matcher
58+
### Step 2. Usage
5259

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
5466

5567
+ natural: find any person named Xiermires and who weights more than 70 kg
5668
+ 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 }.
58138

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"))```
60142

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

‎src/main/java/org/matcher/bean/BeanBasedWhereBuilder.java‎

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 numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class BeanBasedWhereBuilder extends WhereBuilder<BeanBasedWhereBuilder> {
3838
private final NameBasedWhereBuilder delegate;
3939

4040
public BeanBasedWhereBuilder(NameBasedWhereBuilder wrapped) {
41+
super(null, null);
4142
this.delegate = wrapped;
4243
}
4344

@@ -59,7 +60,7 @@ protected NameBasedWhereBuilder getDelegate() {
5960
public Set<Class<?>> getReferents() {
6061
return delegate.getReferents();
6162
}
62-
63+
6364
public <E> BeanBasedWhereBuilder or(E property, BeanBasedWhereBuilder other) {
6465
final Capture lastCapture = InvokationCapturer.getLastCapture();
6566
delegate.or(getReferent(lastCapture), getPropertyName(lastCapture), other.delegate);
Collapse file

‎src/main/java/org/matcher/builder/ClauseBuilder.java‎

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 numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public Set<Class<?>> getReferents() {
114114
* The other builder inherits any types defined in the this builder.
115115
*/
116116
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());
118119
}
119120

120121
/**
@@ -123,7 +124,8 @@ public <T extends Builder> Builder or(T other) {
123124
* The other builder inherits any types defined in the this builder.
124125
*/
125126
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());
127129
}
128130

129131
protected String getOrOperator() {
@@ -183,14 +185,16 @@ protected void parseExpressions(StringBuilder appender, //
183185
}
184186

185187
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+
}
194198
}
195199
}
196200

@@ -206,21 +210,17 @@ void initializeBindings() {
206210
* <p>
207211
* If an operator is specified, it is used to concatenate both this last and other's first expression.
208212
*/
209-
public <Other extends Builder> Builder merge(//
213+
public <Other extends Builder> Builder merge( //
210214
Class<?> referent, //
211215
String property, //
212216
Other other, //
213217
String operator) {
214218

219+
other.overwriteNullReferenceAndProperties(referent, property);
220+
215221
if (operator != null) {
216222
getExpressions().addLast(new ConstantExpression(operator));
217223
}
218-
if (referent != null) {
219-
this.leadingReferent = referent;
220-
}
221-
if (property != null) {
222-
this.leadingProperty = property;
223-
}
224224
getExpressions().addAll(other.getExpressions());
225225
return getThis();
226226
}
Collapse file

‎src/main/java/org/matcher/builder/WhereBuilder.java‎

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 numberDiff line numberDiff line change
@@ -24,35 +24,26 @@
2424
import static org.matcher.expression.Expressions.AND;
2525
import static org.matcher.expression.Expressions.OR;
2626

27-
import java.util.ArrayList;
28-
import java.util.List;
2927
import java.util.Set;
3028

3129
import org.matcher.expression.Expression;
3230
import org.matcher.expression.JoinQualifierExpression;
3331

3432
public abstract class WhereBuilder<T extends WhereBuilder<T>> extends ClauseBuilder<T> {
3533

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);
4836
setClosureOnMerge(true);
4937
}
5038

5139
@Override
5240
public Set<Class<?>> getReferents() {
5341
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+
}
5647
}
5748
return referents;
5849
}
@@ -76,16 +67,4 @@ protected String getOrOperator() {
7667
protected String getAndOperator() {
7768
return AND;
7869
}
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-
}
9170
}
Collapse file

‎src/main/java/org/matcher/expression/Expression.java‎

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 numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ public String getProperty() {
9595
}
9696

9797
public void overwriteNullReferenceAndProperties(Class<?> referent, String property) {
98-
if (getReferent() == null) {
98+
if (referent != null && getReferent() == null) {
9999
setReferent(referent);
100100
}
101-
if (getProperty() == null) {
101+
if (property != null && getProperty() == null) {
102102
setProperty(property);
103103
}
104104
getChildren().forEach(node -> node.getData().overwriteNullReferenceAndProperties(referent, property));
Collapse file

‎src/main/java/org/matcher/expression/Expressions.java‎

Copy file name to clipboardExpand all lines: src/main/java/org/matcher/expression/Expressions.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Expressions {
3131

3232
public static final String OR = " OR ";
3333
public static final String AND = " AND ";
34+
public static final String SPACE = " ";
3435

3536
public static final ConstantExpression COMMA = new ConstantExpression(", ");
3637
public static final ConstantExpression OPEN = new ConstantExpression(" ( ");
Collapse file

‎src/main/java/org/matcher/name/NameBasedExpressions.java‎

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 numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.util.Collection;
2525

26-
import org.matcher.builder.ClauseBuilder;
2726
import org.matcher.builder.GroupByBuilder;
2827
import org.matcher.builder.OrderByBuilder;
2928
import org.matcher.expression.BetweenExpression;
@@ -148,23 +147,39 @@ public static <T> OrderByBuilder<T> orderBy(String property, String... others) {
148147
*/
149148
public static NameBasedWhereBuilder matching(Class<?> other, String otherProperty) {
150149
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;
153166
}
154167

155168
/**
156169
* Typifies the {@code builder} and all its children with a column property.
157170
*/
158-
public static <T extends ClauseBuilder<T>> T matching(String property, T builder) {
171+
public static NameBasedWhereBuilder matching(String property, NameBasedWhereBuilder builder) {
159172
return matching(null, property, builder);
160173
}
161174

162175
/**
163176
* Typifies the {@code builder} and all its children with a referent table and a column property.
164177
*/
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;
168183
}
169184

170185
// expressions

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.