use of org.apache.cayenne.map.SQLResult in project cayenne by apache.
the class SQLSelect method createReplacementQuery.
@Override
protected Query createReplacementQuery(EntityResolver resolver) {
Object root;
if (persistentType != null) {
root = persistentType;
} else if (dataMapName != null) {
DataMap map = resolver.getDataMap(dataMapName);
if (map == null) {
throw new CayenneRuntimeException("Invalid dataMapName '%s'", dataMapName);
}
root = map;
} else {
// will route via default node. TODO: allow explicit node name?
root = null;
}
SQLTemplate template = new SQLTemplate();
template.setFetchingDataRows(isFetchingDataRows());
template.setRoot(root);
template.setDefaultTemplate(getSql());
template.setCacheGroup(cacheGroup);
template.setCacheStrategy(cacheStrategy);
if (prefetches != null) {
template.addPrefetch(prefetches);
}
if (positionalParams != null) {
template.setParamsList(positionalParams);
} else {
template.setParams(params);
}
template.setColumnNamesCapitalization(columnNameCaps);
template.setFetchLimit(limit);
template.setFetchOffset(offset);
template.setPageSize(pageSize);
template.setStatementFetchSize(statementFetchSize);
if (isFetchingScalars()) {
SQLResult resultMap = new SQLResult();
resultMap.addColumnResult("x");
template.setResult(resultMap);
}
return template;
}
use of org.apache.cayenne.map.SQLResult in project cayenne by apache.
the class SelectQueryMetadata method buildResultSetMappingForColumns.
/**
* Build DB result descriptor, that will be used to read and convert raw result of ColumnSelect
* @since 4.0
*/
private void buildResultSetMappingForColumns(SelectQuery<?> query, EntityResolver resolver) {
if (query.getColumns() == null || query.getColumns().isEmpty()) {
return;
}
SQLResult result = new SQLResult();
for (Property<?> column : query.getColumns()) {
Expression exp = column.getExpression();
String name = column.getName() == null ? exp.expName() : column.getName();
boolean fullObject = false;
if (exp.getType() == Expression.OBJ_PATH) {
// check if this is toOne relation
Expression dbPath = this.getObjEntity().translateToDbPath(exp);
DbRelationship rel = findRelationByPath(dbEntity, dbPath);
if (rel != null && !rel.isToMany()) {
// it this path is toOne relation, than select full object for it
fullObject = true;
}
} else if (exp.getType() == Expression.FULL_OBJECT) {
fullObject = true;
}
if (fullObject) {
// detected full object column
if (getPageSize() > 0) {
// for paginated queries keep only IDs
result.addEntityResult(buildEntityIdResultForColumn(column, resolver));
} else {
// will unwrap to full set of db-columns (with join prefetch optionally)
result.addEntityResult(buildEntityResultForColumn(query, column, resolver));
}
} else {
// scalar column
result.addColumnResult(name);
}
}
resultSetMapping = result.getResolvedComponents(resolver);
}
Aggregations