Search in sources :

Example 16 with ProcedureParameter

use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.

the class TestSwaggerMetadataProcessor method testObjectArrayTypes.

@Test
public void testObjectArrayTypes() throws Exception {
    SwaggerExecutionFactory translator = new SwaggerExecutionFactory();
    translator.start();
    MetadataFactory mf = getMetadata(translator, UnitTestUtil.getTestDataPath() + "/doubleclick-swagger.json");
    Procedure p = mf.getSchema().getProcedure("doubleclicksearch.reports.request");
    ProcedureParameter param = p.getParameterByName("filters_values");
    assertEquals("string[]", param.getRuntimeType());
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) Procedure(org.teiid.metadata.Procedure) Test(org.junit.Test)

Example 17 with ProcedureParameter

use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.

the class SwaggerProcedureExecution method buildWSExecution.

private BinaryWSProcedureExecution buildWSExecution(Call obj) throws TranslatorException {
    Procedure procedure = obj.getMetadataObject();
    String uri = procedure.getProperty(RestMetadataExtension.URI, false);
    String method = procedure.getProperty(RestMetadataExtension.METHOD, false);
    StringBuilder queryParameters = new StringBuilder();
    StringBuilder formParameters = new StringBuilder();
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    Object payload = null;
    // body payload document
    SwaggerBodyInputDocument input = null;
    final List<Argument> params = obj.getArguments();
    if (params != null && params.size() != 0) {
        Argument param = null;
        for (int i = 0; i < params.size(); i++) {
            param = params.get(i);
            ProcedureParameter metadata = param.getMetadataObject();
            String argName = WSConnection.Util.httpURLEncode(param.getMetadataObject().getName());
            if (param.getDirection() == Direction.IN || param.getDirection() == Direction.INOUT) {
                String in = metadata.getProperty(RestMetadataExtension.PARAMETER_TYPE, false);
                if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.QUERY.name())) {
                    if (queryParameters.length() != 0) {
                        // $NON-NLS-1$
                        queryParameters.append("&");
                    }
                    Object value = param.getExpression();
                    if (value instanceof Array) {
                        addArgumentValue(argName, (Array) value, metadata.getProperty(SwaggerMetadataProcessor.COLLECION_FORMAT, false), queryParameters);
                    } else {
                        String argValue = getURLValue((Literal) value);
                        queryParameters.append(argName);
                        queryParameters.append(Tokens.EQ);
                        queryParameters.append(argValue);
                    }
                } else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.PATH.name())) {
                    String argValue = getURLValue(param.getArgumentValue());
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String regex = "\\{" + argName + "\\}";
                    uri = uri.replaceAll(regex, argValue);
                } else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.FORM.name()) || in.equalsIgnoreCase(RestMetadataExtension.ParameterType.FORMDATA.name())) {
                    if (formParameters.length() != 0) {
                        // $NON-NLS-1$
                        formParameters.append("&");
                    }
                    Object value = param.getExpression();
                    if (value instanceof Array) {
                        addArgumentValue(argName, (Array) value, metadata.getProperty(SwaggerMetadataProcessor.COLLECION_FORMAT, false), formParameters);
                    } else {
                        formParameters.append(argName);
                        formParameters.append(Tokens.EQ);
                        formParameters.append(getURLValue((Literal) value));
                    }
                } else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.BODY.name())) {
                    if (input == null) {
                        input = new SwaggerBodyInputDocument();
                    }
                    Object expr = param.getExpression();
                    if (expr instanceof Literal) {
                        expr = ((Literal) expr).getValue();
                    }
                    input.addArgument(param.getMetadataObject(), expr);
                } else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.HEADER.name())) {
                    String argValue = param.getArgumentValue().getValue().toString();
                    headers.put(argName, Arrays.asList(argValue));
                }
            } else {
                throw new TranslatorException("Not supported parameter");
            }
        }
    }
    String consumes = procedure.getProperty(RestMetadataExtension.CONSUMES, false);
    if (consumes == null) {
        consumes = "application/json";
    }
    if (input != null) {
        try {
            SwaggerSerializer serializer = getSerializer(consumes);
            InputStream oos = serializer.serialize(input);
            payload = ObjectConverterUtil.convertToString(oos);
        } catch (IOException e) {
            throw new TranslatorException(e);
        }
    }
    if (payload == null && formParameters.length() > 0) {
        payload = formParameters.toString();
    }
    headers.put("Content-Type", Arrays.asList(consumes));
    String produces = procedure.getProperty(RestMetadataExtension.PRODUCES, false);
    if (produces == null) {
        produces = "application/json";
    }
    headers.put("Accept", Arrays.asList(produces));
    if (queryParameters.length() > 0) {
        uri = uri + "?" + queryParameters;
    }
    return buildInvokeHTTP(method, uri, payload, headers);
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) Argument(org.teiid.language.Argument) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Array(org.teiid.language.Array) Literal(org.teiid.language.Literal) Procedure(org.teiid.metadata.Procedure) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException)

