Class SqlBuilder

java.lang.Object
io.github.blyznytsiaorg.bibernate.dao.jdbc.SqlBuilder

public class SqlBuilder extends Object
Utility class for building SQL statements (SELECT, UPDATE, INSERT, DELETE) based on different scenarios.
Since:
1.0
Author:
Blyzhnytsia Team
  • Constructor Details

    • SqlBuilder

      public SqlBuilder()
  • Method Details

    • selectBy

      public String selectBy(String tableName, String whereCondition)
      Generates a SELECT SQL statement for querying records from a specified table with a WHERE condition.
      Parameters:
      tableName - The name of the table from which to select records.
      whereCondition - The WHERE condition for filtering records.
      Returns:
      The generated SELECT SQL statement as a string.
    • selectByWithJoin

      public String selectByWithJoin(String tableName, String whereCondition, Set<JoinInfo> joinInfos, JoinType joinType)
      Builds a SELECT statement with joins and a WHERE condition based on the provided parameters.
      Parameters:
      tableName - the main table name for the SELECT statement
      whereCondition - the WHERE condition for filtering results
      joinInfos - the information about joins to be performed
      joinType - the type of join to be used (e.g., INNER JOIN, LEFT JOIN)
      Returns:
      the dynamically generated SQL SELECT statement
      Throws:
      BibernateGeneralException - if there is an issue retrieving column or join information
    • selectAll

      public String selectAll(String tableName)
      Builds a simple SELECT statement for retrieving all records from a given table.
      Parameters:
      tableName - the name of the table to SELECT from
      Returns:
      the dynamically generated SQL SELECT statement
    • selectAllByFieldName

      public String selectAllByFieldName(String tableName, String filedName, Integer countOfParameters)
      Builds a SELECT statement with a WHERE condition based on a specific field and parameter count.
      Parameters:
      tableName - the main table name for the SELECT statement
      filedName - the field name for the WHERE condition
      countOfParameters - the count of parameters for the WHERE condition
      Returns:
      the dynamically generated SQL SELECT statement
    • update

      public String update(Object entity, String tableName, String fieldIdName, List<ColumnSnapshot> diff)
      Generates an UPDATE SQL statement for updating records in a specified table based on the provided entity, fieldIdName, and list of ColumnSnapshots representing the differences.
      Parameters:
      entity - The entity representing the data to be updated.
      tableName - The name of the table to be updated.
      fieldIdName - The name of the ID field used in the WHERE condition.
      diff - The list of ColumnSnapshots representing differences.
      Returns:
      The generated UPDATE SQL statement as a string.
    • fieldEqualsParameterCondition

      public String fieldEqualsParameterCondition(String fieldName)
      Generates a field equals parameter WHERE condition for use in SQL statements.
      Parameters:
      fieldName - The name of the field.
      Returns:
      The generated WHERE condition as a string.
    • fieldInParametersCondition

      public String fieldInParametersCondition(String fieldName, Integer countOfParameters)
      Generates a SQL condition for a field, representing an "IN" clause with a specified number of parameters.
      Parameters:
      fieldName - The name of the field for which the condition is generated.
      countOfParameters - The count of parameters in the "IN" clause.
      Returns:
      A string representing the SQL condition for the "IN" clause with the specified field name and parameters.
    • insert

      public static String insert(Class<?> entityClass, String tableName)
      Generates an INSERT SQL statement for inserting records into a specified table based on the provided entity class. The method utilizes reflection to extract the fields from the entity class and constructs the INSERT statement accordingly.
      Parameters:
      entityClass - The Class object representing the type of the entity for which records will be inserted.
      tableName - The name of the table into which records will be inserted.
      Returns:
      The generated INSERT SQL statement as a string.
    • insert

      public static String insert(String tableName, List<String> columns)
      Generates an INSERT SQL statement for inserting records into a specified table based on the provided table name and list of column names.
      Parameters:
      tableName - The name of the table into which records will be inserted.
      columns - The names of columns which will take part in the building of the query.
      Returns:
      The generated INSERT SQL statement as a string.
    • delete

      public String delete(String tableName, String fieldIdName)
      Generates a DELETE SQL statement for deleting records from a specified table based on the provided ID field.
      Parameters:
      tableName - The name of the table from which records will be deleted.
      fieldIdName - The name of the ID field used in the WHERE condition.
      Returns:
      The generated DELETE SQL statement as a string.
    • delete

      public String delete(String tableName, String fieldIdName, String version)
      Generates a DELETE SQL statement for deleting records from a specified table based on the provided ID field and version.
      Parameters:
      tableName - The name of the table from which records will be deleted.
      fieldIdName - The name of the ID field used in the WHERE condition.
      version - The version field used in the WHERE condition.
      Returns:
      The generated DELETE SQL statement as a string.
    • selectWithJoin

      public String selectWithJoin(String entityTableName, String entityTableIdFieldName, Field joinTableField)
      Generates a SELECT SQL statement with a JOIN operation between two tables.
      Parameters:
      entityTableName - The name of the entity table to be selected.
      entityTableIdFieldName - The name of the ID field in the entity table.
      joinTableField - The field representing the join relationship.
      Returns:
      The generated SELECT SQL statement with JOIN as a string.
    • mergeQueries

      public String mergeQueries(String leftJoinQuery, String innerJoinQuery)
      Merges a LEFT JOIN query with an INNER JOIN query.
      Parameters:
      leftJoinQuery - the LEFT JOIN query
      innerJoinQuery - the INNER JOIN query
      Returns:
      the merged query
    • getOnCondition

      private static String getOnCondition(String entityTableName, String entityTableIdFieldName, String joinTableName, String inverseJoinColumnName)
      Constructs the ON condition for a JOIN operation between two tables.
      Parameters:
      entityTableName - The name of the entity table participating in the JOIN.
      entityTableIdFieldName - The name of the ID field in the entity table.
      joinTableName - The name of the table being joined with the entity table.
      inverseJoinColumnName - The name of the column in the join table that corresponds to the entity table's ID.
      Returns:
      The constructed ON condition as a string.
    • populateFieldOrIncVersion

      private void populateFieldOrIncVersion(String fieldName, boolean isVersionFound, String finalFieldVersionName, UpdateQueryBuilder update)
      Populates a field in an UPDATE operation with its corresponding value or increments the version field.
      Parameters:
      fieldName - The name of the field to be updated.
      isVersionFound - Indicates whether a version field is found in the entity.
      finalFieldVersionName - The name of the version field, if found.
      update - The UpdateQueryBuilder instance for building the UPDATE statement.