Search in sources :

Example 26 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class CoherenceVisitor method visit.

public void visit(Comparison obj) {
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Parsing Comparison criteria.");
    try {
        Comparison.Operator op = ((Comparison) obj).getOperator();
        Expression lhs = ((Comparison) obj).getLeftExpression();
        Expression rhs = ((Comparison) obj).getRightExpression();
        String lhsString = getExpressionString(lhs);
        String rhsString = getExpressionString(rhs);
        if (lhsString == null || rhsString == null) {
            // $NON-NLS-1$
            final String msg = CoherencePlugin.Util.getString("CoherenceVisitor.missingComparisonExpression");
            exception = new TranslatorException(msg);
        }
        if (rhs instanceof Literal || lhs instanceof Literal) {
            if (rhs instanceof Literal) {
                Literal literal = (Literal) rhs;
                addCompareCriteria(lhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(lhsString, literal.getValue(), op, literal.getType() );
            } else {
                Literal literal = (Literal) lhs;
                addCompareCriteria(rhsString, literal.getValue(), op, literal.getType());
            // filter = CoherenceFilterUtil.createCompareFilter(rhsString, literal.getValue(), op, literal.getType() );
            }
        }
    } catch (TranslatorException t) {
        exception = t;
    }
}
Also used : Comparison(org.teiid.language.Comparison) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) Operator(org.teiid.language.Comparison.Operator)

Example 27 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class BaseQueryExecution method buildInvokeHTTP.

protected BinaryWSProcedureExecution buildInvokeHTTP(String method, String uri, Object payload, Map<String, List<String>> headers) throws TranslatorException {
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_CONNECTOR, MessageLevel.DETAIL)) {
        try {
            LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Source-URL=", // $NON-NLS-1$ //$NON-NLS-2$
            URLDecoder.decode(uri, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
    }
    List<Argument> parameters = new ArrayList<Argument>();
    parameters.add(new Argument(Direction.IN, new Literal(method, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(payload, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(uri, TypeFacility.RUNTIME_TYPES.STRING), null));
    parameters.add(new Argument(Direction.IN, new Literal(true, TypeFacility.RUNTIME_TYPES.BOOLEAN), null));
    // the engine currently always associates out params at resolve time even if the
    // values are not directly read by the call
    parameters.add(new Argument(Direction.OUT, TypeFacility.RUNTIME_TYPES.STRING, null));
    Call call = this.translator.getLanguageFactory().createCall("invokeHttp", parameters, null);
    BinaryWSProcedureExecution execution = new BinaryWSProcedureExecution(call, this.metadata, this.executionContext, null, this.connection);
    execution.setUseResponseContext(true);
    execution.setCustomHeaders(headers);
    return execution;
}
Also used : Call(org.teiid.language.Call) Argument(org.teiid.language.Argument) Literal(org.teiid.language.Literal) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 28 with Literal

use of org.teiid.language.Literal 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 29 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestDayWeekQuarterFunctionModifier method test1.

public void test1() throws Exception {
    Literal arg1 = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(104, 0, 21, 10, 5, 0, 10000000), Timestamp.class);
    helpTestMod(arg1, SourceSystemFunctions.DAYOFYEAR, // $NON-NLS-1$
    "to_number(TO_CHAR({ts '2004-01-21 10:05:00.01'}, 'DDD'))");
}
Also used : Literal(org.teiid.language.Literal)

Example 30 with Literal

use of org.teiid.language.Literal in project teiid by teiid.

the class TestDayWeekQuarterFunctionModifier method test9.

public void test9() throws Exception {
    Literal arg1 = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(104, 0, 21, 10, 5, 0, 10000000), Timestamp.class);
    helpTestMod(arg1, SourceSystemFunctions.QUARTER, // $NON-NLS-1$
    "to_number(TO_CHAR({ts '2004-01-21 10:05:00.01'}, 'Q'))");
}
Also used : Literal(org.teiid.language.Literal)

Aggregations

Literal (org.teiid.language.Literal)82 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 Expression (org.teiid.language.Expression)17 TranslatorException (org.teiid.translator.TranslatorException)16 Function (org.teiid.language.Function)15 Argument (org.teiid.language.Argument)12 ColumnReference (org.teiid.language.ColumnReference)10 Column (org.teiid.metadata.Column)10 Comparison (org.teiid.language.Comparison)9 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 List (java.util.List)5 Call (org.teiid.language.Call)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 Timestamp (java.sql.Timestamp)3 Array (org.teiid.language.Array)3 Command (org.teiid.language.Command)3 Insert (org.teiid.language.Insert)3