Search in sources :

Example 1 with DsqlQuery

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);
}
Also used : DsqlParser.isDsqlQuery(org.mule.runtime.extension.api.dsql.DsqlParser.isDsqlQuery) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) QueryTranslator(org.mule.runtime.extension.api.dsql.QueryTranslator)

Example 2 with DsqlQuery

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"));
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) MetadataType(org.mule.metadata.api.model.MetadataType) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with DsqlQuery

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);
    }
}
Also used : ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) ObjectType(org.mule.metadata.api.model.ObjectType) OutputTypeResolver(org.mule.runtime.api.metadata.resolving.OutputTypeResolver) Field(org.mule.runtime.extension.api.dsql.Field) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) List(java.util.List) ConnectionException(org.mule.runtime.api.connection.ConnectionException) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder) QueryEntityResolver(org.mule.runtime.api.metadata.resolving.QueryEntityResolver) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) Field(org.mule.runtime.extension.api.dsql.Field) ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ObjectFieldTypeBuilder(org.mule.metadata.api.builder.ObjectFieldTypeBuilder) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectTypeBuilder(org.mule.metadata.api.builder.ObjectTypeBuilder)

Example 4 with DsqlQuery

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")));
    });
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) MetadataType(org.mule.metadata.api.model.MetadataType) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 5 with DsqlQuery

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"));
}
Also used : QueryParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.QueryParameterModelProperty) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) Test(org.junit.Test)

Aggregations

DsqlQuery (org.mule.runtime.extension.api.dsql.DsqlQuery)5 Test (org.junit.Test)3 MetadataType (org.mule.metadata.api.model.MetadataType)3 ObjectType (org.mule.metadata.api.model.ObjectType)3 SmallTest (org.mule.tck.size.SmallTest)2 List (java.util.List)1 BaseTypeBuilder (org.mule.metadata.api.builder.BaseTypeBuilder)1 ObjectFieldTypeBuilder (org.mule.metadata.api.builder.ObjectFieldTypeBuilder)1 ObjectTypeBuilder (org.mule.metadata.api.builder.ObjectTypeBuilder)1 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)1 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)1 ConnectionException (org.mule.runtime.api.connection.ConnectionException)1 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)1 MetadataContext (org.mule.runtime.api.metadata.MetadataContext)1 MetadataKey (org.mule.runtime.api.metadata.MetadataKey)1 MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)1 OutputTypeResolver (org.mule.runtime.api.metadata.resolving.OutputTypeResolver)1 QueryEntityResolver (org.mule.runtime.api.metadata.resolving.QueryEntityResolver)1 DsqlParser.isDsqlQuery (org.mule.runtime.extension.api.dsql.DsqlParser.isDsqlQuery)1 Field (org.mule.runtime.extension.api.dsql.Field)1