@Override protected Expression buildIdQualifier(Object id) { return ExpressionFactory.matchDbExp(pk.getName(), id); }
private void createDisjointByIdPrefetchQualifier(String pathPrefix, PrefetchSelectQuery currentQuery, List<DbJoin> joins, Set<List<Object>> values) { Expression allJoinsQualifier; if(currentQuery != null) { for(List<Object> joinValues : values) { allJoinsQualifier = null; for(int i=0; i<joins.size(); i++) { Expression joinQualifier = ExpressionFactory.matchDbExp(pathPrefix + joins.get(i).getTargetName(), joinValues.get(i)); if (allJoinsQualifier == null) { allJoinsQualifier = joinQualifier; } else { allJoinsQualifier = allJoinsQualifier.andExp(joinQualifier); } } currentQuery.orQualifier(allJoinsQualifier); } } }
private Map<?, ?> toMap_AttchedObject_MultiStepPath(ObjectContext context, Persistent persistent) { Iterator<CayenneMapEntry> pathComponents = Cayenne.getObjEntity(persistent).getDbEntity() .resolvePathComponents(this); LinkedList<DbRelationship> reversedPathComponents = new LinkedList<>(); while (pathComponents.hasNext()) { CayenneMapEntry component = pathComponents.next(); if (component instanceof DbRelationship) { DbRelationship rel = (DbRelationship) component; DbRelationship reverseRelationship = rel.getReverseRelationship(); if (reverseRelationship == null) { reverseRelationship = rel.createReverseRelationship(); } reversedPathComponents.addFirst(reverseRelationship); } else { break; // an attribute can only occur at the end of the path } } DbEntity finalEntity = reversedPathComponents.get(0).getSourceEntity(); StringBuilder reversedPathStr = new StringBuilder(); for (int i = 0; i < reversedPathComponents.size(); i++) { reversedPathStr.append(reversedPathComponents.get(i).getName()); if (i < reversedPathComponents.size() - 1) { reversedPathStr.append('.'); } } return ObjectSelect.dbQuery(finalEntity.getName()) .where(ExpressionFactory.matchDbExp(reversedPathStr.toString(), persistent)).selectOne(context); }
private Expression buildIdQualifer(AgEntity<?> entity, AgObjectId id) { Collection<AgAttribute> idAttributes = entity.getIds(); if (idAttributes.size() != id.size()) { throw new AgException(Response.Status.BAD_REQUEST, "Wrong ID size: expected " + idAttributes.size() + ", got: " + id.size()); } Collection<Expression> qualifiers = new ArrayList<>(); for (AgAttribute idAttribute : idAttributes) { Object idValue = id.get(idAttribute.getName()); if (idValue == null) { throw new AgException(Response.Status.BAD_REQUEST, "Failed to build a Cayenne qualifier for entity " + entity.getName() + ": one of the entity's ID parts is missing in this ID: " + idAttribute.getName()); } if (idAttribute instanceof AgPersistentAttribute) { qualifiers.add(ExpressionFactory.matchDbExp( ((AgPersistentAttribute) idAttribute).getColumnName(), idValue)); } else { // can be non-persistent attribute if assembled from @AgId by AgEntityBuilder qualifiers.add(ExpressionFactory.matchDbExp(idAttribute.getName(), idValue)); } } return ExpressionFactory.and(qualifiers); }
while (it.hasNext()) { DbAttribute attribute = (DbAttribute) it.next(); Expression attributeQualifier = ExpressionFactory.matchDbExp(attribute .getName(), qualifierSnapshot.get(attribute.getName()));
select.setQualifier(ExpressionFactory.matchDbExp(buf.toString(), source));
/** * Retrieves fresh snapshot for the failed row. Null row indicates that it was * deleted. * * @since 3.0 */ public Map<?, ?> getFreshSnapshot(ObjectContext context) { Expression qualifier = null; for (DbAttribute attribute : rootEntity.getPrimaryKeys()) { Expression attributeQualifier = ExpressionFactory.matchDbExp( attribute.getName(), qualifierSnapshot.get(attribute.getName())); qualifier = (qualifier != null) ? qualifier.andExp(attributeQualifier) : attributeQualifier; } SelectQuery<DataRow> query = new SelectQuery<DataRow>(rootEntity, qualifier); query.setFetchingDataRows(true); return (Map<?, ?>) Cayenne.objectForQuery(context, query); }
@SuppressWarnings("unchecked") public static <A> A findById(ObjectContext context, Class<A> type, AgEntity<?> agEntity, Object id) { ObjEntity entity = context.getEntityResolver().getObjEntity(type); // sanity checking... if (entity == null) { throw new AgException(Status.INTERNAL_SERVER_ERROR, "Unknown entity class: " + type); } if (id == null) { throw new AgException(Status.BAD_REQUEST, "No id specified"); } if (id instanceof Map) { Map<String, Object> ids = (Map<String, Object>) id; ObjectSelect<A> query = ObjectSelect.query(type); for (Map.Entry<String, Object> entry : ids.entrySet()) { query.and(ExpressionFactory.matchDbExp( entity.getDbEntity().getAttribute(entry.getKey()).getName(), entry.getValue() )); } return query.selectOne(context); } else { AgAttribute attribute = agEntity.getIds().iterator().next(); return ObjectSelect.query(type, new ASTEqual(attribute.getPathExp(), id)).selectOne(context); } } }
"'; one of the parent's ID parts is missing in it's ID: " + join.getTargetName()); expressions.add(ExpressionFactory.matchDbExp(join.getSourceName(), joinValue)); return ExpressionFactory.matchDbExp(objRelationship.getReverseDbRelationshipPath(), id.get());
protected Query createReplacementQuery(EntityResolver resolver) { if (objectId.isTemporary() && !objectId.isReplacementIdAttached()) { throw new CayenneRuntimeException("Can't build a query for relationship '" + relationshipName + "' for temporary id: " + objectId); } ObjRelationship relationship = getRelationship(resolver); // build executable select... Expression qualifier = ExpressionFactory.matchDbExp(relationship .getReverseDbRelationshipPath(), objectId); SelectQuery query = new SelectQuery( (ObjEntity) relationship.getTargetEntity(), qualifier); query.setRefreshingObjects(refreshing); return query; }
@Override protected Query createReplacementQuery(EntityResolver resolver) { if (objectId.isTemporary() && !objectId.isReplacementIdAttached()) { throw new CayenneRuntimeException("Can't build a query for relationship '%s' for temporary id: %s" , relationshipName, objectId); } ObjRelationship relationship = getRelationship(resolver); // build executable select... Expression qualifier = ExpressionFactory.matchDbExp(relationship .getReverseDbRelationshipPath(), objectId); SelectQuery<Object> query = new SelectQuery<Object>( (ObjEntity) relationship.getTargetEntity(), qualifier); query.setStatementFetchSize(statementFetchSize); return query; }