Search in sources :

Example 6 with Expression

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

the class Select method init.

@Override
public void init() {
    if (SysProperties.CHECK && checkInit) {
        DbException.throwInternalError();
    }
    expandColumnList();
    visibleColumnCount = expressions.size();
    ArrayList<String> expressionSQL;
    if (orderList != null || group != null) {
        expressionSQL = New.arrayList();
        for (int i = 0; i < visibleColumnCount; i++) {
            Expression expr = expressions.get(i);
            expr = expr.getNonAliasExpression();
            String sql = expr.getSQL();
            expressionSQL.add(sql);
        }
    } else {
        expressionSQL = null;
    }
    if (orderList != null) {
        initOrder(session, expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
    }
    distinctColumnCount = expressions.size();
    if (having != null) {
        expressions.add(having);
        havingIndex = expressions.size() - 1;
        having = null;
    } else {
        havingIndex = -1;
    }
    Database db = session.getDatabase();
    // and 'GROUP BY' expressions at the end
    if (group != null) {
        int size = group.size();
        int expSize = expressionSQL.size();
        groupIndex = new int[size];
        for (int i = 0; i < size; i++) {
            Expression expr = group.get(i);
            String sql = expr.getSQL();
            int found = -1;
            for (int j = 0; j < expSize; j++) {
                String s2 = expressionSQL.get(j);
                if (db.equalsIdentifiers(s2, sql)) {
                    found = j;
                    break;
                }
            }
            if (found < 0) {
                // special case: GROUP BY a column alias
                for (int j = 0; j < expSize; j++) {
                    Expression e = expressions.get(j);
                    if (db.equalsIdentifiers(sql, e.getAlias())) {
                        found = j;
                        break;
                    }
                    sql = expr.getAlias();
                    if (db.equalsIdentifiers(sql, e.getAlias())) {
                        found = j;
                        break;
                    }
                }
            }
            if (found < 0) {
                int index = expressions.size();
                groupIndex[i] = index;
                expressions.add(expr);
            } else {
                groupIndex[i] = found;
            }
        }
        groupByExpression = new boolean[expressions.size()];
        for (int gi : groupIndex) {
            groupByExpression[gi] = true;
        }
        group = null;
    }
    // map columns in select list and condition
    for (TableFilter f : filters) {
        mapColumns(f, 0);
    }
    if (havingIndex >= 0) {
        Expression expr = expressions.get(havingIndex);
        SelectListColumnResolver res = new SelectListColumnResolver(this);
        expr.mapColumns(res, 0);
    }
    checkInit = true;
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Database(com.wplatform.ddal.engine.Database)

Example 7 with Expression

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

the class Update method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("UPDATE ");
    buff.append(tableFilter.getPlanSQL(false)).append("\nSET\n    ");
    for (int i = 0, size = columns.size(); i < size; i++) {
        Column c = columns.get(i);
        Expression e = expressionMap.get(c);
        buff.appendExceptFirst(",\n    ");
        buff.append(c.getName()).append(" = ").append(e.getSQL());
    }
    if (condition != null) {
        buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
    }
    return buff.toString();
}
Also used : Column(com.wplatform.ddal.dbobject.table.Column) ValueExpression(com.wplatform.ddal.command.expression.ValueExpression) Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 8 with Expression

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

the class ExecuteProcedure method setParameters.

private void setParameters() {
    Prepared prepared = procedure.getPrepared();
    ArrayList<Parameter> params = prepared.getParameters();
    for (int i = 0; params != null && i < params.size() && i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        Parameter p = params.get(i);
        p.setValue(expr.getValue(session));
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Prepared(com.wplatform.ddal.command.Prepared) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 9 with Expression

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

the class Insert 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, len = expr.length; i < len; i++) {
                Expression e = expr[i];
                if (e != null) {
                    e = e.optimize(session);
                    if (e instanceof Parameter) {
                        Parameter p = (Parameter) e;
                        p.setColumn(columns[i]);
                    }
                    expr[i] = e;
                }
            }
        }
    } else {
        query.prepare();
        if (query.getColumnCount() != columns.length) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Parameter(com.wplatform.ddal.command.expression.Parameter)

Example 10 with Expression

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

the class Insert method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(")\n");
    if (insertFromSelect) {
        buff.append("DIRECT ");
    }
    if (sortedInsertMode) {
        buff.append("SORTED ");
    }
    if (list.size() > 0) {
        buff.append("VALUES ");
        int row = 0;
        if (list.size() > 1) {
            buff.append('\n');
        }
        for (Expression[] expr : list) {
            if (row++ > 0) {
                buff.append(",\n");
            }
            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)

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