UNPKG

@abaplint/transpiler

Version:
58 lines 2.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateDatabaseTranspiler = void 0; const abaplint = require("@abaplint/core"); const chunk_1 = require("../chunk"); const expressions_1 = require("../expressions"); class UpdateDatabaseTranspiler { transpile(node, traversal) { const dbtab = node.findFirstExpression(abaplint.Expressions.DatabaseTable); if (dbtab === undefined) { throw new Error("internal error, UpdateDatabaseTranspiler"); } const table = new expressions_1.DatabaseTableTranspiler(false).transpile(dbtab, traversal); const options = []; const cond = node.findFirstExpression(abaplint.Expressions.SQLCond); if (cond) { const ttab = traversal.traverse(cond); options.push(`"where": "` + ttab.getCode() + `"`); } const sets = node.findAllExpressions(abaplint.Expressions.SQLFieldAndValue); if (sets.length > 0) { const s = []; for (const set of sets) { s.push(new expressions_1.SQLFieldAndValueTranspiler().transpile(set, traversal).getCode()); } options.push(`"set": [${s.join(",")}]`); } const sourceExpression = node.findDirectExpression(abaplint.Expressions.SQLSource)?.findDirectExpression(abaplint.Expressions.SimpleSource3); if (sourceExpression) { const sqlSource = new expressions_1.SourceTranspiler(true).transpile(sourceExpression, traversal).getCode(); const tableName = node.findDirectExpression(abaplint.Expressions.DatabaseTable)?.concatTokens(); const tabl = traversal.findTable(tableName); if (tabl === undefined) { return new chunk_1.Chunk(`throw new Error("UpdateDatabaseTranspiler: table ${tableName} not found");`); } const keys = tabl.listKeys(traversal.reg).map(k => k.toLowerCase()); const allFields = tabl.parseType(traversal.reg).getComponents().map(c => { return c.name.toLowerCase(); }); const where = []; const set = []; for (const fieldName of allFields) { const cond = `\\"${fieldName}\\" = '\" + ${sqlSource}.${fieldName}.get() + \"'`; if (keys.includes(fieldName) === true) { where.push(cond); } else { set.push("\"" + cond + "\""); } } options.push(`"where": "` + where.join(" AND ") + `"`); options.push(`"set": [${set.join(",")}]`); } return new chunk_1.Chunk(`await abap.statements.updateDatabase(${table.getCode()}, {${options.join(", ")}});`); } } exports.UpdateDatabaseTranspiler = UpdateDatabaseTranspiler; //# sourceMappingURL=update_database.js.map