Class QueryBuilder

java.lang.Object
org.pgcodekeeper.core.database.base.jdbc.QueryBuilder

public final class QueryBuilder extends Object
SQL query builder for constructing SELECT statements with support for CTEs, joins, WHERE clauses, and GROUP BY. Provides a fluent interface for building complex SQL queries programmatically.
  • Constructor Details

    • QueryBuilder

      public QueryBuilder()
  • Method Details

    • column

      public QueryBuilder column(String column)
      Adds a column to the SELECT clause.
      Parameters:
      column - the column expression to add
      Returns:
      this builder for method chaining
    • column

      public QueryBuilder column(String prefix, QueryBuilder column, String postfix)
      Adds a column with prefix and postfix using a subquery.
      Parameters:
      prefix - text to prepend before the subquery
      column - the subquery builder for the column
      postfix - text to append after the subquery
      Returns:
      this builder for method chaining
    • join

      public QueryBuilder join(String join)
      Adds a JOIN clause to the query.
      Parameters:
      join - the JOIN clause to add
      Returns:
      this builder for method chaining
    • join

      public QueryBuilder join(String prefix, QueryBuilder join, String postfix)
      Adds a JOIN clause with prefix and postfix using a subquery.
      Parameters:
      prefix - text to prepend before the subquery
      join - the subquery builder for the join
      postfix - text to append after the subquery
      Returns:
      this builder for method chaining
    • from

      public QueryBuilder from(String from)
      Sets the FROM clause of the query.
      Parameters:
      from - the FROM clause
      Returns:
      this builder for method chaining
    • from

      public QueryBuilder from(QueryBuilder from, String postfix)
      Sets the FROM clause using a subquery with postfix.
      Parameters:
      from - the subquery builder for the FROM clause
      postfix - text to append after the subquery
      Returns:
      this builder for method chaining
    • where

      public QueryBuilder where(String where)
      Adds a WHERE condition to the query.
      Parameters:
      where - the WHERE condition to add
      Returns:
      this builder for method chaining
    • where

      public QueryBuilder where(String prefix, QueryBuilder where)
      Adds a WHERE condition with prefix using a subquery.
      Parameters:
      prefix - text to prepend before the subquery
      where - the subquery builder for the WHERE condition
      Returns:
      this builder for method chaining
    • postAction

      public QueryBuilder postAction(String action)
      Sets a post-action clause (e.g., ORDER BY, LIMIT) to append after the main query.
      Parameters:
      action - the post-action clause
      Returns:
      this builder for method chaining
    • with

      public QueryBuilder with(String alias, String cte)
      Adds a Common Table Expression (CTE) to the WITH clause.
      Parameters:
      alias - the alias name for the CTE
      cte - the CTE query string
      Returns:
      this builder for method chaining
    • with

      public QueryBuilder with(String alias, QueryBuilder cte)
      Adds a Common Table Expression (CTE) using a subquery builder.
      Parameters:
      alias - the alias name for the CTE
      cte - the subquery builder for the CTE
      Returns:
      this builder for method chaining
    • groupBy

      public QueryBuilder groupBy(String group)
      Adds a GROUP BY expression to the query.
      Parameters:
      group - the GROUP BY expression
      Returns:
      this builder for method chaining
    • orderBy

      public QueryBuilder orderBy(String order)
      Adds a ORDER BY expression to the query.
      Parameters:
      order - the ORDER BY expression
      Returns:
      this builder for method chaining
    • build

      public String build()
      Builds and returns the complete SQL query string.
      Returns:
      the constructed SQL query
    • copy

      public QueryBuilder copy()
      Creates a deep copy of this query builder.
      Returns:
      a new QueryBuilder instance with the same configuration