Class AbstractTableQuery<T extends AbstractTableQuery<T>>

java.lang.Object
com.github.darksoulq.abyssallib.common.database.relational.AbstractTableQuery<T>
Type Parameters:
T - The implementation type used for fluent chaining, allowing methods to return the specific subclass type.
Direct Known Subclasses:
TableQuery, TableQuery, TableQuery, TableQuery, TableQuery

public abstract class AbstractTableQuery<T extends AbstractTableQuery<T>> extends Object
An abstract base class providing a fluent interface for table-level CRUD operations. It supports standard INSERT, REPLACE, UPDATE, and DELETE operations, as well as complex SELECT queries with mapping, ordering, and pagination.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AbstractTableQuery(Connection connection, String table, ExecutorService asyncPool)
    Constructs a new AbstractTableQuery with the required execution context.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Executes a COUNT(*) query based on the current where clause configuration.
    Sets the query operation mode to DELETE.
    int
    Executes the built data modification query synchronously.
    Asynchronously executes the currently built data modification operation.
    boolean
    Determines if at least one row matches the query criteria.
    <R> R
    first(ResultMapper<R> mapper)
    Fetches the first row from the result set and transforms it using the provided mapper.
    Sets the query operation mode to INSERT.
    limit(int limit)
    Sets the maximum number of rows to be returned by the query.
    offset(int offset)
    Sets the starting offset for the result set, primarily used for pagination.
    orderBy(String column, boolean ascending)
    Sets the ordering criteria for the resulting data set.
    Sets the query operation mode to REPLACE.
    <R> List<R>
    select(ResultMapper<R> mapper)
    Selects all columns (*) from the table and maps the results.
    <R> List<R>
    select(ResultMapper<R> mapper, String... columns)
    Selects specific columns and maps the resulting rows into a list of objects.
    Asynchronously selects all columns and maps the results.
    selectAsync(ResultMapper<R> mapper, String... columns)
    Asynchronously selects specific columns and maps the results.
    Sets the query operation mode to UPDATE.
    value(String column, Object value)
    Adds a column-value pair to be used for INSERT, REPLACE, or UPDATE operations.
    where(String clause, Object... params)
    Configures the filtering criteria for the query using a WHERE clause.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AbstractTableQuery

      public AbstractTableQuery(Connection connection, String table, ExecutorService asyncPool)
      Constructs a new AbstractTableQuery with the required execution context.
      Parameters:
      connection - The JDBC connection to be used for executing SQL statements.
      table - The name of the target database table.
      asyncPool - The executor service for handling asynchronous tasks.
  • Method Details

    • insert

      public T insert()
      Sets the query operation mode to INSERT.
      Returns:
      The fluent instance cast to the implementation type T.
    • replace

      public T replace()
      Sets the query operation mode to REPLACE.
      Returns:
      The fluent instance cast to the implementation type T.
    • update

      public T update()
      Sets the query operation mode to UPDATE.
      Returns:
      The fluent instance cast to the implementation type T.
    • delete

      public T delete()
      Sets the query operation mode to DELETE.
      Returns:
      The fluent instance cast to the implementation type T.
    • value

      public T value(String column, Object value)
      Adds a column-value pair to be used for INSERT, REPLACE, or UPDATE operations.
      Parameters:
      column - The name of the target column.
      value - The value to be set for the specified column.
      Returns:
      The fluent instance cast to the implementation type T.
    • where

      public T where(String clause, Object... params)
      Configures the filtering criteria for the query using a WHERE clause.
      Parameters:
      clause - The SQL string for the where clause, using '?' for parameter placeholders.
      params - The objects to bind to the placeholders in the provided clause.
      Returns:
      The fluent instance cast to the implementation type T.
    • orderBy

      public T orderBy(String column, boolean ascending)
      Sets the ordering criteria for the resulting data set.
      Parameters:
      column - The column name to sort by.
      ascending - Set to true for ascending order, false for descending.
      Returns:
      The fluent instance cast to the implementation type T.
    • limit

      public T limit(int limit)
      Sets the maximum number of rows to be returned by the query.
      Parameters:
      limit - The maximum number of results.
      Returns:
      The fluent instance cast to the implementation type T.
    • offset

      public T offset(int offset)
      Sets the starting offset for the result set, primarily used for pagination.
      Parameters:
      offset - The number of rows to skip.
      Returns:
      The fluent instance cast to the implementation type T.
    • execute

      public int execute()
      Executes the built data modification query synchronously.
      Returns:
      The number of rows affected by the execution.
      Throws:
      RuntimeException - If a SQLException occurs during execution.
    • executeAsync

      public CompletableFuture<Integer> executeAsync()
      Asynchronously executes the currently built data modification operation.
      Returns:
      A CompletableFuture containing the number of rows affected.
    • count

      public long count()
      Executes a COUNT(*) query based on the current where clause configuration.
      Returns:
      The total count of rows matching the query criteria.
      Throws:
      RuntimeException - If a SQLException occurs.
    • exists

      public boolean exists()
      Determines if at least one row matches the query criteria.
      Returns:
      True if the matching count is greater than zero, false otherwise.
    • first

      public <R> R first(ResultMapper<R> mapper)
      Fetches the first row from the result set and transforms it using the provided mapper.
      Type Parameters:
      R - The type of the resulting object.
      Parameters:
      mapper - The ResultMapper used to convert the ResultSet row into type R.
      Returns:
      The mapped object, or null if no matching rows were found.
    • select

      public <R> List<R> select(ResultMapper<R> mapper)
      Selects all columns (*) from the table and maps the results.
      Type Parameters:
      R - The type of the resulting objects.
      Parameters:
      mapper - The mapper used for object transformation.
      Returns:
      A list of mapped results.
    • select

      public <R> List<R> select(ResultMapper<R> mapper, String... columns)
      Selects specific columns and maps the resulting rows into a list of objects.
      Type Parameters:
      R - The type of the resulting objects.
      Parameters:
      mapper - The mapper used for object transformation.
      columns - The column names to be included in the selection.
      Returns:
      A list of transformed result objects.
      Throws:
      RuntimeException - If an error occurs during execution or mapping.
    • selectAsync

      public <R> CompletableFuture<List<R>> selectAsync(ResultMapper<R> mapper)
      Asynchronously selects all columns and maps the results.
      Type Parameters:
      R - The target result type.
      Parameters:
      mapper - The mapper for object conversion.
      Returns:
      A CompletableFuture containing the list of results.
    • selectAsync

      public <R> CompletableFuture<List<R>> selectAsync(ResultMapper<R> mapper, String... columns)
      Asynchronously selects specific columns and maps the results.
      Type Parameters:
      R - The target result type.
      Parameters:
      mapper - The mapper for object conversion.
      columns - The column names to select.
      Returns:
      A CompletableFuture containing the list of results.