Search in sources :

Example 1 with Update

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());
}
Also used : SpreadsheetUpdateVisitor(org.teiid.translator.google.visitor.SpreadsheetUpdateVisitor) Update(org.teiid.language.Update)

Example 2 with Update

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());
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Update(org.teiid.language.Update) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 3 with Update

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());
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Update(org.teiid.language.Update) LdapContext(javax.naming.ldap.LdapContext) Test(org.junit.Test)

Example 4 with Update

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;
    }
}
Also used : Table(org.teiid.metadata.Table) Update(org.teiid.language.Update)

Example 5 with Update

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?
    }
}
Also used : Condition(org.teiid.language.Condition) Table(org.teiid.metadata.Table) ForeignKey(org.teiid.metadata.ForeignKey) Update(org.teiid.language.Update) Expression(org.teiid.language.Expression) TranslatorException(org.teiid.translator.TranslatorException) SetClause(org.teiid.language.SetClause) ColumnReference(org.teiid.language.ColumnReference)

Aggregations

Update (org.teiid.language.Update)12 Test (org.junit.Test)4 Insert (org.teiid.language.Insert)4 ArrayList (java.util.ArrayList)3 Delete (org.teiid.language.Delete)3 TranslatorException (org.teiid.translator.TranslatorException)3 List (java.util.List)2 ModificationItem (javax.naming.directory.ModificationItem)2 LdapContext (javax.naming.ldap.LdapContext)2 ColumnReference (org.teiid.language.ColumnReference)2 Command (org.teiid.language.Command)2 Literal (org.teiid.language.Literal)2 SetClause (org.teiid.language.SetClause)2 Table (org.teiid.metadata.Table)2 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)2 DB (com.mongodb.DB)1 BigDecimal (java.math.BigDecimal)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 LinkedHashMap (java.util.LinkedHashMap)1