Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Hdbdd artifacts now process and create views (#1473)
Browse files Browse the repository at this point in the history
* Hdbdd views initial commit

* Parsing hdbdd views sucessfully

* Regenerated antlr4 files

* Added missing cds tokens for not equal to and concatenation

* Fixing sonar code smells and bugs

* Reverted hdb engine sync facade changes

* Adding missing hdbdd tests

* Fixing sonar bugs and code smells

* Moved cds tokens to a separate lexer grammar for better readablity

* Added more tests

* Remove unnecessary code
  • Loading branch information
aihanrashidov authored Apr 8, 2022
1 parent 1849eac commit 0810cc7
Show file tree
Hide file tree
Showing 94 changed files with 6,874 additions and 2,404 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
namespace itest;

@Schema: 'TEST_SCHEMA'

context EmployeesWithViewDefinitions {

entity employees {
key ID : String(32);
NAME : String(500);
ADDRESS : String(5000);
AGE : Integer;
PHONE : String(200);
};

entity employee_roles {
key ID : String(32);
TYPE : String(500);
};

entity employee_salaries {
key ID : String(32);
AMOUNT : String(500);
};

// Basic view definition
define view employees_view_basic as
select from "itest::EmployeesWithViewDefinitions.employees" as EMP
{
"EMP"."ID" as "EmployeeID",
"EMP"."NAME" as "EmployeeName",
"EMP"."ADDRESS" as "EmployeeAddress",
"EMP"."AGE" as "EmployeeAge",
"EMP"."PHONE" as "EmployeePhone"
};

// View definition with join statements
define view employees_view_with_join as
select from "itest::EmployeesWithViewDefinitions.employees"
join employee_roles as ER
on "ER"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
join employee_salaries as ES
on "ES"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
{
"itest::EmployeesWithViewDefinitions.employees"."ID" as "EmployeeId",
"itest::EmployeesWithViewDefinitions.employees"."NAME" as "EmployeeName",
"ER"."TYPE" as "EmployeeRoleType",
"ES"."AMOUNT" as "EmployeeSalary"
};

// View definition with where statements
define view employees_view_with_where as
select from "itest::EmployeesWithViewDefinitions.employees"
join employee_roles as ER
on "ER"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
join employee_salaries as ES
on "ES"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
{
"itest::EmployeesWithViewDefinitions.employees"."ID" as "EmployeeId",
"itest::EmployeesWithViewDefinitions.employees"."NAME" as "EmployeeName",
"ER"."TYPE" as "EmployeeRoleType",
"ES"."AMOUNT" as "EmployeeSalary"
}
where "itest::EmployeesWithViewDefinitions.employees"."NAME" = 'John';

// View definition with union statements and dummy table
define view employees_view_with_union as
select from "itest::EmployeesWithViewDefinitions.employees"
join employee_roles as ER
on "ER"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
join employee_salaries as ES
on "ES"."ID" = "itest::EmployeesWithViewDefinitions.employees"."ID"
{
"itest::EmployeesWithViewDefinitions.employees"."ID" as "EmployeeId",
"itest::EmployeesWithViewDefinitions.employees"."NAME" as "EmployeeName",
"ER"."TYPE" as "EmployeeRoleType",
"ES"."AMOUNT" as "EmployeeSalary"
}
where "itest::EmployeesWithViewDefinitions.employees"."NAME" = 'John'
union
select from DUMMY
{
0 as "EmployeeId",
'Ben' as "EmployeeName",
'Developer' as "EmployeeRoleType",
'2200' as "EmployeeSalary"
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void updateEntities() {
listOfWrappersPhaseThree.addAll(constructListOfTableFunctionModelWrappers(connection, wrappersPhaseThree));
listOfWrappersPhaseThree.addAll(constructListOfScalarFunctionModelWrappers(connection, wrappersPhaseThree));
listOfWrappersPhaseThree.addAll(constructListOfSynonymModelWrappers(connection, wrappersPhaseThree));
listOfWrappersPhaseThree.addAll(constructListOfCdsViewWrappers(connection, wrappersPhaseThree));
createArtefactsOnPhaseThree(errors, listOfWrappersPhaseThree);
}
}
Expand Down Expand Up @@ -195,6 +196,22 @@ private List<XSKTopologyDataStructureModelWrapper> constructListOfCdsModelWrappe
return listOfWrappers;
}

@NotNull
private List<XSKTopologyDataStructureModelWrapper> constructListOfCdsViewWrappers(Connection connection, Map<String, XSKTopologyDataStructureModelWrapper> wrappers) {
final Map<String, XSKDataStructureModel> dataStructureCdsModels = managerServices.get(IXSKDataStructureModel.TYPE_HDBDD).getDataStructureModels();
final List<XSKTopologyDataStructureModelWrapper> listOfWrappers = new ArrayList<>();
dataStructureCdsModels.values().forEach(cdsStructure -> {
final IXSKDataStructureManager<XSKDataStructureHDBViewModel> xskViewManagerService = managerServices.get(IXSKDataStructureModel.TYPE_HDB_VIEW);
((XSKDataStructureCdsModel) cdsStructure).getViewModels().forEach(viewModel -> {
HDBTableSynchronizationArtefactType artefactType = new HDBTableSynchronizationArtefactType();
XSKTopologyDataStructureModelWrapper<XSKDataStructureHDBViewModel> tableWrapper = new XSKTopologyDataStructureModelWrapper(connection, xskViewManagerService, viewModel,
artefactType, wrappers);
listOfWrappers.add(tableWrapper);
});
});
return listOfWrappers;
}

private void createArtefactsOnPhaseThree(List<String> errors, List<XSKTopologyDataStructureModelWrapper> listOfWrappersPhaseThree) {
TopologicalDepleter<XSKTopologyDataStructureModelWrapper> depleter = new TopologicalDepleter<>();
TopologicalSorter<XSKTopologyDataStructureModelWrapper> sorter = new TopologicalSorter<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
import com.sap.xsk.hdb.ds.model.XSKDataStructureModel;
import com.sap.xsk.hdb.ds.model.hdbtable.XSKDataStructureHDBTableModel;
import com.sap.xsk.hdb.ds.model.hdbtabletype.XSKDataStructureHDBTableTypeModel;
import com.sap.xsk.hdb.ds.model.hdbview.XSKDataStructureHDBViewModel;
import java.util.List;

public class XSKDataStructureCdsModel extends XSKDataStructureModel {

private boolean forceUpdate;

private List<XSKDataStructureHDBTableModel> tableModels;

private List<XSKDataStructureHDBTableTypeModel> tableTypeModels;

private List<XSKDataStructureHDBViewModel> viewModels;

public List<XSKDataStructureHDBTableModel> getTableModels() {
return tableModels;
}
Expand All @@ -39,6 +43,14 @@ public void setTableTypeModels(List<XSKDataStructureHDBTableTypeModel> tableType
this.tableTypeModels = tableTypeModels;
}

public void setViewModels(List<XSKDataStructureHDBViewModel> viewModels) {
this.viewModels = viewModels;
}

public List<XSKDataStructureHDBViewModel> getViewModels() {
return viewModels;
}

public boolean isForceUpdate() {
return forceUpdate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@
import com.sap.xsk.hdb.ds.model.hdbdd.XSKDataStructureCdsModel;
import com.sap.xsk.hdb.ds.model.hdbtable.XSKDataStructureHDBTableModel;
import com.sap.xsk.hdb.ds.model.hdbtabletype.XSKDataStructureHDBTableTypeModel;
import com.sap.xsk.hdb.ds.model.hdbview.XSKDataStructureHDBViewModel;
import com.sap.xsk.hdb.ds.module.XSKHDBModule;
import com.sap.xsk.hdb.ds.parser.XSKDataStructureParser;
import com.sap.xsk.hdb.ds.synchronizer.XSKDataStructuresSynchronizer;
import com.sap.xsk.hdb.ds.transformer.hdbdd.HdbddTransformer;
import com.sap.xsk.parser.hdbdd.core.CdsLexer;
import com.sap.xsk.parser.hdbdd.core.CdsParser;
import com.sap.xsk.parser.hdbdd.custom.EntityDefinitionListener;
import com.sap.xsk.parser.hdbdd.custom.ArtifactDefinitionListener;
import com.sap.xsk.parser.hdbdd.custom.ReferenceResolvingListener;
import com.sap.xsk.parser.hdbdd.custom.XSKHdbddErrorListener;
import com.sap.xsk.parser.hdbdd.exception.CDSRuntimeException;
import com.sap.xsk.parser.hdbdd.symbols.SymbolTable;
import com.sap.xsk.parser.hdbdd.symbols.entity.EntitySymbol;
import com.sap.xsk.parser.hdbdd.symbols.type.custom.StructuredDataTypeSymbol;
import com.sap.xsk.parser.hdbdd.symbols.view.ViewSymbol;
import com.sap.xsk.utils.XSKCommonsConstants;
import com.sap.xsk.utils.XSKCommonsUtils;
import com.sap.xsk.utils.XSKConstants;
Expand Down Expand Up @@ -81,13 +83,15 @@ public XSKDataStructureModel parse(XSKDataStructureParametersModel parametersMod
} catch (CDSRuntimeException | XSKArtifactParserException e) {
this.symbolTable.clearSymbolsByFullName();
this.symbolTable.clearEntityGraph();
this.symbolTable.clearViewGraph();
throw new XSKDataStructuresException(e.getMessage());
}
}

XSKDataStructureCdsModel cdsModel = populateXSKDataStructureCdsModel(parametersModel.getLocation(), parametersModel.getContent());
this.symbolTable.clearSymbolsByFullName();
this.symbolTable.clearEntityGraph();
this.symbolTable.clearViewGraph();
parsedNodes.clear();

return cdsModel;
Expand All @@ -97,42 +101,47 @@ public XSKDataStructureModel parse(XSKDataStructureParametersModel parametersMod
private void parseHdbdd(String location, String content) throws IOException, XSKArtifactParserException {
ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
ANTLRInputStream inputStream = new ANTLRInputStream(is);
CdsLexer hdbtiLexer = new CdsLexer(inputStream);
CommonTokenStream tokenStream = new CommonTokenStream(hdbtiLexer);
CdsLexer hdbddLexer = new CdsLexer(inputStream);
CommonTokenStream tokenStream = new CommonTokenStream(hdbddLexer);

XSKHdbddErrorListener lexerErrorListener = new XSKHdbddErrorListener();
hdbtiLexer.removeErrorListeners();//remove the ConsoleErrorListener
hdbtiLexer.addErrorListener(lexerErrorListener);
hdbddLexer.removeErrorListeners();//remove the ConsoleErrorListener
hdbddLexer.addErrorListener(lexerErrorListener);

CdsParser hdbddParser = new CdsParser(tokenStream);
hdbddParser.setBuildParseTree(true);

XSKHdbddErrorListener parserErrorListener = new XSKHdbddErrorListener();
hdbddParser.removeErrorListeners();
hdbddParser.addErrorListener(parserErrorListener);

CdsParser hdbtiParser = new CdsParser(tokenStream);
hdbtiParser.setBuildParseTree(true);
hdbtiParser.removeErrorListeners();
hdbtiParser.addErrorListener(parserErrorListener);
ParseTree parseTree = hdbddParser.cdsFile();

ParseTree parseTree = hdbtiParser.cdsFile();
XSKCommonsUtils.logParserErrors(parserErrorListener.getErrors(), XSKCommonsConstants.PARSER_ERROR, location, XSKCommonsConstants.HDBDD_PARSER);
XSKCommonsUtils.logParserErrors(lexerErrorListener.getErrors(), XSKCommonsConstants.LEXER_ERROR, location, XSKCommonsConstants.HDBDD_PARSER);
XSKCommonsUtils.logParserErrors(parserErrorListener.getErrors(), XSKCommonsConstants.PARSER_ERROR, location,
XSKCommonsConstants.HDBDD_PARSER);
XSKCommonsUtils.logParserErrors(lexerErrorListener.getErrors(), XSKCommonsConstants.LEXER_ERROR, location,
XSKCommonsConstants.HDBDD_PARSER);

EntityDefinitionListener entityDefinitionListener = new EntityDefinitionListener();
entityDefinitionListener.setSymbolTable(symbolTable);
entityDefinitionListener.setFileLocation(location);
ArtifactDefinitionListener artifactDefinitionListener = new ArtifactDefinitionListener();
artifactDefinitionListener.setSymbolTable(symbolTable);
artifactDefinitionListener.setFileLocation(location);

ParseTreeWalker parseTreeWalker = new ParseTreeWalker();
try {
parseTreeWalker.walk(entityDefinitionListener, parseTree);
parseTreeWalker.walk(artifactDefinitionListener, parseTree);
} catch (CDSRuntimeException e) {
XSKCommonsUtils.logCustomErrors(location, XSKCommonsConstants.PARSER_ERROR, "", "", e.getMessage(),
"", XSKCommonsConstants.HDBDD_PARSER, XSKCommonsConstants.MODULE_PARSERS,
XSKCommonsConstants.SOURCE_PUBLISH_REQUEST, XSKCommonsConstants.PROGRAM_XSK);
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location),location,ENTITY_ARTEFACT, ArtefactState.FAILED_CREATE, e.getMessage());
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location), location, ENTITY_ARTEFACT,
ArtefactState.FAILED_CREATE, e.getMessage());
throw new CDSRuntimeException(String.format("Failed to parse file: %s. %s", location, e.getMessage()));
}

entityDefinitionListener.getPackagesUsed().forEach(p -> {
artifactDefinitionListener.getPackagesUsed().forEach(p -> {
String fileLocation = getFileLocation(p);
addFileToDependencyTree(fileLocation, location);
if(!parsedNodes.isEmpty() && parsedNodes.contains(fileLocation)){
if (!parsedNodes.isEmpty() && parsedNodes.contains(fileLocation)) {
return;
}

Expand All @@ -146,24 +155,26 @@ private void parseHdbdd(String location, String content) throws IOException, XSK
XSKCommonsUtils.logCustomErrors(location, XSKCommonsConstants.PARSER_ERROR, "", "", e.getMessage(),
"", XSKCommonsConstants.HDBDD_PARSER, XSKCommonsConstants.MODULE_PARSERS,
XSKCommonsConstants.SOURCE_PUBLISH_REQUEST, XSKCommonsConstants.PROGRAM_XSK);
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location),location,ENTITY_ARTEFACT, ArtefactState.FAILED_CREATE, e.getMessage());
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location), location, ENTITY_ARTEFACT,
ArtefactState.FAILED_CREATE, e.getMessage());
}
});

