Search in sources :

Example 41 with Procedure

use of org.teiid.metadata.Procedure 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 42 with Procedure

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

the class PIMetadataProcessor method getConnectorMetadata.

public void getConnectorMetadata(Connection conn, MetadataFactory metadataFactory) throws SQLException {
    super.getConnectorMetadata(conn, metadataFactory);
    for (String name : metadataFactory.getSchema().getTables().keySet()) {
        if (name.startsWith("ft_")) {
            Table table = metadataFactory.getSchema().getTable(name);
            table.setProperty(TVF, "true");
        }
    }
    for (String name : metadataFactory.getSchema().getProcedures().keySet()) {
        Procedure proc = metadataFactory.getSchema().getProcedure(name);
        proc.setProperty(TVF, "true");
    }
}
Also used : Table(org.teiid.metadata.Table) Procedure(org.teiid.metadata.Procedure)

Example 43 with Procedure

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

the class TempMetadataAdapter method getModelID.

public Object getModelID(Object groupOrElementID) throws TeiidComponentException, QueryMetadataException {
    groupOrElementID = getActualMetadataId(groupOrElementID);
    if (groupOrElementID instanceof TempMetadataID) {
        TempMetadataID tid = (TempMetadataID) groupOrElementID;
        Object oid = tid.getOriginalMetadataID();
        if (oid instanceof Procedure) {
            return actualMetadata.getModelID(oid);
        }
        return TempMetadataAdapter.TEMP_MODEL;
    }
    // special handling for global temp tables
    Object id = groupOrElementID;
    if (groupOrElementID instanceof Column) {
        id = ((Column) id).getParent();
    }
    if (id instanceof Table) {
        Table t = (Table) id;
        if (t.getTableType() == Table.Type.TemporaryTable && t.isVirtual()) {
            return TempMetadataAdapter.TEMP_MODEL;
        }
    }
    return this.actualMetadata.getModelID(groupOrElementID);
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) Procedure(org.teiid.metadata.Procedure)

Example 44 with Procedure

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

the class RestASMBasedWebArchiveBuilder method getViewClass.

protected byte[] getViewClass(String vdbName, String vdbVersion, String modelName, Schema schema, boolean passthroughAuth) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    AnnotationVisitor av0;
    boolean hasValidProcedures = false;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/" + modelName, null, "org/teiid/jboss/rest/TeiidRSProvider", null);
    {
        av0 = cw.visitAnnotation("Ljavax/ws/rs/Path;", true);
        av0.visit("value", "/" + modelName);
        av0.visitEnd();
    }
    {
        av0 = cw.visitAnnotation("Lio/swagger/annotations/Api;", true);
        av0.visit("value", "/" + modelName);
        av0.visitEnd();
    }
    cw.visitInnerClass("javax/ws/rs/core/Response$Status", "javax/ws/rs/core/Response", "Status", ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/TeiidRSProvider", "<init>", "()V");
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    Collection<Procedure> procedures = schema.getProcedures().values();
    for (Procedure procedure : procedures) {
        String uri = procedure.getProperty(REST_NAMESPACE + "URI", false);
        String method = procedure.getProperty(REST_NAMESPACE + "METHOD", false);
        String contentType = procedure.getProperty(REST_NAMESPACE + "PRODUCES", false);
        String charSet = procedure.getProperty(REST_NAMESPACE + "CHARSET", false);
        if (uri != null && method != null) {
            if (contentType == null) {
                contentType = findContentType(procedure);
            }
            if (contentType != null) {
                contentType = contentType.toLowerCase();
                if (contentType.equals("xml")) {
                    contentType = "application/xml";
                } else if (contentType.equals("json")) {
                    contentType = "application/json";
                } else if (contentType.equals("plain")) {
                    contentType = "text/plain";
                }
                buildRestService(vdbName, vdbVersion, modelName, procedure, method, uri, cw, contentType, charSet, passthroughAuth);
                hasValidProcedures = true;
            }
        }
    }
    buildQueryProcedure(vdbName, vdbVersion, modelName, "xml", cw, passthroughAuth);
    buildQueryProcedure(vdbName, vdbVersion, modelName, "json", cw, passthroughAuth);
    cw.visitEnd();
    if (!hasValidProcedures) {
        return null;
    }
    return cw.toByteArray();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) Procedure(org.teiid.metadata.Procedure) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 45 with Procedure

use of org.teiid.metadata.Procedure 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)

Aggregations

Procedure (org.teiid.metadata.Procedure)82 ProcedureParameter (org.teiid.metadata.ProcedureParameter)36 Test (org.junit.Test)35 MetadataFactory (org.teiid.metadata.MetadataFactory)30 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)27 Schema (org.teiid.metadata.Schema)16 Column (org.teiid.metadata.Column)14 MetadataStore (org.teiid.metadata.MetadataStore)11 Properties (java.util.Properties)10 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)10 Table (org.teiid.metadata.Table)9 QueryNode (org.teiid.query.mapping.relational.QueryNode)8 List (java.util.List)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)5 TranslatorException (org.teiid.translator.TranslatorException)5 ODataType (org.teiid.translator.odata4.ODataMetadataProcessor.ODataType)5 ArrayList (java.util.ArrayList)4 Call (org.teiid.language.Call)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3