Skip to content

Commit

Permalink
#2196 Initial transition to ANTLR tokens in fluent API implementation
Browse files Browse the repository at this point in the history
- Replaced the intermediate Tokens class by EqlSentenceBuilder.
- Temporarily commented out the old code which won't compile anymore and
  is staged for removal.
  • Loading branch information
homedirectory committed Feb 28, 2024
1 parent 1daa142 commit f320df3
Show file tree
Hide file tree
Showing 137 changed files with 2,819 additions and 2,742 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static List<String> orderDependentCalcProps(final QuerySourceInfoProvider
calcPropsOfEntityType.add(calcPropChunk.name());
}

// TODO replace by compilation of standalone expression
final Expression1 exp1 = (Expression1) new StandAloneExpressionBuilder(gen, calcPropChunk.data().expression.expressionModel()).getResult().getValue();
final TransformationContextFromStage1To2 prc = TransformationContextFromStage1To2.forCalcPropContext(querySourceInfoProvider).cloneWithAdded(source);
try {
Expand Down Expand Up @@ -198,4 +199,4 @@ private static T3<String, Boolean, Boolean> obtainFirstChunkInfo(final List<Abst
}
throw new EqlStage2ProcessingException(currentPropName, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,33 +93,33 @@ private QueryComponents1 parseTokensIntoComponents(final QueryModel<?> qryModel,

ITokensBuilder active = null;

for (final Pair<TokenCategory, Object> pair : qryModel.getTokens()) {
if (QUERY_TOKEN != pair.getKey()) {
if (active != null) {
active.add(pair.getKey(), pair.getValue());
}
} else {
switch ((QueryTokens) pair.getValue()) {
case WHERE:
active = where;
where.setChild(new ConditionBuilder(where, this));
break;
case FROM:
active = from;
break;
case YIELD:
active = select;
select.setChild(new YieldBuilder(select, this));
break;
case GROUP_BY:
active = groupBy;
groupBy.setChild(new GroupBuilder(groupBy, this));
break;
default:
break;
}
}
}
// for (final Pair<TokenCategory, Object> pair : qryModel.getTokens()) {
// if (QUERY_TOKEN != pair.getKey()) {
// if (active != null) {
// active.add(pair.getKey(), pair.getValue());
// }
// } else {
// switch ((QueryTokens) pair.getValue()) {
// case WHERE:
// active = where;
// where.setChild(new ConditionBuilder(where, this));
// break;
// case FROM:
// active = from;
// break;
// case YIELD:
// active = select;
// select.setChild(new YieldBuilder(select, this));
// break;
// case GROUP_BY:
// active = groupBy;
// groupBy.setChild(new GroupBuilder(groupBy, this));
// break;
// default:
// break;
// }
// }
// }

final IJoinNode1<? extends IJoinNode2<?>> fromModel = from.getModel();
final Conditions1 udfModel = fromModel == null ? EMPTY_CONDITIONS : generateUserDataFilteringCondition(qryModel.isFilterable(), filter, username, fromModel.mainSource());
Expand All @@ -140,43 +140,43 @@ private Conditions1 generateUserDataFilteringCondition(final boolean filterable,
return EMPTY_CONDITIONS;
}

private List<Pair<TokenCategory, Object>> linearizeTokens(final List<Pair<TokenCategory, Object>> tokens) {
final List<Pair<TokenCategory, Object>> result = new ArrayList<>();
for (final Pair<TokenCategory, Object> pair : tokens) {
if (ORDER_TOKENS == pair.getKey()) {
result.addAll(linearizeTokens(((OrderingModel) pair.getValue()).getTokens()));
} else {
result.add(pair);
}
}

return result;
}
// private List<Pair<TokenCategory, Object>> linearizeTokens(final List<Pair<TokenCategory, Object>> tokens) {
// final List<Pair<TokenCategory, Object>> result = new ArrayList<>();
// for (final Pair<TokenCategory, Object> pair : tokens) {
// if (ORDER_TOKENS == pair.getKey()) {
// result.addAll(linearizeTokens(((OrderingModel) pair.getValue()).getTokens()));
// } else {
// result.add(pair);
// }
// }
//
// return result;
// }

private OrderBys1 produceOrderBys(final OrderingModel orderModel) {
final QryOrderingsBuilder orderBy = new QryOrderingsBuilder(null, this);

if (orderModel != null) {
final List<Pair<TokenCategory, Object>> linearizedTokens = linearizeTokens(orderModel.getTokens());
for (final Iterator<Pair<TokenCategory, Object>> iterator = linearizedTokens.iterator(); iterator.hasNext();) {
final Pair<TokenCategory, Object> pair = iterator.next();
if (SORT_ORDER == pair.getKey()) {
orderBy.add(pair.getKey(), pair.getValue());
if (iterator.hasNext()) {
orderBy.setChild(new OrderByBuilder(orderBy, this));
}
} else {
if (orderBy.getChild() == null) {
orderBy.setChild(new OrderByBuilder(orderBy, this));
}
orderBy.add(pair.getKey(), pair.getValue());
}
}
// final List<Pair<TokenCategory, Object>> linearizedTokens = linearizeTokens(orderModel.getTokens());
// for (final Iterator<Pair<TokenCategory, Object>> iterator = linearizedTokens.iterator(); iterator.hasNext();) {
// final Pair<TokenCategory, Object> pair = iterator.next();
// if (SORT_ORDER == pair.getKey()) {
// orderBy.add(pair.getKey(), pair.getValue());
// if (iterator.hasNext()) {
// orderBy.setChild(new OrderByBuilder(orderBy, this));
// }
// } else {
// if (orderBy.getChild() == null) {
// orderBy.setChild(new OrderByBuilder(orderBy, this));
// }
// orderBy.add(pair.getKey(), pair.getValue());
// }
// }
}
return orderBy.getModel();
}

public Object getParamValue(final String paramName) {
return paramValues.get(paramName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public StandAloneConditionBuilder(final QueryModelToStage1Transformer queryBuild
this.negated = negated;
setChild(new ConditionBuilder(this, queryBuilder));

for (final Pair<TokenCategory, Object> tokenPair : exprModel.getTokens()) {
add(tokenPair.getKey(), tokenPair.getValue());
}
throw new UnsupportedOperationException();
// for (final Pair<TokenCategory, Object> tokenPair : exprModel.getTokens()) {
// add(tokenPair.getKey(), tokenPair.getValue());
// }
}

@Override
Expand All @@ -50,4 +51,4 @@ public Conditions1 getModel() {
public Pair<TokenCategory, Object> getResult() {
throw new EqlStage0ProcessingException("Not applicable!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public class StandAloneExpressionBuilder extends AbstractTokensBuilder {
public StandAloneExpressionBuilder(final QueryModelToStage1Transformer queryBuilder, final ExpressionModel exprModel) {
super(null, queryBuilder);

for (final Pair<TokenCategory, Object> tokenPair : exprModel.getTokens()) {
add(tokenPair.getKey(), tokenPair.getValue());
}
throw new UnsupportedOperationException();
// for (final Pair<TokenCategory, Object> tokenPair : exprModel.getTokens()) {
// add(tokenPair.getKey(), tokenPair.getValue());
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private CalcPropsDataAndPendingTails enhanceWithCalcPropsData(

for (final PropChunk calcChunk : getFirstCalcChunks(incomingTails)) {
if (!processedCalcData.containsKey(calcChunk.name())) { // consider only calc props that have not yet been processed on the previous iteration(s)
// TODO replace by compilation of standalone expression
final Expression1 exp1 = (Expression1) (new StandAloneExpressionBuilder(gen, calcChunk.data().expression.expressionModel())).getResult().getValue();
final TransformationContextFromStage1To2 prc = TransformationContextFromStage1To2.forCalcPropContext(querySourceInfoProvider).cloneWithAdded(sourceForCalcPropResolution);
final Expression2 exp2 = exp1.transform(prc);
Expand Down Expand Up @@ -229,4 +230,4 @@ public String toString() {
return this.getClass().getSimpleName() +" [links = " + links +", resolution = " + resolution + "]";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
package ua.com.fielden.platform.entity.query.fluent;

import static ua.com.fielden.platform.utils.EntityUtils.equalsEx;

import ua.com.fielden.platform.entity.query.exceptions.EqlException;
import ua.com.fielden.platform.utils.EntityUtils;

import java.util.Objects;

abstract class AbstractQueryLink {

private final Tokens tokens;
protected AbstractQueryLink(final Tokens tokens) {
if (tokens == null) {
public final EqlSentenceBuilder builder;

protected AbstractQueryLink(final EqlSentenceBuilder builder) {
if (builder == null) {
throw new EqlException("Invalid argument -- tokens should not be null.");
}

this.tokens = tokens;
this.builder = builder;
}

public EqlSentenceBuilder getBuilder() {
return builder;
}

@Override
public String toString() {
return builder.toString();
}

@Override
public int hashCode() {
return 31 * builder.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

final AbstractQueryLink that = (AbstractQueryLink) obj;
return Objects.equals(this.builder, that.builder);
}

public Tokens getTokens() {
return tokens;
}

@Override
public String toString() {
return getTokens().toString();
}

@Override
public int hashCode() {
return 31 * tokens.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

final AbstractQueryLink that = (AbstractQueryLink) obj;
return equalsEx(this.tokens, that.tokens);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@ abstract class ArithmeticalOperator<T> //
extends AbstractQueryLink //
implements IArithmeticalOperator<T> {

protected ArithmeticalOperator(final Tokens tokens) {
super(tokens);
}
protected abstract T nextForArithmeticalOperator(final Tokens tokens);
protected ArithmeticalOperator(final EqlSentenceBuilder builder) {
super(builder);
}

protected abstract T nextForArithmeticalOperator(final EqlSentenceBuilder builder);

@Override
public T add() {
return nextForArithmeticalOperator(getTokens().add());
return nextForArithmeticalOperator(builder.add());
}

@Override
public T sub() {
return nextForArithmeticalOperator(getTokens().subtract());
return nextForArithmeticalOperator(builder.subtract());
}

@Override
public T mult() {
return nextForArithmeticalOperator(getTokens().multiply());
return nextForArithmeticalOperator(builder.multiply());
}

@Override
public T div() {
return nextForArithmeticalOperator(getTokens().divide());
return nextForArithmeticalOperator(builder.divide());
}

@Override
public T mod() {
return nextForArithmeticalOperator(getTokens().modulo());
return nextForArithmeticalOperator(builder.modulo());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ abstract class CaseWhenFunctionArgument<T, ET extends AbstractEntity<?>> //
extends ExprOperand<ICaseWhenFunctionWhen<T, ET>, IExprOperand0<ICaseWhenFunctionWhen<T, ET>, ET>, ET> //
implements ICaseWhenFunctionArgument<T, ET> {

protected CaseWhenFunctionArgument(final Tokens tokens) {
super(tokens);
}
protected abstract T nextForCaseWhenFunctionArgument(final Tokens tokens);
protected CaseWhenFunctionArgument(final EqlSentenceBuilder builder) {
super(builder);
}

protected abstract T nextForCaseWhenFunctionArgument(final EqlSentenceBuilder builder);

@Override
protected IExprOperand0<ICaseWhenFunctionWhen<T, ET>, ET> nextForExprOperand(final Tokens tokens) {
return new ExprOperand0<ICaseWhenFunctionWhen<T, ET>, ET>(tokens) {
protected IExprOperand0<ICaseWhenFunctionWhen<T, ET>, ET> nextForExprOperand(final EqlSentenceBuilder builder) {
return new ExprOperand0<ICaseWhenFunctionWhen<T, ET>, ET>(builder) {

@Override
protected ICaseWhenFunctionWhen<T, ET> nextForExprOperand0(final Tokens tokens) {
return new CaseWhenFunctionWhen<T, ET>(tokens) {
protected ICaseWhenFunctionWhen<T, ET> nextForExprOperand0(final EqlSentenceBuilder builder) {
return new CaseWhenFunctionWhen<T, ET>(builder) {

@Override
protected T nextForCaseWhenFunctionEnd(final Tokens tokens) {
return CaseWhenFunctionArgument.this.nextForCaseWhenFunctionArgument(tokens);
protected T nextForCaseWhenFunctionEnd(final EqlSentenceBuilder builder) {
return CaseWhenFunctionArgument.this.nextForCaseWhenFunctionArgument(builder);
}

};
Expand All @@ -35,14 +35,15 @@ protected T nextForCaseWhenFunctionEnd(final Tokens tokens) {
}

@Override
protected ICaseWhenFunctionWhen<T, ET> nextForSingleOperand(final Tokens tokens) {
return new CaseWhenFunctionWhen<T, ET>(tokens) {
protected ICaseWhenFunctionWhen<T, ET> nextForSingleOperand(final EqlSentenceBuilder builder) {
return new CaseWhenFunctionWhen<T, ET>(builder) {

@Override
protected T nextForCaseWhenFunctionEnd(final Tokens tokens) {
return CaseWhenFunctionArgument.this.nextForCaseWhenFunctionArgument(tokens);
protected T nextForCaseWhenFunctionEnd(final EqlSentenceBuilder builder) {
return CaseWhenFunctionArgument.this.nextForCaseWhenFunctionArgument(builder);
}

};
}
}

}
Loading

0 comments on commit f320df3

Please sign in to comment.