Example 18 with ProcedureParameter

use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.

the class OracleExecutionFactory method getSQLConversionVisitor.

@Override
public SQLConversionVisitor getSQLConversionVisitor() {
    return new SQLConversionVisitor(this) {

        @Override
        public void visit(Select select) {
            if (select.getFrom() == null || select.getFrom().isEmpty()) {
                select.setFrom(Arrays.asList((TableReference) new NamedTable(DUAL, null, null)));
            }
            super.visit(select);
        }

        @Override
        public void visit(Comparison obj) {
            if (isFixedChar(obj.getLeftExpression())) {
                if (obj.getRightExpression() instanceof Literal) {
                    Literal l = (Literal) obj.getRightExpression();
                    l.setType(FixedCharType.class);
                } else if (obj.getRightExpression() instanceof Parameter) {
                    Parameter p = (Parameter) obj.getRightExpression();
                    p.setType(FixedCharType.class);
                }
            }
            if (obj.getLeftExpression().getType() == TypeFacility.RUNTIME_TYPES.BOOLEAN && (obj.getLeftExpression() instanceof Function) && obj.getRightExpression() instanceof Literal) {
                Function f = (Function) obj.getLeftExpression();
                if (STRING_BOOLEAN_FUNCTIONS.contains(f.getName())) {
                    Boolean b = (Boolean) ((Literal) obj.getRightExpression()).getValue();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    obj.setRightExpression(new Literal(b != null ? (b ? "TRUE" : "FALSE") : null, TypeFacility.RUNTIME_TYPES.STRING));
                }
            }
            super.visit(obj);
        }

        @Override
        protected void appendRightComparison(Comparison obj) {
            if (obj.getRightExpression() instanceof Array) {
                // oracle needs rhs arrays nested in extra parens
                buffer.append(SQLConstants.Tokens.LPAREN);
                super.appendRightComparison(obj);
                buffer.append(SQLConstants.Tokens.RPAREN);
            } else {
                super.appendRightComparison(obj);
            }
        }

        private boolean isFixedChar(Expression obj) {
            if (!isOracleSuppliedDriver() || !(obj instanceof ColumnReference)) {
                return false;
            }
            ColumnReference cr = (ColumnReference) obj;
            return cr.getType() == TypeFacility.RUNTIME_TYPES.STRING && cr.getMetadataObject() != null && (// $NON-NLS-1$
            "CHAR".equalsIgnoreCase(cr.getMetadataObject().getNativeType()) || // $NON-NLS-1$
            "NCHAR".equalsIgnoreCase(cr.getMetadataObject().getNativeType()));
        }

        @Override
        public void visit(In obj) {
            if (isFixedChar(obj.getLeftExpression())) {
                for (Expression exp : obj.getRightExpressions()) {
                    if (exp instanceof Literal) {
                        Literal l = (Literal) exp;
                        l.setType(FixedCharType.class);
                    } else if (exp instanceof Parameter) {
                        Parameter p = (Parameter) exp;
                        p.setType(FixedCharType.class);
                    }
                }
            }
            super.visit(obj);
        }

        @Override
        public void visit(NamedTable table) {
            stripDualAlias(table);
            super.visit(table);
        }

        private void stripDualAlias(NamedTable table) {
            if (table.getCorrelationName() != null) {
                if (isDual(table)) {
                    table.setCorrelationName(null);
                }
            }
        }

        @Override
        public void visit(ColumnReference obj) {
            if (obj.getTable() != null) {
                stripDualAlias(obj.getTable());
            }
            super.visit(obj);
        }

        @Override
        public void visit(Call call) {
            if (oracleSuppliedDriver && call.getResultSetColumnTypes().length > 0 && call.getMetadataObject() != null) {
                if (call.getReturnType() == null && call.getMetadataObject().getProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, false) == null) {
                    // assume stored function handling
                    if (!setOutCursorType(call)) {
                        call.setReturnType(RefCursorType.class);
                    }
                } else {
                    // TODO we only will allow a single out cursor
                    if (call.getMetadataObject() != null) {
                        ProcedureParameter param = call.getReturnParameter();
                        if (param != null && REF_CURSOR.equalsIgnoreCase(param.getNativeType())) {
                            call.setReturnType(RefCursorType.class);
                        }
                    }
                    setOutCursorType(call);
                }
            }
            super.visit(call);
        }

        private boolean setOutCursorType(Call call) {
            boolean set = false;
            for (Argument arg : call.getArguments()) {
                if (arg.getDirection() == Direction.OUT) {
                    ProcedureParameter param = arg.getMetadataObject();
                    if (param != null && REF_CURSOR.equalsIgnoreCase(param.getNativeType())) {
                        arg.setType(RefCursorType.class);
                        set = true;
                    }
                }
            }
            return set;
        }

        @Override
        public void visit(Like obj) {
            if (obj.getMode() == MatchMode.REGEX) {
                if (obj.isNegated()) {
                    // $NON-NLS-1$
                    buffer.append("NOT(");
                }
                // $NON-NLS-1$
                buffer.append("REGEXP_LIKE(");
                append(obj.getLeftExpression());
                // $NON-NLS-1$
                buffer.append(", ");
                append(obj.getRightExpression());
                // $NON-NLS-1$
                buffer.append(")");
                if (obj.isNegated()) {
                    // $NON-NLS-1$
                    buffer.append(")");
                }
            } else {
                super.visit(obj);
            }
        }

        @Override
        public void visit(WithItem obj) {
            if (obj.getColumns() != null) {
                List<ColumnReference> cols = obj.getColumns();
                if (!obj.isRecusive()) {
                    // oracle 10 does not support recursion nor a column list
                    obj.setColumns(null);
                    Select select = obj.getSubquery().getProjectedQuery();
                    List<DerivedColumn> selectClause = select.getDerivedColumns();
                    for (int i = 0; i < cols.size(); i++) {
                        selectClause.get(i).setAlias(cols.get(i).getName());
                    }
                }
            }
            super.visit(obj);
        }
    };
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) ProcedureParameter(org.teiid.metadata.ProcedureParameter)

