use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class OptimisticLockException method getFreshSnapshot.
/**
* 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);
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class PrefetchProcessorJointNode method buildRowMapping.
/**
* Configures row columns mapping for this node entity.
*/
private void buildRowMapping() {
final Map<String, ColumnDescriptor> targetSource = new TreeMap<>();
// build a DB path .. find parent node that terminates the joint group...
PrefetchTreeNode jointRoot = this;
while (jointRoot.getParent() != null && !jointRoot.isDisjointPrefetch() && !jointRoot.isDisjointByIdPrefetch()) {
jointRoot = jointRoot.getParent();
}
final String prefix;
if (jointRoot != this) {
Expression objectPath = ExpressionFactory.exp(getPath(jointRoot));
ASTPath translated = (ASTPath) ((PrefetchProcessorNode) jointRoot).getResolver().getEntity().translateToDbPath(objectPath);
// make sure we do not include "db:" prefix
prefix = translated.getOperand(0) + ".";
} else {
prefix = "";
}
if (getParent() != null && !getParent().isPhantom() && getIncoming() != null && !getIncoming().getRelationship().isFlattened()) {
DbRelationship r = getIncoming().getRelationship().getDbRelationships().get(0);
for (final DbJoin join : r.getJoins()) {
appendColumn(targetSource, join.getTargetName(), prefix + join.getTargetName());
}
}
ClassDescriptor descriptor = resolver.getDescriptor();
descriptor.visitAllProperties(new PropertyVisitor() {
public boolean visitAttribute(AttributeProperty property) {
String target = property.getAttribute().getDbAttributePath();
appendColumn(targetSource, target, prefix + target);
return true;
}
public boolean visitToMany(ToManyProperty property) {
return visitRelationship(property);
}
public boolean visitToOne(ToOneProperty property) {
return visitRelationship(property);
}
private boolean visitRelationship(ArcProperty arc) {
DbRelationship dbRel = arc.getRelationship().getDbRelationships().get(0);
for (DbAttribute attribute : dbRel.getSourceAttributes()) {
String target = attribute.getName();
appendColumn(targetSource, target, prefix + target);
}
return true;
}
});
// append id columns ... (some may have been appended already via relationships)
for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
appendColumn(targetSource, pkName, prefix + pkName);
}
// append inheritance discriminator columns...
for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
String target = column.getDbAttributePath();
appendColumn(targetSource, target, prefix + target);
}
int size = targetSource.size();
this.rowCapacity = (int) Math.ceil(size / 0.75);
this.columns = new ColumnDescriptor[size];
targetSource.values().toArray(columns);
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class EJBQLIdentifierColumnsTranslator method addPrefetchedColumnsIfAny.
private void addPrefetchedColumnsIfAny(final String visitedIdentifier) {
PrefetchTreeNode prefetchTree = context.getCompiledExpression().getPrefetchTree();
if (prefetchTree != null) {
for (PrefetchTreeNode prefetch : prefetchTree.adjacentJointNodes()) {
ClassDescriptor descriptor = context.getEntityDescriptor(prefetch.getEjbqlPathEntityId());
if (visitedIdentifier.equals(prefetch.getEjbqlPathEntityId())) {
DbEntity table = descriptor.getRootDbEntities().iterator().next();
ObjEntity objectEntity = descriptor.getEntity();
prefetch.setEntityName(objectEntity.getName());
Expression prefetchExp = ExpressionFactory.exp(prefetch.getPath());
Expression dbPrefetch = objectEntity.translateToDbPath(prefetchExp);
DbRelationship r = null;
for (PathComponent<DbAttribute, DbRelationship> component : table.resolvePath(dbPrefetch, context.getMetadata().getPathSplitAliases())) {
r = component.getRelationship();
}
if (r == null) {
throw new CayenneRuntimeException("Invalid joint prefetch '%s' for entity: %s", prefetch, objectEntity.getName());
}
for (DbAttribute attribute : r.getTargetEntity().getAttributes()) {
appendColumn(prefetch.getEjbqlPathEntityId() + "." + prefetch.getPath(), attribute, "", prefetch.getPath() + "." + attribute.getName(), null);
}
}
}
}
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class EJBQLJoinAppender method appendTable.
public String appendTable(EJBQLTableId id) {
DbEntity dbEntity = id.getDbEntity(context);
String tableName = context.getQuotingStrategy().quotedFullyQualifiedName(dbEntity);
String alias;
if (context.isUsingAliases()) {
// TODO: andrus 1/5/2007 - if the same table is joined more than once, this
// will create an incorrect alias.
alias = context.getTableAlias(id.getEntityId(), tableName);
// not using "AS" to separate table name and alias name - OpenBase doesn't
// support "AS", and the rest of the databases do not care
context.append(' ').append(tableName).append(' ').append(alias);
generateJoinsForFlattenedAttributes(id);
} else {
context.append(' ').append(tableName);
alias = tableName;
}
// append inheritance qualifier...
if (id.isPrimaryTable()) {
Expression qualifier = context.getEntityDescriptor(id.getEntityId()).getEntityQualifier();
if (qualifier != null) {
EJBQLExpression ejbqlQualifier = ejbqlQualifierForEntityAndSubclasses(qualifier, id.getEntityId());
context.pushMarker(context.makeWhereMarker(), true);
context.append(" WHERE");
context.popMarker();
context.pushMarker(context.makeEntityQualifierMarker(), false);
ejbqlQualifier.visit(context.getTranslatorFactory().getConditionTranslator(context));
context.popMarker();
}
}
return alias;
}
use of org.apache.cayenne.exp.Expression in project cayenne by apache.
the class DataMapUtils method getParameterNames.
private Map<String, String> getParameterNames(Expression expression, Object root) {
if (expression != null) {
Map<String, String> types = new HashMap<>();
String typeName = "";
List<String> names = new LinkedList<>();
for (int i = 0; i < expression.getOperandCount(); i++) {
Object operand = expression.getOperand(i);
if (operand instanceof Expression) {
types.putAll(getParameterNames((Expression) operand, root));
}
if (operand instanceof ASTObjPath) {
PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
ObjAttribute attribute = component.getAttribute();
if (attribute != null) {
typeName = attribute.getType();
} else {
ObjRelationship relationship = component.getRelationship();
if (relationship != null) {
typeName = relationship.getTargetEntity().getClassName();
} else {
typeName = "Object";
}
}
}
if (operand instanceof ASTList) {
Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
for (Object value : values) {
if (value instanceof ExpressionParameter) {
names.add(((ExpressionParameter) value).getName());
}
}
}
if (operand instanceof ExpressionParameter) {
names.add(((ExpressionParameter) operand).getName());
}
}
for (String name : names) {
types.put(Util.underscoredToJava(name, false), typeName);
}
return types;
}
return Collections.emptyMap();
}
Aggregations