use of org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder in project legend-engine by finos.
the class RelationalExecutionNodeExecutor method getStreamingObjectResultFromRelationalResult.
private Result getStreamingObjectResultFromRelationalResult(ExecutionNode node, RelationalResult relationalResult, DatabaseConnection databaseConnection) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, JsonProcessingException {
Class<?> executeClass = this.getExecuteClass(node);
if (Arrays.asList(executeClass.getInterfaces()).contains(IRelationalClassInstantiationNodeExecutor.class)) {
IRelationalClassInstantiationNodeExecutor executor = (IRelationalClassInstantiationNodeExecutor) executeClass.getConstructor().newInstance();
final ResultSet resultSet = relationalResult.getResultSet();
final String databaseTimeZone = relationalResult.getRelationalDatabaseTimeZone();
final String databaseConnectionString = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().writeValueAsString(databaseConnection);
Iterator<Object> objectIterator = new Iterator<Object>() {
private boolean cursorMove;
private boolean hasNext;
@Override
public boolean hasNext() {
if (!this.cursorMove) {
try {
this.hasNext = resultSet.next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
this.cursorMove = true;
}
return this.hasNext;
}
@Override
public Object next() {
if (this.hasNext()) {
cursorMove = false;
try {
return executor.getObjectFromResultSet(resultSet, databaseTimeZone, databaseConnectionString);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
throw new NoSuchElementException("End of result set reached!");
}
};
Stream<Object> objectStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(objectIterator, Spliterator.ORDERED), false);
ClassBuilder classBuilder = new ClassBuilder(node);
return new StreamingObjectResult<>(objectStream, classBuilder, relationalResult);
} else {
throw new RuntimeException("Unknown execute class " + executeClass.getCanonicalName());
}
}
use of org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder in project legend-engine by finos.
the class RelationalResult method buildTransformersAndBuilder.
private void buildTransformersAndBuilder(ExecutionNode node, DatabaseConnection databaseConnection) throws SQLException {
boolean isDatabaseIdentifiersCaseSensitive = databaseConnection.accept(new DatabaseIdentifiersCaseSensitiveVisitor());
if (ExecutionNodeTDSResultHelper.isResultTDS(node)) {
List<TransformerInput<Integer>> transformerInputs = Lists.mutable.empty();
for (int columnIndex = 1; columnIndex <= this.columnCount; columnIndex++) {
TDSColumn c = ExecutionNodeTDSResultHelper.getTDSColumn(node, this.resultSetMetaData.getColumnLabel(columnIndex), isDatabaseIdentifiersCaseSensitive);
transformerInputs.add(new TransformerInput<>(columnIndex, c.type, (index) -> {
try {
return ExecutionNodeTDSResultHelper.isTDSColumnEnum(node, this.resultSetMetaData.getColumnLabel(index), isDatabaseIdentifiersCaseSensitive);
} catch (Exception e) {
throw new RuntimeException(e);
}
}, (index) -> {
try {
return ExecutionNodeTDSResultHelper.getTDSEnumTransformer(node, this.resultSetMetaData.getColumnLabel(index), isDatabaseIdentifiersCaseSensitive);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
setTransformers.add(new SetImplTransformers(transformerInputs));
this.builder = new TDSBuilder(node, this.sqlColumns, isDatabaseIdentifiersCaseSensitive);
this.columnListForSerializer = ListIterate.collect(((TDSBuilder) this.builder).columns, col -> col.name);
} else if (ExecutionNodeClassResultHelper.isClassResult(node)) {
List<? extends ClassMappingInfo> classMappings = ExecutionNodeClassResultHelper.getClassMappingInfoFromClassResult(node);
for (ClassMappingInfo classMappingInfo : classMappings) {
List<TransformerInput<String>> transformerInputs = Lists.mutable.empty();
for (int i = 1; i <= this.columnCount; i++) {
final String colName = this.resultSetMetaData.getColumnLabel(i);
PropertyInfo profiles = ListIterate.select(classMappingInfo.properties, p -> isDatabaseIdentifiersCaseSensitive ? p.property.equals(colName) : p.property.equalsIgnoreCase(colName)).getFirst();
transformerInputs.add(new TransformerInput<>(profiles != null ? profiles.property : colName, resolveType(profiles, colName), (colNameX) -> {
try {
return !TEMPORAL_DATE_ALIASES.contains(colNameX) && ExecutionNodeClassResultHelper.isClassPropertyEnum(node, classMappingInfo.setImplementationId, colNameX);
} catch (Exception e) {
throw new RuntimeException(e);
}
}, (colNameX) -> {
try {
return ExecutionNodeClassResultHelper.getClassEnumTransformer(node, classMappingInfo.setImplementationId, colNameX);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
setTransformers.add(new SetImplTransformers(transformerInputs));
if (ExecutionNodePartialClassResultHelper.isPartialClassResult(node)) {
this.builder = new PartialClassBuilder(node);
} else {
this.builder = new ClassBuilder(node);
}
}
} else if (ExecutionNodeRelationalResultHelper.isRelationResult(node)) {
SetImplTransformers setImpl = new SetImplTransformers();
for (int columnIndex = 1; columnIndex <= this.columnCount; columnIndex++) {
setImpl.transformers.add(SetImplTransformers.TEMPORARY_DATATYPE_TRANSFORMER);
}
setTransformers.add(setImpl);
this.builder = new RelationBuilder(node);
} else {
SetImplTransformers setImpl = new SetImplTransformers();
for (int i = 1; i <= this.columnCount; i++) {
setImpl.transformers.add(SetImplTransformers.TEMPORARY_DATATYPE_TRANSFORMER);
}
setTransformers.add(setImpl);
this.builder = new DataTypeBuilder(node);
}
}
Aggregations