ReferenceResolvingListener referenceResolvingListener = new ReferenceResolvingListener();
referenceResolvingListener.setCdsFileScope(entityDefinitionListener.getCdsFileScope());
referenceResolvingListener.setCdsFileScope(artifactDefinitionListener.getCdsFileScope());
referenceResolvingListener.setSymbolTable(symbolTable);
referenceResolvingListener.setEntityElements(entityDefinitionListener.getEntityElements());
referenceResolvingListener.setTypeables(entityDefinitionListener.getTypeables());
referenceResolvingListener.setAssociations(entityDefinitionListener.getAssociations());
referenceResolvingListener.setEntityElements(artifactDefinitionListener.getEntityElements());
referenceResolvingListener.setTypeables(artifactDefinitionListener.getTypeables());
referenceResolvingListener.setAssociations(artifactDefinitionListener.getAssociations());

try {
parseTreeWalker.walk(referenceResolvingListener, parseTree);
} catch (CDSRuntimeException e) {
XSKCommonsUtils.logCustomErrors(location, XSKCommonsConstants.PARSER_ERROR, "", "", e.getMessage(),
"", XSKCommonsConstants.HDBDD_PARSER, XSKCommonsConstants.MODULE_PARSERS,
XSKCommonsConstants.SOURCE_PUBLISH_REQUEST, XSKCommonsConstants.PROGRAM_XSK);
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location),location,ENTITY_ARTEFACT, ArtefactState.FAILED_CREATE, e.getMessage());
dataStructuresSynchronizer.applyArtefactState(XSKCommonsUtils.getRepositoryBaseObjectName(location), location, ENTITY_ARTEFACT,
ArtefactState.FAILED_CREATE, e.getMessage());
throw new CDSRuntimeException(String.format("Failed to parse file: %s. %s", location, e.getMessage()));
}
}
Expand Down Expand Up @@ -229,6 +240,7 @@ private XSKDataStructureCdsModel getCdsModelBaseData(String location, String con

private void getCdsModelWithParsedData(XSKDataStructureCdsModel cdsModel) {
List<EntitySymbol> parsedEntities = this.symbolTable.getSortedEntities();
List<ViewSymbol> parsedViews = this.symbolTable.getSortedViews();

List<XSKDataStructureHDBTableModel> tableModels = new ArrayList<>();
parsedEntities.forEach(e -> {
Expand All @@ -241,13 +253,20 @@ private void getCdsModelWithParsedData(XSKDataStructureCdsModel cdsModel) {
List<StructuredDataTypeSymbol> structuredDataTypes = this.symbolTable.getTableTypes();
List<XSKDataStructureHDBTableTypeModel> hdbTableTypeModels = new ArrayList<>();
structuredDataTypes.forEach(sdt -> {
if(!(sdt.getAnnotations().containsKey("GenerateTableType")) || (sdt.getAnnotation("GenerateTableType").getKeyValuePairs().get("booleanValue").getValue()).equals("true")) {
if (!(sdt.getAnnotations().containsKey("GenerateTableType")) || (sdt.getAnnotation("GenerateTableType").getKeyValuePairs()
.get("booleanValue").getValue()).equals("true")) {
hdbTableTypeModels.add(this.hdbddTransformer.transformStructuredDataTypeToHdbTableType(sdt));
}
});

List<XSKDataStructureHDBViewModel> viewModels = new ArrayList<>();
parsedViews.forEach(v ->
viewModels.add(this.hdbddTransformer.transformViewSymbolToHdbViewModel(v, cdsModel.getLocation()))
);

cdsModel.setTableModels(tableModels);
cdsModel.setTableTypeModels(hdbTableTypeModels);
cdsModel.setViewModels(viewModels);
}

private String getFileLocation(String fullPackagePath) {
Expand Down
Loading

0 comments on commit 0810cc7

Please sign in to comment.