@abaplint/transpiler
Version:
58 lines • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectTranspiler = void 0;
const abaplint = require("@abaplint/core");
const select_1 = require("../statements/select");
const chunk_1 = require("../chunk");
const unique_identifier_1 = require("../unique_identifier");
const expressions_1 = require("../expressions");
class SelectTranspiler {
transpile(node, traversal) {
const ret = new chunk_1.Chunk();
const selectStatement = node.findFirstStatement(abaplint.Statements.SelectLoop);
if (selectStatement === undefined) {
throw "Structure, select loop not found";
}
const concat = selectStatement.concatTokens().toUpperCase();
const from = selectStatement.findFirstExpression(abaplint.Expressions.SQLFromSource)?.concatTokens().toUpperCase();
const sTarget = selectStatement.findFirstExpression(abaplint.Expressions.SQLTarget);
let intoName = "";
if (sTarget === undefined) {
intoName = from.toLowerCase();
}
else {
intoName = new expressions_1.SQLTargetTranspiler().transpile(sTarget, traversal).getCode();
}
// note: this implementation SELECTs everything into memory, which might be bad, and sometimes not correct
const targetName = unique_identifier_1.UniqueIdentifier.get();
const loopName = unique_identifier_1.UniqueIdentifier.get();
ret.appendString(`let ${targetName} = new abap.types.Table(abap.DDIC["${from}"].type());\n`);
ret.appendChunk(new select_1.SelectTranspiler().transpile(selectStatement, traversal, targetName));
// todo: optimize, it should do real streaming?
const packageSize = node.findFirstExpression(abaplint.Expressions.SelectLoop)?.findExpressionAfterToken("SIZE");
if (packageSize) {
const getSize = new expressions_1.SQLSourceTranspiler().transpile(packageSize, traversal).getCode() + ".get()";
ret.appendString(`if (${targetName}.array().length > ${getSize}) {
throw new Error("PACKAGE SIZED loop larger than package size not supported");
};
abap.statements.append({source: ${targetName}, target: ${intoName}, lines: true});
{\n`);
}
else if (concat.includes(" INTO CORRESPONDING FIELDS OF ")) {
ret.appendString(`\nfor (const ${loopName} of ${targetName}.array()) {\n`);
ret.appendString(`abap.statements.moveCorresponding(${loopName}, ${intoName});\n`);
}
else {
ret.appendString(`\nfor (const ${loopName} of ${targetName}.array()) {\n`);
ret.appendString(`${intoName}.set(${loopName});\n`);
}
const body = node.findDirectStructure(abaplint.Structures.Body);
if (body) {
ret.appendChunk(traversal.traverse(body));
}
ret.appendString("}\n");
return ret;
}
}
exports.SelectTranspiler = SelectTranspiler;
//# sourceMappingURL=select.js.map