Package org.pgcodekeeper.core.loader
Class QueryBuilder
java.lang.Object
org.pgcodekeeper.core.loader.QueryBuilder
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds and returns the complete SQL query string.Adds a column to the SELECT clause.column(String prefix, QueryBuilder column, String postfix) Adds a column with prefix and postfix using a subquery.copy()Creates a deep copy of this query builder.Sets the FROM clause of the query.from(QueryBuilder from, String postfix) Sets the FROM clause using a subquery with postfix.Adds a GROUP BY expression to the query.Adds a JOIN clause to the query.join(String prefix, QueryBuilder join, String postfix) Adds a JOIN clause with prefix and postfix using a subquery.postAction(String action) Sets a post-action clause (e.g., ORDER BY, LIMIT) to append after the main query.Adds a WHERE condition to the query.where(String prefix, QueryBuilder where) Adds a WHERE condition with prefix using a subquery.Adds a Common Table Expression (CTE) to the WITH clause.with(String alias, QueryBuilder cte) Adds a Common Table Expression (CTE) using a subquery builder.
-
Constructor Details
-
QueryBuilder
public QueryBuilder()
-
-
Method Details
-
column
Adds a column to the SELECT clause.- Parameters:
column- the column expression to add- Returns:
- this builder for method chaining
-
column
Adds a column with prefix and postfix using a subquery.- Parameters:
prefix- text to prepend before the subquerycolumn- the subquery builder for the columnpostfix- text to append after the subquery- Returns:
- this builder for method chaining
-
join
Adds a JOIN clause to the query.- Parameters:
join- the JOIN clause to add- Returns:
- this builder for method chaining
-
join
Adds a JOIN clause with prefix and postfix using a subquery.- Parameters:
prefix- text to prepend before the subqueryjoin- the subquery builder for the joinpostfix- text to append after the subquery- Returns:
- this builder for method chaining
-
from
Sets the FROM clause of the query.- Parameters:
from- the FROM clause- Returns:
- this builder for method chaining
-
from
Sets the FROM clause using a subquery with postfix.- Parameters:
from- the subquery builder for the FROM clausepostfix- text to append after the subquery- Returns:
- this builder for method chaining
-
where
Adds a WHERE condition to the query.- Parameters:
where- the WHERE condition to add- Returns:
- this builder for method chaining
-
where
Adds a WHERE condition with prefix using a subquery.- Parameters:
prefix- text to prepend before the subquerywhere- the subquery builder for the WHERE condition- Returns:
- this builder for method chaining
-
postAction
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
Adds a Common Table Expression (CTE) to the WITH clause.- Parameters:
alias- the alias name for the CTEcte- the CTE query string- Returns:
- this builder for method chaining
-
with
Adds a Common Table Expression (CTE) using a subquery builder.- Parameters:
alias- the alias name for the CTEcte- the subquery builder for the CTE- Returns:
- this builder for method chaining
-
groupBy
Adds a GROUP BY expression to the query.- Parameters:
group- the GROUP BY expression- Returns:
- this builder for method chaining
-
build
Builds and returns the complete SQL query string.- Returns:
- the constructed SQL query
-
copy
Creates a deep copy of this query builder.- Returns:
- a new QueryBuilder instance with the same configuration
-