Skip to content

Commit

Permalink
Replace param with simplePath
Browse files Browse the repository at this point in the history
  • Loading branch information
luxbe committed Dec 19, 2024
1 parent 2328b6c commit 7e302bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 113 deletions.
34 changes: 11 additions & 23 deletions jopa-impl/src/main/antlr4/cz/cvut/kbss/jopa/query/soql/Soql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@ selectClause: SELECT (DISTINCT)? selectItem (',' selectItem)* ;

selectItem: selectExpression;

selectExpression: param | count ;
selectExpression: simplePath | aggregateExpression ;

param: objWithAttr | objWithOutAttr ;

objWithAttr: object DOT attribute;

objWithOutAttr: object ;

object: IDENTIFICATION_VARIABLE ;

count: COUNT '(' param ')' ;

attribute: IDENTIFICATION_VARIABLE ;

joinedParams: object DOT attribute (DOT attribute)+ ;
aggregateExpression: COUNT '(' (DISTINCT)? simplePath ')';

fromClause: FROM entityName identificationVariable;

Expand Down Expand Up @@ -62,7 +50,7 @@ simpleConditionalExpression
;

inExpression
: whereClauseParam (NOT)? IN '('? (inItem (',' inItem)*) ')'?
: simplePath (NOT)? IN '('? (inItem (',' inItem)*) ')'?
;

inItem
Expand All @@ -79,21 +67,19 @@ likeExpression
;

memberOfExpression
: inItem (NOT)? MEMBEROF whereClauseParam
: inItem (NOT)? MEMBER OF simplePath
;

comparisonExpression
: stringExpression COMPARISON_OPERATOR stringExpression
| simpleArithmeticExpression COMPARISON_OPERATOR simpleArithmeticExpression
| whereClauseParam COMPARISON_OPERATOR ( whereClauseParam | whereClauseValue )
| simplePath COMPARISON_OPERATOR ( simplePath | whereClauseValue )
;

whereClauseValue: (QMARK TEXT QMARK) | inputParameter ;

whereClauseParam: param | joinedParams ;

stringExpression
: whereClauseParam
: simplePath
| inputParameter
| functionsReturningStrings
;
Expand All @@ -103,7 +89,7 @@ functionsReturningStrings
| 'SUBSTRING' '(' stringExpression ',' simpleArithmeticExpression ',' simpleArithmeticExpression ')'
| 'LOWER' '(' stringExpression ')'
| 'UPPER' '(' stringExpression ')'
| 'LANG' '(' whereClauseParam ')'
| 'LANG' '(' simplePath ')'
;

simpleArithmeticExpression
Expand All @@ -119,7 +105,7 @@ arithmeticFactor
;

arithmeticPrimary
: param
: simplePath
| literal
| '(' simpleArithmeticExpression ')'
| inputParameter
Expand Down Expand Up @@ -161,6 +147,8 @@ OR: 'OR' ;

BY: 'BY' ;

OF: 'OF' ;

ORDER: 'ORDER' ;

GROUP: 'GROUP' ;
Expand All @@ -177,7 +165,7 @@ LIKE: 'LIKE' ;

IN: 'IN' ;

MEMBEROF: 'MEMBER OF' ;
MEMBER: 'MEMBER' ;

COMPARISON_OPERATOR: '>' | '<' | '>=' | '<=' | '=' | '<>' | '!=' ;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,58 @@ public void exitObjectPathExpression(SoqlParser.ObjectPathExpressionContext ctx)

@Override
public void enterSimplePath(SoqlParser.SimplePathContext ctx) {
if(ctx.simplePath() == null) {
return;
}

if(ctx.getChildCount() == 1) {
return;
}

// node was already processed by parent
if(ctx.getParent() instanceof SoqlParser.SimplePathContext) {
return;
}

SoqlNode owner = linkSimplePath(ctx);

// don't add top level references multiple times
if(!owner.hasChild() && objectTypes.containsKey(owner.getValue())) {
return;
}

SoqlAttribute newAttr = new SoqlAttribute(owner);
if (owner.hasChild() && isIdentifier(owner, owner.getChild())) {
this.isInObjectIdentifierExpression = true;
if (projectedVariable.equals(owner.getValue()) && currentPointerIsNotAttributeReference()) {
attrPointer.setProjected(true);
} else {
newAttr.setProjected(true);
pushNewAttribute(newAttr);
}
} else {
pushNewAttribute(newAttr);
}
}

private SoqlNode linkSimplePath(ParserRuleContext ctx) {
AttributeNode firstNode = new AttributeNode(getOwnerFromParam(ctx));
AttributeNode currentNode = firstNode;

while (ctx.getChildCount() == 3) {
ctx = (ParserRuleContext) ctx.getChild(2);
SoqlNode prevNode = currentNode;
currentNode = new AttributeNode(prevNode, ctx.getChild(0).getText());
prevNode.setChild(currentNode);
}
setIris(firstNode);
if (currentNode.getIri().isEmpty()) {
this.isInObjectIdentifierExpression = true;
if (projectedVariable != null && projectedVariable.equals(firstNode.getValue()) && currentPointerIsNotAttributeReference()) {
attrPointer.setProjected(true);
}
}
return firstNode;
}

