Methods Summary |
---|
public void | addAttribute(java.lang.String itemName)PUBLIC:
Add the attribute from the reference class to be included in the result.
EXAMPLE: reportQuery.addAttribute("firstName");
addItem(itemName, getExpressionBuilder().get(itemName));
|
public void | addAttribute(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)PUBLIC:
Add the attribute to be included in the result.
EXAMPLE: reportQuery.addAttribute("city", expBuilder.get("address").get("city"));
addItem(itemName, attributeExpression);
|
public void | addAttribute(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.Class type)PUBLIC:
Add the attribute to be included in the result. Return the result as the provided class
EXAMPLE: reportQuery.addAttribute("city", expBuilder.get("period").get("startTime"), Time.class);
addItem(itemName, attributeExpression, type);
|
public void | addAverage(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)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("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.average());
|
public void | addAverage(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.Class resultType)PUBLIC:
Add the average value of the attribute to be included in the result and
return it as the specified resultType.
Aggregation functions can be used with a group by, or on the entire result set.
EXAMPLE: reportQuery.addAverage("managerSalary", expBuilder.get("manager").get("salary"), Double.class);
addItem(itemName, attributeExpression.average(), resultType);
|
public void | addAverage(java.lang.String itemName)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");
addAverage(itemName, getExpressionBuilder().get(itemName));
|
public void | addAverage(java.lang.String itemName, java.lang.Class resultType)PUBLIC:
Add the average value of the attribute to be included in the result and
return it as the specified resultType.
Aggregation functions can be used with a group by, or on the entire result set.
EXAMPLE: reportQuery.addAverage("salary", Float.class);
addAverage(itemName, getExpressionBuilder().get(itemName), resultType);
|
public void | addConstructorReportItem(oracle.toplink.essentials.queryframework.ConstructorReportItem item)PUBLIC:
Add a ConstructorReportItem to this query's set of return values.
addItem(item);
|
public void | addCount()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 ...
addCount("COUNT", getExpressionBuilder());
|
public void | addCount(java.lang.String attributeName)PUBLIC:
Include the number of rows returned by the query in the result, where attributeExpression is not null.
Aggregation functions can be used with a group by, or on the entire result set.
Example:
TopLink: reportQuery.addCount("id");
SQL: SELECT COUNT (t0.EMP_ID) FROM EMPLOYEE t0, ...
addCount(attributeName, getExpressionBuilder().get(attributeName));
|
public void | addCount(java.lang.String attributeName, java.lang.Class resultType)PUBLIC:
Include the number of rows returned by the query in the result, where attributeExpression is not null.
Aggregation functions can be used with a group by, or on the entire result set.
Set the count to be returned as the specified resultType.
Example:
TopLink: reportQuery.addCount("id", Long.class);
SQL: SELECT COUNT (t0.EMP_ID) FROM EMPLOYEE t0, ...
addCount(attributeName, getExpressionBuilder().get(attributeName), resultType);
|
public void | addCount(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)PUBLIC:
Include the number of rows returned by the query in the result, where attributeExpression
is not null.
Aggregation functions can be used with a group by, or on the entire result set.
Example:
TopLink: reportQuery.addCount("Count", getExpressionBuilder().get("id"));
SQL: SELECT COUNT (t0.EMP_ID) FROM EMPLOYEE t0, ...
Example: counting only distinct values of an attribute.
TopLink: reportQuery.addCount("Count", getExpressionBuilder().get("address").distinct());
SQL: SELECT COUNT (DISTINCT t0.ADDR_ID) FROM EMPLOYEE t0, ...
objectAttributes can be specified also, even accross many to many
mappings.
addItem(itemName, attributeExpression.count());
|
public void | addCount(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.Class resultType)PUBLIC:
Include the number of rows returned by the query in the result, where attributeExpression
is not null.
Aggregation functions can be used with a group by, or on the entire result set.
Set the count to be returned as the specified resultType.
Example:
TopLink: reportQuery.addCount("Count", getExpressionBuilder().get("id"), Integer.class);
SQL: SELECT COUNT (t0.EMP_ID) FROM EMPLOYEE t0, ...
Example: counting only distinct values of an attribute.
TopLink: reportQuery.addCount("Count", getExpressionBuilder().get("address").distinct());
SQL: SELECT COUNT (DISTINCT t0.ADDR_ID) FROM EMPLOYEE t0, ...
objectAttributes can be specified also, even accross many to many
mappings.
addItem(itemName, attributeExpression.count(), resultType);
|
public void | addFunctionItem(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.String functionName)ADVANCED:
Add the function against the attribute expression to be included in the result.
Aggregation functions can be used with a group by, or on the entire result set.
Example: reportQuery.addFunctionItem("average", expBuilder.get("salary"), "average");
Expression functionExpression = attributeExpression;
functionExpression = attributeExpression.getFunction(functionName);
ReportItem item = new ReportItem(itemName, functionExpression);
addItem(item);
|
public void | addGrouping(java.lang.String attributeName)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")
addGrouping(getExpressionBuilder().get(attributeName));
|
public void | addGrouping(oracle.toplink.essentials.expressions.Expression expression)PUBLIC:
Add the attribute expression 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(expBuilder.get("address").get("country"))
getGroupByExpressions().addElement(expression);
//Bug2804042 Must un-prepare if prepared as the SQL may change.
setIsPrepared(false);
|
private void | addItem(oracle.toplink.essentials.internal.queryframework.ReportItem item)INTERNAL:
Method used to abstract addToConstructorItem behavour from the public addItem methods
if (addToConstructorItem && (getItems().size()>0) &&(((ReportItem)getItems().lastElement()).isContructorItem() )){
((ConstructorReportItem)getItems().lastElement()).addItem(item);
}else{
getItems().addElement(item);
}
//Bug2804042 Must un-prepare if prepared as the SQL may change.
setIsPrepared(false);
|
public void | addItem(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)ADVANCED:
Add the expression value to be included in the result.
EXAMPLE: reportQuery.addItem("name", expBuilder.get("firstName").toUpperCase());
ReportItem item = new ReportItem(itemName, attributeExpression);
addItem(item);
|
public void | addItem(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.util.List joinedExpressions)ADVANCED:
Add the expression value to be included in the result.
EXAMPLE: reportQuery.addItem("name", expBuilder.get("firstName").toUpperCase());
ReportItem item = new ReportItem(itemName, attributeExpression);
item.getJoinedAttributeManager().setJoinedAttributeExpressions_(joinedExpressions);
addItem(item);
|
protected void | addItem(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.Class resultType)INTERNAL:
Add the expression value to be included in the result.
EXAMPLE: reportQuery.addItem("name", expBuilder.get("firstName").toUpperCase());
The resultType can be specified to support EJBQL that adheres to the
EJB 3.0 spec.
ReportItem item = new ReportItem(itemName, attributeExpression);
item.setResultType(resultType);
addItem(item);
|
public void | addMaximum(java.lang.String 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");
addMaximum(itemName, getExpressionBuilder().get(itemName));
|
public void | addMaximum(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)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("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.maximum());
|
public void | addMinimum(java.lang.String itemName)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");
addMinimum(itemName, getExpressionBuilder().get(itemName));
|
public void | addMinimum(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)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("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.minimum());
|
public void | addStandardDeviation(java.lang.String itemName)PUBLIC:
Add the standard deviation 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.addStandardDeviation("salary");
addStandardDeviation(itemName, getExpressionBuilder().get(itemName));
|
public void | addStandardDeviation(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)PUBLIC:
Add the standard deviation 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.addStandardDeviation("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.standardDeviation());
|
public void | addSum(java.lang.String itemName)PUBLIC:
Add the sum 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.addSum("salary");
addSum(itemName, getExpressionBuilder().get(itemName));
|
public void | addSum(java.lang.String itemName, java.lang.Class resultType)PUBLIC:
Add the sum value of the attribute to be included in the result and
return it as the specified resultType.
Aggregation functions can be used with a group by, or on the entire result set.
EXAMPLE: reportQuery.addSum("salary", Float.class);
addSum(itemName, getExpressionBuilder().get(itemName), resultType);
|
public void | addSum(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)PUBLIC:
Add the sum 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.addSum("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.sum());
|
public void | addSum(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression, java.lang.Class resultType)PUBLIC:
Add the sum value of the attribute to be included in the result and
return it as the specified resultType.
Aggregation functions can be used with a group by, or on the entire result set.
EXAMPLE: reportQuery.addSum("managerSalary", expBuilder.get("manager").get("salary"), Float.class);
addItem(itemName, attributeExpression.sum(), resultType);
|
public void | addVariance(java.lang.String itemName)PUBLIC:
Add the variance 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.addVariance("salary");
addVariance(itemName, getExpressionBuilder().get(itemName));
|
public void | addVariance(java.lang.String itemName, oracle.toplink.essentials.expressions.Expression attributeExpression)PUBLIC:
Add the variance 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.addVariance("managerSalary", expBuilder.get("manager").get("salary"));
addItem(itemName, attributeExpression.variance());
|
public oracle.toplink.essentials.queryframework.ConstructorReportItem | beginAddingConstructorArguments(java.lang.Class constructorClass)PUBLIC: Call a constructor for the given class with the results of this query.
ConstructorReportItem citem = new ConstructorReportItem(constructorClass.getName());
citem.setResultType(constructorClass);
//add directly to avoid addToConstructorItem behaviour
getItems().add(citem);
//Bug2804042 Must un-prepare if prepared as the SQL may change.
setIsPrepared(false);
this.addToConstructorItem=true;
return citem;
|
public oracle.toplink.essentials.queryframework.ConstructorReportItem | beginAddingConstructorArguments(java.lang.Class constructorClass, java.lang.Class[] constructorArgTypes)PUBLIC: Call a constructor for the given class with the results of this query.
ConstructorReportItem citem =beginAddingConstructorArguments(constructorClass);
citem.setConstructorArgTypes(constructorArgTypes);
return citem;
|
public java.lang.Object | buildObject(oracle.toplink.essentials.internal.sessions.AbstractRecord row, java.util.Vector toManyJoinData)INTERNAL:
Construct a result from a row. Either return a ReportQueryResult or just the attribute.
ReportQueryResult reportQueryResult = new ReportQueryResult(this, row, toManyJoinData);
//GF_ISSUE_395
if (this.returnedKeys != null){
if (this.returnedKeys.contains(reportQueryResult.getResultKey())){
return RESULT_IGNORED; //distinguish between null values and thrown away duplicates
} else {
this.returnedKeys.add(reportQueryResult.getResultKey());
}
}
//end GF_ISSUE_395
if (this.shouldReturnSingleAttribute()) {
return reportQueryResult.getResults().firstElement();
}
if (this.shouldReturnWithoutReportQueryResult()){
if (reportQueryResult.getResults().size() == 1){
return reportQueryResult.getResults().firstElement();
}
return reportQueryResult.toArray();
}
return reportQueryResult;
|
public java.lang.Object | buildObjects(java.util.Vector rows)INTERNAL:
Construct a container of ReportQueryResult from the rows.
If only one result or value was asked for only return that.
if (shouldReturnSingleResult() || shouldReturnSingleValue()) {
if (rows.isEmpty()) {
return null;
}
ReportQueryResult result = (ReportQueryResult)buildObject((AbstractRecord)rows.firstElement(), rows);
if (shouldReturnSingleValue()) {
return result.elements().nextElement();
}
return result;
}
ContainerPolicy containerPolicy = getContainerPolicy();
Object reportResults = containerPolicy.containerInstance(rows.size());
// GF_ISSUE_395
if (shouldDistinctBeUsed()){
this.returnedKeys = new HashSet();
}
//end GF_ISSUE
//If only the attribute is desired, then buildObject will only get the first attribute each time
for (Enumeration rowsEnum = rows.elements(); rowsEnum.hasMoreElements();) {
// GF_ISSUE_395
Object result = buildObject((AbstractRecord)rowsEnum.nextElement(), rows);
if (result != RESULT_IGNORED){
containerPolicy.addInto(result, reportResults, getSession());
}
//end GF_ISSUE
}
return reportResults;
|
protected java.lang.Object | checkEarlyReturnImpl(oracle.toplink.essentials.internal.sessions.AbstractSession session, oracle.toplink.essentials.internal.sessions.AbstractRecord translationRow)INTERNAL:
The cache check is done before the prepare as a hit will not require the work to be done.
// Check for in-memory only query.
if (shouldCheckCacheOnly()) {
throw QueryException.cannotSetShouldCheckCacheOnlyOnReportQuery();
} else {
return null;
}
|
public void | clearItems()INTERNAL:
Clear the ReportQueryItems
items = new Vector();
setIsPrepared(false);
|
public void | copyReportItems(java.util.Dictionary alreadyDone)INTERNAL: Required for a very special case of bug 2612185:
ReportItems from parallelExpressions, on a ReportQuery which is a subQuery,
which is being batch read.
In a batch query the selection criteria is effectively cloned twice, meaning
the ReportItems need to be cloned an extra time also to stay in sync.
Each call to copiedVersionFrom() will take O(1) time as the expression was
already cloned.
items = (Vector)items.clone();
for (int i = items.size() - 1; i >= 0; i--) {
ReportItem item = (ReportItem)items.elementAt(i);
Expression expression = item.getAttributeExpression();
if ((expression != null) && (alreadyDone.get(expression.getBuilder()) != null)) {
expression = expression.copiedVersionFrom(alreadyDone);
}
items.set(i, new ReportItem(item.getName(), expression));
}
if (groupByExpressions != null) {
groupByExpressions = (Vector)groupByExpressions.clone();
for (int i = groupByExpressions.size() - 1; i >= 0; i--) {
Expression item = (Expression)groupByExpressions.elementAt(i);
if (alreadyDone.get(item.getBuilder()) != null) {
groupByExpressions.set(i, item.copiedVersionFrom(alreadyDone));
}
}
}
if (orderByExpressions != null) {
for (int i = orderByExpressions.size() - 1; i >= 0; i--) {
Expression item = (Expression)orderByExpressions.elementAt(i);
if (alreadyDone.get(item.getBuilder()) != null) {
orderByExpressions.set(i, item.copiedVersionFrom(alreadyDone));
}
}
}
|
public void | dontRetrievePrimaryKeys()PUBLIC:
Set if the query results should contain the primary keys or each associated object.
This make retrieving the real object easier.
By default they are not retrieved.
setShouldRetrievePrimaryKeys(false);
//Bug2804042 Must un-prepare if prepared as the SQL may change.
setIsPrepared(false);
|
public void | dontReturnSingleAttribute()PUBLIC:
Don't simplify the result by returning the single attribute. Wrap in a ReportQueryResult.
if (shouldReturnSingleAttribute()) {
returnChoice = 0;
}
|
public void | dontReturnSingleResult()PUBLIC:
Simplifies the result by only returning the first result.
This can be used if it known that only one row is returned by the report query.
if (shouldReturnSingleResult()) {
returnChoice = 0;
}
|
public void | dontReturnSingleValue()PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
if (shouldReturnSingleValue()) {
returnChoice = 0;
}
|
public void | dontReturnWithoutReportQueryResult()PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
if (shouldReturnWithoutReportQueryResult()) {
returnChoice = 0;
}
|
public void | endAddingToConstructorItem()PUBLIC:
Used in conjunction with beginAddingConstructorArguments to signal that expressions should no longer be
be added to the collection used in the constructor
Get the rows and build the object from the rows.
this.addToConstructorItem=false;
|
public java.lang.Object | executeDatabaseQuery()INTERNAL:
Execute the query.
Get the rows and build the object from the rows.
// ensure a pessimistic locking query will go down the write connection
if (isLockQuery() && getSession().isUnitOfWork()) {
UnitOfWorkImpl unitOfWork = (UnitOfWorkImpl)getSession();
// Note if a nested unit of work this will recursively start a
// transaction early on the parent also.
if (isLockQuery()) {
if ((!unitOfWork.getCommitManager().isActive()) && (!unitOfWork.wasTransactionBegunPrematurely())) {
unitOfWork.beginTransaction();
unitOfWork.setWasTransactionBegunPrematurely(true);
}
}
}
if (getContainerPolicy().overridesRead()) {
return getContainerPolicy().execute();
}
if (getQueryId() == 0) {
setQueryId(getSession().getNextQueryId());
}
Vector rows = getQueryMechanism().selectAllReportQueryRows();
// If using -m joins, must set all rows.
return buildObjects(rows);
|
public java.util.Vector | getGroupByExpressions()INTERNAL:
Return the group bys.
return groupByExpressions;
|
public oracle.toplink.essentials.expressions.Expression | getHavingExpression()INTERNAL:
Return the Having expression.
return havingExpression;
|
public java.util.Vector | getItemExpressions()INTERNAL:
return a collection of expressions from the items. Ignore the null (place holders).
Vector fieldExpressions = new Vector(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());
}
for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) {
ReportItem item = (ReportItem)itemsEnum.nextElement();
Expression fieldExpression = item.getAttributeExpression();
if (fieldExpression != null) {
fieldExpressions.addElement(fieldExpression);
}
}
return fieldExpressions;
|
public java.util.Vector | getItems()INTERNAL:
return items;
|
private oracle.toplink.essentials.mappings.DatabaseMapping | getMappingOfFirstPrimaryKey(oracle.toplink.essentials.descriptors.ClassDescriptor descriptor)
if (descriptor != null) {
for (Iterator i = descriptor.getMappings().iterator(); i.hasNext(); ) {
DatabaseMapping m = (DatabaseMapping)i.next();
if (m.isPrimaryKeyMapping()) {
return m;
}
}
}
return null;
|
public java.util.Vector | getNames()INTERNAL:
Lazily initialize and return the names of the items requested for use in each result object
if (names == null) {
names = new Vector();
for (Enumeration e = getItems().elements(); e.hasMoreElements();) {
names.addElement(((ReportItem)e.nextElement()).getName());
}
}
return names;
|
public java.util.Vector | getQueryExpressions()INTERNAL:
return a collection of expressions if PK's are used.
Vector fieldExpressions = new Vector(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 boolean | isReportQuery()PUBLIC:
Return if this is a report query.
return true;
|
protected void | prepare()INTERNAL:
Prepare the receiver for execution in a session.
Initialize each item with its DTF mapping
// Oct 19, 2000 JED
// Added exception to be thrown if no attributes have been added to the query
if (getItems().size() > 0) {
try {
for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) {
((ReportItem)itemsEnum.nextElement()).initialize(this);
}
} catch (QueryException exception) {
exception.setQuery(this);
throw exception;
}
} else {
if ((!shouldRetrievePrimaryKeys()) && (!shouldRetrieveFirstPrimaryKey())) {
throw QueryException.noAttributesForReportQuery(this);
}
}
super.prepare();
|
protected void | prepareObjectAttributeCount(java.util.Dictionary clonedExpressions)INTERNAL:
Prepare a report query with a count defined on an object attribute.
Added to fix bug 3268040, addCount(objectAttribute) not supported.
prepareObjectAttributeCount(getItems(), clonedExpressions);
|
private void | prepareObjectAttributeCount(java.util.List items, java.util.Dictionary clonedExpressions)
int numOfReportItems = items.size();
//gf675: need to loop through all items to fix all count(..) instances
for (int i =0;i<numOfReportItems; i++){
ReportItem item = (ReportItem)items.get(i);
if (item == null) {
continue;
} else if (item instanceof ConstructorReportItem) {
// recursive call to process child ReportItems
prepareObjectAttributeCount(((ConstructorReportItem)item).getReportItems(), clonedExpressions);
} else if (item.getAttributeExpression() instanceof FunctionExpression) {
FunctionExpression count = (FunctionExpression)item.getAttributeExpression();
if (count.getOperator().getSelector() == ExpressionOperator.Count) {
Expression baseExp = count.getBaseExpression();
boolean distinctUsed = false;
if (baseExp.isFunctionExpression() && (((FunctionExpression)baseExp).getOperator().getSelector() == ExpressionOperator.Distinct)) {
distinctUsed = true;
baseExp = ((FunctionExpression)baseExp).getBaseExpression();
}
boolean outerJoin = false;
ClassDescriptor newDescriptor = null;
if (baseExp.isQueryKeyExpression()) {
// now need to find out if it is a direct to field or something else.
DatabaseMapping mapping = getLeafMappingFor(baseExp, getDescriptor());
if ((mapping != null) && !mapping.isDirectToFieldMapping()) {
newDescriptor = mapping.getReferenceDescriptor();
outerJoin = ((QueryKeyExpression)baseExp).shouldUseOuterJoin();
}
} else if (baseExp.isExpressionBuilder()) {
newDescriptor = getSession().getDescriptor(((ExpressionBuilder)baseExp).getQueryClass());
}
if (newDescriptor != null) {
// At this point we are committed to rewriting the query.
if (newDescriptor.hasSimplePrimaryKey()) {
// case 1: simple PK =>
// treat COUNT(entity) as COUNT(entity.pk)
DatabaseMapping pk = getMappingOfFirstPrimaryKey(newDescriptor);
Expression countArg = baseExp.get(pk.getAttributeName());
if (distinctUsed) {
countArg = countArg.distinct();
}
count.setBaseExpression(countArg);
count.getChildren().setElementAt(countArg, 0);
} else if (!distinctUsed) {
// case 2: composite PK, but no DISTINCT =>
// pick a PK column for the COUNT aggregate
DatabaseMapping pk = getMappingOfFirstPrimaryKey(newDescriptor);
Expression countArg = baseExp.get(pk.getAttributeName());
while (pk.isAggregateObjectMapping()) {
newDescriptor = ((AggregateObjectMapping)pk).getReferenceDescriptor();
pk = getMappingOfFirstPrimaryKey(newDescriptor);
countArg = countArg.get(pk.getAttributeName());
}
count.setBaseExpression(countArg);
count.getChildren().setElementAt(countArg, 0);
} else if (!outerJoin) {
// case 3: composite PK and DISTINCT, but no
// outer join => previous solution using
// COUNT(*) and EXISTS subquery
// If this is a subselect baseExp is yet uncloned,
// and will miss out if moved now from items into a selection criteria.
if (clonedExpressions != null) {
if (clonedExpressions.get(baseExp.getBuilder()) != null) {
baseExp = (QueryKeyExpression)baseExp.copiedVersionFrom(clonedExpressions);
} else {
baseExp = (QueryKeyExpression)baseExp.rebuildOn(getExpressionBuilder());
}
}
// Now the reference class of the query needs to be reversed.
// See the bug description for an explanation.
ExpressionBuilder countBuilder = baseExp.getBuilder();
ExpressionBuilder outerBuilder = new ExpressionBuilder();
ReportQuery subSelect = new ReportQuery(getReferenceClass(), countBuilder);
subSelect.setShouldRetrieveFirstPrimaryKey(true);
// Make sure the outerBuilder does not appear on the left of the subselect.
// Putting a builder on the left is desirable to trigger an optimization.
if (getSelectionCriteria() != null) {
outerBuilder.setQueryClass(newDescriptor.getJavaClass());
subSelect.setSelectionCriteria(baseExp.equal(outerBuilder).and(getSelectionCriteria()));
} else {
subSelect.setSelectionCriteria(baseExp.equal(outerBuilder));
}
setSelectionCriteria(outerBuilder.exists(subSelect));
count.setBaseExpression(outerBuilder);
count.getChildren().setElementAt( outerBuilder, 0);
setReferenceClass(newDescriptor.getJavaClass());
changeDescriptor(getSession());
} else {
// case 4: composite PK, DISTINCT, outer join =>
// not supported, throw exception
throw QueryException.distinctCountOnOuterJoinedCompositePK(
newDescriptor, this);
}
}
}
}
}
|
protected void | prepareSelectAllRows()INTERNAL:
Prepare the mechanism.
prepareObjectAttributeCount(null);
getQueryMechanism().prepareReportQuerySelectAllRows();
|
public synchronized void | prepareSubSelect(oracle.toplink.essentials.internal.sessions.AbstractSession session, oracle.toplink.essentials.internal.sessions.AbstractRecord translationRow, java.util.Dictionary clonedExpressions)INTERNAL:
Prepare the receiver for being printed inside a subselect.
This prepares the statement but not the call.
if (isPrepared()) {
return;
}
setIsPrepared(true);
setSession(session);
setTranslationRow(translationRow);
checkDescriptor(getSession());
if (descriptor.isAggregateDescriptor()) {
// Not allowed
throw QueryException.aggregateObjectCannotBeDeletedOrWritten(descriptor, this);
}
try {
for (Enumeration itemsEnum = getItems().elements(); itemsEnum.hasMoreElements();) {
((ReportItem)itemsEnum.nextElement()).initialize(this);
}
} catch (QueryException exception) {
exception.setQuery(this);
throw exception;
}
prepareObjectAttributeCount(clonedExpressions);
getQueryMechanism().prepareReportQuerySubSelect();
setSession(null);
setTranslationRow(null);
|
public void | retrievePrimaryKeys()PUBLIC:
Set if the query results should contain the primary keys or each associated object.
This make retrieving the real object easier.
By default they are not retrieved.
setShouldRetrievePrimaryKeys(true);
//Bug2804042 Must un-prepare if prepared as the SQL may change.
setIsPrepared(false);
|
public void | returnSingleAttribute()PUBLIC:
Simplify the result by returning a single attribute. Don't wrap in a ReportQueryResult.
returnChoice = ShouldReturnSingleAttribute;
|
public void | returnSingleResult()PUBLIC:
Simplifies the result by only returning the first result.
This can be used if it known that only one row is returned by the report query.
returnChoice = ShouldReturnSingleResult;
|
public void | returnSingleValue()PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
returnChoice = ShouldReturnSingleValue;
|
public void | returnWithoutReportQueryResult()PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
this.returnChoice = ShouldReturnWithoutReportQueryResult;
|
public void | setHavingExpression(oracle.toplink.essentials.expressions.Expression expression)PUBLIC:
Add the expression to the query to be used in the HAVING clause.
This epression will be used to filter the result sets after they are grouped. It must be used in conjunction with the GROUP BY clause.
Example: reportQuery.setHavingExpression(expBuilder.get("address").get("country").equal("Canada"))
havingExpression = expression;
setIsPrepared(false);
|
public void | setShouldRetrieveFirstPrimaryKey(boolean shouldRetrieveFirstPrimaryKey)ADVANCED:
Sets if the query results should contain the first primary key of each associated object.
Usefull if this is an EXISTS subquery and you don't care what fields are returned
so long as it is a single field.
The default value is false.
This should only be used with a subquery.
this.shouldRetrievePrimaryKeys = (shouldRetrieveFirstPrimaryKey ? FIRST_PRIMARY_KEY : NO_PRIMARY_KEY);
|
public void | setShouldRetrievePrimaryKeys(boolean shouldRetrievePrimaryKeys)PUBLIC:
Set if the query results should contain the primary keys or each associated object.
This make retrieving the real object easier.
By default they are not retrieved.
this.shouldRetrievePrimaryKeys = (shouldRetrievePrimaryKeys ? FULL_PRIMARY_KEY : NO_PRIMARY_KEY);
|
public void | setShouldReturnSingleAttribute(boolean newChoice)PUBLIC:
Simplifies the result by only returning the attribute (as opposed to wrapping in a ReportQueryResult).
This can be used if it is known that only one attribute is returned by the report query.
if (newChoice) {
returnSingleAttribute();
} else {
dontReturnSingleAttribute();
}
|
public void | setShouldReturnSingleResult(boolean newChoice)PUBLIC:
Simplifies the result by only returning the first result.
This can be used if it known that only one row is returned by the report query.
if (newChoice) {
returnSingleResult();
} else {
dontReturnSingleResult();
}
|
public void | setShouldReturnSingleValue(boolean newChoice)PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
if (newChoice) {
returnSingleValue();
} else {
dontReturnSingleValue();
}
|
public void | setShouldReturnWithoutReportQueryResult(boolean newChoice)PUBLIC:
Simplifies the result by returning a nested list instead of the ReportQueryResult.
This is used by EJB 3.
if (newChoice) {
returnWithoutReportQueryResult();
} else {
dontReturnWithoutReportQueryResult();
}
|
public boolean | shouldRetrieveFirstPrimaryKey()PUBLIC:
Return if the query results should contain the first primary key of each associated object.
Usefull if this is an EXISTS subquery and you don't care what fields are returned
so long as it is a single field.
return (shouldRetrievePrimaryKeys == FIRST_PRIMARY_KEY);
|
public boolean | shouldRetrievePrimaryKeys()PUBLIC:
Return if the query results should contain the primary keys or each associated object.
This make retrieving the real object easier.
return (shouldRetrievePrimaryKeys == FULL_PRIMARY_KEY);
|
public boolean | shouldReturnSingleAttribute()PUBLIC:
Answer if we are only returning the attribute (as opposed to wrapping in a ReportQueryResult).
This can be used if it is known that only one attribute is returned by the report query.
return returnChoice == ShouldReturnSingleAttribute;
|
public boolean | shouldReturnSingleResult()PUBLIC:
Simplifies the result by only returning the first result.
This can be used if it known that only one row is returned by the report query.
return returnChoice == ShouldReturnSingleResult;
|
public boolean | shouldReturnSingleValue()PUBLIC:
Simplifies the result by only returning a single value.
This can be used if it known that only one row is returned by the report query and only a single item is added
to the report.
return returnChoice == ShouldReturnSingleValue;
|
public boolean | shouldReturnWithoutReportQueryResult()PUBLIC:
Simplifies the result by returning a nested list instead of the ReportQueryResult.
This is used by EJB 3.
return returnChoice == ShouldReturnWithoutReportQueryResult;
|