### Query Examples
Source: https://queritylib.github.io/spring-mvc/query-language.html
Common query patterns for filtering, sorting, and pagination.
```text
lastName="Skywalker"
lastName!="Skywalker"
lastName starts with "Sky"
lastName ends with "walker"
lastName contains "wal"
and(firstName="Luke", lastName="Skywalker")
age>30
age<30
height>=1.80
height<=1.80
and(lastName="Skywalker", age>30)
and(or(firstName="Luke", firstName="Anakin"), lastName="Skywalker") sort by age desc
and(not(firstName="Luke"), lastName="Skywalker")
lastName="Skywalker" page 2,10
lastName is null
lastName is not null
lastName in ("Skywalker", "Solo")
lastName not in ("Skywalker", "Solo")
deleted=false
address.city="Rome"
distinct and(orders.totalPrice>1000,currency="EUR")
sort by lastName asc, age desc page 1,10
```
--------------------------------
### Install Querity dependency
Source: https://queritylib.github.io/installing.html
Add the required dependency to your project build file.
```xml
io.github.queritylib
querity-spring-data-jpa
4.0.0
```
```gradle
implementation "io.github.queritylib:querity-spring-data-jpa:4.0.0"
```
--------------------------------
### Textual Query Language Example
Source: https://queritylib.github.io/available-modules.html
This module enables parsing of Querity objects from a simple textual query language. It provides an alternative to the JSON approach offered by `querity-spring-web`.
```shell
curl 'http://localhost:8080/people?q=and(lastName="Skywalker",firstName="Luke")'
```
--------------------------------
### Advanced Queries with SELECT, GROUP BY, and HAVING
Source: https://queritylib.github.io/spring-mvc/query-language.html
Examples for advanced queries involving projections and grouping, typically parsed via QuerityParser.parseAdvancedQuery().
```text
select firstName, lastName, address.city
select id, UPPER(lastName) as upperName
select category, COUNT(id) as itemCount group by category
select category, SUM(amount) as total group by category having SUM(amount)>1000
select region, category, AVG(price) as avgPrice group by region, category sort by avgPrice desc
```
--------------------------------
### Nested Functions
Source: https://queritylib.github.io/features/functions.html
Illustrates nesting Querity functions to apply multiple operations sequentially. For example, applying `trim()` before `upper()` to a property.
```java
// Nested functions
upper(trim(prop("name"))) // UPPER(TRIM(name))
length(lower(prop("email"))) // LENGTH(LOWER(email))
```
--------------------------------
### Spring Web MVC JSON Query Example
Source: https://queritylib.github.io/available-modules.html
Use this module to pass a JSON Query or Condition object as a request parameter in your Spring @RestController for automatic deserialization into a Querity object.
```shell
curl 'http://localhost:8080/people?q={"filter":{"and":[{"propertyName":"lastName","operator":"EQUALS","value":"Skywalker"},{"propertyName":"lastName","operator":"EQUALS","value":"Luke"}]}}'
```
--------------------------------
### Building and Executing Queries with Querity
Source: https://queritylib.github.io/features.html
This section details how to initialize a query, run it to retrieve all matching entities, or count entities based on a condition.
```APIDOC
## Building and Executing Queries
### Description
This section covers the basic usage of the Querity library for building and executing queries against a database.
### Static Methods
#### Build Empty Query
Use the static method `Querity.query().build()` to create an empty query object. This can then be further customized using various query building methods.
```java
// Example of building an empty query
Querity.query().build();
```
### Instance Methods (using Querity Service)
To use the instance methods, you first need to `@Autowire` the `Querity` service. This service allows you to interact with the configured database.
#### Find All Entities
Retrieves all entities that match the given query.
- **Method**: `Querity.findAll(entityClass, query)`
- **Parameters**:
- `entityClass` (*Class>*) - The class of the entity to query.
- `query` (*Query*) - The built query object.
```java
// Example of finding all entities
List results = querityService.findAll(MyEntity.class, query);
```
#### Count Entities
Counts the number of entities that satisfy the specified condition.
- **Method**: `Querity.count(entityClass, condition)`
- **Parameters**:
- `entityClass` (*Class>*) - The class of the entity to count.
- `condition` (*Condition*) - The condition to filter the entities by.
```java
// Example of counting entities
long count = querityService.count(MyEntity.class, condition);
```
### Query Customization
The Querity library supports various features for customizing queries, including:
- Filters
- Sorting
- Pagination
- Distinct results
- Projections (Select)
- Field-to-Field Comparison
- Function Expressions
- GROUP BY and HAVING
- Native Sort Expressions
- Query Customizers (JPA)
- Modify an existing Query
```
--------------------------------
### Simple Entity Query vs. Projection Query
Source: https://queritylib.github.io/features/projections.html
Demonstrates the difference between a simple entity query using `Query` and `findAll()`, and a projection query using `AdvancedQuery` and `findAllProjected()`.
```java
// Simple entity query
Query query = Querity.query()
.filter(filterBy("lastName", EQUALS, "Skywalker"))
.build();
List entities = querity.findAll(Person.class, query);
```
```java
// Projection query
AdvancedQuery advQuery = Querity.advancedQuery()
.select(selectBy("firstName", "lastName"))
.filter(filterBy("lastName", EQUALS, "Skywalker"))
.build();
List