Search in sources :

Example 21 with Literal

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

the class TestSQLConversionVisitor method testDayOfMonth.

@Test
public void testDayOfMonth() throws Exception {
    Literal arg1 = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(117, 0, 13, 10, 5, 0, 10000000), Timestamp.class);
    // $NON-NLS-1$
    helpTestMod(arg1, SourceSystemFunctions.DAYOFMONTH, "day_of_month(timestamp '2017-01-13 10:05:00.01')");
}
Also used : Literal(org.teiid.language.Literal) Test(org.junit.Test)

Example 22 with Literal

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

the class TestSQLConversionVisitor method testDayOfWeek.

@Test
public void testDayOfWeek() throws Exception {
    Literal arg1 = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(117, 0, 13, 10, 5, 0, 10000000), Timestamp.class);
    // $NON-NLS-1$
    helpTestMod(arg1, SourceSystemFunctions.DAYOFWEEK, "day_of_week(timestamp '2017-01-13 10:05:00.01')");
}
Also used : Literal(org.teiid.language.Literal) Test(org.junit.Test)

Example 23 with Literal

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

the class JPQLUpdateExecution method handleInsert.

private Object handleInsert(Insert insert) throws TranslatorException {
    try {
        String entityClassName = insert.getTable().getMetadataObject().getProperty(JPAMetadataProcessor.ENTITYCLASS, false);
        Object entity = ReflectionHelper.create(entityClassName, null, this.executionContext.getCommandContext().getVDBClassLoader());
        List<ColumnReference> columns = insert.getColumns();
        List<Expression> values = ((ExpressionValueSource) insert.getValueSource()).getValues();
        if (columns.size() != values.size()) {
            throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14007));
        }
        for (int i = 0; i < columns.size(); i++) {
            Column column = columns.get(i).getMetadataObject();
            Object value = values.get(i);
            // do not add the derived columns
            String name = column.getProperty(JPAMetadataProcessor.KEY_ASSOSIATED_WITH_FOREIGN_TABLE, false);
            if (name == null) {
                if (value instanceof Literal) {
                    Literal literalValue = (Literal) value;
                    PropertiesUtils.setBeanProperty(entity, column.getName(), literalValue.getValue());
                } else {
                    PropertiesUtils.setBeanProperty(entity, column.getName(), value);
                }
            }
        }
        return entity;
    } catch (TeiidException e) {
        throw new TranslatorException(e);
    }
}
Also used : Expression(org.teiid.language.Expression) Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference) ExpressionValueSource(org.teiid.language.ExpressionValueSource) TeiidException(org.teiid.core.TeiidException)

Example 24 with Literal

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

the class JPQLDirectQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    if (query.length() < 7) {
        throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14008));
    }
    String firstToken = query.substring(0, 7);
    String jpql = query.substring(7);
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "JPA Source-Query:", jpql);
    if (firstToken.equalsIgnoreCase("search;")) {
        // //$NON-NLS-1$
        StringBuilder buffer = new StringBuilder();
        SQLStringVisitor.parseNativeQueryParts(jpql, arguments, buffer, new SQLStringVisitor.Substitutor() {

            @Override
            public void substitute(Argument arg, StringBuilder builder, int index) {
                Literal argumentValue = arg.getArgumentValue();
                builder.append(argumentValue);
            }
        });
        jpql = buffer.toString();
        Query queryCommand = this.enityManager.createQuery(jpql);
        List<?> results = queryCommand.getResultList();
        this.resultsIterator = results.iterator();
    } else if (firstToken.equalsIgnoreCase("create;")) {
        // //$NON-NLS-1$
        Object entity = arguments.get(0).getArgumentValue().getValue();
        this.enityManager.merge(entity);
        this.resultsIterator = Arrays.asList(1).iterator();
    } else if (firstToken.equalsIgnoreCase("update;") || firstToken.equalsIgnoreCase("delete;")) {
        // //$NON-NLS-1$ //$NON-NLS-2$
        Query queryCmd = this.enityManager.createQuery(jpql);
        this.resultsIterator = Arrays.asList(queryCmd.executeUpdate()).iterator();
    } else {
        throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14008));
    }
}
Also used : SQLStringVisitor(org.teiid.language.visitor.SQLStringVisitor) Argument(org.teiid.language.Argument) Query(javax.persistence.Query) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException)

Example 25 with Literal

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

the class ParmHolder method createObject.

public Object createObject(List<ColumnReference> columnList, List<Expression> valueList, CoherenceVisitor visitor, Table t) throws TranslatorException {
    if (columnList.size() != valueList.size()) {
        throw new TranslatorException("Error:  columns.size and values.size are not the same.");
    }
    // create the new object that will either be added as a top level object or added to the parent container
    String tableName = visitor.getNameFromTable(t);
    Object newObject = createObjectFromMetadata(tableName);
    for (int i = 0; i < columnList.size(); i++) {
        final ColumnReference insertElement = columnList.get(i);
        if (!insertElement.getMetadataObject().isUpdatable())
            continue;
        final String elementName = visitor.getNameFromElement(insertElement.getMetadataObject());
        final Object value = valueList.get(i);
        Object val;
        if (value instanceof Literal) {
            Literal literalValue = (Literal) value;
            val = literalValue.getValue();
            // .toString();
            if (null != val && val instanceof String) {
                // !val.isEmpty()) {
                val = stripQutes((String) val);
            }
        } else {
            val = value;
        // .toString();
        }
        setValue(tableName, elementName, newObject, val, insertElement.getType());
    }
    return newObject;
}
Also used : Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) ColumnReference(org.teiid.language.ColumnReference)

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