Search in sources :

Example 6 with Row

use of com.wplatform.ddal.result.Row in project jdbc-shards by wplatform.

the class UpdateExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableFilter tableFilter = prepared.getTableFilter();
    TableMate table = castTableMate(tableFilter.getTable());
    List<Column> columns = prepared.getColumns();
    Map<Column, Expression> valueMap = prepared.getExpressionMap();
    table.check();
    session.getUser().checkRight(table, Right.UPDATE);
    Row updateRow = table.getTemplateRow();
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        Expression e = valueMap.get(c);
        int index = c.getColumnId();
        if (e != null) {
            // e can be null (DEFAULT)
            e = e.optimize(session);
            try {
                Value v = c.convert(e.getValue(session));
                updateRow.setValue(index, v);
            } catch (DbException ex) {
                ex.addSQL("evaluate expression " + e.getSQL());
                throw ex;
            }
        }
    }
    return updateRow(table, updateRow, tableFilter.getIndexConditions());
}
Also used : TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) TableMate(com.wplatform.ddal.dbobject.table.TableMate) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) DbException(com.wplatform.ddal.message.DbException)

Example 7 with Row

use of com.wplatform.ddal.result.Row in project jdbc-shards by wplatform.

the class Update method updateRows.

protected int updateRows() {
    tableFilter.startQuery(session);
    tableFilter.reset();
    RowList rows = new RowList(session);
    try {
        Table table = tableFilter.getTable();
        session.getUser().checkRight(table, Right.UPDATE);
        // table.lock(session, true, false);
        int columnCount = table.getColumns().length;
        // get the old rows, compute the new rows
        setCurrentRowNumber(0);
        int count = 0;
        Column[] columns = table.getColumns();
        int limitRows = -1;
        if (limitExpr != null) {
            Value v = limitExpr.getValue(session);
            if (v != ValueNull.INSTANCE) {
                limitRows = v.getInt();
            }
        }
        while (tableFilter.next()) {
            setCurrentRowNumber(count + 1);
            if (limitRows >= 0 && count >= limitRows) {
                break;
            }
            if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
                Row oldRow = tableFilter.get();
                Row newRow = table.getTemplateRow();
                for (int i = 0; i < columnCount; i++) {
                    Expression newExpr = expressionMap.get(columns[i]);
                    Value newValue;
                    if (newExpr == null) {
                        newValue = oldRow.getValue(i);
                    } else if (newExpr == ValueExpression.getDefault()) {
                        Column column = table.getColumn(i);
                        newValue = table.getDefaultValue(session, column);
                    } else {
                        Column column = table.getColumn(i);
                        newValue = column.convert(newExpr.getValue(session));
                    }
                    newRow.setValue(i, newValue);
                }
                table.validateConvertUpdateSequence(session, newRow);
                rows.add(oldRow);
                rows.add(newRow);
                count++;
            }
        }
        // table.updateRows(this, session, rows);
        return count;
    } finally {
        rows.close();
    }
}
Also used : RowList(com.wplatform.ddal.result.RowList) Table(com.wplatform.ddal.dbobject.table.Table) Column(com.wplatform.ddal.dbobject.table.Column) ValueExpression(com.wplatform.ddal.command.expression.ValueExpression) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) Row(com.wplatform.ddal.result.Row)

Aggregations

Row (com.wplatform.ddal.result.Row)7 Value (com.wplatform.ddal.value.Value)7 Column (com.wplatform.ddal.dbobject.table.Column)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Expression (com.wplatform.ddal.command.expression.Expression)5 TableMate (com.wplatform.ddal.dbobject.table.TableMate)5 DbException (com.wplatform.ddal.message.DbException)5 Query (com.wplatform.ddal.command.dml.Query)3 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)1 Table (com.wplatform.ddal.dbobject.table.Table)1 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)1 RoutingResult (com.wplatform.ddal.dispatch.rule.RoutingResult)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)1 RowList (com.wplatform.ddal.result.RowList)1 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1