Skip to content

Commit

Permalink
[Enhancement #138] Working on support for selection by entity identif…
Browse files Browse the repository at this point in the history
…ier in SOQL.
  • Loading branch information
ledsoft committed Apr 4, 2023
1 parent 2d1b0b6 commit cc5b4bf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
Expand Up @@ -86,10 +86,10 @@ public String getFilter() {
return operator.toFilterExpression(getAsParam(), toVariable(value));
}

public String getTriplePattern() {
StringBuilder buildTP = new StringBuilder("?x ");
public String getTriplePattern(String rootVariable) {
StringBuilder buildTP = new StringBuilder(rootVariable).append(' ');
if (isObject()) {
buildTP.append(SoqlConstants.RDF_TYPE).append(" ")
buildTP.append(SoqlConstants.RDF_TYPE).append(' ')
.append(toIri(getFirstNode())).append(TRIPLE_END);
} else {
SoqlNode pointer = getFirstNode().getChild();
Expand All @@ -106,14 +106,14 @@ public String getTriplePattern() {
param = toVariable(value);
}
}
buildTP.append(toIri(pointer)).append(" ").append(param).append(TRIPLE_END);
buildTP.append(toIri(pointer)).append(' ').append(param).append(TRIPLE_END);
while (pointer.hasNextChild()) {
SoqlNode newPointer = pointer.getChild();
buildTP.append("?").append(pointer.getValue())
.append(" ").append(toIri(newPointer)).append(" ");
buildTP.append('?').append(pointer.getValue())
.append(' ').append(toIri(newPointer)).append(' ');
buildParam.append(newPointer.getCapitalizedValue());
if (newPointer.hasNextChild()) {
buildTP.append("?").append(pointer.getChild().getValue());
buildTP.append('?').append(pointer.getChild().getValue());
} else {
if (isFilter()) {
buildTP.append(buildParam);
Expand Down
Expand Up @@ -37,10 +37,10 @@ public SoqlNode getFirstNode() {
return firstNode;
}

public String getAsValue() {
public String getAsValue(String rootVariable) {
StringBuilder buildParam = new StringBuilder("?");
if (!firstNode.hasNextChild()) {
return "?x";
return rootVariable;
}
SoqlNode pointer = firstNode.getChild();
buildParam.append(pointer.getValue());
Expand Down
Expand Up @@ -52,6 +52,8 @@ public class SoqlQueryListener implements SoqlListener {

private boolean isSelectedParamCount = false;

private String rootVariable = "?x";


public SoqlQueryListener(MetamodelImpl metamodel) {
this.metamodel = Objects.requireNonNull(metamodel);
Expand Down Expand Up @@ -153,8 +155,8 @@ public void enterObjWithAttr(SoqlParser.ObjWithAttrContext ctx) {
String owner = getOwnerFromParam(ctx);
String attribute = getAttributeFromParam(ctx);
SoqlNode firstNode = new SoqlNode(owner);
SoqlNode lastNode = new SoqlNode(firstNode, attribute);
firstNode.setChild(lastNode);
SoqlNode childNode = new SoqlNode(firstNode, attribute);
firstNode.setChild(childNode);
setIris(firstNode);
SoqlAttribute myAttr = new SoqlAttribute(firstNode);
attributes.add(myAttr);
Expand Down Expand Up @@ -413,7 +415,7 @@ public void enterOrderByParam(SoqlParser.OrderByParamContext ctx) {
}
if (!attrSet) {
SoqlAttribute myAttr = new SoqlAttribute(firstNode);
myAttr.setValue(orderParam.getAsValue());
myAttr.setValue(orderParam.getAsValue(rootVariable));
myAttr.setOrderBy(true);
attributes.add(1, myAttr);
orderParam.setAttribute(myAttr);
Expand Down Expand Up @@ -454,7 +456,7 @@ public void enterGroupByParam(SoqlParser.GroupByParamContext ctx) {
}
if (!attrSet) {
SoqlAttribute myAttr = new SoqlAttribute(firstNode);
myAttr.setValue(groupParam.getAsValue());
myAttr.setValue(groupParam.getAsValue(rootVariable));
myAttr.setGroupBy(true);
attributes.add(1, myAttr);
groupParam.setAttribute(myAttr);
Expand Down Expand Up @@ -528,7 +530,10 @@ private EntityTypeImpl<?> getEntityType(String name) {
return null;
}

private void setAllNodesIris(ManagedType<?> entityType, SoqlNode node) {
private void setAllNodesIris(EntityType<?> entityType, SoqlNode node) {
if (entityType.getIdentifier().getName().equals(node.getValue())) {
return;
}
final Attribute<?, ?> abstractAttribute = entityType.getAttribute(node.getValue());
//not implemented case of 3 or more fragments (chained SoqlNodes)
node.setIri(abstractAttribute.getIRI().toString());
Expand All @@ -537,7 +542,7 @@ private void setAllNodesIris(ManagedType<?> entityType, SoqlNode node) {
if (type.getPersistenceType() != Type.PersistenceType.ENTITY) {
return;
}
setAllNodesIris((ManagedType<?>) type, node.getChild());
setAllNodesIris((EntityType<?>) type, node.getChild());
}
}

Expand Down Expand Up @@ -578,9 +583,9 @@ private void buildQueryString() {
newQueryBuilder.append(getCountPart());
} else {
if (isSelectedParamDistinct) {
newQueryBuilder.append(" ").append(SoqlConstants.DISTINCT);
newQueryBuilder.append(' ').append(SoqlConstants.DISTINCT);
}
newQueryBuilder.append(" ?x ");
newQueryBuilder.append(' ').append(rootVariable).append(' ');
}
newQueryBuilder.append("WHERE { ");
newQueryBuilder.append(processSupremeAttributes());
Expand All @@ -591,22 +596,22 @@ private void buildQueryString() {
if (!objectOfNextOr.isEmpty()) {
newQueryBuilder.append("} ");
}
newQueryBuilder.append("}");
newQueryBuilder.append('}');
if (!groupAttributes.isEmpty()) {
newQueryBuilder.append(" ").append(buildGrouping());
newQueryBuilder.append(' ').append(buildGrouping());
}
if (!orderAttributes.isEmpty()) {
newQueryBuilder.append(" ").append(buildOrdering());
newQueryBuilder.append(' ').append(buildOrdering());
}
newQuery = newQueryBuilder.toString();
}

private StringBuilder getCountPart() {
StringBuilder countPart = new StringBuilder(" (COUNT(");
if (isSelectedParamDistinct) {
countPart.append(SoqlConstants.DISTINCT).append(" ");
countPart.append(SoqlConstants.DISTINCT).append(' ');
}
countPart.append("?x) AS ?count) ");
countPart.append(rootVariable).append(") AS ?count) ");
return countPart;
}

Expand Down Expand Up @@ -694,21 +699,21 @@ private StringBuilder processInvFilter(ArrayList<SoqlAttribute> toInvFilter) {
}

private StringBuilder processAttribute(SoqlAttribute attr) {
return new StringBuilder(attr.getTriplePattern());
return new StringBuilder(attr.getTriplePattern(rootVariable));
}

private StringBuilder buildOrdering() {
StringBuilder sb = new StringBuilder("ORDER BY");
for (SoqlOrderParameter orderParam : orderAttributes) {
sb.append(" ").append(orderParam.getOrderByPart());
sb.append(' ').append(orderParam.getOrderByPart());
}
return sb;
}

private StringBuilder buildGrouping() {
StringBuilder sb = new StringBuilder("GROUP BY");
for (SoqlGroupParameter groupParam : groupAttributes) {
sb.append(" ").append(groupParam.getGroupByPart());
sb.append(' ').append(groupParam.getGroupByPart());
}
return sb;
}
Expand Down
Expand Up @@ -180,7 +180,7 @@ public String assembleQuery() {
/**
* Generates a VALUES clause for query parameters that are set and appear in SELECT projection.
* <p>
* Note that the current implementation does not support collection-valued parameters.
* TODO Note that the current implementation does not support collection-valued parameters.
*
* @param parameters Projected parameters to output into query as VALUES clause
* @return VALUES clause, if there were any set parameters
Expand Down
Expand Up @@ -608,4 +608,12 @@ public void testParseInequalityOperatorQuery() {
holder = sut.parseQuery(javaLikeSoql);
assertEquals(expectedSparqlQuery, holder.getQuery());
}

@Test
void testParseQueryWithIdentifierVariable() {
final String soql = "SELECT p FROM Person p WHERE p.uri = :pUri";
final String expectedSparql = "SELECT ?pUri WHERE { ?pUri a <" + Vocabulary.c_Person + "> . }";
QueryHolder holder = sut.parseQuery(soql);
assertEquals(expectedSparql, holder.getQuery());
}
}

0 comments on commit cc5b4bf

Please sign in to comment.