use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method getResult_noChangesMade_withMacros.
@Test
public void getResult_noChangesMade_withMacros() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
builder.startNewEntity("Player");
builder.addSingleValueAttribute(Date.class, "birthDate");
builder.addReferenceAttribute("team", "Team");
JpqlEntityModel playerEntity = builder.produce();
DomainModel model = new DomainModel(playerEntity, teamEntity);
QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @between(p.birthDate, now-2, now+2, month) ");
String result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @between ( p.birthDate, now - 2, now + 2, month)", result);
transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateBefore(p.birthDate, :d) ");
result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @dateBefore ( p.birthDate, :d)", result);
transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateAfter(p.birthDate, :d) ");
result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @dateAfter ( p.birthDate, :d)", result);
transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d) ");
result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d)", result);
transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d, USER_TIMEZONE) ");
result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d, USER_TIMEZONE)", result);
transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @today(p.birthDate) ");
result = transformerAstBased.getResult();
assertEquals("SELECT p FROM Player p where @today ( p.birthDate)", result);
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryTransformationTest method prepareDomainModel.
private DomainModel prepareDomainModel() {
EntityBuilder builder = new EntityBuilder();
builder.startNewEntity("sec$GroupHierarchy");
builder.addStringAttribute("group");
builder.addStringAttribute("createdBy");
builder.addReferenceAttribute("parent", "sec$GroupHierarchy");
builder.addReferenceAttribute("other", "sec$GroupHierarchy");
builder.addCollectionReferenceAttribute("constraints", "sec$Constraint");
JpqlEntityModel groupHierarchy = builder.produce();
JpqlEntityModel constraintEntity = builder.produceImmediately("sec$Constraint");
return new DomainModel(groupHierarchy, constraintEntity);
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class EntityPointer method next.
@Override
public Pointer next(DomainModel model, String field) {
Attribute attribute = entity.getAttributeByName(field);
if (attribute == null) {
return NoPointer.instance();
}
if (!attribute.isEntityReferenceAttribute()) {
return new SimpleAttributePointer(entity, attribute);
}
String targetEntityName = attribute.getReferencedEntityName();
try {
JpqlEntityModel targetEntity = model.getEntityByName(targetEntityName);
return attribute.isCollection() ? new CollectionPointer(targetEntity) : new EntityPointer(targetEntity);
} catch (UnknownEntityNameException e) {
return NoPointer.instance();
}
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryTransformerAstBased method getReturnedEntityName.
private String getReturnedEntityName() {
if (returnedEntityName == null) {
String returnedVariableName = getQueryTransformer().getFirstReturnedVariableName();
JpqlEntityModel entity = getQueryTransformer().getRootQueryVariableContext().getEntityByVariableName(returnedVariableName);
if (entity != null) {
returnedEntityName = entity.getName();
}
}
return returnedEntityName;
}
use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel in project cuba by cuba-platform.
the class QueryTreeTransformer method getPathNodesForOrderBy.
protected List<PathNode> getPathNodesForOrderBy(PathEntityReference pathEntityReference) {
List<PathNode> pathNodes = new ArrayList<>();
String entityName = pathEntityReference.getPathStartingEntityName();
PathNode pathNode = pathEntityReference.getPathNode();
try {
JpqlEntityModel entity = model.getEntityByName(entityName);
String currentVariableName = pathNode.getEntityVariableName();
PathNode currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
for (int i = 0; i < pathNode.getChildCount(); i++) {
String fieldName = pathNode.getChild(i).toString();
Attribute entityAttribute = entity.getAttributeByName(fieldName);
if (entityAttribute.isEntityReferenceAttribute() && !entityAttribute.isEmbedded()) {
currentPathNode.addDefaultChild(fieldName);
pathNodes.add(currentPathNode);
currentVariableName = currentPathNode.asPathString('_');
currentPathNode = new PathNode(pathNode.getToken(), currentVariableName);
} else {
currentPathNode.addDefaultChild(fieldName);
}
if (entityAttribute.isEntityReferenceAttribute()) {
entityName = entityAttribute.getReferencedEntityName();
entity = model.getEntityByName(entityName);
}
}
pathNodes.add(currentPathNode);
} catch (UnknownEntityNameException e) {
throw new RuntimeException(String.format("Could not find entity by name %s", entityName), e);
}
return pathNodes;
}
Aggregations