Class DefaultSemanticValidator

  • All Implemented Interfaces:
    ExpressionVisitor

    public class DefaultSemanticValidator
    extends AbstractSemanticValidator
    This validator is responsible to gather the problems found in a JPQL query by validating the content to make sure it is semantically valid. The grammar is not validated by this visitor.

    For instance, the function AVG accepts a state field path. The property it represents has to be of numeric type. AVG(e.name) is parsable but is not semantically valid because the type of name is a string (the property signature is: "private String name").

    Provisional API: This interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

    Version:
    2.5
    See Also:
    DefaultGrammarValidator
    Author:
    Pascal Filion
    Since:
    2.3
    • Constructor Detail

      • DefaultSemanticValidator

        public DefaultSemanticValidator​(JPQLQueryContext queryContext)
        Creates a new DefaultSemanticValidator.
        Parameters:
        queryContext - The context used to query information about the JPQL query
        Throws:
        java.lang.NullPointerException - The given JPQLQueryContext cannot be null
      • DefaultSemanticValidator

        public DefaultSemanticValidator​(SemanticValidatorHelper helper)
        Creates a new DefaultSemanticValidator.
        Parameters:
        helper - The given helper allows the validator to access the JPA artifacts without using Hermes SPI directly
        Throws:
        java.lang.NullPointerException - The given SemanticValidatorHelper cannot be null
        Since:
        2.4
    • Method Detail

      • areTypesEquivalent

        protected boolean areTypesEquivalent​(java.lang.Object[] typeDeclarations1,
                                             java.lang.Object[] typeDeclarations2)
      • getType

        protected java.lang.Object getType​(Expression expression)
      • getTypeHelper

        protected ITypeHelper getTypeHelper()
      • isBooleanType

        protected boolean isBooleanType​(Expression expression)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        Returns:
        true if the given Expression passes the checks; false otherwise
      • isComparisonEquivalentType

        protected boolean isComparisonEquivalentType​(Expression expression1,
                                                     Expression expression2)
      • isEquivalentBetweenType

        protected boolean isEquivalentBetweenType​(Expression expression1,
                                                  Expression expression2)
      • isIntegralType

        protected boolean isIntegralType​(Expression expression)
        Determines whether the given Expression is of the correct type based on these rules:
        • The Expression's type is an integral type (long or integer).
        Parameters:
        expression - The Expression to validate
        Returns:
        true if the given Expression passes the checks; false otherwise
      • isNullValue

        protected boolean isNullValue​(Expression expression)
      • isNumericType

        protected boolean isNumericType​(Expression expression)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        Returns:
        true if the given Expression passes the checks; false otherwise
      • isStringType

        protected boolean isStringType​(Expression expression)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        Returns:
        true if the given Expression passes the checks; false otherwise
      • isValidWithFindQueryBNF

        protected boolean isValidWithFindQueryBNF​(AbstractExpression expression,
                                                  java.lang.String queryBNF)
      • validateAbsExpression

        protected boolean validateAbsExpression​(AbsExpression expression)
        Validates the encapsulated expression of the given ABS expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateAbsExpression in class AbstractSemanticValidator
        Parameters:
        expression - The AbsExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateArithmeticExpression

        protected int validateArithmeticExpression​(ArithmeticExpression expression,
                                                   java.lang.String leftExpressionWrongTypeMessageKey,
                                                   java.lang.String rightExpressionWrongTypeMessageKey)
        Validates the type of the left and right expressions defined by the given ArithmeticExpression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateArithmeticExpression in class AbstractSemanticValidator
        Parameters:
        expression - The ArithmeticExpression to validate
        leftExpressionWrongTypeMessageKey - The key used to describe the left expression does not have a valid type
        rightExpressionWrongTypeMessageKey - The key used to describe the right expression does not have a valid type
        Returns:
        A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
      • validateAvgFunction

        protected boolean validateAvgFunction​(AvgFunction expression)
        Validates the encapsulated expression of the given AVG expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateAvgFunction in class AbstractSemanticValidator
        Parameters:
        expression - The AvgFunction to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateBooleanType

        protected boolean validateBooleanType​(Expression expression,
                                              java.lang.String messageKey)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        messageKey - The key used to retrieve the localized message describing the problem
      • validateComparisonExpression

        protected boolean validateComparisonExpression​(ComparisonExpression expression)
        Validates the left and right expressions of the given ComparisonExpression. The tests to perform are:
        • If the comparison operator is either '=' or '<>'. The expressions can only be
          • Two identification variables;
          • Two path expressions resolving to an association field;
          • One can be a path expression resolving to a basic field and the other one has to resolve to a basic value.
        • If the comparison operator is either '<', '<=', '>=', '>'. The expressions cannot be
          • Two identification variables;
          • Two path expressions resolving to an association field;
        Overrides:
        validateComparisonExpression in class AbstractSemanticValidator
        Parameters:
        expression - The ConcatExpression to validate by validating its left and right expressions
        Returns:
        The status of the comparison between the left and right expression: true if the two expressions pass the rules defined by this method; false otherwise
      • validateConcatExpression

        protected boolean validateConcatExpression​(ConcatExpression expression)
        Validates the encapsulated expression of the given CONCAT expression. The tests to perform are:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateConcatExpression in class AbstractSemanticValidator
        Parameters:
        expression - The ConcatExpression to validate by validating its encapsulated expression
        Returns:
        false if the first encapsulated expression was validated and is invalid; true otherwise
      • validateIdentificationVariable

        protected boolean validateIdentificationVariable​(IdentificationVariable expression,
                                                         java.lang.String variable)
        Validates the given identification variable. The default behavior is to not validate it.
        Overrides:
        validateIdentificationVariable in class AbstractSemanticValidator
        Parameters:
        expression - The IdentificationVariable that is being visited
        variable - The actual identification variable, which is never an empty string
        Returns:
        true if the given identification variable is valid; false otherwise
      • validateIntegralType

        protected boolean validateIntegralType​(Expression expression,
                                               java.lang.String queryBNF,
                                               java.lang.String messageKey)
        Determines whether the given Expression is of the correct type based on these rules:
        • The Expression returns a integral value;
        • The Expression's type is an integral type (long or integer).
        Parameters:
        expression - The Expression to validate
        queryBNF - The unique identifier of the query BNF used to validate the type
        messageKey - The key used to retrieve the localized message describing the problem
        Returns:
        false if the given expression was validated and is invalid; true otherwise
      • validateLengthExpression

        protected boolean validateLengthExpression​(LengthExpression expression)
        Validates the encapsulated expression of the given LENGTH expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateLengthExpression in class AbstractSemanticValidator
        Parameters:
        expression - The LengthExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateLocateExpression

        protected int validateLocateExpression​(LocateExpression expression)
        Validates the encapsulated expression of the given LOCATE expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateLocateExpression in class AbstractSemanticValidator
        Parameters:
        expression - The LocateExpression to validate by validating its encapsulated expression
        Returns:
        A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
      • validateLowerExpression

        protected boolean validateLowerExpression​(LowerExpression expression)
        Validates the encapsulated expression of the given LOWER expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateLowerExpression in class AbstractSemanticValidator
        Parameters:
        expression - The LowerExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateModExpression

        protected int validateModExpression​(ModExpression expression)
        Validates the encapsulated expression of the given MOD expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateModExpression in class AbstractSemanticValidator
        Parameters:
        expression - The ModExpression to validate by validating its encapsulated expression
        Returns:
        A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
      • validateNumericType

        protected boolean validateNumericType​(Expression expression,
                                              java.lang.String messageKey)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        messageKey - The key used to retrieve the localized message describing the problem
        Returns:
        false if the given expression was validated and is invalid; true otherwise
      • validateSqrtExpression

        protected boolean validateSqrtExpression​(SqrtExpression expression)
        Validates the encapsulated expression of the given SQRT expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateSqrtExpression in class AbstractSemanticValidator
        Parameters:
        expression - The SqrtExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateStringType

        protected boolean validateStringType​(Expression expression,
                                             java.lang.String messageKey)
        Determines whether the given Expression is of the correct type based on these rules:
        Parameters:
        expression - The Expression to validate
        messageKey - The key used to retrieve the localized message describing the problem
        Returns:
        false if the given expression was validated and is invalid; true otherwise
      • validateSubstringExpression

        protected int validateSubstringExpression​(SubstringExpression expression)
        Validates the encapsulated expression of the given SUBSTRING expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateSubstringExpression in class AbstractSemanticValidator
        Parameters:
        expression - The SubstringExpression to validate by validating its encapsulated expression
        Returns:
        A number indicating the validation result. AbstractSemanticValidator.isValid(int, int) can be used to determine the validation status of an expression based on its position
      • validateSumFunction

        protected boolean validateSumFunction​(SumFunction expression)
        Validates the encapsulated expression of the given MOD expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateSumFunction in class AbstractSemanticValidator
        Parameters:
        expression - The ModExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise
      • validateUpdateItem

        protected boolean validateUpdateItem​(UpdateItem expression)
        Validates the given UpdateItem by validating the traversability of the path expression. The path expression is valid if it follows one of the following rules:
        • The identification variable is omitted if it's not defined in the FROM clause;
        • The last path is a state field;
        • Only embedded field can be traversed.
        Overrides:
        validateUpdateItem in class AbstractSemanticValidator
        Parameters:
        expression - UpdateItem to validate its path expression
        Returns:
        true if the path expression is valid; false otherwise
      • validateUpdateItemTypes

        protected void validateUpdateItemTypes​(UpdateItem expression,
                                               java.lang.Object type)
      • validateUpperExpression

        protected boolean validateUpperExpression​(UpperExpression expression)
        Validates the encapsulated expression of the given UPPER expression. The test to perform is:
        • If the encapsulated expression is a path expression, validation makes sure it is a basic mapping, an association field is not allowed.
        • If the encapsulated expression is not a path expression, validation will be redirected to that expression but the returned status will not be changed.
        Overrides:
        validateUpperExpression in class AbstractSemanticValidator
        Parameters:
        expression - The UpperExpression to validate by validating its encapsulated expression
        Returns:
        false if the encapsulated expression was validated and is invalid; true otherwise