Class SelectQueryBuilder

java.lang.Object
io.github.blyznytsiaorg.bibernate.dao.jdbc.dsl.QueryBuilder
io.github.blyznytsiaorg.bibernate.dao.jdbc.dsl.SelectQueryBuilder

public class SelectQueryBuilder extends QueryBuilder
Represents a SQL SELECT query builder for constructing SELECT statements with optional clauses such as JOIN, WHERE, GROUP BY, HAVING, and UNION. Extends the base class QueryBuilder. Example usage: ```java SelectQueryBuilder.from("users") .selectField("*") .join("orders", "users.id = orders.user_id", JoinType.LEFT) .whereCondition("age > ?") .groupBy("name") .havingCondition("COUNT(*) > 1") .buildSelectStatement(); Result will be: SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id WHERE age > ? GROUP BY name HAVING COUNT(*) > 1; ```
Since:
1.0
Author:
Blyzhnytsia Team
  • Field Details

    • groupByField

      private String groupByField
      The field to be used for GROUP BY clauses.
    • havingCondition

      private String havingCondition
      The condition for HAVING clauses.
    • selectedFields

      private final List<String> selectedFields
      The list of fields to be selected in the SELECT statement.
    • joinClauses

      private final List<JoinClause> joinClauses
      The list of JOIN clauses to be included in the SELECT statement.
    • unionQueries

      private final List<SelectQueryBuilder> unionQueries
      The list of SELECT queries to be combined using UNION.
  • Constructor Details

    • SelectQueryBuilder

      public SelectQueryBuilder(String tableName)
      Constructs a new SelectQueryBuilder with the specified table name.
      Parameters:
      tableName - The name of the table from which to select records.
  • Method Details

    • from

      public static SelectQueryBuilder from(String tableName)
      Creates a new SelectQueryBuilder with the specified table name.
      Parameters:
      tableName - The name of the table from which to select records.
      Returns:
      A new instance of SelectQueryBuilder.
    • selectField

      public SelectQueryBuilder selectField(String fieldName)
      Adds a field to the list of fields to be selected in the SELECT statement.
      Parameters:
      fieldName - The name of the field to be selected.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • selectFields

      public SelectQueryBuilder selectFields(List<String> fieldNames)
    • selectFieldsFromTable

      public SelectQueryBuilder selectFieldsFromTable(String tableName)
      Adds all fields from a specified table to the list of fields to be selected.
      Parameters:
      tableName - The name of the table from which to select all fields.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • selectFieldFromTable

      public SelectQueryBuilder selectFieldFromTable(String tableName, String fieldName)
      Adds a specific field from a specified table to the list of fields to be selected.
      Parameters:
      tableName - The name of the table from which to select the field.
      fieldName - The name of the field to be selected.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • join

      public SelectQueryBuilder join(String joinedTable, String onCondition, JoinType joinType)
      Adds a JOIN clause to the SELECT statement.
      Parameters:
      joinedTable - The name of the table to be joined.
      onCondition - The ON condition specifying how the tables should be joined.
      joinType - The type of JOIN (INNER, LEFT, RIGHT, FULL).
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • join

      public SelectQueryBuilder join(List<JoinClause> joinClausesList)
      Adds a list of join clauses to the current select query builder.
      Parameters:
      joinClausesList - The list of join clauses to be added.
      Returns:
      The updated SelectQueryBuilder instance with the specified join clauses.
    • whereCondition

      public SelectQueryBuilder whereCondition(String condition)
      Adds a WHERE condition to the SELECT statement.
      Parameters:
      condition - The WHERE condition to be added.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • andCondition

      public SelectQueryBuilder andCondition(String condition)
      Adds an AND condition to the existing WHERE conditions.
      Parameters:
      condition - The AND condition to be added.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • orCondition

      public SelectQueryBuilder orCondition(String condition)
      Adds an OR condition to the existing WHERE conditions.
      Parameters:
      condition - The OR condition to be added.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • betweenCondition

      public SelectQueryBuilder betweenCondition(String field, Object lowerBound, Object upperBound)
      Adds a BETWEEN condition to the existing WHERE conditions.
      Parameters:
      field - The field to which the BETWEEN condition applies.
      lowerBound - The lower bound of the range.
      upperBound - The upper bound of the range.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • groupBy

      public SelectQueryBuilder groupBy(String groupByField)
      Sets the field for GROUP BY clauses in the SELECT statement.
      Parameters:
      groupByField - The field to be used for GROUP BY clauses.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • havingCondition

      public SelectQueryBuilder havingCondition(String havingCondition)
      Sets the condition for HAVING clauses in the SELECT statement.
      Parameters:
      havingCondition - The condition for HAVING clauses.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • union

      public SelectQueryBuilder union(SelectQueryBuilder otherQuery)
      Combines the current SELECT query with another query using the UNION operator.
      Parameters:
      otherQuery - The SelectQueryBuilder representing the other query.
      Returns:
      The current SelectQueryBuilder instance for method chaining.
    • buildSelectStatement

      public String buildSelectStatement()
      Builds the SELECT SQL statement based on the configured conditions and clauses.
      Returns:
      The generated SELECT SQL statement as a string.