Skip to content

Commit f4b3710

Browse files
Merge pull request #135 from xdev-software/query-keywords
Query keywords
2 parents 522804b + 45ac7db commit f4b3710

File tree

17 files changed

+1606
-434
lines changed

17 files changed

+1606
-434
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Implemented auto-id-generation for UUIDs.
44
* Implemented composite primary keys.
5+
* Keyword "ignoreCase" now available for queries.
56

67
# 2.0.1
78

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/query/EclipseStoreQueryCreator.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ private AbstractCriteriaNode<T> from(
126126
{
127127
Objects.requireNonNull(criteria);
128128
final Part.Type type = Objects.requireNonNull(part).getType();
129+
final Part.IgnoreCaseType ignoreCaseType = part.shouldIgnoreCase();
130+
final boolean doIgnoreCase =
131+
ignoreCaseType == Part.IgnoreCaseType.ALWAYS || ignoreCaseType == Part.IgnoreCaseType.WHEN_POSSIBLE;
129132

130133
switch(type)
131134
{
@@ -169,27 +172,27 @@ private AbstractCriteriaNode<T> from(
169172
}
170173
case LIKE ->
171174
{
172-
return criteria.like((String)Objects.requireNonNull(parameters).next());
175+
return criteria.like((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
173176
}
174177
case STARTING_WITH ->
175178
{
176-
return criteria.startWith((String)Objects.requireNonNull(parameters).next());
179+
return criteria.startWith((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
177180
}
178181
case ENDING_WITH ->
179182
{
180-
return criteria.endWith((String)Objects.requireNonNull(parameters).next());
183+
return criteria.endWith((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
181184
}
182185
case CONTAINING ->
183186
{
184-
return criteria.containing((String)Objects.requireNonNull(parameters).next());
187+
return criteria.containing((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
185188
}
186189
case NOT_LIKE ->
187190
{
188-
return criteria.notLike((String)Objects.requireNonNull(parameters).next());
191+
return criteria.notLike((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
189192
}
190193
case NOT_CONTAINING ->
191194
{
192-
return criteria.notContaining((String)Objects.requireNonNull(parameters).next());
195+
return criteria.notContaining((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
193196
}
194197
case EXISTS ->
195198
{

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/query/criteria/AbstractCriteriaNode.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,51 +147,59 @@ public AbstractCriteriaNode<T> exists(final boolean value)
147147
return this;
148148
}
149149

150-
public AbstractCriteriaNode<T> like(final String like)
150+
public AbstractCriteriaNode<T> like(final String like, final boolean doIgnoreCase)
151151
{
152-
final String completeRegex = sqlLikeStringToRegex(like);
152+
final String completeRegex = sqlLikeStringToRegex(like, doIgnoreCase);
153153
this.predicates.add(entity -> {
154154
final String fieldValue = (String)Objects.requireNonNull(this.field).readValue(entity);
155-
return fieldValue != null && fieldValue.toUpperCase().matches(completeRegex);
155+
if(fieldValue == null)
156+
{
157+
return false;
158+
}
159+
return (doIgnoreCase ? fieldValue.toUpperCase() : fieldValue).matches(completeRegex);
156160
});
157161
return this;
158162
}
159163

160-
private static String sqlLikeStringToRegex(final String like)
164+
private static String sqlLikeStringToRegex(final String like, final boolean doIgnoreCase)
161165
{
162-
String regex = like.toUpperCase();
166+
String regex = doIgnoreCase ? like.toUpperCase() : like;
163167
regex = regex.replace(".", "\\.");
164168
regex = regex.replace("_", ".");
165169
return regex.replace("%", ".*");
166170
}
167171

168-
public AbstractCriteriaNode<T> startWith(final String startString)
172+
public AbstractCriteriaNode<T> startWith(final String startString, final boolean doIgnoreCase)
169173
{
170-
return this.like(startString + "%");
174+
return this.like(startString + "%", doIgnoreCase);
171175
}
172176

173-
public AbstractCriteriaNode<T> endWith(final String endString)
177+
public AbstractCriteriaNode<T> endWith(final String endString, final boolean doIgnoreCase)
174178
{
175-
return this.like("%" + endString);
179+
return this.like("%" + endString, doIgnoreCase);
176180
}
177181

178-
public AbstractCriteriaNode<T> containing(final String containedString)
182+
public AbstractCriteriaNode<T> containing(final String containedString, final boolean doIgnoreCase)
179183
{
180-
return this.like("%" + containedString + "%");
184+
return this.like("%" + containedString + "%", doIgnoreCase);
181185
}
182186

183-
public AbstractCriteriaNode<T> notLike(final String notLikeString)
187+
public AbstractCriteriaNode<T> notLike(final String notLikeString, final boolean doIgnoreCase)
184188
{
185-
final String completeRegex = sqlLikeStringToRegex(notLikeString);
189+
final String completeRegex = sqlLikeStringToRegex(notLikeString, doIgnoreCase);
186190
this.predicates.add(entity -> {
187191
final String fieldValue = (String)Objects.requireNonNull(this.field).readValue(entity);
188-
return fieldValue != null && !fieldValue.toUpperCase().matches(completeRegex);
192+
if(fieldValue == null)
193+
{
194+
return false;
195+
}
196+
return !(doIgnoreCase ? fieldValue.toUpperCase() : fieldValue).matches(completeRegex);
189197
});
190198
return this;
191199
}
192200

193-
public AbstractCriteriaNode<T> notContaining(final String containedString)
201+
public AbstractCriteriaNode<T> notContaining(final String containedString, final boolean doIgnoreCase)
194202
{
195-
return this.notLike("%" + containedString + "%");
203+
return this.notLike("%" + containedString + "%", doIgnoreCase);
196204
}
197205
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;
17+
18+
import java.util.Objects;
19+
20+
21+
public class Child
22+
{
23+
private String firstName;
24+
private String lastName;
25+
26+
public Child(final String firstName, final String lastName)
27+
{
28+
this.firstName = firstName;
29+
this.lastName = lastName;
30+
}
31+
32+
public String getFirstName()
33+
{
34+
return this.firstName;
35+
}
36+
37+
public void setFirstName(final String firstName)
38+
{
39+
this.firstName = firstName;
40+
}
41+
42+
public String getLastName()
43+
{
44+
return this.lastName;
45+
}
46+
47+
public void setLastName(final String lastName)
48+
{
49+
this.lastName = lastName;
50+
}
51+
52+
@Override
53+
public String toString()
54+
{
55+
return String.format(
56+
"Child[firstName='%s', lastName='%s']",
57+
this.firstName, this.lastName);
58+
}
59+
60+
@Override
61+
public boolean equals(final Object o)
62+
{
63+
if(this == o)
64+
{
65+
return true;
66+
}
67+
if(o == null || this.getClass() != o.getClass())
68+
{
69+
return false;
70+
}
71+
final Child customer = (Child)o;
72+
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
73+
this.lastName,
74+
customer.lastName);
75+
}
76+
77+
@Override
78+
public int hashCode()
79+
{
80+
return Objects.hash(this.firstName, this.lastName);
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;
17+
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
22+
public class Customer
23+
{
24+
private String firstName;
25+
private String lastName;
26+
27+
public Customer(final String firstName, final String lastName)
28+
{
29+
this.firstName = firstName;
30+
this.lastName = lastName;
31+
}
32+
33+
public String getFirstName()
34+
{
35+
return this.firstName;
36+
}
37+
38+
public void setFirstName(final String firstName)
39+
{
40+
this.firstName = firstName;
41+
}
42+
43+
public String getLastName()
44+
{
45+
return this.lastName;
46+
}
47+
48+
public void setLastName(final String lastName)
49+
{
50+
this.lastName = lastName;
51+
}
52+
53+
@Override
54+
public String toString()
55+
{
56+
return String.format(
57+
"Customer[firstName='%s', lastName='%s']",
58+
this.firstName, this.lastName);
59+
}
60+
61+
@Override
62+
public boolean equals(final Object o)
63+
{
64+
if(this == o)
65+
{
66+
return true;
67+
}
68+
if(o == null || this.getClass() != o.getClass())
69+
{
70+
return false;
71+
}
72+
final Customer customer = (Customer)o;
73+
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
74+
this.lastName,
75+
customer.lastName);
76+
}
77+
78+
@Override
79+
public int hashCode()
80+
{
81+
return Objects.hash(this.firstName, this.lastName);
82+
}
83+
84+
@SuppressWarnings("OptionalGetWithoutIsPresent")
85+
public static Customer getCustomerWithFirstName(final List<Customer> customers, final String firstName)
86+
{
87+
return customers.stream().filter(customer -> customer.getFirstName().equals(firstName)).findFirst().get();
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;
17+
18+
import java.util.List;
19+
import java.util.Optional;
20+
21+
import org.springframework.data.domain.Page;
22+
import org.springframework.data.domain.Pageable;
23+
import org.springframework.data.domain.Sort;
24+
import org.springframework.data.repository.CrudRepository;
25+
import org.springframework.data.repository.PagingAndSortingRepository;
26+
27+
28+
public interface CustomerRepository
29+
extends CrudRepository<Customer, String>, PagingAndSortingRepository<Customer, String>
30+
{
31+
Optional<Customer> findByFirstName(String firstName);
32+
33+
Iterable<Customer> findAllByLastName(String lastName);
34+
35+
@Override
36+
Page<Customer> findAll(Pageable pageable);
37+
38+
Page<Customer> findAllByLastName(String lastName, Pageable pageable);
39+
40+
List<Customer> findByOrderByLastNameAsc();
41+
42+
Iterable<Customer> findAllByLastName(String lastName, Sort sort);
43+
44+
List<Customer> findAllByFirstName(String lastName, Pageable pageable);
45+
}

0 commit comments

Comments
 (0)