Search in sources :

Example 76 with IType

use of org.eclipse.persistence.jpa.jpql.tools.spi.IType in project eclipselink by eclipse-ee4j.

the class EclipseLinkJPQLQueryHelperTest2_4 method test_ResultType_FromSubquery_1.

@Test
public void test_ResultType_FromSubquery_1() throws Exception {
    // Select e3.salary from Employee e, (Select count(e2), e2.department from Employee e2 group by e2.department) e3 where e.department = e3.department
    IQuery namedQuery = namedQuery("Employee", "employee.fromSubquery2");
    AbstractJPQLQueryHelper helper = buildQueryHelper(namedQuery);
    IType type = helper.getResultType();
    assertNotNull("The result type should have been found", type);
    assertEquals("The result type was not calculated correctly", getType(namedQuery, Object.class), type);
}
Also used : IQuery(org.eclipse.persistence.jpa.jpql.tools.spi.IQuery) AbstractJPQLQueryHelper(org.eclipse.persistence.jpa.jpql.tools.AbstractJPQLQueryHelper) IType(org.eclipse.persistence.jpa.jpql.tools.spi.IType) Test(org.junit.Test)

Example 77 with IType

use of org.eclipse.persistence.jpa.jpql.tools.spi.IType in project eclipselink by eclipse-ee4j.

the class AbstractJPQLQueryHelper method getParameterType.

/**
 * Retrieves, if it can be determined, the type of the given input parameter with the given name.
 * The type will be guessed based on its location within expression.
 * <p>
 * Note: Both named and positional input parameter can be used.
 *
 * @param parameterName The name of the input parameter to retrieve its type, which needs to be
 * prepended by ':' or '?'
 * @return Either the closest type of the input parameter or <code>null</code> if the type
 * couldn't be determined
 */
public IType getParameterType(String parameterName) {
    // Retrieve the input parameter's qualifier (':' or '?')
    char character = parameterName.length() > 0 ? parameterName.charAt(0) : '\0';
    // Does not begin with either ':' or '?'
    if ((character != ':') && (character != '?')) {
        return getTypeHelper().objectType();
    }
    // Find the InputParameters with the given parameter name
    Collection<InputParameter> inputParameters = getQueryContext().findInputParameters(parameterName);
    // No InputParameter was found
    if (inputParameters.isEmpty()) {
        return getTypeHelper().objectType();
    }
    // Now find the closest type for each location
    TreeSet<IType> types = new TreeSet<>(buildNumericTypeComparator());
    for (InputParameter inputParameter : inputParameters) {
        IType type = queryContext.getParameterType(inputParameter);
        // The first :name cannot be used to calculate the type
        if (type.isResolvable()) {
            types.add(type);
        }
    }
    return types.isEmpty() ? getTypeHelper().objectType() : types.first();
}
Also used : TreeSet(java.util.TreeSet) InputParameter(org.eclipse.persistence.jpa.jpql.parser.InputParameter) IType(org.eclipse.persistence.jpa.jpql.tools.spi.IType)

Example 78 with IType

use of org.eclipse.persistence.jpa.jpql.tools.spi.IType in project eclipselink by eclipse-ee4j.

the class TypeHelper method convertPrimitive.

/**
 * Converts the given {@link IType}, if it's representing a primitive type, into the class of the
 * same type.
 *
 * @param type Type to possibly convert from the primitive into the class
 * @return The given {@link IType} if it's not a primitive type otherwise the primitive type will
 * have been converted into the class of that primitive
 */
public IType convertPrimitive(IType type) {
    // byte
    IType newType = toByteType(type);
    if (newType != type) {
        return newType;
    }
    // short
    newType = toShortType(type);
    if (newType != type) {
        return newType;
    }
    // int
    newType = toIntegerType(type);
    if (newType != type) {
        return newType;
    }
    // long
    newType = longType(type);
    if (newType != type) {
        return newType;
    }
    // float
    newType = toFloatType(type);
    if (newType != type) {
        return newType;
    }
    // double
    newType = toDoubleType(type);
    if (newType != type) {
        return newType;
    }
    // boolean
    newType = toBooleanType(type);
    if (newType != type) {
        return newType;
    }
    return type;
}
Also used : IType(org.eclipse.persistence.jpa.jpql.tools.spi.IType)

Example 79 with IType

use of org.eclipse.persistence.jpa.jpql.tools.spi.IType in project eclipselink by eclipse-ee4j.

the class AbstractContentAssistVisitor method visitEnumConstant.

