Search in sources :

Example 61 with Value

use of com.wplatform.ddal.value.Value 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 62 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class TableFilter method getValue.

@Override
public Value getValue(Column column) {
    if (currentSearchRow == null) {
        return null;
    }
    int columnId = column.getColumnId();
    if (columnId == -1) {
        return ValueLong.get(currentSearchRow.getKey());
    }
    if (current == null) {
        Value v = currentSearchRow.getValue(columnId);
        if (v != null) {
            return v;
        }
        current = cursor.get();
        if (current == null) {
            return ValueNull.INSTANCE;
        }
    }
    return current.getValue(columnId);
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 63 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class RoutingHandlerImpl method enumRange.

private List<Value> enumRange(Value firstV, Value listV) {
    if (firstV.getType() != listV.getType()) {
        return null;
    }
    int type = firstV.getType();
    switch(type) {
        case Value.BYTE:
        case Value.INT:
        case Value.LONG:
        case Value.SHORT:
            if (listV.subtract(firstV).getLong() > 200) {
                return null;
            }
            List<Value> enumValues = New.arrayList(10);
            Value enumValue = firstV;
            while (database.compare(enumValue, listV) <= 0) {
                enumValues.add(enumValue);
                Value increase = ValueLong.get(1).convertTo(enumValue.getType());
                enumValue = enumValue.add(increase);
            }
            return enumValues;
        default:
            return null;
    }
}
Also used : Value(com.wplatform.ddal.value.Value)

Example 64 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class RoutingHandlerImpl method doRoute.

@Override
public RoutingResult doRoute(TableMate table, Session session, List<IndexCondition> indexConditions) {
    TableRouter tr = table.getTableRouter();
    if (tr == null) {
        return fixedRoutingResult(table.getShards());
    } else {
        Map<String, List<Value>> routingArgs = New.hashMap();
        List<RuleColumn> ruleCols = tr.getRuleColumns();
        SearchRow start = null, end = null;
        for (IndexCondition condition : indexConditions) {
            Column column = condition.getColumn();
            String colName = column.getName();
            RuleColumn matched = null;
            for (RuleColumn ruleColumn : ruleCols) {
                if (colName.equalsIgnoreCase(ruleColumn.getName())) {
                    matched = ruleColumn;
                }
            }
            if (matched == null) {
                continue;
            }
            List<Value> values = routingArgs.get(matched.getName());
            if (values == null) {
                values = New.arrayList();
                routingArgs.put(matched.getName(), values);
            }
            if (condition.getCompareType() == Comparison.IN_LIST) {
                Value[] inList = condition.getCurrentValueList(session);
                for (Value value : inList) {
                    values.add(value);
                }
            } else if (condition.getCompareType() == Comparison.IN_QUERY) {
                ResultInterface result = condition.getCurrentResult();
                while (result.next()) {
                    Value v = result.currentRow()[0];
                    if (v != ValueNull.INSTANCE) {
                        v = column.convert(v);
                        values.add(v);
                    }
                }
            } else {
                int columnId = column.getColumnId();
                Value v = condition.getCurrentValue(session);
                boolean isStart = condition.isStart();
                boolean isEnd = condition.isEnd();
                if (isStart) {
                    start = getSearchRow(table, session, start, columnId, v, true);
                }
                if (isEnd) {
                    end = getSearchRow(table, session, end, columnId, v, false);
                }
            }
        }
        exportRangeArg(table, start, end, routingArgs);
        RoutingResult rr = trc.calculate(tr, routingArgs);
        return rr;
    }
}
Also used : ResultInterface(com.wplatform.ddal.result.ResultInterface) Column(com.wplatform.ddal.dbobject.table.Column) Value(com.wplatform.ddal.value.Value) List(java.util.List) IndexCondition(com.wplatform.ddal.dbobject.index.IndexCondition) SearchRow(com.wplatform.ddal.result.SearchRow)

Example 65 with Value

use of com.wplatform.ddal.value.Value in project jdbc-shards by wplatform.

the class BatchUpdateWorker method doWork.

@Override
public Integer[] doWork() {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        if (array == null || array.size() < 1) {
            throw new IllegalArgumentException();
        }
        DataSource dataSource = getDataSource();
        Optional optional = Optional.build().shardName(shardName).readOnly(false);
        if (trace.isDebugEnabled()) {
            trace.debug("{0} Fetching connection from DataSource.", shardName);
        }
        conn = session.applyConnection(dataSource, optional);
        attach(conn);
        if (trace.isDebugEnabled()) {
            trace.debug("{0} Preparing: {};", shardName, sql);
        }
        stmt = conn.prepareStatement(sql);
        attach(stmt);
        applyQueryTimeout(stmt);
        for (List<Value> params : array) {
            if (params != null) {
                for (int i = 0, size = params.size(); i < size; i++) {
                    Value v = params.get(i);
                    v.set(stmt, i + 1);
                    if (trace.isDebugEnabled()) {
                        trace.debug("{0} setParameter: {1} -> {2};", shardName, i + 1, v.getSQL());
                    }
                }
                stmt.addBatch();
                if (trace.isDebugEnabled()) {
                    trace.debug("{0} addBatch.", shardName);
                }
            }
        }
        int[] affected = stmt.executeBatch();
        Integer[] rows = new Integer[affected.length];
        for (int i = 0; i < rows.length; i++) {
            rows[i] = affected[i];
        }
        if (trace.isDebugEnabled()) {
            trace.debug("{0} executeUpdate: {1} affected.", shardName, Arrays.toString(affected));
        }
        return rows;
    } catch (SQLException e) {
        error(e);
        throw wrapException(sql, e);
    } catch (Throwable e) {
        error(e);
        throw DbException.convert(e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataSource(javax.sql.DataSource) Value(com.wplatform.ddal.value.Value)

Aggregations

Value (com.wplatform.ddal.value.Value)84 Expression (com.wplatform.ddal.command.expression.Expression)14 Column (com.wplatform.ddal.dbobject.table.Column)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)13 DbException (com.wplatform.ddal.message.DbException)9 SQLException (java.sql.SQLException)8 Row (com.wplatform.ddal.result.Row)7 SearchRow (com.wplatform.ddal.result.SearchRow)7 PreparedStatement (java.sql.PreparedStatement)7 TableMate (com.wplatform.ddal.dbobject.table.TableMate)6 LocalResult (com.wplatform.ddal.result.LocalResult)6 ResultInterface (com.wplatform.ddal.result.ResultInterface)5 Connection (java.sql.Connection)5 Parameter (com.wplatform.ddal.command.expression.Parameter)4 List (java.util.List)4 DataSource (javax.sql.DataSource)4 Query (com.wplatform.ddal.command.dml.Query)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)3