/** * INTERNAL: * Method used to abstract addToConstructorItem behavour from the public addItem methods */ private void addItem(ReportItem item){ if (this.addToConstructorItem && (getItems().size() > 0) && (getItems().get(getItems().size() - 1).isConstructorItem())) { ((ConstructorReportItem)getItems().get(getItems().size() - 1)).addItem(item); } else { getItems().add(item); } //Bug2804042 Must un-prepare if prepared as the SQL may change. setIsPrepared(false); }
private void initializeReportQuery() { ReportQuery query = new ReportQuery(); query.returnWithoutReportQueryResult(); query.dontUseDistinct(); this.query = query; }
private void addAttribute(String generateName, Expression queryExpression) { if (resultVariable != null) { generateName = resultVariable; queryContext.addQueryExpression(resultVariable.toUpperCase(), queryExpression); } query.addAttribute(generateName, queryExpression); }
/** * INTERNAL: * Prepare a report query with a count defined on an object attribute. * Added to fix bug 3268040, addCount(objectAttribute) not supported. */ protected void prepareObjectAttributeCount(Map clonedExpressions) { prepareObjectAttributeCount(getItems(), clonedExpressions); }
/** * PUBLIC: * Include the number of rows returned by the query in the result. * Aggregation functions can be used with a group by, or on the entire result set. * EXAMPLE: * Java: * reportQuery.addCount(); * SQL: * SELECT COUNT (*) FROM ... * @see #addCount(java.lang.String) */ public void addCount() { addCount("COUNT", getExpressionBuilder()); }
/** * INTERNAL * Generate the EclipseLink expression for this node */ public Expression generateExpression(GenerationContext context) { SubqueryNode subqueryNode = (SubqueryNode)getLeft(); ReportQuery reportQuery = subqueryNode.getReportQuery(context); // Replace the SELECT clause of the exists subquery by SELECT 1 to // avoid problems with databases not supporting multiple columns in the // subquery SELECT clause in SQL. // The original select clause expressions might include relationship // navigations which should result in FK joins in the generated SQL, // e.g. ... EXISTS (SELECT o.customer FROM Order o ...). Add the // select clause expressions as non fetch join attributes to the // ReportQuery representing the subquery. This make sure the FK joins // get generated. List items = reportQuery.getItems(); for (Iterator i = items.iterator(); i.hasNext();) { ReportItem item = (ReportItem)i.next(); Expression expr = item.getAttributeExpression(); reportQuery.addNonFetchJoinedAttribute(expr); } reportQuery.clearItems(); Expression one = new ConstantExpression(new Integer(1), new ExpressionBuilder()); reportQuery.addItem("one", one); reportQuery.dontUseDistinct(); Expression expr = context.getBaseExpression(); return notIndicated() ? expr.notExists(reportQuery) : expr.exists(reportQuery); }
if (criteriaBase != null && (subQuery.getItems() == null || subQuery.getItems().isEmpty())){ if (baseExpression.getSession() != null && ((ObjectExpression)baseExpression).getDescriptor() != null){ Class sourceClass = ((ObjectExpression)baseExpression).getDescriptor().getJavaClass(); DatabaseMapping mapping = descriptor.getMappingForAttributeName(attribute); if (mapping != null && mapping.isDirectCollectionMapping()){ subQuery.setExpressionBuilder(baseExpression.getBuilder()); subQuery.setReferenceClass(sourceClass); subQuery.addCount(attribute, subQuery.getExpressionBuilder().anyOf(attribute), returnType); return; subQuery.addCount("COUNT", subQuery.getExpressionBuilder(), returnType); if (attribute != null){ subQuery.setSelectionCriteria(subQuery.getExpressionBuilder().equal(criteriaBase.anyOf(attribute))); } else { subQuery.setSelectionCriteria(subQuery.getExpressionBuilder().equal(criteriaBase));
protected SQLSelectStatement createSQLSelectStatementForAssignedExpressionForUpdateAll(Expression value) { ReportQuery reportQuery = new ReportQuery(getQuery().getReferenceClass(), value.getBuilder()); reportQuery.setDescriptor(getQuery().getDescriptor()); reportQuery.setSession(getSession()); reportQuery.addAttribute("", value); SQLSelectStatement selectStatement = ((ExpressionQueryMechanism)reportQuery.getQueryMechanism()).buildReportQuerySelectStatement(false); reportQuery.setSession(null); return selectStatement; }
this.queryType = selection.getJavaType(); this.subQuery.getItems().clear(); this.subQuery.addItem(String.valueOf(count), ((InternalSelection) select).getCurrentNode()); ++count; this.subQuery.setExpressionBuilder(((InternalSelection)selection.getCompoundSelectionItems().get(0)).getCurrentNode().getBuilder()); } else { TypeImpl type = ((MetamodelImpl)this.metamodel).getType(selection.getJavaType()); if (type != null && type.getPersistenceType().equals(PersistenceType.ENTITY)) { this.subQuery.addAttribute("", new ConstantExpression(1, ((InternalSelection)selection).getCurrentNode().getBuilder())); this.subQuery.addNonFetchJoinedAttribute(((InternalSelection)selection).getCurrentNode()); } else { String itemName = selection.getAlias(); itemName = ((InternalSelection) selection).getCurrentNode().getName(); this.subQuery.addItem(itemName, ((InternalSelection) selection).getCurrentNode()); this.subQuery.setExpressionBuilder(((InternalSelection)selection).getCurrentNode().getBuilder());
/** * PUBLIC: A logical expression for the size of collection <code>attributeName</code>. * <p>Example: * <pre><blockquote> * TopLink: employee.size("phoneNumbers") * Java: employee.getPhoneNumbers().size() * SQL: SELECT ... FROM EMP t0 WHERE ... * (SELECT COUNT(*) FROM PHONE t1 WHERE (t0.EMP_ID = t1.EMP_ID)) * </blockquote></pre> * This is a case where a fast operation in java does not translate to an * equally fast operation in SQL, requiring a correlated subselect. */ public Expression size(String attributeName) { // Create an anoymous subquery that will get its reference class // set during SubSelectExpression.normalize. ReportQuery subQuery = new ReportQuery(); subQuery.addCount(); subQuery.setSelectionCriteria(subQuery.getExpressionBuilder().equal(this.anyOf(attributeName))); return subQuery(subQuery); }
/** * INTERNAL: * return a collection of expressions if PK's are used. */ public Vector getQueryExpressions() { Vector fieldExpressions = NonSynchronizedVector.newInstance(getItems().size()); // For bug 3115576 and an EXISTS subquery only need to return a single field. if (shouldRetrieveFirstPrimaryKey()) { if (!getDescriptor().getPrimaryKeyFields().isEmpty()) { fieldExpressions.addElement(getDescriptor().getPrimaryKeyFields().get(0)); } } if (shouldRetrievePrimaryKeys()) { fieldExpressions.addAll(getDescriptor().getPrimaryKeyFields()); } return fieldExpressions; }
/** */ public ReportQuery getReportQuery(GenerationContext context) { ReportQuery innerQuery = new ReportQuery(); GenerationContext innerContext = subqueryParseTree.populateSubquery(innerQuery, context); Expression joins = innerContext.joinVariables(outerVars); if (joins != null) { Expression where = innerQuery.getSelectionCriteria(); where = appendExpression(where, joins); innerQuery.setSelectionCriteria(where); } return innerQuery; }
protected void integrateRoot(RootImpl root) { if (this.roots.isEmpty()) { TypeImpl type = ((MetamodelImpl)this.metamodel).getType(this.queryType); if ((type != null && type.getPersistenceType() == PersistenceType.ENTITY) || queryType.equals(ClassConstants.OBJECT)) { // this is the first root, set return type and selection and query type if (this.selection == null) { this.selection = root; this.subQuery.getItems().clear(); this.subQuery.addAttribute("", new ConstantExpression(1, root.getCurrentNode().getBuilder())); this.queryResult = ResultType.ENTITY; } } this.subQuery.setReferenceClass(root.getJavaType()); this.subQuery.setExpressionBuilder(root.getCurrentNode().getBuilder()); this.queryType = root.getJavaType(); this.currentNode.setBaseExpression(((CommonAbstractCriteriaImpl)this.parent).getBaseExpression()); } super.integrateRoot(root); }
/** * PUBLIC: * Add the average value of the attribute to be included in the result. * Aggregation functions can be used with a group by, or on the entire result set. * EXAMPLE: reportQuery.addAverage("salary"); */ public void addAverage(String itemName) { addAverage(itemName, getExpressionBuilder().get(itemName)); }
/** * PUBLIC: * Add the attribute to the group by expressions. * This will group the result set on that attribute and is normally used in conjunction with aggregation functions. * Example: reportQuery.addGrouping("lastName") */ public void addGrouping(String attributeName) { addGrouping(getExpressionBuilder().get(attributeName)); }
/** * PUBLIC: * Add a ConstructorReportItem to this query's set of return values. * @param item used to specify a class constructor and values to pass in from this query * @see ConstructorReportItem */ public void addConstructorReportItem(ConstructorReportItem item){ addItem(item); }
/** * INTERNAL * Apply this node to the passed query */ public void applyToQuery(ObjectLevelReadQuery theQuery, GenerationContext context) { if (theQuery.isReportQuery()) { ReportQuery reportQuery = (ReportQuery)theQuery; reportQuery.addAttribute("size", getLeft().generateExpression(context).count(), (Class)getType()); reportQuery.addGrouping(getLeft().getLeft().generateExpression(context)); } }
/** * PUBLIC: * Add the minimum value of the attribute to be included in the result. * Aggregation functions can be used with a group by, or on the entire result set. * EXAMPLE: reportQuery.addMinimum("salary"); */ public void addMinimum(String itemName) { addMinimum(itemName, getExpressionBuilder().get(itemName)); }
/** * PUBLIC: * Add the maximum value of the attribute to be included in the result. * Aggregation functions can be used with a group by, or on the entire result set. * EXAMPLE: reportQuery.addMaximum("salary"); */ public void addMaximum(String itemName) { addMaximum(itemName, getExpressionBuilder().get(itemName)); }
public SubQueryImpl(Metamodel metamodel, Class result, CriteriaBuilderImpl queryBuilder, CommonAbstractCriteria parent){ super(metamodel, ResultType.OTHER, queryBuilder, result); this.subQuery = new ReportQuery(); TypeImpl queryType = ((MetamodelImpl)metamodel).getType(result); if (queryType != null && queryType.getPersistenceType() == PersistenceType.ENTITY){ this.subQuery.setReferenceClass(result); } this.subQuery.setDistinctState(ObjectLevelReadQuery.DONT_USE_DISTINCT); this.correlatedJoins = new HashSet(); this.correlations = new HashSet(); this.currentNode = new SubSelectExpression(subQuery, new ExpressionBuilder()); this.parent = parent; }