@abaplint/core
Version:
abaplint - Core API
200 lines • 8.24 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataDefinition = void 0;
const nodes_1 = require("../abap/nodes");
const cds_determine_types_1 = require("../cds/cds_determine_types");
const cds_parser_1 = require("../cds/cds_parser");
const expressions_1 = require("../cds/expressions");
const _abstract_object_1 = require("./_abstract_object");
class DataDefinition extends _abstract_object_1.AbstractObject {
constructor() {
super(...arguments);
this.parserError = undefined;
this.parsedData = undefined;
}
getType() {
return "DDLS";
}
getAllowedNaming() {
return {
maxLength: 40,
allowNamespace: true,
};
}
getSQLViewName() {
var _a;
this.parse();
return (_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.sqlViewName;
}
getDefinitionName() {
var _a;
this.parse();
return (_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.definitionName;
}
getDescription() {
var _a;
this.parse();
return (_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.description;
}
parseType(reg) {
this.parse();
return new cds_determine_types_1.CDSDetermineTypes().parseType(reg, this.parsedData, this.getName());
}
getParsedData() {
return this.parsedData;
}
listSources() {
var _a;
this.parse();
return (_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.sources;
}
setDirty() {
this.parsedData = undefined;
this.parserError = undefined;
super.setDirty();
}
findSourceFile() {
return this.getFiles().find(f => f.getFilename().endsWith(".asddls") || f.getFilename().endsWith(".acds"));
}
hasParserError() {
return this.parserError;
}
listKeys() {
var _a;
this.parse();
const ret = [];
for (const field of ((_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.fields) || []) {
if (field.key === true) {
ret.push(field.name);
}
}
return ret;
}
parse() {
var _a, _b;
if (this.isDirty() === false) {
return { updated: false, runtime: 0 };
}
const start = Date.now();
this.parsedData = {
sqlViewName: undefined,
definitionName: undefined,
description: this.findDescription(),
fields: [],
sources: [],
relations: [],
associations: [],
tree: undefined,
};
this.findSQLViewName();
this.parsedData.tree = new cds_parser_1.CDSParser().parse(this.findSourceFile());
if (this.parsedData.tree) {
for (const c of ((_a = this.parsedData.tree) === null || _a === void 0 ? void 0 : _a.getChildren()) || []) {
if (c.get() instanceof expressions_1.CDSAnnotation) {
continue;
}
if (c instanceof nodes_1.ExpressionNode) {
this.parsedData.definitionName = (_b = c.findFirstExpression(expressions_1.CDSName)) === null || _b === void 0 ? void 0 : _b.concatTokens().replace(/ /g, "");
break;
}
}
this.findSourcesAndRelations(this.parsedData.tree);
this.findFieldNames(this.parsedData.tree);
}
else {
this.parserError = true;
}
this.dirty = false;
return { updated: true, runtime: Date.now() - start };
}
getTree() {
var _a;
return (_a = this.parsedData) === null || _a === void 0 ? void 0 : _a.tree;
}
//////////
findSQLViewName() {
var _a;
const match = (_a = this.findSourceFile()) === null || _a === void 0 ? void 0 : _a.getRaw().match(/\.sqlViewName: '([\w/]+)'/);
if (match) {
this.parsedData.sqlViewName = match[1].toUpperCase();
}
}
findDescription() {
var _a;
const match = (_a = this.findSourceFile()) === null || _a === void 0 ? void 0 : _a.getRaw().match(/\.label: '([\w,.:-=#%&() ]+)'/);
if (match) {
return match[1];
}
return undefined;
}
findFieldNames(tree) {
var _a, _b;
let expr = tree.findFirstExpression(expressions_1.CDSSelect);
if (expr === undefined) {
expr = tree.findFirstExpression(expressions_1.CDSAnnotate);
}
if (expr === undefined) {
expr = tree.findFirstExpression(expressions_1.CDSDefineProjection);
}
for (const e of (expr === null || expr === void 0 ? void 0 : expr.findDirectExpressions(expressions_1.CDSElement)) || []) {
let prefix = "";
let found = (_a = e.findDirectExpression(expressions_1.CDSAs)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(expressions_1.CDSName);
if (found === undefined) {
const list = e.findAllExpressions(expressions_1.CDSName);
if (e.concatTokens().toUpperCase().includes(" REDIRECTED TO ")) {
found = list[0];
}
else {
found = list[list.length - 1];
if (list.length > 1) {
prefix = list[0].concatTokens();
}
}
}
if (found === undefined) {
continue;
}
const name = found === null || found === void 0 ? void 0 : found.concatTokens();
if ((_b = this.parsedData) === null || _b === void 0 ? void 0 : _b.associations.some(a => { var _a; return a.name.toUpperCase() === name.toUpperCase() || ((_a = a.as) === null || _a === void 0 ? void 0 : _a.toUpperCase()) === name.toUpperCase(); })) {
continue;
}
const annotations = [];
for (const a of e.findDirectExpressions(expressions_1.CDSAnnotation)) {
annotations.push(a.concatTokens());
}
this.parsedData.fields.push({
name: name,
annotations: annotations,
prefix: prefix,
key: e.findDirectTokenByText("KEY") !== undefined,
});
}
}
findSourcesAndRelations(tree) {
var _a, _b, _c, _d, _e, _f, _g;
for (const e of tree.findAllExpressions(expressions_1.CDSSource)) {
const name = ((_a = e.getFirstChild()) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase().replace(/ /g, "")) || "ERROR";
const as = (_c = (_b = e.findDirectExpression(expressions_1.CDSAs)) === null || _b === void 0 ? void 0 : _b.findDirectExpression(expressions_1.CDSName)) === null || _c === void 0 ? void 0 : _c.concatTokens().toUpperCase();
this.parsedData.sources.push({ name, as });
}
for (const e of tree.findAllExpressions(expressions_1.CDSRelation)) {
const name = e.getFirstToken().getStr();
const as = (_e = (_d = e.findDirectExpression(expressions_1.CDSAs)) === null || _d === void 0 ? void 0 : _d.findDirectExpression(expressions_1.CDSName)) === null || _e === void 0 ? void 0 : _e.getFirstToken().getStr();
this.parsedData.relations.push({ name, as });
}
for (const e of tree.findAllExpressions(expressions_1.CDSAssociation)) {
const j = e.findDirectExpression(expressions_1.CDSRelation);
if (j === undefined) {
continue;
}
const name = j.getFirstToken().getStr();
const as = (_g = (_f = j.findDirectExpression(expressions_1.CDSAs)) === null || _f === void 0 ? void 0 : _f.findDirectExpression(expressions_1.CDSName)) === null || _g === void 0 ? void 0 : _g.getFirstToken().getStr();
this.parsedData.associations.push({
name: name || "ERROR",
as: as,
});
}
}
}
exports.DataDefinition = DataDefinition;
//# sourceMappingURL=data_definition.js.map