@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()); }
@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()); } }); StringTokenizer st = new StringTokenizer(buffer.toString(), ";"); String collectionName = st.nextToken(); 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("{")) { 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 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())); } }
MongoDBDirectQueryExecution extends MongoDBBaseExecution implements ProcedureExecution { @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()); } }); StringTokenizer st = new StringTokenizer(buffer.toString(), ";"); String collectionName = st.nextToken(); 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("{")) { 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 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())); } } }
MongoDBDirectQueryExecution extends MongoDBBaseExecution implements ProcedureExecution { @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()); } }); StringTokenizer st = new StringTokenizer(buffer.toString(), ";"); String collectionName = st.nextToken(); 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("{")) { 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 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())); } } MongoDBDirectQueryExecution(List<Argument> arguments, @SuppressWarnings("unused") Command cmd,
            ExecutionContext executionContext, RuntimeMetadata metadata,
            MongoDBConnection connection, String nativeQuery, boolean returnsArray, MongoDBExecutionFactory ef); }
MongoDBDirectQueryExecution extends MongoDBBaseExecution implements ProcedureExecution { @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()); } }); StringTokenizer st = new StringTokenizer(buffer.toString(), ";"); String collectionName = st.nextToken(); 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("{")) { 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 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())); } } MongoDBDirectQueryExecution(List<Argument> arguments, @SuppressWarnings("unused") Command cmd,
            ExecutionContext executionContext, RuntimeMetadata metadata,
            MongoDBConnection connection, String nativeQuery, boolean returnsArray, MongoDBExecutionFactory ef); @Override void execute(); @Override List<?> getOutputParameterValues(); @Override List<?> next(); @SuppressWarnings("unused") DBObject nextRow(); @Override void close(); @Override void cancel(); }
MongoDBDirectQueryExecution extends MongoDBBaseExecution implements ProcedureExecution { @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()); } }); StringTokenizer st = new StringTokenizer(buffer.toString(), ";"); String collectionName = st.nextToken(); 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("{")) { 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 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())); } } MongoDBDirectQueryExecution(List<Argument> arguments, @SuppressWarnings("unused") Command cmd,
            ExecutionContext executionContext, RuntimeMetadata metadata,
            MongoDBConnection connection, String nativeQuery, boolean returnsArray, MongoDBExecutionFactory ef); @Override void execute(); @Override List<?> getOutputParameterValues(); @Override List<?> next(); @SuppressWarnings("unused") DBObject nextRow(); @Override void close(); @Override void cancel();  }