Example 19 with ProcedureParameter

use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.

the class TestOracleTranslator method getOracleSpecificMetadata.

/**
 * create fake BQT metadata to test this case, name in source is important
 */
private QueryMetadataInterface getOracleSpecificMetadata() {
    MetadataStore metadataStore = new MetadataStore();
    // $NON-NLS-1$
    Schema foo = RealMetadataFactory.createPhysicalModel("BQT1", metadataStore);
    // $NON-NLS-1$
    Table table = RealMetadataFactory.createPhysicalGroup("SmallA", foo);
    // $NON-NLS-1$
    Table x = RealMetadataFactory.createPhysicalGroup("x", foo);
    x.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select c from d");
    // $NON-NLS-1$
    Table dual = RealMetadataFactory.createPhysicalGroup("DUAL", foo);
    // $NON-NLS-1$
    table.setNameInSource("SmallishA");
    String[] elemNames = new String[] { // $NON-NLS-1$
    "DoubleNum", // $NON-NLS-1$
    "ID", // $NON-NLS-1$
    "timestampvalue", "description", "ndescription" };
    String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING };
    RealMetadataFactory.createElements(x, elemNames, elemTypes);
    List<Column> cols = RealMetadataFactory.createElements(table, elemNames, elemTypes);
    cols.get(1).setAutoIncremented(true);
    // $NON-NLS-1$
    cols.get(1).setNameInSource("ID:SEQUENCE=MYSEQUENCE.nextVal");
    // $NON-NLS-1$
    cols.get(2).setNativeType("date");
    cols.get(3).setNativeType("CHAR");
    cols.get(4).setNativeType("NCHAR");
    // $NON-NLS-1$
    List<Column> dualCols = RealMetadataFactory.createElements(dual, new String[] { "something" }, new String[] { DataTypeManager.DefaultDataTypes.STRING });
    dualCols.get(0).setNameInSource("seq.nextval");
    // $NON-NLS-1$
    ProcedureParameter in1 = RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER);
    // $NON-NLS-1$ //$NON-NLS-2$
    ColumnSet<Procedure> rs3 = RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER });
    Procedure p = RealMetadataFactory.createStoredProcedure("proc", foo, Arrays.asList(in1));
    p.setResultSet(rs3);
    p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select x from y where z = $1");
    p = RealMetadataFactory.createStoredProcedure("proc1", foo, Arrays.asList(RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.STRING)));
    // $NON-NLS-1$ //$NON-NLS-2$
    p.setResultSet(RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER }));
    p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select $1 from y");
    p.setProperty(SQLConversionVisitor.TEIID_NON_PREPARED, "true");
    CompositeMetadataStore store = new CompositeMetadataStore(metadataStore);
    return new TransformationMetadata(null, store, null, RealMetadataFactory.SFM.getSystemFunctions(), null);
}
Also used : CompositeMetadataStore(org.teiid.query.metadata.CompositeMetadataStore) MetadataStore(org.teiid.metadata.MetadataStore) ProcedureParameter(org.teiid.metadata.ProcedureParameter) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Table(org.teiid.metadata.Table) CompositeMetadataStore(org.teiid.query.metadata.CompositeMetadataStore) Column(org.teiid.metadata.Column) Schema(org.teiid.metadata.Schema) Procedure(org.teiid.metadata.Procedure)

