Search in sources :

Example 56 with Literal

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

the class TestPathFunctionModifier method test1.

public void test1() throws Exception {
    // $NON-NLS-1$
    Literal arg1 = LANG_FACTORY.createLiteral("car", String.class);
    // $NON-NLS-1$
    helpTestMod(arg1, "PATH('car')", "PATH");
}
Also used : Literal(org.teiid.language.Literal)

Example 57 with Literal

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

the class MongoDBDirectQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    StringBuilder buffer = new StringBuilder();
    SQLStringVisitor.parseNativeQueryParts(query, arguments, buffer, new SQLStringVisitor.Substitutor() {

        @Override
        public void substitute(Argument arg, StringBuilder builder, int index) {
            Literal argumentValue = arg.getArgumentValue();
            builder.append(argumentValue.getValue());
        }
    });
    // $NON-NLS-1$
    StringTokenizer st = new StringTokenizer(buffer.toString(), ";");
    String collectionName = st.nextToken();
    // $NON-NLS-1$
    boolean shellOperation = collectionName.equalsIgnoreCase("$ShellCmd");
    String shellOperationName = null;
    if (shellOperation) {
        collectionName = st.nextToken();
        shellOperationName = st.nextToken();
    }
    DBCollection collection = this.mongoDB.getCollection(collectionName);
    if (collection == null) {
        throw new TranslatorException(MongoDBPlugin.Event.TEIID18020, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18020, collectionName));
    }
    if (shellOperation) {
        ArrayList<Object> operations = new ArrayList<Object>();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token.startsWith("{")) {
                // $NON-NLS-1$
                operations.add(JSON.parse(token));
            } else {
                operations.add(token);
            }
        }
        try {
            ReflectionHelper helper = new ReflectionHelper(DBCollection.class);
            Method method = helper.findBestMethodOnTarget(shellOperationName, operations.toArray(new Object[operations.size()]));
            Object result = method.invoke(collection, operations.toArray(new Object[operations.size()]));
            if (result instanceof Cursor) {
                this.results = (Cursor) result;
            } else if (result instanceof WriteResult) {
                WriteResult wr = (WriteResult) result;
                if (!wr.wasAcknowledged()) {
                    // throw error for unacknowledged write
                    throw new TranslatorException(wr.toString());
                }
            }
        } catch (NoSuchMethodException e) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18034, e, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18034, buffer.toString()));
        } catch (SecurityException e) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18034, e, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18034, buffer.toString()));
        } catch (IllegalAccessException e) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18034, e, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18034, buffer.toString()));
        } catch (IllegalArgumentException e) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18034, e, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18034, buffer.toString()));
        } catch (InvocationTargetException e) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18034, e.getTargetException(), MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18034, buffer.toString()));
        }
    } else {
        ArrayList<DBObject> operations = new ArrayList<DBObject>();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            operations.add((DBObject) JSON.parse(token));
        }
        if (operations.isEmpty()) {
            throw new TranslatorException(MongoDBPlugin.Event.TEIID18021, MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18021, collectionName));
        }
        this.results = collection.aggregate(operations, this.executionFactory.getOptions(this.executionContext.getBatchSize()));
    }
}
Also used : SQLStringVisitor(org.teiid.language.visitor.SQLStringVisitor) Argument(org.teiid.language.Argument) ArrayList(java.util.ArrayList) Cursor(com.mongodb.Cursor) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) Literal(org.teiid.language.Literal) ReflectionHelper(org.teiid.core.util.ReflectionHelper) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) WriteResult(com.mongodb.WriteResult) TranslatorException(org.teiid.translator.TranslatorException) DBObject(com.mongodb.DBObject)

Example 58 with Literal

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

the class TestQueryExecution method testMultiAttribute.

@Test
public void testMultiAttribute() throws NamingException {
    Column c = new Column();
    c.setDefaultValue(LDAPQueryExecution.MULTIVALUED_CONCAT);
    Attribute a = LDAPUpdateExecution.createBasicAttribute("x", new Literal("a?b?c", String.class), c);
    assertEquals(3, a.size());
    assertEquals("b", Collections.list(a.getAll()).get(1));
}
Also used : Column(org.teiid.metadata.Column) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Literal(org.teiid.language.Literal) Test(org.junit.Test)

Example 59 with Literal

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

the class TestMongoDBDirectQueryExecution method testShellDirect.

@Test
public void testShellDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("$ShellCmd;MyTable;remove;{ qty: { $gt: 20 }}", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    Mockito.verify(dbCollection).remove(QueryBuilder.start("qty").greaterThan(20).get());
}
Also used : DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) Argument(org.teiid.language.Argument) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 60 with Literal

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

the class TestSybaseIQConvertModifier method testTimestampToDate.

@Test
public void testTimestampToDate() throws Exception {
    Literal c = LANG_FACTORY.createLiteral(TimestampUtil.createTimestamp(89, 2, 3, 7, 8, 12, 99999), Timestamp.class);
    Function func = // $NON-NLS-1$
    LANG_FACTORY.createFunction(// $NON-NLS-1$
    "convert", new Expression[] { c, // $NON-NLS-1$
    LANG_FACTORY.createLiteral("date", String.class) }, java.sql.Date.class);
    // $NON-NLS-1$
    helpGetString1(func, "cast(CAST('1989-03-03 07:08:12.0' AS TIMESTAMP) AS date)");
}
Also used : Function(org.teiid.language.Function) Literal(org.teiid.language.Literal) Test(org.junit.Test)

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