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;
}
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());
}
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);
}
Aggregations