Search in sources :

Example 21 with Column

use of com.wplatform.ddal.dbobject.table.Column 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 22 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Function method exportParameters.

@Override
public String exportParameters(TableFilter filter, List<Value> container) {
    StatementBuilder buff = new StatementBuilder(info.name);
    if (info.type == CASE) {
        if (args[0] != null) {
            buff.append(" ").append(args[0].exportParameters(filter, container));
        }
        for (int i = 1, len = args.length - 1; i < len; i += 2) {
            buff.append(" WHEN ").append(args[i].exportParameters(filter, container));
            buff.append(" THEN ").append(args[i + 1].exportParameters(filter, container));
        }
        if (args.length % 2 == 0) {
            buff.append(" ELSE ").append(args[args.length - 1].exportParameters(filter, container));
        }
        return buff.append(" END").toString();
    }
    buff.append('(');
    switch(info.type) {
        case CAST:
            {
                buff.append(args[0].exportParameters(filter, container)).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                break;
            }
        case CONVERT:
            {
                buff.append(args[0].exportParameters(filter, container)).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                break;
            }
        case EXTRACT:
            {
                ValueString v = (ValueString) args[0].getValue(null);
                buff.append(v.getString()).append(" FROM ").append(args[1].exportParameters(filter, container));
                break;
            }
        default:
            {
                for (Expression e : args) {
                    buff.appendExceptFirst(", ");
                    buff.append(e.exportParameters(filter, container));
                }
            }
    }
    return buff.append(')').toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column)

Example 23 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Function method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder(info.name);
    if (info.type == CASE) {
        if (args[0] != null) {
            buff.append(" ").append(args[0].getSQL());
        }
        for (int i = 1, len = args.length - 1; i < len; i += 2) {
            buff.append(" WHEN ").append(args[i].getSQL());
            buff.append(" THEN ").append(args[i + 1].getSQL());
        }
        if (args.length % 2 == 0) {
            buff.append(" ELSE ").append(args[args.length - 1].getSQL());
        }
        return buff.append(" END").toString();
    }
    buff.append('(');
    switch(info.type) {
        case CAST:
            {
                buff.append(args[0].getSQL()).append(" AS ").append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                break;
            }
        case CONVERT:
            {
                buff.append(args[0].getSQL()).append(',').append(new Column(null, dataType, precision, scale, displaySize).getCreateSQL());
                break;
            }
        case EXTRACT:
            {
                ValueString v = (ValueString) args[0].getValue(null);
                buff.append(v.getString()).append(" FROM ").append(args[1].getSQL());
                break;
            }
        default:
            {
                for (Expression e : args) {
                    buff.appendExceptFirst(", ");
                    buff.append(e.getSQL());
                }
            }
    }
    return buff.append(')').toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column)

Example 24 with Column

use of com.wplatform.ddal.dbobject.table.Column 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)

Example 25 with Column

use of com.wplatform.ddal.dbobject.table.Column in project jdbc-shards by wplatform.

the class Update method prepare.

@Override
public void prepare() {
    if (condition != null) {
        condition.mapColumns(tableFilter, 0);
        condition = condition.optimize(session);
        condition.createIndexConditions(session, tableFilter);
    }
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        Expression e = expressionMap.get(c);
        e.mapColumns(tableFilter, 0);
        expressionMap.put(c, e.optimize(session));
    }
    PlanItem item = tableFilter.getBestPlanItem(session, 1);
    tableFilter.setPlanItem(item);
    tableFilter.prepare();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) ValueExpression(com.wplatform.ddal.command.expression.ValueExpression) Expression(com.wplatform.ddal.command.expression.Expression) PlanItem(com.wplatform.ddal.dbobject.table.PlanItem)

Aggregations

Column (com.wplatform.ddal.dbobject.table.Column)38 Expression (com.wplatform.ddal.command.expression.Expression)15 Value (com.wplatform.ddal.value.Value)14 TableMate (com.wplatform.ddal.dbobject.table.TableMate)12 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)8 DbException (com.wplatform.ddal.message.DbException)7 Row (com.wplatform.ddal.result.Row)6 SearchRow (com.wplatform.ddal.result.SearchRow)6 Query (com.wplatform.ddal.command.dml.Query)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)4 ResultInterface (com.wplatform.ddal.result.ResultInterface)4 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Index (com.wplatform.ddal.dbobject.index.Index)3 Table (com.wplatform.ddal.dbobject.table.Table)3 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Database (com.wplatform.ddal.engine.Database)3 List (java.util.List)3 Prepared (com.wplatform.ddal.command.Prepared)2