Search in sources :

Example 6 with SQLResult

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;
}
Also used : SQLResult(org.apache.cayenne.map.SQLResult) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 7 with SQLResult

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);
}
Also used : SQLResult(org.apache.cayenne.map.SQLResult) Expression(org.apache.cayenne.exp.Expression) DbRelationship(org.apache.cayenne.map.DbRelationship)

Aggregations

SQLResult (org.apache.cayenne.map.SQLResult)7 DataMap (org.apache.cayenne.map.DataMap)5 SQLTemplate (org.apache.cayenne.query.SQLTemplate)4 Test (org.junit.Test)4 FrontBaseAdapter (org.apache.cayenne.dba.frontbase.FrontBaseAdapter)3 OpenBaseAdapter (org.apache.cayenne.dba.openbase.OpenBaseAdapter)3 EntityResult (org.apache.cayenne.map.EntityResult)2 HashMap (java.util.HashMap)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 EJBQLCompiledExpression (org.apache.cayenne.ejbql.EJBQLCompiledExpression)1 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)1 EJBQLExpression (org.apache.cayenne.ejbql.EJBQLExpression)1 Expression (org.apache.cayenne.exp.Expression)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 ObjRelationship (org.apache.cayenne.map.ObjRelationship)1 PrefetchTreeNode (org.apache.cayenne.query.PrefetchTreeNode)1 ArcProperty (org.apache.cayenne.reflect.ArcProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1