Search in sources :

Example 1 with SetClause

use of org.teiid.language.SetClause 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)

Example 2 with SetClause

use of org.teiid.language.SetClause in project teiid by teiid.

the class UpdateExecutionImpl method processIds.

@Override
protected int processIds(String[] ids, IQueryProvidingVisitor visitor) throws ResourceException {
    List<DataPayload> updateDataList = new ArrayList<DataPayload>();
    for (int i = 0; i < ids.length; i++) {
        DataPayload data = new DataPayload();
        for (SetClause clause : ((Update) command).getChanges()) {
            ColumnReference element = clause.getSymbol();
            Column column = element.getMetadataObject();
            String val = ((Literal) clause.getValue()).toString();
            data.addField(column.getSourceName(), Util.stripQutes(val));
        }
        data.setType(visitor.getTableName());
        data.setID(ids[i]);
        updateDataList.add(data);
    }
    return getConnection().update(updateDataList);
}
Also used : Column(org.teiid.metadata.Column) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList) Update(org.teiid.language.Update) SetClause(org.teiid.language.SetClause) ColumnReference(org.teiid.language.ColumnReference)

Example 3 with SetClause

use of org.teiid.language.SetClause in project teiid by teiid.

the class AccumuloUpdateExecution method performUpdate.

private void performUpdate(Update update) throws TranslatorException, TableNotFoundException, MutationsRejectedException {
    Table table = update.getTable().getMetadataObject();
    if (update.getParameterValues() != null) {
        throw new TranslatorException(AccumuloPlugin.Event.TEIID19005, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19005));
    }
    AccumuloQueryVisitor visitor = new AccumuloQueryVisitor(this.aef);
    visitor.visitNode(update.getWhere());
    if (!visitor.exceptions.isEmpty()) {
        throw visitor.exceptions.get(0);
    }
    Connector connector = this.connection.getInstance();
    BatchWriter writer = createBatchWriter(table, connector);
    Text prevRow = null;
    Iterator<Entry<Key, Value>> results = AccumuloQueryExecution.runQuery(this.aef, this.connection.getInstance(), this.connection.getAuthorizations(), visitor.getRanges(), table, visitor.scanIterators());
    while (results.hasNext()) {
        Key key = results.next().getKey();
        Text rowId = key.getRow();
        if (prevRow == null || !prevRow.equals(rowId)) {
            prevRow = rowId;
            Mutation mutation = new Mutation(rowId);
            List<SetClause> changes = update.getChanges();
            for (SetClause clause : changes) {
                Column column = clause.getSymbol().getMetadataObject();
                if (SQLStringVisitor.getRecordName(column).equalsIgnoreCase(AccumuloMetadataProcessor.ROWID)) {
                    throw new TranslatorException(AccumuloPlugin.Event.TEIID19002, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19002, table.getName()));
                }
                Expression value = clause.getValue();
                if (value instanceof Literal) {
                    buildMutation(mutation, column, ((Literal) value).getValue());
                } else {
                    throw new TranslatorException(AccumuloPlugin.Event.TEIID19001, AccumuloPlugin.Util.gs(AccumuloPlugin.Event.TEIID19001));
                }
            }
            writer.addMutation(mutation);
            this.updateCount++;
        }
    }
    writer.close();
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Table(org.teiid.metadata.Table) Text(org.apache.hadoop.io.Text) Entry(java.util.Map.Entry) Column(org.teiid.metadata.Column) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) TranslatorException(org.teiid.translator.TranslatorException) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Key(org.apache.accumulo.core.data.Key) SetClause(org.teiid.language.SetClause)

Example 4 with SetClause

use of org.teiid.language.SetClause in project teiid by teiid.

the class SpreadsheetUpdateVisitor method visit.

public void visit(Update obj) {
    worksheetTitle = obj.getTable().getName();
    changes = new ArrayList<UpdateSet>();
    String columnName;
    if (obj.getTable().getMetadataObject().getNameInSource() != null) {
        this.worksheetTitle = obj.getTable().getMetadataObject().getNameInSource();
    }
    for (SetClause s : obj.getChanges()) {
        if (s.getSymbol().getMetadataObject().getNameInSource() != null) {
            columnName = s.getSymbol().getMetadataObject().getNameInSource();
        } else {
            columnName = s.getSymbol().getMetadataObject().getName();
        }
        changes.add(new UpdateSet(columnName, getStringValue(s.getValue())));
    }
    translateWhere(obj.getWhere());
}
Also used : UpdateSet(org.teiid.translator.google.api.UpdateSet) SetClause(org.teiid.language.SetClause)

Aggregations

SetClause (org.teiid.language.SetClause)4 ColumnReference (org.teiid.language.ColumnReference)2 Expression (org.teiid.language.Expression)2 Literal (org.teiid.language.Literal)2 Update (org.teiid.language.Update)2 Column (org.teiid.metadata.Column)2 Table (org.teiid.metadata.Table)2 TranslatorException (org.teiid.translator.TranslatorException)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 BatchWriter (org.apache.accumulo.core.client.BatchWriter)1 Connector (org.apache.accumulo.core.client.Connector)1 Key (org.apache.accumulo.core.data.Key)1 Mutation (org.apache.accumulo.core.data.Mutation)1 Text (org.apache.hadoop.io.Text)1 Condition (org.teiid.language.Condition)1 ForeignKey (org.teiid.metadata.ForeignKey)1 UpdateSet (org.teiid.translator.google.api.UpdateSet)1