Class JpqlValidatorSupport

java.lang.Object
io.jmix.aitools.dataload.validation.validator.JpqlValidatorSupport

public final class JpqlValidatorSupport extends Object
Shared helpers for JPQL validators.
  • Method Details

    • containsWord

      public static boolean containsWord(String text, String word)
      Returns whether the text contains the given word as a whole word.
      Parameters:
      text - text to search
      word - word to look for
      Returns:
      true if the word is present
    • containsFunctionCall

      public static boolean containsFunctionCall(String text, String functionName)
      Returns whether the text contains a call to the given function (its name followed by '(').
      Parameters:
      text - text to search
      functionName - function name to look for
      Returns:
      true if such a function call is present
    • stripStringLiterals

      public static String stripStringLiterals(String jpql)
      Replaces every single-quoted string literal with an empty literal (''), so the literal's content (e.g. a :-prefixed word or an uppercase token) is not mistaken for a JPQL parameter, keyword or constant.
      Parameters:
      jpql - JPQL text
      Returns:
      the text with all string literals emptied
    • referencedParameters

      public static Set<String> referencedParameters(String jpql)
      Returns the named parameters the query text references, in order of first appearance. This is the reading the add-on applies when it accepts or rejects a query: a :name occurrence counts as a parameter reference unless it sits inside a string literal (see stripStringLiterals(String)). Consumers that store a query together with a parameter declaration should derive that declaration with this method, so a query the consumer writes and a query the add-on validates read the same.
      Parameters:
      jpql - JPQL text
      Returns:
      the referenced parameter names in order of appearance; empty if there are none
    • selectedEntityExpressions

      public static List<String> selectedEntityExpressions(QueryParser parser, Metadata metadata)
      Returns the select-clause expressions whose result column is an entity or embeddable instance rather than a value: a bare identification variable (select c) or a path ending in a reference or embedded attribute (select o.customer). An aggregate over such an expression (count(o)) yields a value and is not returned; neither is a path the data model does not know.
      Parameters:
      parser - parser of the query
      metadata - metadata used to resolve the attributes a path ends in
      Returns:
      the offending expressions as the parser reports them, in select-clause order; empty if none
    • extractAliases

      public static List<String> extractAliases(String jpql)
      Extracts the alias names declared with AS at the top level of the query — that is, identification variables (entity and join aliases) and select result variables. An AS inside parentheses (such as the target type of CAST(... AS ...) or TREAT(... AS ...)) is not an alias declaration and is skipped. String literals are ignored.
      Parameters:
      jpql - JPQL text
      Returns:
      the declared alias names in order of appearance; empty if there are none
    • selectedValueCount

      public static int selectedValueCount(String jpql)
      Counts the values the select clause of a query selects. A comma inside a function call, a subquery or a string literal does not separate selected values.
      Parameters:
      jpql - JPQL select query
      Returns:
      the number of selected values
    • selectedAliases

      public static List<@Nullable String> selectedAliases(String jpql)
      Returns the AS alias of each value the select clause selects, in select-clause order, so that a value can be told from the name it is bound to by position. An as inside a value, such as the target type of cast(... as string), is not an alias.
      Parameters:
      jpql - JPQL select query
      Returns:
      one entry per selected value: its alias, or null when the value has none
    • selectedExpressions

      public static List<String> selectedExpressions(String jpql)
      Returns the expression of each value the select clause selects, in select-clause order, without its AS alias and with whitespace collapsed, so that two queries can be compared value by value.
      Parameters:
      jpql - JPQL select query
      Returns:
      one expression per selected value
    • resultAliases

      public static List<String> resultAliases(String jpql)
      Returns the names of the columns of a query: the AS aliases of its select clause, in select-clause order. Columns are bound to values by position, so the aliases name the columns only when every selected value has one; a partial list is not returned.
      Parameters:
      jpql - JPQL select query
      Returns:
      the aliases, or an empty list when not every selected value is aliased
    • getQueryParser

      public static @Nullable QueryParser getQueryParser(@Nullable QueryTransformerFactory queryTransformerFactory, @Nullable String jpql)
      Parses the given JPQL into a QueryParser.
      Parameters:
      queryTransformerFactory - factory used to create the parser, may be null if unavailable
      jpql - JPQL text to parse
      Returns:
      the parser, or null if the factory is missing, the text is blank, or parsing fails