Search in sources :

Example 1 with ExpressionJavaPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyPowerOperator.

// Emulate POWER(:a,:b) as EXP((:b)*LN(:a))
private static ExpressionOperator derbyPowerOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[1]);
            first.printSQL(printer);
            printer.printString(getDatabaseStrings()[2]);
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
                printer.getCall().setUsesBinding(false);
            }
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[1]);
            first.printJava(printer);
            printer.printString(getDatabaseStrings()[2]);
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printJavaDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Power);
    exOperator.setName("POWER");
    List<String> v = new ArrayList<>(4);
    v.add("EXP((");
    v.add(")*LN(");
    v.add("))");
    exOperator.printsAs(v);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 2 with ExpressionJavaPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter in project eclipselink by eclipse-ee4j.

the class ProjectClassGenerator method buildExpressionString.

// Build expression string for orderBy, batchRead, joined, selectionCriteria and groupBy. e.g. reportQuery1.addGrouping(exp)
protected void buildExpressionString(String builderString, NonreflectiveMethodDefinition method, String queryIdentifier, Expression exp, String attrString) {
    StringWriter writer = new StringWriter();
    ExpressionJavaPrinter javaPrinter = new ExpressionJavaPrinter(builderString, writer, project.getDatasourceLogin().getPlatform());
    exp.printJava(javaPrinter);
    method.addLine(queryIdentifier + attrString + writer + ");");
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) StringWriter(java.io.StringWriter)

Example 3 with ExpressionJavaPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyRoundOperator.

// Emulate ROUND as FLOOR((:x)*1e:n+0.5)/1e:n
private static ExpressionOperator derbyRoundOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printSQL(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printSQL(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (printer.getPlatform().isDynamicSQLRequiredForFunctions() && !isBindingSupported()) {
                printer.getCall().setUsesBinding(false);
            }
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            printer.printString(getDatabaseStrings()[0]);
            first.printJava(printer);
            printer.printString(getDatabaseStrings()[1]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[2]);
            if (second != null) {
                second.printJava(printer);
            } else {
                printer.printString("0");
            }
            printer.printString(getDatabaseStrings()[3]);
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() > 0) {
                Expression firstItem = (Expression) items.get(0);
                Expression secondItem = items.size() > 1 ? (Expression) items.get(1) : null;
                printJavaDuo(firstItem, secondItem, printer);
            } else {
                throw new IllegalArgumentException("List of items shall contain at least one item");
            }
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Round);
    exOperator.setName("ROUND");
    List<String> v = new ArrayList<>(4);
    v.add("FLOOR((");
    v.add(")*1e");
    v.add("+0.5)/1e");
    v.add("");
    exOperator.printsAs(v);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 4 with ExpressionJavaPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter in project eclipselink by eclipse-ee4j.

the class DerbyPlatform method derbyExtractOperator.

/**
 * INTERNAL:
 * Derby does not support EXTRACT, but does have YEAR, MONTH, DAY, etc.
 */
