Class MySQLQueryBuilder

java.lang.Object
com.rosivanyshyn.utils.MySQLQueryBuilder

public class MySQLQueryBuilder extends Object
MySQL Query Builder.
It contains methods to build dynamic query depends on business requirements.
  • Field Details

  • Constructor Details

    • MySQLQueryBuilder

      public MySQLQueryBuilder()
  • Method Details

    • setLabel

      public void setLabel(String label)
      Setter
      Parameters:
      label - name of DB table
    • join

      public void join(String table, String label, String tableField, String field)
      Returns rows of current table, that are existed in table B
      Parameters:
      table - table to comparisons
      label - abbreviation of the compared table name
      tableField - field name of the compared table
      field - field name of current table
    • excludeJoin

      public void excludeJoin(String table, String label, String tableField, String field)
      Returns rows of current table, that are not exist in table B
      Parameters:
      table - table to comparisons
      label - abbreviation of the compared table name
      tableField - field name of the compared table
      field - field name of current table
    • where

      public void where(String field, MySQLQueryBuilder.LogicalOperation logicalOperation, boolean and)
      Filter the table by the specified field
      Parameters:
      field - field by which to filter
      and - combining with previous condition. True -> the preconditions, including the new one, must be true to return records. False -> return a records if any of the conditions is true.
    • addWhere

      protected void addWhere(String clause, boolean and)
      Combine several filters
      Parameters:
      clause - new filter
      and - Combining with previous condition. True -> the preconditions, including the new one, must be true to return records. False -> return a records if any of the conditions is true.
    • subcondition

      public void subcondition(Boolean state)
      Open or close new sub-condition in WHERE clause
      Parameters:
      state - - State of condition. True - open new sub-condition. False - close previous sub-condition
    • limit

      public void limit(Integer offset, Integer limit)
      Specify the number of records to return.
      Parameters:
      offset - which record to start sampling from
      limit - records count to get
    • order

      public void order(String field, boolean desc)
      Sort records by field in ascending or descending order.
      Parameters:
      field - field by which to sort
      desc - if true -> sort in descending order
    • getQuery

      public String getQuery()
      Build the query from all the previous added parts.
      Returns:
      string represent of query.
    • clear

      public void clear()
      Clear query builder