Search in sources :

Example 26 with Expression

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

the class Replace method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (list.size() > 0 && list.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = table.getColumns();
        }
    }
    if (list.size() > 0) {
        for (Expression[] expr : list) {
            if (expr.length != columns.length) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            for (int i = 0; i < expr.length; i++) {
                Expression e = expr[i];
                if (e != null) {
                    expr[i] = e.optimize(session);
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    if (keys == null) {
        Index idx = table.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    // INSERT
    for (Column key : keys) {
        boolean found = false;
        for (Column column : columns) {
            if (column.getColumnId() == key.getColumnId()) {
                found = true;
                break;
            }
        }
        if (!found) {
            return;
        }
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.getSQL()).append(" SET ");
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append("=?");
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (Column c : keys) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append("=?");
    }
    String sql = buff.toString();
    update = session.prepare(sql);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Index(com.wplatform.ddal.dbobject.index.Index)

Example 27 with Expression

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

the class Merge method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (keys != null) {
        buff.append(" KEY(");
        buff.resetCount();
        for (Column c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    }
    buff.append('\n');
    if (list.size() > 0) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 28 with Expression

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

the class Merge method prepare.

@Override
public void prepare() {
    if (columns == null) {
        if (list.size() > 0 && list.get(0).length == 0) {
            // special case where table is used as a sequence
            columns = new Column[0];
        } else {
            columns = table.getColumns();
        }
    }
    if (list.size() > 0) {
        for (Expression[] expr : list) {
            if (expr.length != columns.length) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            for (int i = 0; i < expr.length; i++) {
                Expression e = expr[i];
                if (e != null) {
                    expr[i] = e.optimize(session);
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    if (keys == null) {
        Index idx = table.getPrimaryKey();
        if (idx == null) {
            throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, "PRIMARY KEY");
        }
        keys = idx.getColumns();
    }
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(table.getSQL()).append(" SET ");
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL()).append("=?");
    }
    buff.append(" WHERE ");
    buff.resetCount();
    for (Column c : keys) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append("=?");
    }
    String sql = buff.toString();
    update = session.prepare(sql);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Column(com.wplatform.ddal.dbobject.table.Column) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Index(com.wplatform.ddal.dbobject.index.Index)

Example 29 with Expression

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

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

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