Package io.ran

Interface CrudRepository.InlineQuery<T,Q extends CrudRepository.InlineQuery<T,Q>>

Type Parameters:
T -
Q -
All Known Subinterfaces:
ValqueriesQuery<T>
All Known Implementing Classes:
BaseValqueriesQuery, CrudRepoBaseQuery, TestDoubleQuery, TestDoubleQuery, ValqueriesQueryImpl
Enclosing interface:
CrudRepository<T,K>

public static interface CrudRepository.InlineQuery<T,Q extends CrudRepository.InlineQuery<T,Q>>
Represents a query within a CrudRepository. First, you specify any number of conditions, followed by a single terminal operation:

- execute() to get the results as a Stream

- count() to get the number of rows that match the query

- delete() to delete all rows that match the query

- distinct(Function) to get all distinct values of a field that match the query

- selectField(Function) to get all values of a field that match the query

  • Method Details

    • and

      Q and(Consumer<Q> subQuery)
      Match if all the sub-conditions match. The conditions in your sub query will be AND-ed together. Since this is the default behavior, this method is intended to be used inside or(Consumer).

      Example:

      
       query.or(or -> or.eq(Person::getName, "Mike").and(and -> and.eq(Person::getName, "Bob").eq(Person::getAge, 42)));
       
      Will result into the following SQL: WHERE (name = 'Mike' OR (name = 'Bob' AND age = 42))

      Parameters:
      subQuery - the sub query to be inside the AND statement
      Returns:
      the query, for chaining
    • or

      Q or(Consumer<Q> subQuery)
      Match if any of the sub-conditions match. The conditions in your sub query will be OR-ed together.

      Example:

      
       query.or(sub -> sub.eq(Person::getName, "Bob").eq(Person::getAge, 42));
       
      Will result into the following SQL: WHERE (name = 'Bob' OR age = 42)

      Parameters:
      subQuery - the sub query to be inside the OR statement
      Returns:
      the query, for chaining
    • eq

      <X> Q eq(Function<T,X> field, X value)
      Match if the value of a field equals the specified value. If you want to match null values, you have to use isNull(Function).

      Example:

      
       query.eq(Person::getName, "Bob")
       

      This will match people with the name "Bob".

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • eq

      <X> Q eq(BiConsumer<T,X> field, X value)
      Match if the value of a field equals the specified value. If you want to match null values, you have to use isNull(BiConsumer).

      Example:

      
       query.eq(Person::setName, "Bob")
       

      This will match people with the name "Bob".

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • notEq

      <X> Q notEq(Function<T,X> field, X value)
      Match if the value of a field is not equal to the specified value.

      Example:

      
       query.notEq(Person::getName, "Bob")
       

      This will match people with a name other than "Bob".

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • notEq

      <X> Q notEq(BiConsumer<T,X> field, X value)
      Match if the value of a field is not equal to the specified value.

      Example:

      
       query.notEq(Person::setName, "Bob")
       

      This will match people with a name other than "Bob".

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • gt

      <X extends Comparable<? super X>> Q gt(Function<T,X> field, X value)
      Match if the value of a field is greater than the specified value.

      Example:

      
       query.gt(Person::getAge, 18)
       

      This will match people older than 18.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • gt

      <X extends Comparable<? super X>> Q gt(BiConsumer<T,X> field, X value)
      Match if the value of a field is greater than the specified value.

      Example:

      
       query.gt(Person::setAge, 18)
       

      This will match people older than 18.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • lt

      <X extends Comparable<? super X>> Q lt(Function<T,X> field, X value)
      Match if the value of a field is less than the specified value.

      Example:

      
       query.lt(Person::getAge, 18)
       

      This will match people younger than 18.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • lt

      <X extends Comparable<? super X>> Q lt(BiConsumer<T,X> field, X value)
      Match if the value of a field is less than the specified value.

      Example:

      
       query.lt(Person::setAge, 18)
       

      This will match people younger than 18.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • gte

      <X extends Comparable<? super X>> Q gte(Function<T,X> field, X value)
      Match if the value of a field is greater than or equal to the specified value.

      Example:

      
       query.gte(Person::getAge, 18)
       

      This will match people that are 18 or older.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • gte

      <X extends Comparable<? super X>> Q gte(BiConsumer<T,X> field, X value)
      Match if the value of a field is greater than or equal to the specified value.

      Example:

      
       query.gte(Person::setAge, 18)
       

      This will match people that are 18 or older.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • lte

      <X extends Comparable<? super X>> Q lte(Function<T,X> field, X value)
      Match if the value of a field is less than or equal to the specified value.

      Example:

      
       query.lte(Person::getAge, 18)
       

      This will match people that are 18 or younger.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the getter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • lte

      <X extends Comparable<? super X>> Q lte(BiConsumer<T,X> field, X value)
      Match if the value of a field is less than or equal to the specified value.

      Example:

      
       query.lte(Person::setAge, 18)
       

      This will match people that are 18 or younger.

      Type Parameters:
      X - the type of the field and value
      Parameters:
      field - a method reference to the setter of the field
      value - the value to check against
      Returns:
      the query, for chaining
    • isNull

      Q isNull(Function<T,?> field)
      Match if the value of a field is null.

      Example:

      
           query.isNull(Person::getEmail)
           

      This will match people whose email is null.

      Note: if the model class has fields of primitive type, they won't be null, even if the database contains NULL values. In this case isNull will still match correctly based on the values in the database.

      Parameters:
      field - a method reference to the getter of the field
      Returns:
      the query, for chaining
    • isNull

      Q isNull(BiConsumer<T,?> field)
      Match if the value of a field is null.

      Example:

      
       query.isNull(Person::setEmail)
       

      This will match people whose email is null.

      Note: if the model class has fields of primitive type, they won't be null, even if the database contains NULL values. In this case isNull will still match correctly based on the values in the database.

      Parameters:
      field - a method reference to the setter of the field
      Returns:
      the query, for chaining
    • withEager

      Q withEager(Function<T,?> field)
      Causes a Relation field to be eagerly fetched. Useful to avoid multiple queries if you know that you will access the relation.

      Eagerness is defined by the implementation, but it generally means that the field will be fetched in the same query as the parent object, rather than in a separate query when the field is accessed (lazy loading).

      Example, returning a stream of all phones of people that are 18:

      
       query.eq(Person::getAge, 18).withEager(Person::getPhone).execute().map(Person::getPhone)
       

      This will result in one query to the database, instead of creating a separate query to get each phone.

      Parameters:
      field - a method reference to the getter of the field
      Returns:
      the query, for chaining
    • withEager

      Q withEager(BiConsumer<T,?> field)
      Causes a Relation field to be eagerly fetched. Useful to avoid multiple queries if you know that you will access the relation.

      Eagerness is defined by the implementation, but it generally means that the field will be fetched in the same query as the parent object, rather than in a separate query when the field is accessed (lazy loading).

      Example, returning a stream of all phones of people that are 18:

      
       query.eq(Person::setAge, 18).withEager(Person::getPhone).execute().map(Person::getPhone)
       

      This will result in one query to the database, instead of creating a separate query to get each phone.

      Parameters:
      field - a method reference to the setter of the field
      Returns:
      the query, for chaining
    • sortAscending

      <X extends Comparable<? super X>> Q sortAscending(Function<T,X> field)
      Sort the results by the given field in ascending order.

      Example:

      
       query.sortAscending(Person::getAge)
       

      This will sort the results by age in ascending order.

      Type Parameters:
      X - the type of the field
      Parameters:
      field - a method reference to the getter of the field
      Returns:
      the query, for chaining
    • sortAscending

      <X extends Comparable<? super X>> Q sortAscending(BiConsumer<T,X> field)
      Sort the results by the given field in ascending order.

      Meant to be used with a setter method reference.

      Example:

      
       query.sortAscending(Person::setAge)
       

      This will sort the results by age in ascending order.

      Type Parameters:
      X - the type of the field
      Parameters:
      field - a method reference to the setter of the field
      Returns:
      the query, for chaining
    • sortDescending

      <X extends Comparable<? super X>> Q sortDescending(Function<T,X> property)
      Sort the results by the given field in descending order.

      Example:

      
           query.sortDescending(Person::getAge)
           

      This will sort the results by age in descending order.

      Type Parameters:
      X - the type of the field
      Parameters:
      property - a method reference to the getter of the field
      Returns:
      the query, for chaining
    • sortDescending

      <X extends Comparable<? super X>> Q sortDescending(BiConsumer<T,X> property)
      Sort the results by the given field in descending order.

      Meant to be used with a setter method reference.

      Example:

      
       query.sortDescending(Person::setAge)
       

      This will sort the results by age in descending order.

      Type Parameters:
      X - the type of the field
      Parameters:
      property - a method reference to the setter of the field
      Returns:
      the query, for chaining
    • limit

      Q limit(int offset, int limit)
      Limit the results to the given number of rows, starting at the given offset.

      Offset is 0-based (the first row is 0).

      Offset is inclusive, limit is the size of the returned collection.

      Example:

      
       query.limit(10, 20)
       

      This will return rows 10, 11, 12, ..., 29.

      Parameters:
      offset - the offset to start at
      limit - the number of rows to return
      Returns:
      the query, for chaining
      See Also:
    • limit

      Q limit(int limit)
      Limit the results to the given number of rows.

      Example:

      
       query.limit(10)
       

      This will return the first 10 rows.

      Parameters:
      limit - the number of rows to return
      Returns:
      the query, for chaining
      See Also:
    • execute

      @CheckReturnValue Stream<T> execute()
      Execute the query and return the results as a Stream.

      This is a terminal operation. As such, the query should not be reused after this method is called.

      The resulting Stream from the operation may be a lazy stream, meaning that the results are not fetched from the database until they are accessed. As such it is important that a terminal operation is performed on the stream within a reasonable time frame to avoid holding database connections open.

      Returns:
      a Stream of the results
    • count

      @CheckReturnValue long count()
      Count the number of rows that match the query.

      This is a terminal operation. As such, the query should not be reused after this method is called.

      Returns:
      the number of rows that match the query
    • distinct

      @CheckReturnValue <X> Stream<X> distinct(Function<T,X> field)
      Execute the query and return all distinct values of a field that match the query.

      This is useful if you need just a single field from an object, as it will only transfer that column over the network and will only deserialize that field.

      Sorting can be used, but only on the same column that is being selected.

      This is a terminal operation. As such, the query should not be reused after this method is called.

      The resulting Stream from the operation may be a lazy stream, meaning that the results are not fetched from the database until they are accessed. As such it is important that a terminal operation is performed on the stream within a reasonable time frame to avoid holding database connections open.

      Type Parameters:
      X - the type of the field
      Parameters:
      field - a method reference to the getter of the field
      Returns:
      a Stream of distinct values of the field
    • selectField

      @CheckReturnValue <X> Stream<X> selectField(Function<T,X> field)
      Execute the query and return all values of a field that match the query.

      This is useful if you need just a single field from an object, as it will only transfer that column over the network and will only deserialize that field.

      This is a terminal operation. As such, the query should not be reused after this method is called.

      The resulting Stream from the operation may be a lazy stream, meaning that the results are not fetched from the database until they are accessed. As such it is important that a terminal operation is performed on the stream within a reasonable time frame to avoid holding database connections open.

      Type Parameters:
      X - the type of the field
      Parameters:
      field - a method reference to the getter of the field
      Returns:
      a Stream of values of the field
    • delete

      Delete all entries that match the query.

      This is a terminal operation. As such, the query should not be reused after this method is called.

      WARNING: If you invoke no query methods prior to this, all rows in the table with be deleted.

      Returns:
      a CrudRepository.CrudUpdateResult with the number of rows affected
      See Also:
    • eq

      Q eq(Property.PropertyValue<?> property)
    • notEq

      Q notEq(Property.PropertyValue<?> property)
    • gt

      <X extends Comparable<? super X>> Q gt(Property.PropertyValue<X> property)
    • gte

      <X extends Comparable<? super X>> Q gte(Property.PropertyValue<X> property)
    • lt

      <X extends Comparable<? super X>> Q lt(Property.PropertyValue<X> property)
    • lte

      <X extends Comparable<? super X>> Q lte(Property.PropertyValue<X> property)
    • isNull

      Q isNull(Property<?> property)
    • withEager

      Q withEager(RelationDescriber relation)
    • sortAscending

      <X extends Comparable<? super X>> Q sortAscending(Property<X> property)
    • sortDescending

      <X extends Comparable<? super X>> Q sortDescending(Property<X> property)
    • subQuery

      <X, Z extends CrudRepository.InlineQuery<X, Z>> Q subQuery(RelationDescriber relationDescriber, Consumer<Z> subQuery)
    • distinct

      @CheckReturnValue <X> Stream<X> distinct(Property<X> property)
    • selectField

      @CheckReturnValue <X> Stream<X> selectField(Property<X> property)