Interface QueryBuilder<T>

Type Parameters:
T - the entity type

public interface QueryBuilder<T>
Query builder for complex database queries.

Provides a fluent interface for building queries with multiple conditions, ordering, and pagination.

Example:


List<UserSetting> results = repository.query()
    .where("user_id", 12345L)
    .where("enabled", true)
    .whereLike("setting_name", "theme%")
    .orderBy("created_at", false)  // descending
    .limit(10)
    .offset(0)
    .list();

// Or get a single result
Optional<UserSetting> result = repository.query()
    .where("user_id", 12345L)
    .where("setting_name", "theme")
    .findOne();

// Count
long count = repository.query()
    .where("enabled", true)
    .count();

  • Method Details

    • where

      QueryBuilder<T> where(String column, Object value)
      Add an equality condition (column = value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereNot

      QueryBuilder<T> whereNot(String column, Object value)
      Add a not-equal condition (column != value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereGreaterThan

      QueryBuilder<T> whereGreaterThan(String column, Object value)
      Add a greater-than condition (column > value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereGreaterThanOrEqual

      QueryBuilder<T> whereGreaterThanOrEqual(String column, Object value)
      Add a greater-than-or-equal condition (column >= value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereLessThan

      QueryBuilder<T> whereLessThan(String column, Object value)
      Add a less-than condition (column < value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereLessThanOrEqual

      QueryBuilder<T> whereLessThanOrEqual(String column, Object value)
      Add a less-than-or-equal condition (column <= value).
      Parameters:
      column - the column name
      value - the value
      Returns:
      this builder
    • whereLike

      QueryBuilder<T> whereLike(String column, String pattern)
      Add a LIKE condition for pattern matching.

      Use % for wildcard matching: - "abc%" matches strings starting with "abc" - "%abc" matches strings ending with "abc" - "%abc%" matches strings containing "abc"

      Parameters:
      column - the column name
      pattern - the pattern with % wildcards
      Returns:
      this builder
    • whereILike

      QueryBuilder<T> whereILike(String column, String pattern)
      Add a case-insensitive LIKE condition.
      Parameters:
      column - the column name
      pattern - the pattern with % wildcards
      Returns:
      this builder
    • whereIn

      QueryBuilder<T> whereIn(String column, List<?> values)
      Add an IN condition (column IN (values)).
      Parameters:
      column - the column name
      values - the values to match
      Returns:
      this builder
    • whereNotIn

      QueryBuilder<T> whereNotIn(String column, List<?> values)
      Add a NOT IN condition.
      Parameters:
      column - the column name
      values - the values to exclude
      Returns:
      this builder
    • whereNull

      QueryBuilder<T> whereNull(String column)
      Add an IS NULL condition.
      Parameters:
      column - the column name
      Returns:
      this builder
    • whereNotNull

      QueryBuilder<T> whereNotNull(String column)
      Add an IS NOT NULL condition.
      Parameters:
      column - the column name
      Returns:
      this builder
    • whereBetween

      QueryBuilder<T> whereBetween(String column, Object start, Object end)
      Add a BETWEEN condition (column BETWEEN start AND end).
      Parameters:
      column - the column name
      start - the start value (inclusive)
      end - the end value (inclusive)
      Returns:
      this builder
    • orderBy

      QueryBuilder<T> orderBy(String column, boolean ascending)
      Add ordering.
      Parameters:
      column - the column to order by
      ascending - true for ASC, false for DESC
      Returns:
      this builder
    • orderByAsc

      default QueryBuilder<T> orderByAsc(String column)
      Add ascending ordering.
      Parameters:
      column - the column to order by
      Returns:
      this builder
    • orderByDesc

      default QueryBuilder<T> orderByDesc(String column)
      Add descending ordering.
      Parameters:
      column - the column to order by
      Returns:
      this builder
    • limit

      QueryBuilder<T> limit(int limit)
      Limit the number of results.
      Parameters:
      limit - maximum number of results
      Returns:
      this builder
    • offset

      QueryBuilder<T> offset(int offset)
      Skip a number of results.
      Parameters:
      offset - number of results to skip
      Returns:
      this builder
    • list

      List<T> list()
      Execute the query and return all matching entities.
      Returns:
      list of matching entities
    • findOne

      Optional<T> findOne()
      Execute the query and return the first matching entity.
      Returns:
      the first matching entity
    • findOneOrThrow

      T findOneOrThrow()
      Execute the query and return the first matching entity, or throw if not found.
      Returns:
      the first matching entity
      Throws:
      NoSuchElementException - if no match found
    • count

      long count()
      Count matching entities.
      Returns:
      count of matching entities
    • exists

      boolean exists()
      Check if any matching entities exist.
      Returns:
      true if at least one match exists
    • delete

      int delete()
      Delete all matching entities.
      Returns:
      number of entities deleted