use of org.teiid.language.Update in project teiid by teiid.
the class TestSQLtoSpreadsheetQuery method testUpdateConversion.
private void testUpdateConversion(String sql, String expectedCriteria) throws Exception {
Update update = (Update) getCommand(sql);
SpreadsheetUpdateVisitor spreadsheetVisitor = new SpreadsheetUpdateVisitor(people);
spreadsheetVisitor.visit(update);
assertEquals(expectedCriteria, spreadsheetVisitor.getCriteriaQuery());
}
use of org.teiid.language.Update in project teiid by teiid.
the class TestIQueryToLdapSearchParser method testUpdateArray.
@Test
public void testUpdateArray() throws Exception {
// $NON-NLS-1$
String sql = "update LdapModel.People set userid = 1, vals = ('a','b') where dn = 'x'";
QueryMetadataInterface metadata = exampleLdap();
Update query = (Update) getCommand(sql, metadata);
LDAPExecutionFactory config = new LDAPExecutionFactory();
LdapContext context = Mockito.mock(LdapContext.class);
Mockito.stub(context.lookup("")).toReturn(context);
LDAPUpdateExecution lue = new LDAPUpdateExecution(query, context);
lue.execute();
ArgumentCaptor<ModificationItem[]> captor = ArgumentCaptor.forClass(ModificationItem[].class);
Mockito.verify(context).modifyAttributes(ArgumentCaptor.forClass(String.class).capture(), captor.capture());
ModificationItem[] modifications = captor.getValue();
assertEquals(2, modifications.length);
assertEquals("uid: 1", modifications[0].getAttribute().toString());
assertEquals("vals: a, b", modifications[1].getAttribute().toString());
}
use of org.teiid.language.Update in project teiid by teiid.
the class TestIQueryToLdapSearchParser method testUpdateNull.
@Test
public void testUpdateNull() throws Exception {
// $NON-NLS-1$
String sql = "update LdapModel.People set userid = 1, name = null where dn = 'x'";
QueryMetadataInterface metadata = exampleLdap();
Update query = (Update) getCommand(sql, metadata);
LDAPExecutionFactory config = new LDAPExecutionFactory();
LdapContext context = Mockito.mock(LdapContext.class);
Mockito.stub(context.lookup("")).toReturn(context);
LDAPUpdateExecution lue = new LDAPUpdateExecution(query, context);
lue.execute();
ArgumentCaptor<ModificationItem[]> captor = ArgumentCaptor.forClass(ModificationItem[].class);
Mockito.verify(context).modifyAttributes(ArgumentCaptor.forClass(String.class).capture(), captor.capture());
ModificationItem[] modifications = captor.getValue();
assertEquals(2, modifications.length);
assertEquals("uid: 1", modifications[0].getAttribute().toString());
assertEquals("cn: null", modifications[1].getAttribute().toString());
}
use of org.teiid.language.Update in project teiid by teiid.
the class CoherenceUpdateExecution method executeUpdate.
// Private method to actually do an update operation.
private void executeUpdate() throws TranslatorException {
Update ucommand = (Update) command;
Table t = metadata.getTable(ucommand.getTable().getMetadataObject().getFullName());
// if the table has a foreign key, its must be a child (contained) object in the root
if (t.getForeignKeys() != null && t.getForeignKeys().size() > 0) {
updateChildObject(t);
return;
}
}
use of org.teiid.language.Update in project teiid by teiid.
the class CoherenceUpdateExecution method updateChildObject.
private void updateChildObject(Table t) throws TranslatorException {
List<ForeignKey> fks = t.getForeignKeys();
ForeignKey fk = fks.get(0);
Table parentTable = fk.getParent();
// the name of the method to obtain the collection is the nameInSource of the foreginKey
String parentToChildMethod = fk.getNameInSource();
if (parentToChildMethod == null) {
// $NON-NLS-1$
final String msg = CoherencePlugin.Util.getString("CoherenceUpdateExecution.noNameInSourceForForeingKey", new Object[] { fk.getName() });
throw new TranslatorException(msg);
}
// there must only be 1 column in the primary key
String parentColName = visitor.getNameFromElement(fk.getPrimaryKey().getColumns().get(0));
List<SetClause> updateList = ((Update) command).getChanges();
Condition criteria = ((Update) command).getWhere();
ColumnReference leftElement;
Expression rightExpr;
String nameLeftElement;
Object valueRightExpr;
// API).
for (int i = 0; i < updateList.size(); i++) {
SetClause setClause = updateList.get(i);
// trust that connector API is right and left side
// will always be an IElement
leftElement = setClause.getSymbol();
// call utility method to get NameInSource/Name for element
nameLeftElement = visitor.getNameFromElement(leftElement.getMetadataObject());
// get right expression - if it is not a literal we
// can't handle that so throw an exception
rightExpr = setClause.getValue();
// if (!(rightExpr instanceof Literal)) {
// final String msg = CoherencePlugin.Util.getString("LDAPUpdateExecution.valueNotLiteralError",nameLeftElement); //$NON-NLS-1$
// throw new TranslatorException(msg);
// }
valueRightExpr = ((Literal) rightExpr).getValue();
// add in the modification as a replacement - meaning
// any existing value(s) for this attribute will
// be replaced by the new value. If the attribute
// didn't exist, it will automatically be created
// TODO - since null is a valid attribute
// value, we don't do any special handling of it right
// now. But maybe null should mean to delete an
// attribute?
}
}
Aggregations