use of org.teiid.language.Delete in project teiid by teiid.
the class InfinispanUpdateExecution method execute.
@Override
public void execute() throws TranslatorException {
if (useAliasCache) {
if (useAliasCache) {
InfinispanQueryExecution.useModifiedGroups(this.connection, this.executionContext, this.metadata, this.command);
}
}
final InfinispanUpdateVisitor visitor = new InfinispanUpdateVisitor(this.metadata);
visitor.append(this.command);
if (!visitor.exceptions.isEmpty()) {
throw visitor.exceptions.get(0);
}
TeiidTableMarsheller marshaller = null;
try {
Table table = visitor.getParentTable();
Column pkColumn = visitor.getPrimaryKey();
if (pkColumn == null) {
throw new TranslatorException(InfinispanPlugin.Util.gs(InfinispanPlugin.Event.TEIID25013, table.getName()));
}
final String PK = MarshallerBuilder.getDocumentAttributeName(pkColumn, false, this.metadata);
DocumentFilter docFilter = null;
if (visitor.isNestedOperation() && visitor.getWhereClause() != null) {
Action action = Action.ALWAYSADD;
if (command instanceof Delete) {
action = Action.REMOVE;
}
SQLStringVisitor ssv = new SQLStringVisitor() {
@Override
public void visit(ColumnReference obj) {
String groupName = null;
NamedTable group = obj.getTable();
if (group.getCorrelationName() != null) {
groupName = group.getCorrelationName();
} else {
Table groupID = group.getMetadataObject();
if (groupID.getFullName().equals(visitor.getParentTable().getFullName())) {
groupName = visitor.getParentNamedTable().getCorrelationName();
} else {
groupName = visitor.getQueryNamedTable().getCorrelationName();
}
}
buffer.append(groupName).append(Tokens.DOT).append(getName(obj.getMetadataObject()));
}
@Override
public String getName(AbstractMetadataRecord object) {
return object.getName();
}
};
ssv.append(visitor.getWhereClause());
docFilter = new ComplexDocumentFilter(visitor.getParentNamedTable(), visitor.getQueryNamedTable(), this.metadata, ssv.toString(), action);
}
marshaller = MarshallerBuilder.getMarshaller(table, this.metadata, docFilter);
this.connection.registerMarshaller(marshaller);
// if the message in defined in different cache than the default, switch it out now.
final RemoteCache<Object, Object> cache = InfinispanQueryExecution.getCache(table, connection);
if (visitor.getOperationType() == OperationType.DELETE) {
paginateDeleteResults(cache, visitor.getDeleteQuery(), new Task() {
@Override
public void run(Object row) throws TranslatorException {
if (visitor.isNestedOperation()) {
String childName = ProtobufMetadataProcessor.getMessageName(visitor.getQueryTable());
InfinispanDocument document = (InfinispanDocument) row;
cache.replace(document.getProperties().get(PK), document);
// false below means count that not matched, i.e. deleted count
updateCount = updateCount + document.getUpdateCount(childName, false);
} else {
Object key = ((Object[]) row)[0];
cache.remove(key);
updateCount++;
}
}
}, this.executionContext.getBatchSize());
} else if (visitor.getOperationType() == OperationType.UPDATE) {
paginateUpdateResults(cache, visitor.getUpdateQuery(), new Task() {
@Override
public void run(Object row) throws TranslatorException {
InfinispanDocument previous = (InfinispanDocument) row;
Object key = previous.getProperties().get(PK);
int count = previous.merge(visitor.getInsertPayload());
cache.replace(key, previous);
updateCount = updateCount + count;
}
}, this.executionContext.getBatchSize());
} else if (visitor.getOperationType() == OperationType.INSERT) {
performInsert(visitor, table, cache, false, marshaller);
} else if (visitor.getOperationType() == OperationType.UPSERT) {
performInsert(visitor, table, cache, true, marshaller);
}
} finally {
if (marshaller != null) {
this.connection.unRegisterMarshaller(marshaller);
}
}
}
use of org.teiid.language.Delete in project teiid by teiid.
the class TestSQLtoSpreadsheetQuery method testDeleteConversion.
private void testDeleteConversion(String sql, String expectedCriteria) throws Exception {
Delete delete = (Delete) getCommand(sql);
SpreadsheetDeleteVisitor spreadsheetVisitor = new SpreadsheetDeleteVisitor(people);
spreadsheetVisitor.visit(delete);
assertEquals(expectedCriteria, spreadsheetVisitor.getCriteriaQuery());
}
use of org.teiid.language.Delete in project teiid by teiid.
the class TestMongoDBUpdateVisitor method helpExecute.
private void helpExecute(String query, String collection, String expected, String match, MergeDetails pushKey, List<MergeDetails> pullKeys) throws Exception {
Command cmd = this.utility.parseCommand(query);
MongoDBUpdateVisitor visitor = new MongoDBUpdateVisitor(this.translator, this.utility.createRuntimeMetadata(), Mockito.mock(DB.class));
visitor.visitNode(cmd);
if (!visitor.exceptions.isEmpty()) {
throw visitor.exceptions.get(0);
}
assertEquals(collection, visitor.mongoDoc.getTargetTable().getName());
if (cmd instanceof Insert) {
assertEquals("wrong insert", expected, visitor.getInsert(this.docs).toString());
} else if (cmd instanceof Update) {
assertEquals("wrong update", expected, visitor.getUpdate(this.docs).toString());
} else if (cmd instanceof Delete) {
}
if (visitor.match != null) {
assertEquals("match wrong", match, visitor.match.toString());
}
MongoDocument doc = visitor.mongoDoc;
if (doc.isMerged()) {
assertEquals("Wrong PushKey", pushKey.toString(), visitor.mongoDoc.getMergeKey().toString());
}
if (!visitor.mongoDoc.getEmbeddedReferences().isEmpty()) {
assertEquals("Wrong PullKeys", visitor.mongoDoc.getEmbeddedReferences().toString(), pullKeys.toString());
}
this.docs = null;
}
use of org.teiid.language.Delete in project teiid by teiid.
the class TestUpdates method testIds.
@Test
public void testIds() throws Exception {
Delete delete = (Delete) translationUtility.parseCommand("delete from contacts");
SalesforceConnection connection = Mockito.mock(SalesforceConnection.class);
SalesForceExecutionFactory config = new SalesForceExecutionFactory();
DeleteExecutionImpl updateExecution = new DeleteExecutionImpl(config, delete, connection, Mockito.mock(RuntimeMetadata.class), Mockito.mock(ExecutionContext.class));
ArgumentCaptor<String> queryArgument = ArgumentCaptor.forClass(String.class);
QueryResult qr = new QueryResult();
SObject so = new SObject();
so.setType("Contact");
so.addField("Id", "x");
qr.setRecords(new SObject[] { so });
qr.setSize(1);
qr.setDone(true);
Mockito.stub(connection.query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean())).toReturn(qr);
Mockito.stub(connection.delete(new String[] { "x" })).toReturn(1);
while (true) {
try {
updateExecution.execute();
org.junit.Assert.assertArrayEquals(new int[] { 1 }, updateExecution.getUpdateCounts());
break;
} catch (DataNotAvailableException e) {
continue;
}
}
Mockito.verify(connection, Mockito.times(1)).query(queryArgument.capture(), Mockito.anyInt(), Mockito.anyBoolean());
String query = queryArgument.getValue();
assertEquals("SELECT Id FROM Contact ", query);
}
use of org.teiid.language.Delete in project teiid by teiid.
the class TestBatchedUpdatesImpl method testGetUpdateCommands.
@Test
public void testGetUpdateCommands() throws Exception {
List updates = example().getUpdateCommands();
assertEquals(3, updates.size());
assertTrue(updates.get(0) instanceof Insert);
assertTrue(updates.get(1) instanceof Update);
assertTrue(updates.get(2) instanceof Delete);
}
Aggregations