Search in sources :

Example 6 with Argument

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

the class JDBCDirectQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    String sourceSQL = (String) this.arguments.get(0).getArgumentValue().getValue();
    List<Argument> parameters = this.arguments.subList(1, this.arguments.size());
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Source-specific command: ", sourceSQL);
    context.logCommand(sourceSQL);
    int paramCount = parameters.size();
    try {
        Statement stmt;
        boolean hasResults = false;
        if (paramCount > 0) {
            PreparedStatement pstatement = getPreparedStatement(sourceSQL);
            for (int i = 0; i < paramCount; i++) {
                Argument arg = parameters.get(i);
                // TODO: if ParameterMetadata is supported we could use that type
                this.executionFactory.bindValue(pstatement, arg.getArgumentValue().getValue(), arg.getArgumentValue().getType(), i + 1);
            }
            stmt = pstatement;
            hasResults = pstatement.execute();
        } else {
            // TODO: when array support becomes more robust calling like "exec native('sql', ARRAY[]) could still be prepared
            stmt = getStatement();
            hasResults = stmt.execute(sourceSQL);
        }
        if (hasResults) {
            this.results = stmt.getResultSet();
            this.columnCount = this.results.getMetaData().getColumnCount();
        } else {
            this.updateCount = stmt.getUpdateCount();
        }
        addStatementWarnings();
    } catch (SQLException e) {
        throw new JDBCExecutionException(JDBCPlugin.Event.TEIID11008, e, sourceSQL);
    }
}
Also used : Argument(org.teiid.language.Argument) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement)

Example 7 with Argument

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

the class DirectSpreadsheetQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    String worksheet = null;
    Integer limit = null;
    Integer offset = null;
    String toQuery = query;
    List<String> parts = StringUtil.tokenize(query, ';');
    for (String var : parts) {
        int index = var.indexOf('=');
        if (index == -1) {
            continue;
        }
        String key = var.substring(0, index).trim();
        String value = var.substring(index + 1).trim();
        if (key.equalsIgnoreCase(WORKSHEET)) {
            worksheet = value;
        } else if (key.equalsIgnoreCase(QUERY)) {
            StringBuilder buffer = new StringBuilder();
            SQLStringVisitor.parseNativeQueryParts(value, arguments, buffer, new SQLStringVisitor.Substitutor() {

                @Override
                public void substitute(Argument arg, StringBuilder builder, int index) {
                    Literal argumentValue = arg.getArgumentValue();
                    SpreadsheetSQLVisitor visitor = new SpreadsheetSQLVisitor(connection.getSpreadsheetInfo());
                    visitor.visit(argumentValue);
                    builder.append(visitor.getTranslatedSQL());
                }
            });
            toQuery = buffer.toString();
        } else if (key.equalsIgnoreCase(LIMIT)) {
            limit = Integer.parseInt(value);
        } else if (key.equalsIgnoreCase(OFFEST)) {
            offset = Integer.parseInt(value);
        }
    }
    this.rowIterator = this.connection.executeQuery(worksheet, toQuery, offset, limit, executionContext.getBatchSize()).iterator();
}
Also used : Argument(org.teiid.language.Argument) SpreadsheetSQLVisitor(org.teiid.translator.google.visitor.SpreadsheetSQLVisitor) Literal(org.teiid.language.Literal)

Example 8 with Argument

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

the class LDAPDirectCreateUpdateDeleteQueryExecution method getAttributes.

private ArrayList<BasicAttribute> getAttributes(StringTokenizer st) throws TranslatorException {
    if (!st.hasMoreTokens()) {
        throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
    }
    ArrayList<BasicAttribute> attributes = new ArrayList<BasicAttribute>();
    if (st.hasMoreElements()) {
        String var = st.nextToken();
        int index = var.indexOf('=');
        if (index == -1) {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
        }
        String key = var.substring(0, index).trim();
        String value = var.substring(index + 1).trim();
        if (key.equalsIgnoreCase(ATTRIBUTES)) {
            // $NON-NLS-1$
            StringTokenizer attrTokens = new StringTokenizer(value, ",");
            int attrCount = 0;
            while (attrTokens.hasMoreElements()) {
                String name = attrTokens.nextToken().trim();
                if (arguments.size() <= attrCount) {
                    throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12012, name));
                }
                Argument argument = arguments.get(attrCount++);
                Object anObj = null;
                if (argument.getArgumentValue().getValue() != null) {
                    anObj = IQueryToLdapSearchParser.getLiteralString(argument.getArgumentValue());
                }
                attributes.add(new BasicAttribute(name, anObj));
            }
        } else {
            throw new TranslatorException(LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12011));
        }
    }
    return attributes;
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) StringTokenizer(java.util.StringTokenizer) Argument(org.teiid.language.Argument) ArrayList(java.util.ArrayList) TranslatorException(org.teiid.translator.TranslatorException)

Example 9 with Argument

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

the class TestMongoDBDirectQueryExecution method testDirect.

@Test
public void testDirect() 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);
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
    Argument arg2 = new Argument(Direction.IN, null, String.class, null);
    arg2.setArgumentValue(new Literal("foo", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : AggregationOptions(com.mongodb.AggregationOptions) Argument(org.teiid.language.Argument) AggregationOutput(com.mongodb.AggregationOutput) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) BasicDBObject(com.mongodb.BasicDBObject) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Example 10 with Argument

use of org.teiid.language.Argument 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)

Aggregations

Argument (org.teiid.language.Argument)27 TranslatorException (org.teiid.translator.TranslatorException)13 Literal (org.teiid.language.Literal)12 ArrayList (java.util.ArrayList)9 Call (org.teiid.language.Call)9 SQLException (java.sql.SQLException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 List (java.util.List)4 Test (org.junit.Test)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3 HashMap (java.util.HashMap)3 StringTokenizer (java.util.StringTokenizer)3 ResourceException (javax.resource.ResourceException)3 DB (com.mongodb.DB)2 DBObject (com.mongodb.DBObject)2 SObject (com.sforce.soap.partner.sobject.SObject)2 XmlObject (com.sforce.ws.bind.XmlObject)2