protected void visitEnumConstant(AbstractPathExpression expression) {
    int position = queryPosition.getPosition(expression);
    String text = expression.toActualText();
    int lastDotIndex = text.lastIndexOf(DOT);
    // Check to see if an enum constant can be used at the expression's location
    if (isEnumAllowed(expression)) {
        boolean enumConstant = false;
        // The position is after the last dot, check for enum constants
        if (position > lastDotIndex) {
            // Retrieve the enum type if the path up to the last dot is a fully qualified enum type
            String enumType = expression.toParsedText().substring(0, lastDotIndex);
            IType type = queryContext.getType(enumType);
            // The path expression before the last dot is an enum type
            if (type.isResolvable() && type.isEnum()) {
                enumConstant = true;
                // Now retrieve the portion of the enum constant based on the cursor position
                String word = text.substring(lastDotIndex + 1, position);
                // Add the enum constants and filter them based on what's already proposed
                addEnumConstants(type, word);
            }
        }
        // Enum type
        if (!enumConstant) {
            // Now retrieve the portion of the enum constant based on the cursor position
            text = text.substring(0, position);
            // Set the possible starting of a fully qualified enum type
            proposals.setClassNamePrefix(text, ClassType.ENUM);
        }
    }
}
Also used : IType(org.eclipse.persistence.jpa.jpql.tools.spi.IType)

Example 80 with IType

use of org.eclipse.persistence.jpa.jpql.tools.spi.IType in project eclipselink by eclipse-ee4j.

the class ContentAssistTest method retrieveMappingNames.

protected final Iterable<String> retrieveMappingNames(Class<?> persistentType, MappingType mappingType, Class<?> allowedType) throws Exception {
    IManagedType managedType = getPersistenceUnit().getManagedType(persistentType.getName());
    if (managedType == null) {
        return Collections.emptyList();
    }
    List<String> names = new ArrayList<>();
    IType type = (allowedType != null) ? getPersistenceUnit().getTypeRepository().getType(allowedType) : null;
    for (IMapping mapping : managedType.mappings()) {
        if (mappingType.isValid(mapping) && ((type == null) || mapping.getType().isAssignableTo(type))) {
            names.add(mapping.getName());
        } else // Allow incomplete path
        if (mappingType == MappingType.SINGLE_VALUED_OBJECT_FIELD) {
            if (mapping.isRelationship() && !mapping.isCollection()) {
                names.add(mapping.getName());
            }
        }
    }
    return names;
}
Also used : IManagedType(org.eclipse.persistence.jpa.jpql.tools.spi.IManagedType) ArrayList(java.util.ArrayList) IType(org.eclipse.persistence.jpa.jpql.tools.spi.IType) IMapping(org.eclipse.persistence.jpa.jpql.tools.spi.IMapping)

Aggregations

IType (org.eclipse.persistence.jpa.jpql.tools.spi.IType)137 Test (org.junit.Test)113 AbstractJPQLQueryHelper (org.eclipse.persistence.jpa.jpql.tools.AbstractJPQLQueryHelper)112 IQuery (org.eclipse.persistence.jpa.jpql.tools.spi.IQuery)112 JPQLCoreTest (org.eclipse.persistence.jpa.tests.jpql.JPQLCoreTest)103 BigInteger (java.math.BigInteger)16 TypeHelper (org.eclipse.persistence.jpa.jpql.tools.TypeHelper)8 ITypeDeclaration (org.eclipse.persistence.jpa.jpql.tools.spi.ITypeDeclaration)7 Date (java.util.Date)6 ArrayList (java.util.ArrayList)3 AbsExpression (org.eclipse.persistence.jpa.jpql.parser.AbsExpression)2 AbstractDoubleEncapsulatedExpression (org.eclipse.persistence.jpa.jpql.parser.AbstractDoubleEncapsulatedExpression)2 AbstractExpression (org.eclipse.persistence.jpa.jpql.parser.AbstractExpression)2 AbstractPathExpression (org.eclipse.persistence.jpa.jpql.parser.AbstractPathExpression)2 AbstractSingleEncapsulatedExpression (org.eclipse.persistence.jpa.jpql.parser.AbstractSingleEncapsulatedExpression)2 AbstractTripleEncapsulatedExpression (org.eclipse.persistence.jpa.jpql.parser.AbstractTripleEncapsulatedExpression)2 AdditionExpression (org.eclipse.persistence.jpa.jpql.parser.AdditionExpression)2 AllOrAnyExpression (org.eclipse.persistence.jpa.jpql.parser.AllOrAnyExpression)2 AndExpression (org.eclipse.persistence.jpa.jpql.parser.AndExpression)2 ArithmeticExpression (org.eclipse.persistence.jpa.jpql.parser.ArithmeticExpression)2