Search in sources :

Example 11 with Expression

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

the class SelectExecutor method queryFlat.

private void queryFlat(int columnCount, ResultTarget result, long limitRows) {
    // limitRows is never 0 here
    if (limitRows > 0 && offsetExpr != null) {
        int offset = offsetExpr.getValue(session).getInt();
        if (offset > 0) {
            limitRows += offset;
        }
    }
    int rowNumber = 0;
    prepared.setCurrentRowNumber(0);
    int sampleSize = getSampleSizeValue(session);
    while (topTableFilter.next()) {
        prepared.setCurrentRowNumber(rowNumber + 1);
        if (condition == null || Boolean.TRUE.equals(condition.getBooleanValue(session))) {
            Value[] row = new Value[columnCount];
            for (int i = 0; i < columnCount; i++) {
                Expression expr = expressions.get(i);
                row[i] = expr.getValue(session);
            }
            result.addRow(row);
            rowNumber++;
            if ((sort == null) && limitRows > 0 && result.getRowCount() >= limitRows) {
                break;
            }
            if (sampleSize > 0 && rowNumber >= sampleSize) {
                break;
            }
        }
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value)

Example 12 with Expression

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

the class SelectExecutor method queryGroupAccordant.

private void queryGroupAccordant(int columnCount, ResultTarget result) {
    Value[] row = new Value[columnCount];
    for (int i = 0; i < columnCount; i++) {
        Expression expr = expressions.get(i);
        row[i] = expr.getValue(session);
    }
    result.addRow(row);
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Value(com.wplatform.ddal.value.Value)

Example 13 with Expression

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

the class SelectExecutor method getPlanSQL.

public String getPlanSQL() {
    // can not use the field sqlStatement because the parameter
    // indexes may be incorrect: ? may be in fact ?2 for a subquery
    // but indexes may be set manually as well
    Expression[] exprList = expressions.toArray(new Expression[expressions.size()]);
    StatementBuilder buff = new StatementBuilder("SELECT");
    if (distinct) {
        buff.append(" DISTINCT");
    }
    for (int i = 0; i < visibleColumnCount; i++) {
        buff.appendExceptFirst(",");
        buff.append('\n');
        buff.append(StringUtils.indent(exprList[i].getSQL(), 4, false));
    }
    buff.append("\nFROM ");
    TableFilter filter = topTableFilter;
    if (filter != null) {
        buff.resetCount();
        int i = 0;
        do {
            buff.appendExceptFirst("\n");
            buff.append(filter.getPlanSQL(i++ > 0));
            filter = filter.getJoin();
        } while (filter != null);
    } else {
        buff.resetCount();
        int i = 0;
        for (TableFilter f : topFilters) {
            do {
                buff.appendExceptFirst("\n");
                buff.append(f.getPlanSQL(i++ > 0));
                f = f.getJoin();
            } while (f != null);
        }
    }
    if (condition != null) {
        buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
    }
    if (groupIndex != null) {
        buff.append("\nGROUP BY ");
        buff.resetCount();
        for (int gi : groupIndex) {
            Expression g = exprList[gi];
            g = g.getNonAliasExpression();
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.unEnclose(g.getSQL()));
        }
    }
    if (group != null) {
        buff.append("\nGROUP BY ");
        buff.resetCount();
        for (Expression g : group) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.unEnclose(g.getSQL()));
        }
    }
    if (having != null) {
        // could be set in addGlobalCondition
        // in this case the query is not run directly, just getPlanSQL is
        // called
        Expression h = having;
        buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
    } else if (havingIndex >= 0) {
        Expression h = exprList[havingIndex];
        buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
    }
    if (sort != null) {
        buff.append("\nORDER BY ").append(sort.getSQL(exprList, visibleColumnCount));
    }
    if (limitExpr != null) {
        buff.append("\nLIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
        if (offsetExpr != null) {
            buff.append(" OFFSET ").append(StringUtils.unEnclose(offsetExpr.getSQL()));
        }
    }
    if (sampleSizeExpr != null) {
        buff.append("\nSAMPLE_SIZE ").append(StringUtils.unEnclose(sampleSizeExpr.getSQL()));
    }
    if (isForUpdate) {
        buff.append("\nFOR UPDATE");
    }
    return buff.toString();
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) TableFilter(com.wplatform.ddal.dbobject.table.TableFilter) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 14 with Expression

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

the class SetExecutor method getIntValue.

private int getIntValue() {
    Expression expression = prepared.getExpression();
    expression = expression.optimize(session);
    return expression.getValue(session).getInt();
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression)

Example 15 with Expression

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

the class InsertExecutor method executeUpdate.

@Override
public int executeUpdate() {
    TableMate table = castTableMate(prepared.getTable());
    table.check();
    session.getUser().checkRight(table, Right.INSERT);
    prepared.setCurrentRowNumber(0);
    rowNumber = 0;
    affectRows = 0;
    ArrayList<Expression[]> list = prepared.getList();
    Column[] columns = prepared.getColumns();
    int listSize = list.size();
    if (listSize > 0) {
        int columnLen = columns.length;
        for (int x = 0; x < listSize; x++) {
            Row newRow = table.getTemplateRow();
            Expression[] expr = list.get(x);
            prepared.setCurrentRowNumber(x + 1);
            for (int i = 0; i < columnLen; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    e = e.optimize(session);
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw prepared.setRow(ex, x, Prepared.getSQL(expr));
                    }
                }
            }
            rowNumber++;
            table.validateConvertUpdateSequence(session, newRow);
            addNewRow(newRow);
        }
    } else {
        Query query = prepared.getQuery();
        if (prepared.isInsertFromSelect()) {
            query.query(0, this);
        } else {
            ResultInterface rows = query.query(0);
            while (rows.next()) {
                Value[] r = rows.currentRow();
                addRow(r);
            }
            rows.close();
        }
    }
    flushNewRows();
    return affectRows;
}
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)

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