Skip to content

Commit

Permalink
refactor: changed query parser to use accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Oct 2, 2023
1 parent c099e21 commit feeb43e
Show file tree
Hide file tree
Showing 13 changed files with 619 additions and 607 deletions.
43 changes: 22 additions & 21 deletions core/src/main/grammar/OrientSQL.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2310,11 +2310,11 @@ OLevelZeroIdentifier LevelZeroIdentifier():
}

OSuffixIdentifier SuffixIdentifier():
{}
{OIdentifier identifier;}
{
(
LOOKAHEAD( Identifier() )
jjtThis.identifier = Identifier()
identifier =Identifier() { jjtThis.setIdentifier(identifier); }
|
LOOKAHEAD( RecordAttribute() )
jjtThis.recordAttribute = RecordAttribute()
Expand Down Expand Up @@ -2516,13 +2516,13 @@ OMathExpression ParenthesisExpression():
}

OBaseExpression BaseExpression():
{}
{OBaseIdentifier identifier;}
{
(
jjtThis.number = Number()
|
(
jjtThis.identifier = BaseIdentifier()
identifier = BaseIdentifier() { jjtThis.setIdentifier(identifier);}
[
LOOKAHEAD( Modifier() )
jjtThis.modifier = Modifier()
Expand Down Expand Up @@ -2601,6 +2601,7 @@ OFromItem FromItem():
jjtThis.inputParams = new java.util.ArrayList();
ORid lastRid;
OInputParameter lastParam;
OIdentifier identifier;
}
{
(
Expand Down Expand Up @@ -2658,7 +2659,7 @@ OFromItem FromItem():
)
|
(
jjtThis.identifier = Identifier()
identifier = Identifier() {jjtThis.setIdentifier(identifier); }
[
LOOKAHEAD( Modifier() )
jjtThis.modifier = Modifier()
Expand Down Expand Up @@ -5295,41 +5296,41 @@ OWhileBlock WhileBlock():


OSecurityResourceSegment SecurityResourceSegment():
{}
{OIdentifier identifier;}
{
(
(
<STAR> { jjtThis.star = true; }
|
jjtThis.identifier = Identifier()
identifier = Identifier() {jjtThis.setIdentifier(identifier); }
|
token = <CLUSTER> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CLUSTER> { jjtThis.setIdentifier( new OIdentifier(token.image)); }
|
token = <CREATE> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CREATE> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <DELETE> { jjtThis.identifier = new OIdentifier(token.image); }
token = <DELETE> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <INSERT> { jjtThis.identifier = new OIdentifier(token.image); }
token = <INSERT> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <SELECT> { jjtThis.identifier = new OIdentifier(token.image); }
token = <SELECT> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <LIKE> { jjtThis.identifier = new OIdentifier(token.image); }
token = <LIKE> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <VERTEX> { jjtThis.identifier = new OIdentifier(token.image); }
token = <VERTEX> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <EDGE> { jjtThis.identifier = new OIdentifier(token.image); }
token = <EDGE> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <CONTAINS> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CONTAINS> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <CONTAINSALL> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CONTAINSALL> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <CONTAINSANY> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CONTAINSANY> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <CONTAINSKEY> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CONTAINSKEY> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <CONTAINSVALUE> { jjtThis.identifier = new OIdentifier(token.image); }
token = <CONTAINSVALUE> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
|
token = <FROM> { jjtThis.identifier = new OIdentifier(token.image); }
token = <FROM> { jjtThis.setIdentifier(new OIdentifier(token.image)); }
)
[
<DOT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OBaseExpression extends OMathExpression {

protected ONumber number;

protected OBaseIdentifier identifier;
private OBaseIdentifier identifier;

protected OInputParameter inputParam;

Expand Down Expand Up @@ -383,8 +383,8 @@ public OModifier getModifier() {

public List<String> getMatchPatternInvolvedAliases() {
if (this.identifier != null && this.identifier.toString().equals("$matched")) {
if (modifier != null && modifier.suffix != null && modifier.suffix.identifier != null) {
return Collections.singletonList(modifier.suffix.identifier.toString());
if (modifier != null && modifier.suffix != null && modifier.suffix.getIdentifier() != null) {
return Collections.singletonList(modifier.suffix.getIdentifier().toString());
}
}
return Collections.emptyList();
Expand Down Expand Up @@ -499,7 +499,7 @@ public boolean isIndexChain(OCommandContext ctx, OClass clazz) {
return false;
}
if (identifier.isIndexChain(ctx, clazz)) {
OProperty prop = clazz.getProperty(identifier.getSuffix().identifier.getStringValue());
OProperty prop = clazz.getProperty(identifier.getSuffix().getIdentifier().getStringValue());
OClass linkedClass = prop.getLinkedClass();
if (linkedClass != null) {
return modifier.isIndexChain(ctx, linkedClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public boolean isCacheable() {

public boolean isIndexChain(OCommandContext ctx, OClass clazz) {
if (suffix != null && suffix.isBaseIdentifier()) {
OProperty prop = clazz.getProperty(suffix.identifier.getStringValue());
OProperty prop = clazz.getProperty(suffix.getIdentifier().getStringValue());
if (prop == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public Optional<OUpdateItem> transformToUpdateItem() {
OUpdateItem result = new OUpdateItem(-1);
result.operator = OUpdateItem.OPERATOR_EQ;
OBaseExpression baseExp = ((OBaseExpression) left.mathExpression);
result.left = baseExp.identifier.suffix.identifier.copy();
result.left = baseExp.getIdentifier().suffix.getIdentifier().copy();
result.leftModifier = baseExp.modifier == null ? null : baseExp.modifier.copy();
result.right = right.copy();
return Optional.of(result);
Expand All @@ -269,9 +269,9 @@ private boolean checkCanTransformToUpdate() {
return false;
}
OBaseExpression base = (OBaseExpression) left.mathExpression;
if (base.identifier == null
|| base.identifier.suffix == null
|| base.identifier.suffix.identifier == null) {
if (base.getIdentifier() == null
|| base.getIdentifier().suffix == null
|| base.getIdentifier().suffix.getIdentifier() == null) {
return false;
}
return true;
Expand Down Expand Up @@ -346,7 +346,7 @@ public void translateLuceneOperator() {
OExpression newLeft = new OExpression(-1);
newLeft.mathExpression = new OBaseExpression(-1);
OBaseIdentifier identifirer = new OBaseIdentifier(-1);
((OBaseExpression) newLeft.mathExpression).identifier = identifirer;
((OBaseExpression) newLeft.mathExpression).setIdentifier(identifirer);
identifirer.levelZero = new OLevelZeroIdentifier(-1);
OFunctionCall function = new OFunctionCall(-1);
identifirer.levelZero.functionCall = function;
Expand All @@ -364,31 +364,33 @@ public void translateLuceneOperator() {

private OExpression fieldNamesToStrings(OExpression left) {
if (left.isBaseIdentifier()) {
OIdentifier identifier = ((OBaseExpression) left.mathExpression).identifier.suffix.identifier;
OIdentifier identifier =
((OBaseExpression) left.mathExpression).getIdentifier().suffix.getIdentifier();
OCollection newColl = new OCollection(-1);
newColl.expressions = new ArrayList<>();
newColl.expressions.add(identifierToStringExpr(identifier));
OExpression result = new OExpression(-1);
OBaseExpression newBase = new OBaseExpression(-1);
result.mathExpression = newBase;
newBase.identifier = new OBaseIdentifier(-1);
newBase.identifier.levelZero = new OLevelZeroIdentifier(-1);
newBase.identifier.levelZero.collection = newColl;
OBaseIdentifier newIdentifier = new OBaseIdentifier(-1);
newIdentifier.levelZero = new OLevelZeroIdentifier(-1);
newIdentifier.levelZero.collection = newColl;
newBase.setIdentifier(newIdentifier);
return result;
} else if (left.mathExpression instanceof OBaseExpression) {
OBaseExpression base = (OBaseExpression) left.mathExpression;
if (base.identifier != null
&& base.identifier.levelZero != null
&& base.identifier.levelZero.collection != null) {
OCollection coll = base.identifier.levelZero.collection;
if (base.getIdentifier() != null
&& base.getIdentifier() != null
&& base.getIdentifier().levelZero.collection != null) {
OCollection coll = base.getIdentifier().levelZero.collection;

OCollection newColl = new OCollection(-1);
newColl.expressions = new ArrayList<>();

for (OExpression exp : coll.expressions) {
if (exp.isBaseIdentifier()) {
OIdentifier identifier =
((OBaseExpression) exp.mathExpression).identifier.suffix.identifier;
((OBaseExpression) exp.mathExpression).getIdentifier().suffix.getIdentifier();
OExpression val = identifierToStringExpr(identifier);
newColl.expressions.add(val);
} else {
Expand All @@ -399,9 +401,10 @@ private OExpression fieldNamesToStrings(OExpression left) {
OExpression result = new OExpression(-1);
OBaseExpression newBase = new OBaseExpression(-1);
result.mathExpression = newBase;
newBase.identifier = new OBaseIdentifier(-1);
newBase.identifier.levelZero = new OLevelZeroIdentifier(-1);
newBase.identifier.levelZero.collection = newColl;
OBaseIdentifier newIdentifier = new OBaseIdentifier(-1);
newIdentifier.levelZero = new OLevelZeroIdentifier(-1);
newIdentifier.levelZero.collection = newColl;
newBase.setIdentifier(newIdentifier);
return result;
}
}
Expand Down Expand Up @@ -452,16 +455,19 @@ public OBooleanExpression rewriteIndexChainsAsSubqueries(OCommandContext ctx, OC

result.left = new OExpression(-1);
OBaseExpression base = new OBaseExpression(-1);
base.identifier = new OBaseIdentifier(-1);
base.identifier.suffix = new OSuffixIdentifier(-1);
base.identifier.suffix.identifier =
((OBaseExpression) left.mathExpression).identifier.suffix.identifier;
OBaseIdentifier identifier = new OBaseIdentifier(-1);
identifier.suffix = new OSuffixIdentifier(-1);
identifier.suffix.setIdentifier(
((OBaseExpression) left.mathExpression).getIdentifier().suffix.getIdentifier());
base.setIdentifier(identifier);
result.left.mathExpression = base;

result.operator = new OInOperator(-1);

OClass nextClazz =
clazz.getProperty(base.identifier.suffix.identifier.getStringValue()).getLinkedClass();
clazz
.getProperty(base.getIdentifier().suffix.getIdentifier().getStringValue())
.getLinkedClass();
result.rightStatement =
indexChainToStatement(
((OBaseExpression) left.mathExpression).modifier, nextClazz, right, ctx);
Expand All @@ -487,8 +493,8 @@ public static OSelectStatement indexChainToStatement(

base.left = new OExpression(-1);
base.left.mathExpression = new OBaseExpression(-1);
((OBaseExpression) base.left.mathExpression).identifier =
new OBaseIdentifier(modifier.suffix.identifier);
((OBaseExpression) base.left.mathExpression)
.setIdentifier(new OBaseIdentifier(modifier.suffix.getIdentifier()));
((OBaseExpression) base.left.mathExpression).modifier =
modifier.next == null ? null : modifier.next.copy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,17 @@ public OBooleanExpression rewriteIndexChainsAsSubqueries(OCommandContext ctx, OC

result.left = new OExpression(-1);
OBaseExpression base = new OBaseExpression(-1);
base.identifier = new OBaseIdentifier(-1);
base.identifier.suffix = new OSuffixIdentifier(-1);
base.identifier.suffix.identifier =
((OBaseExpression) left.mathExpression).identifier.suffix.identifier;
OBaseIdentifier identifier = new OBaseIdentifier(-1);
identifier.suffix = new OSuffixIdentifier(-1);
identifier.suffix.setIdentifier(
((OBaseExpression) left.mathExpression).getIdentifier().suffix.getIdentifier());
base.setIdentifier(identifier);
result.left.mathExpression = base;

OClass nextClazz =
clazz.getProperty(base.identifier.suffix.identifier.getStringValue()).getLinkedClass();
clazz
.getProperty(base.getIdentifier().suffix.getIdentifier().getStringValue())
.getLinkedClass();
OParenthesisExpression newRight = new OParenthesisExpression(-1);
newRight.statement =
OBinaryCondition.indexChainToStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public OIdentifier getDefaultAlias() {
identifier =
new OIdentifier(
((OBaseExpression) mathExpression)
.identifier
.getIdentifier()
.getSuffix()
.identifier
.getIdentifier()
.getStringValue());
} else {
identifier = new OIdentifier(this.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public boolean isStar() {
return false;
}
OBaseExpression base = (OBaseExpression) param.mathExpression;
if (base.identifier == null || base.identifier.suffix == null) {
if (base.getIdentifier() == null || base.getIdentifier().suffix == null) {
return false;
}
return base.identifier.suffix.star;
return base.getIdentifier().suffix.star;
}

public List<OExpression> getParams() {
Expand Down Expand Up @@ -381,7 +381,7 @@ private OProjectionItem createProjection(OFunctionCall newFunct, OIdentifier ali
OBaseIdentifier l1 = new OBaseIdentifier(-1);
l1.levelZero = l0;
OBaseExpression l2 = new OBaseExpression(-1);
l2.identifier = l1;
l2.setIdentifier(l1);
OExpression l3 = new OExpression(-1);
l3.mathExpression = l2;
OProjectionItem item = new OProjectionItem(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ protected Object toParsedTree(Object value) {
OExpression result = new OExpression(-1);
OFunctionCall funct = new OFunctionCall(-1);
result.mathExpression = new OBaseExpression(-1);
((OBaseExpression) result.mathExpression).identifier = new OBaseIdentifier(-1);
((OBaseExpression) result.mathExpression).identifier.levelZero = new OLevelZeroIdentifier(-1);
((OBaseExpression) result.mathExpression).identifier.levelZero.functionCall = funct;
OBaseIdentifier identifier = new OBaseIdentifier(-1);
identifier.levelZero = new OLevelZeroIdentifier(-1);
identifier.levelZero.functionCall = funct;
((OBaseExpression) result.mathExpression).setIdentifier(identifier);
funct.name = new OIdentifier("decimal");
OExpression stringExp = new OExpression(-1);
stringExp.mathExpression = new OBaseExpression(((BigDecimal) value).toPlainString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public boolean isCacheable() {

public boolean isIndexChain(OCommandContext ctx, OClass clazz) {
if (suffix != null && suffix.isBaseIdentifier()) {
OProperty prop = clazz.getProperty(suffix.identifier.getStringValue());
OProperty prop = clazz.getProperty(suffix.getIdentifier().getStringValue());
if (prop != null
&& prop.getAllIndexes().stream()
.anyMatch(idx -> idx.getDefinition().getFields().size() == 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public void toGenericStatement(StringBuilder builder) {
}
}

public void setIdentifier(OIdentifier identifier) {
this.identifier = identifier;
}

@Override
public OSecurityResourceSegment copy() {
OSecurityResourceSegment result = new OSecurityResourceSegment(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ public OIdentifier getIdentifier() {
return identifier;
}

public void setIdentifier(OIdentifier identifier) {
this.identifier = identifier;
}

public boolean isCacheable() {
return true;
}
Expand Down
Loading

0 comments on commit feeb43e

Please sign in to comment.