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);
}
}
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();
}
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;
}
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));
}
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));
}
}
Aggregations