Search in sources :

Example 56 with Value

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

Example 57 with Value

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

the class ReplaceExecutor method update.

private int update(Row row) {
    Prepared update = prepared.getUpdate();
    Column[] columns = prepared.getColumns();
    Column[] keys = prepared.getKeys();
    // the statement degenerates to an INSERT
    if (update == null) {
        return 0;
    }
    ArrayList<Parameter> k = update.getParameters();
    for (int i = 0; i < columns.length; i++) {
        Column col = columns[i];
        Value v = row.getValue(col.getColumnId());
        Parameter p = k.get(i);
        p.setValue(v);
    }
    for (int i = 0; i < keys.length; i++) {
        Column col = keys[i];
        Value v = row.getValue(col.getColumnId());
        if (v == null) {
            throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
        }
        Parameter p = k.get(columns.length + i);
        p.setValue(v);
    }
    return update.update();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Prepared(com.wplatform.ddal.command.Prepared) Value(com.wplatform.ddal.value.Value) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 58 with Value

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

the class SelectExecutor method queryGroup.

private void queryGroup(int columnCount, LocalResult result) {
    ValueHashMap<HashMap<Expression, Object>> groups = ValueHashMap.newInstance();
    int rowNumber = 0;
    prepared.setCurrentRowNumber(0);
    prepared.setCurrentGroup(null);
    ValueArray defaultGroup = ValueArray.get(new Value[0]);
    int sampleSize = getSampleSizeValue(session);
    while (topTableFilter.next()) {
        prepared.setCurrentRowNumber(rowNumber + 1);
        if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
            Value key;
            rowNumber++;
            if (groupIndex == null) {
                key = defaultGroup;
            } else {
                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);
                }
                key = ValueArray.get(keyValues);
            }
            HashMap<Expression, Object> values = groups.get(key);
            if (values == null) {
                values = new HashMap<Expression, Object>();
                groups.put(key, values);
            }
            prepared.setCurrentGroup(values);
            prepared.increaseCurrentGroupRowId();
            int len = columnCount;
            for (int i = 0; i < len; i++) {
                if (groupByExpression == null || !groupByExpression[i]) {
                    Expression expr = expressions.get(i);
                    expr.updateAggregate(session);
                }
            }
            if (sampleSize > 0 && rowNumber >= sampleSize) {
                break;
            }
        }
    }
    if (groupIndex == null && groups.size() == 0) {
        groups.put(defaultGroup, new HashMap<Expression, Object>());
    }
    ArrayList<Value> keys = groups.keys();
    for (Value v : keys) {
        ValueArray key = (ValueArray) v;
        prepared.setCurrentGroup(groups.get(key));
        Value[] keyValues = key.getList();
        Value[] row = new Value[columnCount];
        for (int j = 0; groupIndex != null && j < groupIndex.length; j++) {
            row[groupIndex[j]] = keyValues[j];
        }
        for (int j = 0; j < columnCount; j++) {
            if (groupByExpression != null && groupByExpression[j]) {
                continue;
            }
            Expression expr = expressions.get(j);
            row[j] = expr.getValue(session);
        }
        if (isHavingNullOrFalse(row)) {
            continue;
        }
        row = keepOnlyDistinct(row, columnCount);
        result.addRow(row);
    }
}
Also used : HashMap(java.util.HashMap) ValueHashMap(com.wplatform.ddal.util.ValueHashMap) Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value) ValueArray(com.wplatform.ddal.value.ValueArray)

Example 59 with Value

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

the class SelectExecutor method executeQuery.

@Override
public LocalResult executeQuery(int maxRows, ResultTarget target) {
    int limitRows = maxRows == 0 ? -1 : maxRows;
    if (limitExpr != null) {
        Value v = limitExpr.getValue(session);
        int l = v == ValueNull.INSTANCE ? -1 : v.getInt();
        if (limitRows < 0) {
            limitRows = l;
        } else if (l >= 0) {
            limitRows = Math.min(l, limitRows);
        }
    }
    int columnCount = expressions.size();
    LocalResult result = null;
    if (target == null || !session.getDatabase().getSettings().optimizeInsertFromSelect) {
        result = createLocalResult(result);
    }
    if (sort != null) {
        result = createLocalResult(result);
        result.setSortOrder(sort);
    }
    if (distinct) {
        result = createLocalResult(result);
        result.setDistinct();
    }
    if (randomAccessResult) {
        result = createLocalResult(result);
    }
    if (isGroupQuery) {
        result = createLocalResult(result);
    }
    if (limitRows >= 0 || offsetExpr != null) {
        result = createLocalResult(result);
    }
    topTableFilter.startQuery(session);
    topTableFilter.reset();
    topTableFilter.lock(session, isForUpdate, isForUpdate);
    ResultTarget to = result != null ? result : target;
    if (limitRows != 0) {
        if (isAccordantQuery) {
            if (isGroupQuery) {
                queryGroupAccordant(columnCount, to);
            } else {
                queryFlatAccordant(columnCount, to, limitRows);
            }
        } else {
            if (isGroupQuery) {
                queryGroup(columnCount, result);
            } else {
                queryFlat(columnCount, to, limitRows);
            }
        }
    }
    if (offsetExpr != null) {
        result.setOffset(offsetExpr.getValue(session).getInt());
    }
    if (result != null) {
        result.done();
        if (target != null) {
            while (result.next()) {
                target.addRow(result.currentRow());
            }
            result.close();
            return null;
        }
        return result;
    }
    return null;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) ResultTarget(com.wplatform.ddal.result.ResultTarget) Value(com.wplatform.ddal.value.Value)

Example 60 with Value

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

the class SelectExecutor method addGroupSortedRow.

private void addGroupSortedRow(Value[] keyValues, int columnCount, ResultTarget result) {
    Value[] row = new Value[columnCount];
    for (int j = 0; groupIndex != null && j < groupIndex.length; j++) {
        row[groupIndex[j]] = keyValues[j];
    }
    for (int j = 0; j < columnCount; j++) {
        if (groupByExpression != null && groupByExpression[j]) {
            continue;
        }
        Expression expr = expressions.get(j);
        row[j] = expr.getValue(session);
    }
    if (isHavingNullOrFalse(row)) {
        return;
    }
    row = keepOnlyDistinct(row, columnCount);
    result.addRow(row);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) 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