Search in sources :

Example 16 with Expression

use of com.wplatform.ddal.command.expression.Expression 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 17 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class UpdateExecutor method doTranslate.

@Override
protected List<Value> doTranslate(TableNode node, SearchRow row, StatementBuilder buff) {
    ArrayList<Value> params = New.arrayList();
    TableFilter tableFilter = prepared.getTableFilter();
    String forTable = node.getCompositeObjectName();
    List<Column> columns = prepared.getColumns();
    Expression condition = prepared.getCondition();
    Expression limitExpr = prepared.getLimitExpr();
    buff.append("UPDATE ");
    buff.append(identifier(forTable)).append(" SET ");
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append(" = ");
        Value v = row.getValue(i);
        buff.appendExceptFirst(", ");
        if (v == null) {
            buff.append("DEFAULT");
        } else if (isNull(v)) {
            buff.append("NULL");
        } else {
            buff.append('?');
            params.add(v);
        }
    }
    if (condition != null) {
        condition.exportParameters(tableFilter, params);
        buff.append(" WHERE ").append(StringUtils.unEnclose(condition.getSQL()));
    }
    if (limitExpr != null) {
        limitExpr.exportParameters(tableFilter, params);
        buff.append(" LIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
    }
    return params;
}
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)

Example 18 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class Table method getDefaultValue.

/**
 * Get or generate a default value for the given column.
 *
 * @param session the session
 * @param column the column
 * @return the value
 */
public Value getDefaultValue(Session session, Column column) {
    Expression defaultExpr = column.getDefaultExpression();
    Value v;
    if (defaultExpr == null) {
        v = column.validateConvertUpdateSequence(session, null);
    } else {
        v = defaultExpr.getValue(session);
    }
    return column.convert(v);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) SimpleRowValue(com.wplatform.ddal.result.SimpleRowValue) Value(com.wplatform.ddal.value.Value)

Example 19 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class SelectExecutor method queryGroupSorted.

private void queryGroupSorted(int columnCount, ResultTarget result) {
    int rowNumber = 0;
    prepared.setCurrentRowNumber(0);
    prepared.setCurrentGroup(null);
    Value[] previousKeyValues = null;
    while (topTableFilter.next()) {
        // for rownum expression
        prepared.setCurrentRowNumber(rowNumber + 1);
        if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
            rowNumber++;
            Value[] keyValues = new Value[groupIndex.length];
            // update group
            for (int i = 0; i < groupIndex.length; i++) {
                int idx = groupIndex[i];
                Expression expr = expressions.get(idx);
                keyValues[i] = expr.getValue(session);
            }
            if (previousKeyValues == null) {
                previousKeyValues = keyValues;
                prepared.setCurrentGroup(New.<Expression, Object>hashMap());
            } else if (!Arrays.equals(previousKeyValues, keyValues)) {
                addGroupSortedRow(previousKeyValues, columnCount, result);
                previousKeyValues = keyValues;
                prepared.setCurrentGroup(New.<Expression, Object>hashMap());
            }
            prepared.increaseCurrentGroupRowId();
            for (int i = 0; i < columnCount; i++) {
                if (groupByExpression == null || !groupByExpression[i]) {
                    Expression expr = expressions.get(i);
                    expr.updateAggregate(session);
                }
            }
        }
    }
    if (previousKeyValues != null) {
        addGroupSortedRow(previousKeyValues, columnCount, result);
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value)

Example 20 with Expression

use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.

the class Select method prepare.

@Override
public void prepare() {
    if (isPrepared) {
        // sometimes a subquery is prepared twice (CREATE TABLE AS SELECT)
        return;
    }
    if (SysProperties.CHECK && !checkInit) {
        DbException.throwInternalError("not initialized");
    }
    if (orderList != null) {
        sort = prepareOrder(orderList, expressions.size());
        orderList = null;
    }
    for (int i = 0; i < expressions.size(); i++) {
        Expression e = expressions.get(i);
        expressions.set(i, e.optimize(session));
    }
    if (condition != null) {
        condition = condition.optimize(session);
        for (TableFilter f : filters) {
            // left outer join child on p = pc where c is null;
            if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
                condition.createIndexConditions(session, f);
            }
        }
    }
    if (isGroupQuery && groupIndex == null && havingIndex < 0) {
        isQuickAggregateQuery = true;
    }
    cost = preparePlan();
    TableMate last = null;
    for (TableFilter filter : filters) {
        if (!filter.isFromTableMate()) {
            break;
        }
        if (filters.size() == 1) {
            isAccordantQuery = true;
            break;
        }
        TableMate table = (TableMate) filter.getTable();
        if (last != null && !last.isRelationSymmetry(table)) {
            break;
        }
        last = table;
    }
    if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery && filters.size() == 1 && expressions.size() == 1 && condition == null) {
    // 分布式的查询不合适
    }
    if (sort != null && !isGroupQuery) {
    }
    if (isGroupQuery && getGroupByExpressionCount() > 0) {
    }
    expressionArray = new Expression[expressions.size()];
    expressions.toArray(expressionArray);
    isPrepared = true;
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression)

Aggregations

Expression (com.wplatform.ddal.command.expression.Expression)40 Column (com.wplatform.ddal.dbobject.table.Column)15 Value (com.wplatform.ddal.value.Value)14 StatementBuilder (com.wplatform.ddal.util.StatementBuilder)9 Row (com.wplatform.ddal.result.Row)5 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)4 TableMate (com.wplatform.ddal.dbobject.table.TableMate)4 DbException (com.wplatform.ddal.message.DbException)4 SearchRow (com.wplatform.ddal.result.SearchRow)4 Query (com.wplatform.ddal.command.dml.Query)3 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)3 ValueExpression (com.wplatform.ddal.command.expression.ValueExpression)3 Database (com.wplatform.ddal.engine.Database)3 ResultInterface (com.wplatform.ddal.result.ResultInterface)3 Parameter (com.wplatform.ddal.command.expression.Parameter)2 Index (com.wplatform.ddal.dbobject.index.Index)2 LocalResult (com.wplatform.ddal.result.LocalResult)2 Prepared (com.wplatform.ddal.command.Prepared)1 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 SelectOrderBy (com.wplatform.ddal.command.dml.SelectOrderBy)1