Class QueryUtil

java.lang.Object
com.ssgllc.fish.service.util.registered.QueryUtil

@Component public class QueryUtil extends Object
  • Constructor Details

    • QueryUtil

      public QueryUtil(jakarta.persistence.EntityManager entityManager, com.ssgllc.fish.service.GenericQueryService genericQueryService)
  • Method Details

    • queryList

      public static List<Object> queryList(String queryStr) throws Exception
      Executes an HQL query and returns the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      Returns:
      A list of query results.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryList("select f.id from FieldConfig f")
    • queryListLimit

      public static List<Object> queryListLimit(String queryStr, Integer limit) throws Exception
      Executes an HQL query with a result limit and returns the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      limit - The maximum number of results to return.
      Returns:
      A list of query results, limited to the specified number.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryListLimit("select f.id from FieldConfig f", 5)
    • queryListLimitPage

      public static List<Object> queryListLimitPage(String queryStr, Integer limit, Integer pageNumber) throws Exception
      Executes an HQL query with pagination and a result limit, returning the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      limit - The maximum number of results per page.
      pageNumber - The zero-based page number to retrieve.
      Returns:
      A list of query results for the specified page, limited to the given number of results.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryListLimitPage("select f.id from FieldConfig f", 5, 1)
    • queryListParam

      public static List<Object> queryListParam(String queryStr, Map<String,Object> params) throws Exception
      Executes an HQL query with named parameters and returns the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      params - A map of named parameters to be set in the query.
      Returns:
      A list of query results.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryListParam("select f.id from FieldConfig f where f.name = :name", [name: "title"])
    • queryListParamLimit

      public static List<Object> queryListParamLimit(String queryStr, Map<String,Object> params, Integer limit) throws Exception
      Executes an HQL query with named parameters and a result limit, returning the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      params - A map of named parameters to be set in the query.
      limit - The maximum number of results to return.
      Returns:
      A list of query results, limited to the specified number.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryListParamLimit("select f.id from FieldConfig f where f.createdDate > :date", [date: dateUtil.startOfToday()], 10)
    • queryListParamLimitPage

      public static List<Object> queryListParamLimitPage(String queryStr, Map<String,Object> params, Integer limit, Integer pageNumber) throws Exception
      Executes an HQL query with named parameters, pagination, and a result limit, returning the result as a list of objects.
      Parameters:
      queryStr - The HQL query string to execute.
      params - A map of named parameters to be set in the query.
      limit - The maximum number of results per page.
      pageNumber - The zero-based page number to retrieve.
      Returns:
      A list of query results for the specified page, limited to the given number of results.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      return queryUtil.queryListParamLimitPage("select f.id from FieldConfig f where f.createdDate > :date", [date: dateUtil.startOfToday()], 10, 2)
    • queryListParamLimitPageSkipLocked

      public static List<Object> queryListParamLimitPageSkipLocked(String queryStr, Map<String,Object> params, Integer limit, Integer pageNumber) throws Exception
      Executes an HQL query with named parameters, pagination, and skip-locked pessimistic locking, returning the result as a list of objects.
      This method ensures that rows currently locked by other transactions are skipped to avoid blocking.
      Parameters:
      queryStr - The HQL query string to execute.
      params - A map of named parameters to be set in the query.
      limit - The maximum number of results per page.
      pageNumber - The zero-based page number to retrieve.
      Returns:
      A list of query results for the specified page, limited to the given number of results, skipping any locked rows.
      Throws:
      Exception - If an error occurs while executing the query.

      Groovy example:
      def params = [:] params.calcTypeId = stringUtil.uuidFromString(conceptUtil.getConceptIdFromCode('CalculationType', 'CALC_DYNAMIC')) return queryUtil.queryListParamLimitPageSkipLocked("select f.id from FieldConfig f where f.calcType.id = :calcTypeId", params, 20, 0)

      Returns:
      A list of FieldConfig IDs with calcType of "CALC_DYNAMIC", up to 20 results, skipping locked rows. Note: This method uses pessimistic locking with SKIP_LOCKED, which ensures that locked rows are not included in the result set. Also, any rows that are joined will be locked, so be careful with things like not locking concept table rows.
    • querySingle

      public static Object querySingle(String queryStr) throws Exception
      Executes an HQL query and returns a single result.
      Parameters:
      queryStr - The HQL query string to execute.
      Returns:
      A single query result, or null if no result is found.
      Throws:
      Exception - If an error occurs while executing the query including finding more than one result.

      Groovy example:
      return queryUtil.querySingle("select f.id from FieldConfig f where f.name = 'deduplicationEnabled'")
    • querySingleParam

      public static Object querySingleParam(String queryStr, Map<String,Object> params) throws Exception
      Executes an HQL query with named parameters and returns a single result.
      Parameters:
      queryStr - The HQL query string to execute.
      params - A map of named parameters to be set in the query.
      Returns:
      A single query result, or null if no result is found.
      Throws:
      Exception - If an error occurs while executing the query including finding more than one result.

      Groovy example:
      return queryUtil.querySingleParam("select f.id from FieldConfig f where f.name = :name", [name: "deduplicationEnabled"])
    • getEntityManager

      public static jakarta.persistence.EntityManager getEntityManager()
      Retrieves the EntityManager instance used for executing queries.
      Returns:
      The EntityManager instance.

      Groovy example:
      return queryUtil.getEntityManager()