Search in sources :

Example 1 with SQLResult

use of org.apache.cayenne.map.SQLResult in project cayenne by apache.

the class DataContextSQLTemplateIT method testSQLResultSetMappingScalarArray.

@Test
public void testSQLResultSetMappingScalarArray() throws Exception {
    createFourArtists();
    String sql = "SELECT count(1) AS X, 77 AS Y FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X, 77 Y FROM ARTIST GROUP BY Y");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X, 77 Y FROM ARTIST GROUP BY 77");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    rsMap.addColumnResult("Y");
    query.setResult(rsMap);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Object o = objects.get(0);
    assertTrue(o instanceof Object[]);
    Object[] row = (Object[]) o;
    assertEquals(2, row.length);
    assertEquals(4, ((Number) row[0]).intValue());
    assertEquals(77, ((Number) row[1]).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 2 with SQLResult

use of org.apache.cayenne.map.SQLResult in project cayenne by apache.

the class DataContextSQLTemplateIT method testSQLResultSetMappingScalar.

@Test
public void testSQLResultSetMappingScalar() throws Exception {
    createFourArtists();
    String sql = "SELECT count(1) AS X FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X FROM ARTIST");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) X FROM ARTIST");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    query.setResult(rsMap);
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    Object o = objects.get(0);
    assertTrue("Expected Number: " + o, o instanceof Number);
    assertEquals(4, ((Number) o).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 3 with SQLResult

use of org.apache.cayenne.map.SQLResult in project cayenne by apache.

the class CayenneIT method testScalarObjectForQuery.

@Test
public void testScalarObjectForQuery() throws Exception {
    createTwoArtists();
    String sql = "SELECT count(1) AS X FROM ARTIST";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setTemplate(FrontBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
    query.setTemplate(OpenBaseAdapter.class.getName(), "SELECT COUNT(ARTIST_ID) AS X FROM ARTIST");
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    SQLResult rsMap = new SQLResult();
    rsMap.addColumnResult("X");
    query.setResult(rsMap);
    Object object = Cayenne.objectForQuery(context, query);
    assertNotNull(object);
    assertTrue(object instanceof Number);
    assertEquals(2, ((Number) object).intValue());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) FrontBaseAdapter(org.apache.cayenne.dba.frontbase.FrontBaseAdapter) OpenBaseAdapter(org.apache.cayenne.dba.openbase.OpenBaseAdapter) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 4 with SQLResult

use of org.apache.cayenne.map.SQLResult in project cayenne by apache.

the class DataContextSQLTemplateIT method testSQLResultSetMappingMixed.

@Test
public void testSQLResultSetMappingMixed() throws Exception {
    createFourArtistsAndThreePaintingsDataSet();
    String sql = "SELECT #result('t0.ARTIST_ID' 'long' 'X'), #result('t0.ARTIST_NAME' 'String' 'Y'), #result('t0.DATE_OF_BIRTH' 'Date' 'Z'), #result('count(t1.PAINTING_ID)' 'int' 'C') " + "FROM ARTIST t0 LEFT JOIN PAINTING t1 ON (t0.ARTIST_ID = t1.ARTIST_ID) " + "GROUP BY t0.ARTIST_ID, t0.ARTIST_NAME, t0.DATE_OF_BIRTH " + "ORDER BY t0.ARTIST_ID";
    DataMap map = context.getEntityResolver().getDataMap("testmap");
    SQLTemplate query = new SQLTemplate(map, sql, false);
    query.setColumnNamesCapitalization(CapsStrategy.UPPER);
    EntityResult artistResult = new EntityResult(Artist.class);
    artistResult.addDbField(Artist.ARTIST_ID_PK_COLUMN, "X");
    artistResult.addObjectField(Artist.ARTIST_NAME.getName(), "Y");
    artistResult.addObjectField(Artist.DATE_OF_BIRTH.getName(), "Z");
    SQLResult rsMap = new SQLResult();
    rsMap.addEntityResult(artistResult);
    rsMap.addColumnResult("C");
    query.setResult(rsMap);
    List<?> objects = context.performQuery(query);
    assertEquals(4, objects.size());
    Object o1 = objects.get(0);
    assertTrue("Expected Object[]: " + o1, o1 instanceof Object[]);
    Object[] array1 = (Object[]) o1;
    assertEquals(2, array1.length);
    Object[] array2 = (Object[]) objects.get(1);
    assertEquals(2, array2.length);
    Object[] array3 = (Object[]) objects.get(2);
    assertEquals(2, array3.length);
    Object[] array4 = (Object[]) objects.get(3);
    assertEquals(2, array3.length);
    assertEquals(new Integer(1), array1[1]);
    assertEquals(new Integer(1), array2[1]);
    assertEquals(new Integer(0), array3[1]);
    assertEquals(new Integer(0), array4[1]);
    assertTrue("Unexpected DataObject: " + array1[0], array1[0] instanceof Artist);
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SQLResult(org.apache.cayenne.map.SQLResult) Artist(org.apache.cayenne.testdo.testmap.Artist) EntityResult(org.apache.cayenne.map.EntityResult) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 5 with SQLResult

use of org.apache.cayenne.map.SQLResult in project cayenne by apache.

the class Compiler method compile.

CompiledExpression compile(String source, EJBQLExpression parsed) {
    parsed.visit(new CompilationVisitor());
    Map<EJBQLPath, Integer> pathsInSelect = new HashMap<>();
    for (int i = 0; i < parsed.getChildrenCount(); i++) {
        if (parsed.getChild(i) instanceof EJBQLSelectClause) {
            EJBQLExpression parsedTemp = parsed.getChild(i);
            boolean stop = false;
            while (parsedTemp.getChildrenCount() > 0 && !stop) {
                EJBQLExpression newParsedTemp = null;
                for (int j = 0; j < parsedTemp.getChildrenCount(); j++) {
                    if (parsedTemp.getChild(j) instanceof EJBQLSelectExpression) {
                        for (int k = 0; k < parsedTemp.getChild(j).getChildrenCount(); k++) {
                            if (parsedTemp.getChild(j).getChild(k) instanceof EJBQLPath) {
                                pathsInSelect.put((EJBQLPath) parsedTemp.getChild(j).getChild(k), j);
                            }
                        }
                    } else {
                        if (parsedTemp.getChild(j).getChildrenCount() == 0) {
                            stop = true;
                        } else {
                            newParsedTemp = parsedTemp.getChild(j);
                        }
                    }
                }
                if (!stop && newParsedTemp != null) {
                    parsedTemp = newParsedTemp;
                } else {
                    stop = true;
                }
            }
        }
    }
    // postprocess paths, now that all id vars are resolved
    if (paths != null) {
        for (EJBQLPath path : paths) {
            String id = normalizeIdPath(path.getId());
            ClassDescriptor descriptor = descriptorsById.get(id);
            if (descriptor == null) {
                throw new EJBQLException("Unmapped id variable: " + id);
            }
            StringBuilder buffer = new StringBuilder(id);
            ObjRelationship incoming = null;
            String pathRelationshipString = "";
            for (int i = 1; i < path.getChildrenCount(); i++) {
                String pathChunk = path.getChild(i).getText();
                if (pathChunk.endsWith(Entity.OUTER_JOIN_INDICATOR)) {
                    pathChunk = pathChunk.substring(0, pathChunk.length() - 1);
                }
                buffer.append('.').append(pathChunk);
                PropertyDescriptor property = descriptor.getProperty(pathChunk);
                if (property instanceof ArcProperty) {
                    incoming = ((ArcProperty) property).getRelationship();
                    descriptor = ((ArcProperty) property).getTargetDescriptor();
                    pathRelationshipString = buffer.substring(0, buffer.length());
                    descriptorsById.put(pathRelationshipString, descriptor);
                    incomingById.put(pathRelationshipString, incoming);
                }
            }
            if (pathsInSelect.size() > 0 && incoming != null && pathRelationshipString.length() > 0 && pathRelationshipString.equals(buffer.toString())) {
                EJBQLIdentifier ident = new EJBQLIdentifier(0);
                ident.text = pathRelationshipString;
                Integer integer = pathsInSelect.get(path);
                if (integer != null) {
                    resultComponents.remove(integer.intValue());
                    resultComponents.add(integer, ident);
                    rootId = pathRelationshipString;
                }
            }
        }
    }
    CompiledExpression compiled = new CompiledExpression();
    compiled.setExpression(parsed);
    compiled.setSource(source);
    compiled.setRootId(rootId);
    compiled.setDescriptorsById(descriptorsById);
    compiled.setIncomingById(incomingById);
    compiled.setPrefetchTree(prefetchTree);
    if (resultComponents != null) {
        SQLResult mapping = new SQLResult();
        for (int i = 0; i < resultComponents.size(); i++) {
            Object nextMapping = resultComponents.get(i);
            if (nextMapping instanceof String) {
                mapping.addColumnResult((String) nextMapping);
            } else if (nextMapping instanceof EJBQLExpression) {
                EntityResult compileEntityResult = compileEntityResult((EJBQLExpression) nextMapping, i);
                if (prefetchTree != null) {
                    for (PrefetchTreeNode prefetch : prefetchTree.getChildren()) {
                        if (((EJBQLExpression) nextMapping).getText().equals(prefetch.getEjbqlPathEntityId())) {
                            EJBQLIdentifier ident = new EJBQLIdentifier(0);
                            ident.text = prefetch.getEjbqlPathEntityId() + "." + prefetch.getPath();
                            compileEntityResult = compileEntityResultWithPrefetch(compileEntityResult, ident);
                        }
                    }
                }
                mapping.addEntityResult(compileEntityResult);
            }
        }
        compiled.setResult(mapping);
    }
    return compiled;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) HashMap(java.util.HashMap) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) EntityResult(org.apache.cayenne.map.EntityResult) EJBQLCompiledExpression(org.apache.cayenne.ejbql.EJBQLCompiledExpression) SQLResult(org.apache.cayenne.map.SQLResult) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

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