Skip to content

Query keywords #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Implemented auto-id-generation for UUIDs.
* Implemented composite primary keys.
* Keyword "ignoreCase" now available for queries.

# 2.0.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ private AbstractCriteriaNode<T> from(
{
Objects.requireNonNull(criteria);
final Part.Type type = Objects.requireNonNull(part).getType();
final Part.IgnoreCaseType ignoreCaseType = part.shouldIgnoreCase();
final boolean doIgnoreCase =
ignoreCaseType == Part.IgnoreCaseType.ALWAYS || ignoreCaseType == Part.IgnoreCaseType.WHEN_POSSIBLE;

switch(type)
{
Expand Down Expand Up @@ -169,27 +172,27 @@ private AbstractCriteriaNode<T> from(
}
case LIKE ->
{
return criteria.like((String)Objects.requireNonNull(parameters).next());
return criteria.like((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case STARTING_WITH ->
{
return criteria.startWith((String)Objects.requireNonNull(parameters).next());
return criteria.startWith((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case ENDING_WITH ->
{
return criteria.endWith((String)Objects.requireNonNull(parameters).next());
return criteria.endWith((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case CONTAINING ->
{
return criteria.containing((String)Objects.requireNonNull(parameters).next());
return criteria.containing((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case NOT_LIKE ->
{
return criteria.notLike((String)Objects.requireNonNull(parameters).next());
return criteria.notLike((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case NOT_CONTAINING ->
{
return criteria.notContaining((String)Objects.requireNonNull(parameters).next());
return criteria.notContaining((String)Objects.requireNonNull(parameters).next(), doIgnoreCase);
}
case EXISTS ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,59 @@ public AbstractCriteriaNode<T> exists(final boolean value)
return this;
}

public AbstractCriteriaNode<T> like(final String like)
public AbstractCriteriaNode<T> like(final String like, final boolean doIgnoreCase)
{
final String completeRegex = sqlLikeStringToRegex(like);
final String completeRegex = sqlLikeStringToRegex(like, doIgnoreCase);
this.predicates.add(entity -> {
final String fieldValue = (String)Objects.requireNonNull(this.field).readValue(entity);
return fieldValue != null && fieldValue.toUpperCase().matches(completeRegex);
if(fieldValue == null)
{
return false;
}
return (doIgnoreCase ? fieldValue.toUpperCase() : fieldValue).matches(completeRegex);
});
return this;
}

private static String sqlLikeStringToRegex(final String like)
private static String sqlLikeStringToRegex(final String like, final boolean doIgnoreCase)
{
String regex = like.toUpperCase();
String regex = doIgnoreCase ? like.toUpperCase() : like;
regex = regex.replace(".", "\\.");
regex = regex.replace("_", ".");
return regex.replace("%", ".*");
}

public AbstractCriteriaNode<T> startWith(final String startString)
public AbstractCriteriaNode<T> startWith(final String startString, final boolean doIgnoreCase)
{
return this.like(startString + "%");
return this.like(startString + "%", doIgnoreCase);
}

public AbstractCriteriaNode<T> endWith(final String endString)
public AbstractCriteriaNode<T> endWith(final String endString, final boolean doIgnoreCase)
{
return this.like("%" + endString);
return this.like("%" + endString, doIgnoreCase);
}

public AbstractCriteriaNode<T> containing(final String containedString)
public AbstractCriteriaNode<T> containing(final String containedString, final boolean doIgnoreCase)
{
return this.like("%" + containedString + "%");
return this.like("%" + containedString + "%", doIgnoreCase);
}

public AbstractCriteriaNode<T> notLike(final String notLikeString)
public AbstractCriteriaNode<T> notLike(final String notLikeString, final boolean doIgnoreCase)
{
final String completeRegex = sqlLikeStringToRegex(notLikeString);
final String completeRegex = sqlLikeStringToRegex(notLikeString, doIgnoreCase);
this.predicates.add(entity -> {
final String fieldValue = (String)Objects.requireNonNull(this.field).readValue(entity);
return fieldValue != null && !fieldValue.toUpperCase().matches(completeRegex);
if(fieldValue == null)
{
return false;
}
return !(doIgnoreCase ? fieldValue.toUpperCase() : fieldValue).matches(completeRegex);
});
return this;
}

public AbstractCriteriaNode<T> notContaining(final String containedString)
public AbstractCriteriaNode<T> notContaining(final String containedString, final boolean doIgnoreCase)
{
return this.notLike("%" + containedString + "%");
return this.notLike("%" + containedString + "%", doIgnoreCase);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;

import java.util.Objects;


public class Child
{
private String firstName;
private String lastName;

public Child(final String firstName, final String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName()
{
return this.firstName;
}

public void setFirstName(final String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return this.lastName;
}

public void setLastName(final String lastName)
{
this.lastName = lastName;
}

@Override
public String toString()
{
return String.format(
"Child[firstName='%s', lastName='%s']",
this.firstName, this.lastName);
}

@Override
public boolean equals(final Object o)
{
if(this == o)
{
return true;
}
if(o == null || this.getClass() != o.getClass())
{
return false;
}
final Child customer = (Child)o;
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
this.lastName,
customer.lastName);
}

@Override
public int hashCode()
{
return Objects.hash(this.firstName, this.lastName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;

import java.util.List;
import java.util.Objects;


public class Customer
{
private String firstName;
private String lastName;

public Customer(final String firstName, final String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName()
{
return this.firstName;
}

public void setFirstName(final String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return this.lastName;
}

public void setLastName(final String lastName)
{
this.lastName = lastName;
}

@Override
public String toString()
{
return String.format(
"Customer[firstName='%s', lastName='%s']",
this.firstName, this.lastName);
}

@Override
public boolean equals(final Object o)
{
if(this == o)
{
return true;
}
if(o == null || this.getClass() != o.getClass())
{
return false;
}
final Customer customer = (Customer)o;
return Objects.equals(this.firstName, customer.firstName) && Objects.equals(
this.lastName,
customer.lastName);
}

@Override
public int hashCode()
{
return Objects.hash(this.firstName, this.lastName);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
public static Customer getCustomerWithFirstName(final List<Customer> customers, final String firstName)
{
return customers.stream().filter(customer -> customer.getFirstName().equals(firstName)).findFirst().get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright © 2024 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store.integration.isolated.tests.query.by.string;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;


public interface CustomerRepository
extends CrudRepository<Customer, String>, PagingAndSortingRepository<Customer, String>
{
Optional<Customer> findByFirstName(String firstName);

Iterable<Customer> findAllByLastName(String lastName);

@Override
Page<Customer> findAll(Pageable pageable);

Page<Customer> findAllByLastName(String lastName, Pageable pageable);

List<Customer> findByOrderByLastNameAsc();

Iterable<Customer> findAllByLastName(String lastName, Sort sort);

List<Customer> findAllByFirstName(String lastName, Pageable pageable);
}
Loading