All files / src/abap/2_statements/expressions sql_compare.ts

100% Statements 51/51
100% Branches 1/1
100% Functions 1/1
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x  
import {ver, seq, optPrio, altPrio, Expression, plusPrio, tok, AlsoIn} from "../combi";
import {SQLSource, SQLFieldName, SQLAliasField, Dynamic, SQLIn, SQLCompareOperator, SQLFunction, SQLAggregation, SQLCase, Source, SimpleSource3, SQLPathForColumn, ConstantString} from ".";
import {Release} from "../../../version";
import {IStatementRunnable} from "../statement_runnable";
import {DashW, ParenLeftW, WAt, WDash, WDashW, WParenLeftW, WParenRightW} from "../../1_lexer/tokens";
import {SQLSetOpGroup} from "./sql_set_op_group";
import {buildSelectCore} from "./_select_core";
import {SQLLikeRegexpr} from "./sql_like_regexpr";
 
export class SQLCompare extends Expression {
  public getRunnable(): IStatementRunnable {
    const subSelect = ver(Release.v750, SQLSetOpGroup, {also: AlsoIn.OpenABAP});
    const simpleSubSelect = seq("(", "SELECT", buildSelectCore(undefined, false), ")");
    const simpleScalarSubSelect = ver(Release.v740sp08, simpleSubSelect, {also: AlsoIn.OpenABAP});
 
    const between = seq("BETWEEN", SQLSource, "AND", SQLSource);
 
    const like = seq("LIKE", SQLSource, optPrio(seq("ESCAPE", SQLSource)));
 
    const nul = seq("IS", optPrio("NOT"), altPrio("NULL", ver(Release.v753, "INITIAL")));
 
    const sub = seq(optPrio(altPrio("ALL", "ANY", "SOME")), altPrio(subSelect, simpleScalarSubSelect));
 
    const source = new SQLSource();
 
    const paren = seq(tok(ParenLeftW), Source, tok(WParenRightW));
    const at = ver(Release.v740sp05, seq(tok(WAt), altPrio(SimpleSource3, paren)), {also: AlsoIn.OpenABAP});
 
    // a dash without whitespace on either side is a component selector, eg. "host-comp"
    const minus = altPrio(tok(WDashW), tok(WDash), tok(DashW));
    const arithmetic = seq(altPrio("+", minus, "*", "/"), SQLFieldName);
    const arith = ver(Release.v750, plusPrio(arithmetic), {also: AlsoIn.OpenABAP});
    // the left hand side can also concatenate with &&, the right hand side can not
    const concat = seq("&&", altPrio(SQLFieldName, ConstantString, at));
    const arithLeft = ver(Release.v750, plusPrio(altPrio(arithmetic, concat)), {also: AlsoIn.OpenABAP});
 
    const lhs = altPrio(SQLCase, SQLAggregation, SQLFunction, seq(ConstantString, optPrio(arithLeft)),
                        seq(altPrio(SQLPathForColumn, SQLAliasField, SQLFieldName), optPrio(arithLeft)), seq(at, optPrio(arithLeft)));
    // SQL expressions on the right side of a WHERE comparison, from 7.55, "param EQ ( CASE ... END )"
    const parenCase = ver(Release.v755, seq(tok(WParenLeftW), SQLCase, tok(WParenRightW)), {also: AlsoIn.OpenABAP});
    const rhs = altPrio(seq(SQLCompareOperator, altPrio(sub, SQLCase, parenCase, SQLAggregation, SQLFunction, seq(source, optPrio(arith)))),
                        seq(optPrio("NOT"), altPrio(SQLIn, like, between)),
                        SQLLikeRegexpr,
                        nul);
    const rett = seq(lhs, rhs);
 
    const exists = seq("EXISTS", altPrio(subSelect, simpleSubSelect));
 
    return altPrio(exists, Dynamic, rett);
  }
}