Search in sources :

Example 6 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class StringIdQuery method getIdQueriesByEntity.

protected Map<String, ObjectSelect> getIdQueriesByEntity(EntityResolver resolver) {
    if (this.idQueriesByEntity == null) {
        Map<String, ObjectSelect> idQueriesByEntity = new HashMap<>();
        Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
        for (String id : stringIds) {
            String entityName = EntityIdCoder.getEntityName(id);
            EntityIdCoder coder = codersByEntity.get(entityName);
            ObjectSelect query;
            if (coder == null) {
                coder = new EntityIdCoder(resolver.getObjEntity(entityName));
                query = ObjectSelect.query(Object.class, entityName);
                codersByEntity.put(entityName, coder);
                idQueriesByEntity.put(entityName, query);
            } else {
                query = idQueriesByEntity.get(entityName);
            }
            Expression idExp = ExpressionFactory.matchAllDbExp(coder.toObjectId(id).getIdSnapshot(), Expression.EQUAL_TO);
            query.or(idExp);
        }
        this.idQueriesByEntity = idQueriesByEntity;
    }
    return this.idQueriesByEntity;
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.cayenne.exp.Expression) ObjectSelect(org.apache.cayenne.query.ObjectSelect)

Example 7 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class DefaultSelectTranslatorIT method testCreateSqlString11.

@Test
public void testCreateSqlString11() throws Exception {
    // query with joint prefetches and other joins
    ObjectSelect q = ObjectSelect.query(Artist.class).where(Artist.PAINTING_ARRAY.dot(Painting.PAINTING_TITLE).eq("a")).prefetch(Artist.PAINTING_ARRAY.joint());
    SelectTranslator transl = new DefaultSelectTranslator(q, dataNode.getAdapter(), dataNode.getEntityResolver());
    transl.getSql();
    // assert we only have one join
    assertTrue(transl.hasJoins());
}
Also used : ObjectSelect(org.apache.cayenne.query.ObjectSelect) Test(org.junit.Test)

Example 8 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class ValueForNullIT method test.

@Test
public void test() throws Exception {
    DbEntity dbEntity = map.getDbEntity("PAINTING");
    assertNotNull(dbEntity);
    ObjEntity objEntity = map.getObjEntity("Painting");
    assertNotNull(objEntity);
    // insert some rows before adding "not null" column
    final int nrows = 10;
    for (int i = 0; i < nrows; i++) {
        DataObject o = (DataObject) context.newObject("Painting");
        o.writeProperty("paintingTitle", "ptitle" + i);
    }
    context.commitChanges();
    // create and add new column to model and db
    DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
    column.setMandatory(false);
    column.setMaxLength(10);
    dbEntity.addAttribute(column);
    assertTrue(dbEntity.getAttributes().contains(column));
    assertEquals(column, dbEntity.getAttribute(column.getName()));
    assertTokensAndExecute(1, 0);
    // need obj attr to be able to query
    ObjAttribute objAttr = new ObjAttribute("newcol2");
    objAttr.setDbAttributePath(column.getName());
    objEntity.addAttribute(objAttr);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // set not null
    column.setMandatory(true);
    // merge to db
    assertTokensAndExecute(2, 0);
    // check that is was merged
    assertTokensAndExecute(0, 0);
    // check values for null
    Expression qual = ExpressionFactory.matchExp(objAttr.getName(), DEFAULT_VALUE_STRING);
    ObjectSelect query = ObjectSelect.query(Painting.class).where(qual);
    @SuppressWarnings("unchecked") List<Persistent> rows = context.performQuery(query);
    assertEquals(nrows, rows.size());
    // clean up
    dbEntity.removeAttribute(column.getName());
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Expression(org.apache.cayenne.exp.Expression) DbAttribute(org.apache.cayenne.map.DbAttribute) ObjectSelect(org.apache.cayenne.query.ObjectSelect) Persistent(org.apache.cayenne.Persistent) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjectSelect (org.apache.cayenne.query.ObjectSelect)8 Test (org.junit.Test)6 Expression (org.apache.cayenne.exp.Expression)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataChannelQueryFilter (org.apache.cayenne.DataChannelQueryFilter)2 DataChannelQueryFilterChain (org.apache.cayenne.DataChannelQueryFilterChain)2 DataChannelSyncFilter (org.apache.cayenne.DataChannelSyncFilter)2 DataChannelSyncFilterChain (org.apache.cayenne.DataChannelSyncFilterChain)2 DataObject (org.apache.cayenne.DataObject)2 ObjectContext (org.apache.cayenne.ObjectContext)2 QueryResponse (org.apache.cayenne.QueryResponse)2 PostPersist (org.apache.cayenne.annotation.PostPersist)2 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)2 Inject (org.apache.cayenne.di.Inject)2 GraphDiff (org.apache.cayenne.graph.GraphDiff)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 Query (org.apache.cayenne.query.Query)2 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)2