Example 20 with ProcedureParameter

use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.

the class TestODataMetadataProcessor method testFunction.

@Test
public void testFunction() throws Exception {
    CsdlReturnType returnType = new CsdlReturnType();
    returnType.setType("Edm.String");
    MetadataFactory mf = functionMetadata("invoke", returnType, null);
    Procedure p = mf.getSchema().getProcedure("invoke");
    assertNotNull(p);
    assertEquals(3, p.getParameters().size());
    assertNull(p.getResultSet());
    assertNotNull(getReturnParameter(p));
    ProcedureParameter pp = getReturnParameter(p);
    assertEquals("string", pp.getRuntimeType());
    ODataType type = ODataType.valueOf(p.getProperty(ODataMetadataProcessor.ODATA_TYPE, false));
    assertEquals(ODataType.FUNCTION, type);
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) ODataType(org.teiid.translator.odata4.ODataMetadataProcessor.ODataType) Procedure(org.teiid.metadata.Procedure) Test(org.junit.Test)

Aggregations

ProcedureParameter (org.teiid.metadata.ProcedureParameter)43 Procedure (org.teiid.metadata.Procedure)35 Test (org.junit.Test)12 MetadataFactory (org.teiid.metadata.MetadataFactory)9 Schema (org.teiid.metadata.Schema)8 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)8 MetadataStore (org.teiid.metadata.MetadataStore)7 Column (org.teiid.metadata.Column)6 ArrayList (java.util.ArrayList)5 QueryNode (org.teiid.query.mapping.relational.QueryNode)5 List (java.util.List)4 Table (org.teiid.metadata.Table)4 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)4 EdmPrimitiveTypeKind (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind)2 FullQualifiedName (org.apache.olingo.commons.api.edm.FullQualifiedName)2 Argument (org.teiid.language.Argument)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)2 TranslatorException (org.teiid.translator.TranslatorException)2 ODataType (org.teiid.translator.odata4.ODataMetadataProcessor.ODataType)2