All files / src/abap/2_statements/statements merge_database.ts

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

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x  
import {IStatement} from "./_statement";
import {seq, optPrio, alt, altPrio, tok, ver, verNotLang, starPrio} from "../combi";
import {WParenLeftW, WParenRightW, WAt} from "../../1_lexer/tokens";
import {LanguageVersion, Release} from "../../../version";
import {DatabaseTable, SQLAsName, SQLCond, SQLField, SQLFieldName, Dynamic, SQLPrivilegedAccess, SQLClient} from "../expressions";
import {IStatementRunnable} from "../statement_runnable";
import {buildSelectCore} from "../expressions/_select_core";
 
export class MergeDatabase implements IStatement {
 
  public getMatcher(): IStatementRunnable {
    const targetSpec = seq(DatabaseTable, optPrio(seq("AS", SQLAsName)));
 
    const subSelect = seq(tok(WParenLeftW), "SELECT", buildSelectCore(false, false), tok(WParenRightW),
                          optPrio(seq("AS", SQLAsName)));
    const itabSource = seq(tok(WAt), SQLAsName, "AS", SQLAsName,
                           optPrio(seq("DECLARE CLIENT", SQLFieldName)));
    const tableSource = seq(DatabaseTable, optPrio(seq("AS", SQLAsName)));
    const usingClause = seq("USING", altPrio(subSelect, itabSource, tableSource));
 
    const onClause = seq("ON", SQLCond);
 
    const setEntry = seq(SQLFieldName, "=", SQLField);
    const setList = seq(setEntry, starPrio(seq(",", setEntry)));
    const matchedAction = altPrio(
      "DELETE",
      seq("UPDATE", altPrio("ALL", seq("SET", setList))),
    );
 
    const whenMatched = seq("WHEN MATCHED",
                            optPrio(seq("AND", SQLCond)),
                            "THEN",
                            matchedAction);
 
    const insertCols = seq(tok(WParenLeftW), SQLFieldName, starPrio(seq(",", SQLFieldName)), tok(WParenRightW));
    const insertValues = seq(tok(WParenLeftW), SQLField, starPrio(seq(",", SQLField)), tok(WParenRightW));
    const notMatchedAction = seq("INSERT",
                                 altPrio("ALL", seq(insertCols, "VALUES", insertValues)));
 
    const whenNotMatched = seq("WHEN NOT MATCHED",
                               optPrio(seq("AND", SQLCond)),
                               "THEN",
                               notMatchedAction);
 
    const whenClauses = altPrio(
      seq(whenMatched, optPrio(whenNotMatched)),
      whenNotMatched,
    );
 
    const optionsClause = seq("OPTIONS",
                              altPrio(SQLPrivilegedAccess, SQLClient,
                                      seq(SQLClient, SQLPrivilegedAccess),
                                      seq(SQLPrivilegedAccess, SQLClient)));
    const trailingOptions = optPrio(optionsClause);
 
    const staticForm = seq("INTO", targetSpec,
                           optPrio(usingClause),
                           optPrio(onClause),
                           optPrio(whenClauses),
                           trailingOptions);
 
    const ret = ver(Release.v917, seq("MERGE", alt(staticForm, Dynamic)));
    return verNotLang(LanguageVersion.KeyUser, ret);
  }
 
}