Search in sources :

Example 1 with Row

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

the class InsertExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableMate table = castTableMate(prepared.getTable());
    table.check();
    session.getUser().checkRight(table, Right.INSERT);
    prepared.setCurrentRowNumber(0);
    rowNumber = 0;
    affectRows = 0;
    ArrayList<Expression[]> list = prepared.getList();
    Column[] columns = prepared.getColumns();
    int listSize = list.size();
    if (listSize > 0) {
        int columnLen = columns.length;
        for (int x = 0; x < listSize; x++) {
            Row newRow = table.getTemplateRow();
            Expression[] expr = list.get(x);
            prepared.setCurrentRowNumber(x + 1);
            for (int i = 0; i < columnLen; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    e = e.optimize(session);
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, x, Prepared.getSQL(expr));
                    }
                }
            }
            rowNumber++;
            table.validateConvertUpdateSequence(session, newRow);
            addNewRow(newRow);
        }
    } else {
        Query query = prepared.getQuery();
        if (prepared.isInsertFromSelect()) {
            query.query(0, this);
        } else {
            ResultInterface rows = query.query(0);
            while (rows.next()) {
                Value[] r = rows.currentRow();
                addRow(r);
            }
            rows.close();
        }
    }
    flushNewRows();
    return affectRows;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) 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)

Example 2 with Row

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

the class InsertExecutor method addRow.

@Override
public void addRow(Value[] values) {
    TableMate table = castTableMate(prepared.getTable());
    Row newRow = table.getTemplateRow();
    Column[] columns = prepared.getColumns();
    prepared.setCurrentRowNumber(++rowNumber);
    for (int j = 0, len = columns.length; j < len; j++) {
        Column c = columns[j];
        int index = c.getColumnId();
        try {
            Value v = c.convert(values[j]);
            newRow.setValue(index, v);
        } catch (DbException ex) {
            throw prepared.setRow(ex, rowNumber, Prepared.getSQL(values));
        }
    }
    table.validateConvertUpdateSequence(session, newRow);
    addNewRow(newRow);
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) 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 3 with Row

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

the class PreparedRoutingExecutor method updateRows.

protected int updateRows(TableMate table, List<Row> rows) {
    Map<BatchKey, List<List<Value>>> batches = New.hashMap();
    session.checkCanceled();
    for (Row row : rows) {
        RoutingResult result = routingHandler.doRoute(table, row);
        TableNode[] selectNodes = result.getSelectNodes();
        for (TableNode node : selectNodes) {
            StatementBuilder sqlBuff = new StatementBuilder();
            List<Value> params = doTranslate(node, row, sqlBuff);
            BatchKey batchKey = new BatchKey(node.getShardName(), sqlBuff.toString());
            List<List<Value>> batchArgs = batches.get(batchKey);
            if (batchArgs == null) {
                batchArgs = New.arrayList(10);
                batches.put(batchKey, batchArgs);
            }
            batchArgs.add(params);
        }
    }
    List<JdbcWorker<Integer[]>> workers = New.arrayList(batches.size());
    for (Map.Entry<BatchKey, List<List<Value>>> entry : batches.entrySet()) {
        String shardName = entry.getKey().shardName;
        String sql = entry.getKey().sql;
        List<List<Value>> array = entry.getValue();
        workers.add(createBatchUpdateWorker(shardName, sql, array));
    }
    try {
        addRuningJdbcWorkers(workers);
        int affectRows = 0;
        if (workers.size() > 1) {
            // MILLISECONDS
            int queryTimeout = getQueryTimeout();
            List<Future<Integer[]>> invokeAll;
            if (queryTimeout > 0) {
                invokeAll = jdbcExecutor.invokeAll(workers, queryTimeout, TimeUnit.MILLISECONDS);
            } else {
                invokeAll = jdbcExecutor.invokeAll(workers);
            }
            for (Future<Integer[]> future : invokeAll) {
                Integer[] integers = future.get();
                for (Integer integer : integers) {
                    affectRows += integer;
                }
            }
        } else if (workers.size() == 1) {
            Integer[] integers = workers.get(0).doWork();
            for (Integer integer : integers) {
                affectRows += integer;
            }
        }
        return affectRows;
    } catch (InterruptedException e) {
        throw DbException.convert(e);
    } catch (ExecutionException e) {
        throw DbException.convert(e.getCause());
    } finally {
        removeRuningJdbcWorkers(workers);
        for (JdbcWorker<Integer[]> jdbcWorker : workers) {
            jdbcWorker.closeResource();
        }
    }
}
Also used : JdbcWorker(com.wplatform.ddal.excutor.JdbcWorker) RoutingResult(com.wplatform.ddal.dispatch.rule.RoutingResult) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) Future(java.util.concurrent.Future) Row(com.wplatform.ddal.result.Row) SearchRow(com.wplatform.ddal.result.SearchRow) Map(java.util.Map)

Example 4 with Row

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

the class MergeExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Column[] columns = prepared.getColumns();
    TableMate table = castTableMate(prepared.getTable());
    ArrayList<Expression[]> list = prepared.getList();
    table.check();
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    prepared.setCurrentRowNumber(0);
    if (list.size() > 0) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            prepared.setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, count, Prepared.getSQL(expr));
                    }
                }
            }
            merge(newRow);
            count++;
        }
    } else {
        Query query = prepared.getQuery();
        ResultInterface rows = query.query(0);
        count = 0;
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            prepared.setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw prepared.setRow(ex, count, Prepared.getSQL(r));
                }
            }
            merge(newRow);
        }
        rows.close();
    }
    return count;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) 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)

Example 5 with Row

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

the class ReplaceExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Column[] columns = prepared.getColumns();
    TableMate table = castTableMate(prepared.getTable());
    ArrayList<Expression[]> list = prepared.getList();
    table.check();
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    prepared.setCurrentRowNumber(0);
    if (list.size() > 0) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            prepared.setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, count, Prepared.getSQL(expr));
                    }
                }
            }
            replace(newRow);
            count++;
        }
    } else {
        Query query = prepared.getQuery();
        ResultInterface rows = query.query(0);
        count = 0;
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            prepared.setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw prepared.setRow(ex, count, Prepared.getSQL(r));
                }
            }
            replace(newRow);
        }
        rows.close();
    }
    return count;
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Query(com.wplatform.ddal.command.dml.Query) DbException(com.wplatform.ddal.message.DbException) 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)

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