@Override
Expand All @@ -146,7 +197,6 @@ public void enterObjectField(SoqlParser.ObjectFieldContext ctx) {

@Override
public void exitObjectField(SoqlParser.ObjectFieldContext ctx) {

}

@Override
Expand Down Expand Up @@ -178,21 +228,6 @@ public void enterSelectExpression(SoqlParser.SelectExpressionContext ctx) {

}

@Override
public void enterParam(SoqlParser.ParamContext ctx) {
}

@Override
public void exitParam(SoqlParser.ParamContext ctx) {
}

@Override
public void enterJoinedParams(SoqlParser.JoinedParamsContext ctx) {
SoqlNode firstNode = linkContextNodes(ctx);
SoqlAttribute myAttr = new SoqlAttribute(firstNode);
pushNewAttribute(myAttr);
}

private void pushNewAttribute(SoqlAttribute myAttr) {
attributes.add(myAttr);
this.attrPointer = myAttr;
Expand All @@ -202,10 +237,6 @@ private void popAttribute() {
this.attrPointer = attributes.remove(attributes.size() - 1);
}

@Override
public void exitJoinedParams(SoqlParser.JoinedParamsContext ctx) {
}

@Override
public void exitSelectExpression(SoqlParser.SelectExpressionContext ctx) {
if (!isSelectedParamCount) {
Expand All @@ -214,44 +245,22 @@ public void exitSelectExpression(SoqlParser.SelectExpressionContext ctx) {
}

@Override
public void enterCount(SoqlParser.CountContext ctx) {
isSelectedParamCount = true;
}
public void enterAggregateExpression(SoqlParser.AggregateExpressionContext ctx) {
if(ctx.COUNT() != null) {
isSelectedParamCount = true;
if(ctx.DISTINCT() != null) {
isSelectedParamDistinct = true;
}

@Override
public void exitCount(SoqlParser.CountContext ctx) {
this.projectedVariable = ctx.getChild(2).getText();
if(ctx.simplePath() != null) {
this.projectedVariable = ctx.simplePath().getText();
}
}
}

@Override
public void enterObject(SoqlParser.ObjectContext ctx) {
}
public void exitAggregateExpression(SoqlParser.AggregateExpressionContext ctx) {

@Override
public void exitObject(SoqlParser.ObjectContext ctx) {
}

@Override
public void enterObjWithAttr(SoqlParser.ObjWithAttrContext ctx) {
String owner = getOwnerFromParam(ctx);
String attribute = getAttributeFromParam(ctx);
// objectNode.attributeNode
SoqlNode objectNode = new AttributeNode(owner);
SoqlNode attributeNode = new AttributeNode(objectNode, attribute);
objectNode.setChild(attributeNode);
setIris(objectNode);
SoqlAttribute newAttr = new SoqlAttribute(objectNode);
if (isIdentifier(objectNode, attributeNode)) {
this.isInObjectIdentifierExpression = true;
if (projectedVariable.equals(objectNode.getValue()) && currentPointerIsNotAttributeReference()) {
attrPointer.setProjected(true);
} else {
newAttr.setProjected(true);
pushNewAttribute(newAttr);
}
} else {
pushNewAttribute(newAttr);
}
}

private boolean isIdentifier(SoqlNode objectNode, SoqlNode attributeNode) {
Expand All @@ -270,26 +279,6 @@ private boolean currentPointerIsNotAttributeReference() {
return !attrPointer.getFirstNode().hasChild();
}

@Override
public void exitObjWithAttr(SoqlParser.ObjWithAttrContext ctx) {
}

@Override
public void enterObjWithOutAttr(SoqlParser.ObjWithOutAttrContext ctx) {
}

@Override
public void exitObjWithOutAttr(SoqlParser.ObjWithOutAttrContext ctx) {
}

@Override
public void enterAttribute(SoqlParser.AttributeContext ctx) {
}

@Override
public void exitAttribute(SoqlParser.AttributeContext ctx) {
}

@Override
public void enterConditionalExpression(SoqlParser.ConditionalExpressionContext ctx) {
}
Expand Down Expand Up @@ -340,7 +329,7 @@ public void exitInExpression(SoqlParser.InExpressionContext ctx) {
pushNewAttribute(createSyntheticAttributeForEntityId());
}
final ParseTree value = resolveInExpressionValue(ctx);
if (ctx.getChild(1).getText().equals(SoqlConstants.NOT)) {
if (ctx.NOT() != null) {
attrPointer.setOperator(InOperator.notIn());
} else {
attrPointer.setOperator(InOperator.in());
Expand All @@ -350,17 +339,16 @@ public void exitInExpression(SoqlParser.InExpressionContext ctx) {
}

private SoqlAttribute createSyntheticAttributeForEntityId() {
return new SoqlAttribute(
attrPointer.getFirstNode().hasChild() ? attrPointer.getFirstNode().getChild() :
new AttributeNode(rootVariable.substring(1)));
if(attrPointer.getFirstNode().hasChild()) {
attrPointer.getFirstNode().getChild().setChild(null);
return new SoqlAttribute(attrPointer.getFirstNode().getChild());
}

return new SoqlAttribute(new AttributeNode(rootVariable.substring(1)));
}

private ParseTree resolveInExpressionValue(SoqlParser.InExpressionContext ctx) {
final ParseTree lastToken = ctx.getChild(ctx.getChildCount() - 1);
if (")".equals(lastToken.getText())) {
return ctx.getChild(ctx.getChildCount() - 2);
}
return lastToken;
return ctx.inItem().get(ctx.inItem().size() - 1);
}

@Override
Expand Down Expand Up @@ -505,14 +493,6 @@ public void enterWhereClauseValue(SoqlParser.WhereClauseValueContext ctx) {
public void exitWhereClauseValue(SoqlParser.WhereClauseValueContext ctx) {
}

@Override
public void enterWhereClauseParam(SoqlParser.WhereClauseParamContext ctx) {
}

@Override
public void exitWhereClauseParam(SoqlParser.WhereClauseParamContext ctx) {
}

@Override
public void enterStringExpression(SoqlParser.StringExpressionContext ctx) {

Expand Down

0 comments on commit 7e302bf

Please sign in to comment.