Class JpqlValidatorSupport
java.lang.Object
io.jmix.aitools.dataload.validation.validator.JpqlValidatorSupport
Shared helpers for JPQL validators.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontainsFunctionCall(String text, String functionName) Returns whether the text contains a call to the given function (its name followed by'(').static booleancontainsWord(String text, String word) Returns whether the text contains the given word as a whole word.extractAliases(String jpql) Extracts the alias names declared withASat the top level of the query — that is, identification variables (entity and join aliases) and select result variables.static @Nullable QueryParsergetQueryParser(@Nullable QueryTransformerFactory queryTransformerFactory, @Nullable String jpql) Parses the given JPQL into aQueryParser.referencedParameters(String jpql) Returns the named parameters the query text references, in order of first appearance.resultAliases(String jpql) Returns the names of the columns of a query: theASaliases of its select clause, in select-clause order.selectedAliases(String jpql) Returns theASalias 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.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).selectedExpressions(String jpql) Returns the expression of each value the select clause selects, in select-clause order, without itsASalias and with whitespace collapsed, so that two queries can be compared value by value.static intselectedValueCount(String jpql) Counts the values the select clause of a query selects.static StringstripStringLiterals(String jpql) Replaces every single-quoted string literal with an empty literal (''), so the literal's content (e.g.
-
Method Details
-
containsWord
Returns whether the text contains the given word as a whole word.- Parameters:
text- text to searchword- word to look for- Returns:
trueif the word is present
-
containsFunctionCall
Returns whether the text contains a call to the given function (its name followed by'(').- Parameters:
text- text to searchfunctionName- function name to look for- Returns:
trueif such a function call is present
-
stripStringLiterals
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
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:nameoccurrence counts as a parameter reference unless it sits inside a string literal (seestripStringLiterals(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
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 querymetadata- 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
Extracts the alias names declared withASat the top level of the query — that is, identification variables (entity and join aliases) and select result variables. AnASinside parentheses (such as the target type ofCAST(... AS ...)orTREAT(... 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
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
Returns theASalias 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. Anasinside a value, such as the target type ofcast(... as string), is not an alias.- Parameters:
jpql- JPQL select query- Returns:
- one entry per selected value: its alias, or
nullwhen the value has none
-
selectedExpressions
Returns the expression of each value the select clause selects, in select-clause order, without itsASalias 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
Returns the names of the columns of a query: theASaliases 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 aQueryParser.- Parameters:
queryTransformerFactory- factory used to create the parser, may benullif unavailablejpql- JPQL text to parse- Returns:
- the parser, or
nullif the factory is missing, the text is blank, or parsing fails
-