use of org.mule.runtime.extension.api.dsql.DsqlQuery in project mule by mulesoft.
the class NativeQueryParameterValueResolver method resolve.
/**
* {@inheritDoc}
*/
@Override
public String resolve(ValueResolvingContext context) throws MuleException {
String query = super.resolve(context);
if (!isDsqlQuery(query)) {
return query;
}
DsqlQuery dsqlQuery = dsqlParser.parse(query);
// creates new instances to avoid state related problems of the translator implementation.
QueryTranslator queryTranslator = instantiateTranslator(translatorClass);
return dsqlQuery.translate(queryTranslator);
}
use of org.mule.runtime.extension.api.dsql.DsqlQuery in project mule by mulesoft.
the class DsqlQueryMetadataResolverTestCase method getTrimmedOutputMetadata.
@Test
public void getTrimmedOutputMetadata() throws MetadataResolvingException, ConnectionException {
DsqlQuery dsqlQuery = dsqlParser.parse("dsql:SELECT id FROM Circle WHERE (diameter < 18)");
MetadataType outputMetadata = getQueryMetadataResolver().getOutputType(context, dsqlQuery);
ObjectType type = getAndAssertTypeOf(outputMetadata);
assertThat(type.getFields(), hasSize(1));
ObjectFieldType onlyField = type.getFields().iterator().next();
assertThat(onlyField.getValue(), is(instanceOf(NumberType.class)));
assertThat(onlyField.getKey().getName().getLocalPart(), is("id"));
}
use of org.mule.runtime.extension.api.dsql.DsqlQuery in project mule by mulesoft.
the class DsqlQueryMetadataResolver method getOutputType.
/**
* Automatically resolves the output metadata for the {@link DsqlQuery}.
* <p>
* The base entity is resolved using the component {@link QueryEntityResolver} and assuming the key of the entity is the DSQL
* {@link DsqlQuery#getType() type}.
*
* @param context {@link MetadataContext} of the MetaData resolution
* @param query the {@link DsqlQuery} to resolve the output metadata from.
*/
@Override
public MetadataType getOutputType(MetadataContext context, Object query) throws MetadataResolvingException, ConnectionException {
if (query instanceof DsqlQuery) {
DsqlQuery dsqlQuery = (DsqlQuery) query;
MetadataType entityMetadata = entityResolver.getEntityMetadata(context, dsqlQuery.getType().getName());
BaseTypeBuilder builder = context.getTypeBuilder();
final List<Field> fields = dsqlQuery.getFields();
if (fields.size() == 1 && fields.get(0).getName().equals("*")) {
return entityMetadata;
}
entityMetadata.accept(new MetadataTypeVisitor() {
@Override
public void visitObject(ObjectType objectType) {
ObjectTypeBuilder objectTypeBuilder = builder.objectType();
objectType.getFields().stream().filter(p -> fields.stream().anyMatch(f -> f.getName().equalsIgnoreCase(p.getKey().getName().getLocalPart()))).forEach(p -> {
ObjectFieldTypeBuilder field = objectTypeBuilder.addField();
field.key(p.getKey().getName());
field.value(p.getValue());
});
}
});
return builder.build();
} else {
return nativeOutputResolver.getOutputType(context, query);
}
}
use of org.mule.runtime.extension.api.dsql.DsqlQuery in project mule by mulesoft.
the class DsqlQueryMetadataResolverTestCase method getFullOutputMetadata.
@Test
public void getFullOutputMetadata() throws MetadataResolvingException, ConnectionException {
DsqlQuery dsqlQuery = dsqlParser.parse("dsql:SELECT * FROM Circle WHERE (diameter < 18)");
MetadataType outputMetadata = getQueryMetadataResolver().getOutputType(context, dsqlQuery);
ObjectType type = getAndAssertTypeOf(outputMetadata);
assertThat(type.getFields(), hasSize(3));
type.getFields().forEach(f -> {
String name = f.getKey().getName().getLocalPart();
assertThat(name, isIn(asList("color", "id", "diameter")));
});
}
use of org.mule.runtime.extension.api.dsql.DsqlQuery in project mule by mulesoft.
the class MetadataKeyIdObjectResolverTestCase method resolveDsql.
@Test
public void resolveDsql() throws MetadataResolvingException {
ParameterModel queryParam = mock(ParameterModel.class);
when(queryParam.getModelProperty(QueryParameterModelProperty.class)).thenReturn(Optional.of(new QueryParameterModelProperty(QueryTranslator.class)));
mockMetadataKeyModelProp(queryParam, 1);
setParameters(queryParam);
setMetadataKeyIdModelProperty(String.class);
MetadataKey dsqlKey = newKey("dsql:SELECT id FROM Circle WHERE (diameter < 18)").build();
keyIdObjectResolver = new MetadataKeyIdObjectResolver(componentModel);
final Object resolvedKey = keyIdObjectResolver.resolve(dsqlKey);
assertThat(resolvedKey, is(instanceOf(DsqlQuery.class)));
DsqlQuery query = (DsqlQuery) resolvedKey;
assertThat(query.getFields(), hasSize(1));
assertThat(query.getType().getName(), is("Circle"));
}
Aggregations