public static ExpressionOperator derbyExtractOperator() {
    ExpressionOperator exOperator = new ExpressionOperator() {

        // QUARTER emulation: ((MONTH(:first)+2)/3)
        private final String[] QUARTER_STRINGS = new String[] { "((MONTH(", ")+2)/3)" };

        private void printQuarterSQL(final Expression first, final ExpressionSQLPrinter printer) {
            printer.printString(QUARTER_STRINGS[0]);
            first.printSQL(printer);
            printer.printString(QUARTER_STRINGS[1]);
        }

        private void printQuarterJava(final Expression first, final ExpressionJavaPrinter printer) {
            printer.printString(QUARTER_STRINGS[0]);
            first.printJava(printer);
            printer.printString(QUARTER_STRINGS[1]);
        }

        @Override
        public void printDuo(Expression first, Expression second, ExpressionSQLPrinter printer) {
            if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                printQuarterSQL(first, printer);
            } else {
                super.printDuo(first, second, printer);
            }
        }

        @Override
        public void printCollection(List items, ExpressionSQLPrinter printer) {
            if (items.size() == 2) {
                Expression first = (Expression) items.get(0);
                Expression second = (Expression) items.get(1);
                if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                    printQuarterSQL(first, printer);
                    return;
                }
            }
            super.printCollection(items, printer);
        }

        @Override
        public void printJavaDuo(Expression first, Expression second, ExpressionJavaPrinter printer) {
            if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                printQuarterJava(first, printer);
            } else {
                super.printJavaDuo(first, second, printer);
            }
        }

        @Override
        public void printJavaCollection(List items, ExpressionJavaPrinter printer) {
            if (items.size() == 2) {
                Expression first = (Expression) items.get(0);
                Expression second = (Expression) items.get(1);
                if (second instanceof LiteralExpression && "QUARTER".equals(((LiteralExpression) second).getValue().toUpperCase())) {
                    printQuarterJava(first, printer);
                    return;
                }
            }
            super.printJavaCollection(items, printer);
        }
    };
    exOperator.setType(ExpressionOperator.FunctionOperator);
    exOperator.setSelector(ExpressionOperator.Extract);
    exOperator.setName("EXTRACT");
    List<String> v = new ArrayList<>(3);
    v.add("");
    v.add("(");
    v.add(")");
    exOperator.printsAs(v);
    int[] indices = new int[2];
    indices[0] = 1;
    indices[1] = 0;
    exOperator.setArgumentIndices(indices);
    exOperator.bePrefix();
    exOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
    return exOperator;
}
Also used : ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) Expression(org.eclipse.persistence.expressions.Expression) ExpressionSQLPrinter(org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter) LiteralExpression(org.eclipse.persistence.internal.expressions.LiteralExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ExpressionOperator(org.eclipse.persistence.expressions.ExpressionOperator)

Example 5 with ExpressionJavaPrinter

use of org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter in project eclipselink by eclipse-ee4j.

the class ProjectClassGenerator method addNamedQueryLines.

protected void addNamedQueryLines(NonreflectiveMethodDefinition method, DatabaseQuery query, DescriptorQueryManager queryManager, int iteration) {
    String queryIdentifier = "namedQuery" + iteration;
    method.addLine("// Named Query -- " + query.getName());
    String className;
    if (query.getDescriptor() != null) {
        className = query.getDescriptor().getJavaClassName();
    } else {
        // in default mapping, the query has not been associated with a descriptor before generation.
        className = query.getReferenceClassName();
    }
    if (query.isReportQuery()) {
        method.addLine("ReportQuery " + queryIdentifier + " = new ReportQuery(" + className + ".class, new ExpressionBuilder());");
    } else if (query.isReadAllQuery()) {
        method.addLine("ReadAllQuery " + queryIdentifier + " = new ReadAllQuery(" + className + ".class);");
    } else if (query.isReadObjectQuery()) {
        method.addLine("ReadObjectQuery " + queryIdentifier + " = new ReadObjectQuery(" + className + ".class);");
    } else if (query.isValueReadQuery()) {
        method.addLine("ValueReadQuery " + queryIdentifier + " = new ValueReadQuery();");
    } else if (query.isDataReadQuery()) {
        method.addLine("DataReadQuery " + queryIdentifier + " = new DataReadQuery();");
    } else if (query.isDirectReadQuery()) {
        method.addLine("DirectReadQuery " + queryIdentifier + " = new DirectReadQuery();");
    }
    if (query.getSQLString() != null) {
        method.addLine(queryIdentifier + ".setSQLString(\"" + constructValidSQLorEJBQLLinesForJavaSource(query.getSQLString()) + "\");");
    } else if (query.getEJBQLString() != null) {
        method.addLine(queryIdentifier + ".setEJBQLString(\"" + constructValidSQLorEJBQLLinesForJavaSource(query.getEJBQLString()) + "\");");
    } else if (query.getDatasourceCall() instanceof XMLInteraction) {
        addXMLInteractionLines(method, (XMLInteraction) query.getDatasourceCall(), queryIdentifier + "Call");
        method.addLine(queryIdentifier + ".setCall(" + queryIdentifier + "Call" + ");");
    }
    if ((query.getRedirector() != null) && (query.getRedirector().getClass().equals(MethodBaseQueryRedirector.class))) {
        method.addLine(queryIdentifier + ".setRedirector(new MethodBaseQueryRedirector(" + ((MethodBaseQueryRedirector) query.getRedirector()).getMethodClassName() + ".class, \"" + ((MethodBaseQueryRedirector) query.getRedirector()).getMethodName() + "\"));");
    }
    method.addLine(queryIdentifier + ".setName(\"" + query.getName() + "\");");
    if ((!query.isReadQuery()) || (query.getCascadePolicy() != DatabaseQuery.NoCascading)) {
        String cascadePolicy = "DatabaseQuery.NoCascading";
        if (query.getCascadePolicy() == DatabaseQuery.CascadePrivateParts) {
            cascadePolicy = "DatabaseQuery.CascadePrivateParts";
        } else if (query.getCascadePolicy() == DatabaseQuery.CascadeDependentParts) {
            cascadePolicy = "DatabaseQuery.CascadeDependentParts";
        } else if (query.getCascadePolicy() == DatabaseQuery.CascadeAllParts) {
            cascadePolicy = "DatabaseQuery.CascadeAllParts";
        }
        method.addLine(queryIdentifier + ".setCascadePolicy(" + cascadePolicy + ");");
    }
    if (query.getQueryTimeout() != DescriptorQueryManager.DefaultTimeout) {
        method.addLine(queryIdentifier + ".setQueryTimeout(" + query.getQueryTimeout() + ");");
    }
    if (query.getQueryTimeoutUnit() != DescriptorQueryManager.DefaultTimeoutUnit) {
        method.addLine(queryIdentifier + ".setQueryTimeoutUnit(" + query.getQueryTimeoutUnit() + ");");
    }
    if (!query.shouldUseWrapperPolicy()) {
        method.addLine(queryIdentifier + ".setShouldUseWrapperPolicy(" + query.shouldUseWrapperPolicy() + ");");
    }
    if (!query.shouldIgnoreBindAllParameters()) {
        method.addLine(queryIdentifier + ".setShouldBindAllParameters(" + query.shouldBindAllParameters() + ");");
    }
    if (!query.shouldIgnoreCacheStatement()) {
        method.addLine(queryIdentifier + ".setShouldCacheStatement(" + query.shouldCacheStatement() + ");");
    }
    if (query.getSessionName() != null) {
        method.addLine(queryIdentifier + ".setSessionName(\"" + query.getSessionName() + "\");");
    }
    if (!query.shouldMaintainCache()) {
        method.addLine(queryIdentifier + ".setShouldMaintainCache(" + query.shouldMaintainCache() + ");");
    }
    if (!query.shouldPrepare()) {
        method.addLine(queryIdentifier + ".setShouldPrepare(" + query.shouldPrepare() + ");");
    }
    if (query.isReadQuery()) {
        ReadQuery readQuery = (ReadQuery) query;
        if (readQuery.shouldCacheQueryResults()) {
            method.addLine(queryIdentifier + ".setQueryResultsCachePolicy(new QueryResultsCachePolicy());");
        }
        if (readQuery.getMaxRows() != 0) {
            method.addLine(queryIdentifier + ".setMaxRows(" + readQuery.getMaxRows() + ");");
        }
        if (readQuery.getFirstResult() != 0) {
            method.addLine(queryIdentifier + ".setFirstResult(" + readQuery.getFirstResult() + ");");
        }
    }
    // ExpressionBuilder string
    String builderString = null;
    if (query.isObjectLevelReadQuery()) {
        ObjectLevelReadQuery readQuery = (ObjectLevelReadQuery) query;
        // Refresh.
        if (readQuery.shouldRefreshIdentityMapResult()) {
            method.addLine(queryIdentifier + ".setShouldRefreshIdentityMapResult(" + readQuery.shouldRefreshIdentityMapResult() + ");");
        }
        // Cache usage.
        if (readQuery.getCacheUsage() != ObjectLevelReadQuery.UseDescriptorSetting) {
            String cacheUsage = "ObjectLevelReadQuery.UseDescriptorSetting";
            if (readQuery.getCacheUsage() == ObjectLevelReadQuery.DoNotCheckCache) {
                cacheUsage = "ObjectLevelReadQuery.DoNotCheckCache";
            } else if (readQuery.getCacheUsage() == ObjectLevelReadQuery.CheckCacheByExactPrimaryKey) {
                cacheUsage = "ObjectLevelReadQuery.CheckCacheByExactPrimaryKey";
            } else if (readQuery.getCacheUsage() == ObjectLevelReadQuery.CheckCacheByPrimaryKey) {
                cacheUsage = "ObjectLevelReadQuery.CheckCacheByPrimaryKey";
            } else if (readQuery.getCacheUsage() == ObjectLevelReadQuery.CheckCacheThenDatabase) {
                cacheUsage = "ObjectLevelReadQuery.CheckCacheThenDatabase";
            } else if (readQuery.getCacheUsage() == ObjectLevelReadQuery.CheckCacheOnly) {
                cacheUsage = "ObjectLevelReadQuery.CheckCacheOnly";
            } else if (readQuery.getCacheUsage() == ObjectLevelReadQuery.ConformResultsInUnitOfWork) {
                cacheUsage = "ObjectLevelReadQuery.ConformResultsInUnitOfWork";
            }
            method.addLine(queryIdentifier + ".setCacheUsage(" + cacheUsage + ");");
        }
        // Lock mode.
        if (readQuery.getLockMode() != ObjectLevelReadQuery.DEFAULT_LOCK_MODE) {
            String lockMode = null;
            if (readQuery.getLockMode() == ObjectLevelReadQuery.NO_LOCK && !readQuery.isReportQuery()) {
                lockMode = "ObjectLevelReadQuery.NO_LOCK";
            } else if (readQuery.getLockMode() == ObjectLevelReadQuery.LOCK) {
                lockMode = "ObjectLevelReadQuery.LOCK";
            } else if (readQuery.getLockMode() == ObjectLevelReadQuery.LOCK_NOWAIT) {
                lockMode = "ObjectLevelReadQuery.LOCK_NOWAIT";
            }
            if (lockMode != null) {
                method.addLine(queryIdentifier + ".setLockMode(" + lockMode + ");");
            }
        }
        // Remote refresh.
        if (readQuery.shouldRefreshRemoteIdentityMapResult()) {
            method.addLine(queryIdentifier + ".setShouldRefreshRemoteIdentityMapResult(" + readQuery.shouldRefreshRemoteIdentityMapResult() + ");");
        }
        // Distinct state.
        if (readQuery.getDistinctState() != ObjectLevelReadQuery.UNCOMPUTED_DISTINCT) {
            String distinctState = "ObjectLevelReadQuery.UNCOMPUTED_DISTINCT";
            if (readQuery.getDistinctState() == ObjectLevelReadQuery.USE_DISTINCT) {
                distinctState = "ObjectLevelReadQuery.USE_DISTINCT";
            } else if (readQuery.getDistinctState() == ObjectLevelReadQuery.DONT_USE_DISTINCT) {
                distinctState = "ObjectLevelReadQuery.DONT_USE_DISTINCT";
            }
            method.addLine(queryIdentifier + ".setDistinctState((short)" + distinctState + ");");
        }
        // In-memory policy.
        if (readQuery.getInMemoryQueryIndirectionPolicy().getPolicy() != InMemoryQueryIndirectionPolicy.SHOULD_THROW_INDIRECTION_EXCEPTION) {
            String inMemoryQueryIndirectionPolicy = "InMemoryQueryIndirectionPolicy.SHOULD_THROW_INDIRECTION_EXCEPTION";
            if (readQuery.getInMemoryQueryIndirectionPolicy().getPolicy() == InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION) {
                inMemoryQueryIndirectionPolicy = "InMemoryQueryIndirectionPolicy.SHOULD_TRIGGER_INDIRECTION";
            } else if (readQuery.getInMemoryQueryIndirectionPolicy().getPolicy() == InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_CONFORMED) {
                inMemoryQueryIndirectionPolicy = "InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_CONFORMED";
            } else if (readQuery.getInMemoryQueryIndirectionPolicy().getPolicy() == InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_NOT_CONFORMED) {
                inMemoryQueryIndirectionPolicy = "InMemoryQueryIndirectionPolicy.SHOULD_IGNORE_EXCEPTION_RETURN_NOT_CONFORMED";
            }
            method.addLine(queryIdentifier + ".setInMemoryQueryIndirectionPolicy(new InMemoryQueryIndirectionPolicy(" + inMemoryQueryIndirectionPolicy + "));");
        }
        // Fetch groups.
        if (!readQuery.shouldUseDefaultFetchGroup()) {
            method.addLine(queryIdentifier + ".setShouldUseDefaultFetchGroup(false);");
        }
        if (readQuery.getFetchGroupName() != null) {
            method.addLine(queryIdentifier + ".setFetchGroupName(\"" + readQuery.getFetchGroupName() + "\");");
        } else if (readQuery.getFetchGroup() != null) {
            String fetchGroupIdentifier = readQuery.getFetchGroup().getName() + "FetchGroup";
            addFetchGroupLines(method, readQuery.getFetchGroup(), fetchGroupIdentifier);
            method.addLine(queryIdentifier + ".setFetchGroup(" + fetchGroupIdentifier + ");");
        }
        // Exclusive Connection (VPD).
        if (readQuery.shouldUseExclusiveConnection()) {
            method.addLine(queryIdentifier + ".setShouldUseExclusiveConnection(true);");
        }
        // Read-only
        if (readQuery.isReadOnly()) {
            method.addLine(queryIdentifier + ".setIsReadOnly(true);");
        }
        // Join-subclasses
        if (readQuery.shouldOuterJoinSubclasses()) {
            method.addLine(queryIdentifier + ".setShouldOuterJoinSubclasses(true);");
        }
        // Selection criteria.
        if (readQuery.getSelectionCriteria() != null) {
            builderString = buildBuilderString(builderString, method, iteration, queryIdentifier);
            buildExpressionString(builderString, method, queryIdentifier, readQuery.getSelectionCriteria(), ".setSelectionCriteria(");
        }
        // joinedAttribute
        for (Iterator<Expression> joinedEnum = readQuery.getJoinedAttributeManager().getJoinedAttributeExpressions().iterator(); joinedEnum.hasNext(); ) {
            Expression joinedExp = joinedEnum.next();
            builderString = buildBuilderString(builderString, method, iteration, queryIdentifier);
            buildExpressionString(builderString, method, queryIdentifier, joinedExp, ".addJoinedAttribute(");
        }
    }
    // ReadAllQuery
    if (query.isReadAllQuery()) {
        ReadAllQuery readAllQuery = (ReadAllQuery) query;
        // orderBy
        for (Expression orderbyExpression : readAllQuery.getOrderByExpressions()) {
            builderString = buildBuilderString(builderString, method, iteration, queryIdentifier);
            buildExpressionString(builderString, method, queryIdentifier, orderbyExpression, ".addOrdering(");
        }
        // batchReadAttribute
        for (Expression batchReadExp : readAllQuery.getBatchReadAttributeExpressions()) {
            builderString = buildBuilderString(builderString, method, iteration, queryIdentifier);
            buildExpressionString(builderString, method, queryIdentifier, batchReadExp, ".addBatchReadAttribute(");
        }
        // resultCollection
        if (readAllQuery.getContainerPolicy().isCursoredStreamPolicy()) {
            method.addLine(queryIdentifier + ".useCursoredStream();");
        }
        if (readAllQuery.getContainerPolicy().isScrollableCursorPolicy()) {
            method.addLine(queryIdentifier + ".useScrollableCursor();");
        }
        if (readAllQuery.getContainerPolicy().isCollectionPolicy()) {
            String collectionClass = readAllQuery.getContainerPolicy().getContainerClassName();
            if (!collectionClass.equals("java.util.Vector")) {
                method.addLine(queryIdentifier + ".useCollectionClass(" + collectionClass + ".class);");
            }
        }
    }
    // ReportQuery
    if (query.isReportQuery()) {
        ReportQuery reportQuery = (ReportQuery) query;
        // ExpressionBuilder string
        builderString = buildBuilderString(builderString, method, iteration, queryIdentifier);
        // ReportItems
        for (ReportItem item : reportQuery.getItems()) {
            Expression expression = item.getAttributeExpression();
            String itemName = item.getName();
            StringWriter writer = new StringWriter();
            ExpressionJavaPrinter javaPrinter = new ExpressionJavaPrinter(builderString, writer, project.getDatasourceLogin().getPlatform());
            if (expression != null) {
                String functionString;
                // used in ReportQuery API, e.g. addCount(itemName, baseExpression)
                Expression baseExpression;
                // used in addFunctionItem()
                String databaseString = null;
                if (expression.isQueryKeyExpression()) {
                    functionString = ".addAttribute(\"";
                    baseExpression = expression;
                } else if (expression.isFunctionExpression()) {
                    int selector = expression.getOperator().getSelector();
                    baseExpression = ((FunctionExpression) expression).getBaseExpression();
                    if (selector == ExpressionOperator.Average) {
                        functionString = ".addAverage(\"";
                    } else if (selector == ExpressionOperator.Count) {
                        functionString = ".addCount(\"";
                    } else if (selector == ExpressionOperator.Maximum) {
                        functionString = ".addMaximum(\"";
                    } else if (selector == ExpressionOperator.Minimum) {
                        functionString = ".addMinimum(\"";
                    } else if (selector == ExpressionOperator.StandardDeviation) {
                        functionString = ".addStandardDeviation(\"";
                    } else if (selector == ExpressionOperator.Sum) {
                        functionString = ".addSum(\"";
                    } else if (selector == ExpressionOperator.Variance) {
                        functionString = ".addVariance(\"";
                    } else {
                        // custom function
                        functionString = ".addFunctionItem(\"";
                        databaseString = expression.getOperator().getDatabaseStrings()[0];
                        databaseString = databaseString.substring(0, databaseString.length() - 1);
                    }
                } else {
                    functionString = ".addItem(\"";
                    baseExpression = expression;
                }
                baseExpression.printJava(javaPrinter);
                if (databaseString == null) {
                    // e.g. addCount(itemName, attributeExpression)
                    method.addLine(queryIdentifier + functionString + itemName + "\", " + writer + ");");
                } else {
                    // i.e. addFunctionItem(itemName, attributeExpression, functionName) only
                    method.addLine(queryIdentifier + functionString + itemName + "\", " + writer + ", \"" + databaseString + "\");");
                }
            }
        }
        // groupBy
        for (Expression groupByExp : reportQuery.getGroupByExpressions()) {
            buildExpressionString(builderString, method, queryIdentifier, groupByExp, ".addGrouping(");
        }
        // shouldRetrievePrimaryKeys
        if (reportQuery.shouldRetrieveFirstPrimaryKey()) {
            method.addLine(queryIdentifier + ".setShouldRetrieveFirstPrimaryKey(true);");
        } else if (reportQuery.shouldRetrievePrimaryKeys()) {
            method.addLine(queryIdentifier + ".setShouldRetrievePrimaryKeys(true);");
        }
        // returnChoice
        if (reportQuery.shouldReturnSingleAttribute()) {
            method.addLine(queryIdentifier + ".setShouldReturnSingleAttribute(true);");
        } else if (reportQuery.shouldReturnSingleValue()) {
            method.addLine(queryIdentifier + ".setShouldReturnSingleValue(true);");
        } else {
            method.addLine(queryIdentifier + ".setShouldReturnSingleResult(true);");
        }
    }
    // Query arguments.
    Iterator<String> argumentTypes = query.getArgumentTypeNames().iterator();
    for (Iterator<String> arguments = query.getArguments().iterator(); arguments.hasNext(); ) {
        String argument = arguments.next();
        String argumentTypeName = argumentTypes.next();
        method.addLine(queryIdentifier + ".addArgument(\"" + argument + "\", " + argumentTypeName + ".class);");
    }
    method.addLine("descriptor.getQueryManager().addQuery(\"" + query.getName() + "\", " + queryIdentifier + ");");
    method.addLine("");
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) XMLInteraction(org.eclipse.persistence.eis.interactions.XMLInteraction) ReportItem(org.eclipse.persistence.internal.queries.ReportItem) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ExpressionJavaPrinter(org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) StringWriter(java.io.StringWriter) FunctionExpression(org.eclipse.persistence.internal.expressions.FunctionExpression) Expression(org.eclipse.persistence.expressions.Expression) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery)

Aggregations

ExpressionJavaPrinter (org.eclipse.persistence.internal.expressions.ExpressionJavaPrinter)6 Expression (org.eclipse.persistence.expressions.Expression)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ExpressionOperator (org.eclipse.persistence.expressions.ExpressionOperator)4 ExpressionSQLPrinter (org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter)4 LiteralExpression (org.eclipse.persistence.internal.expressions.LiteralExpression)3 StringWriter (java.io.StringWriter)2 XMLInteraction (org.eclipse.persistence.eis.interactions.XMLInteraction)1 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)1 RelationExpression (org.eclipse.persistence.internal.expressions.RelationExpression)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)1 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)1 ReadQuery (org.eclipse.persistence.queries.ReadQuery)1 ReportQuery (org.eclipse.persistence.queries.ReportQuery)1