Search in sources :

Example 1 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class Parser method getSyntaxError.

private DbException getSyntaxError() {
    if (expectedList == null || expectedList.size() == 0) {
        return DbException.getSyntaxError(sqlCommand, parseIndex);
    }
    StatementBuilder buff = new StatementBuilder();
    for (String e : expectedList) {
        buff.appendExceptFirst(", ");
        buff.append(e);
    }
    return DbException.getSyntaxError(sqlCommand, parseIndex, buff.toString());
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 2 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class Replace method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("REPLACE INTO ");
    buff.append(table.getSQL()).append('(');
    for (Column c : columns) {
        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 3 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class Select method getPlanSQL.

@Override
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 (orderList != null) {
        buff.append("\nORDER BY ");
        buff.resetCount();
        for (SelectOrderBy o : orderList) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.unEnclose(o.getSQL()));
        }
    }
    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");
    }
    buff.append("\n/* cost: " + cost + " */");
    return buff.toString();
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 4 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class ConditionInConstantSet method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder("(");
    buff.append(left.getSQL()).append(" IN(");
    for (Expression e : valueList) {
        buff.appendExceptFirst(", ");
        buff.append(e.getSQL());
    }
    return buff.append("))").toString();
}
Also used : StatementBuilder(com.wplatform.ddal.util.StatementBuilder)

Example 5 with StatementBuilder

use of com.wplatform.ddal.util.StatementBuilder in project jdbc-shards by wplatform.

the class ConditionInSelect method exportParameters.

@Override
public String exportParameters(TableFilter filter, List<Value> container) {
    Session session = filter.getSession();
    LocalResult rows = query(session);
    if (rows.getRowCount() > 0) {
        StatementBuilder buff = new StatementBuilder();
        buff.append('(').append(left.exportParameters(filter, container)).append(' ');
        if (all) {
            //由于all代表全部,所以<all表示小于子查询中返回全部值中的最小值;
            //>all表示大于子查询中返回全部值中的最大值。
            buff.append(Comparison.getCompareOperator(compareType)).append(" ALL");
        } else {
            if (compareType == Comparison.EQUAL) {
                buff.append("IN");
            } else {
                //<any可以理解为小于子查询中返回的任意一个值,因此只要小于最大值即可
                //>any可以理解为大于子查询中返回的任意一个值,因此只要大于最小值即可
                buff.append(Comparison.getCompareOperator(compareType)).append(" ANY");
            }
        }
        buff.append("(");
        while (rows.next()) {
            buff.appendExceptFirst(",");
            buff.append("?");
            Value r = rows.currentRow()[0];
            container.add(r);
        }
        buff.append("))");
        return buff.toString();
    } else {
        return "1 = 0";
    }
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) StatementBuilder(com.wplatform.ddal.util.StatementBuilder) Value(com.wplatform.ddal.value.Value) Session(com.wplatform.ddal.engine.Session)

Aggregations

StatementBuilder (com.wplatform.ddal.util.StatementBuilder)49 Value (com.wplatform.ddal.value.Value)13 Expression (com.wplatform.ddal.command.expression.Expression)9 Column (com.wplatform.ddal.dbobject.table.Column)8 PreparedStatement (java.sql.PreparedStatement)7 SQLException (java.sql.SQLException)4 IndexColumn (com.wplatform.ddal.dbobject.table.IndexColumn)3 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)3 Connection (java.sql.Connection)3 DataSource (javax.sql.DataSource)3 Index (com.wplatform.ddal.dbobject.index.Index)2 JdbcWorker (com.wplatform.ddal.excutor.JdbcWorker)2 DbException (com.wplatform.ddal.message.DbException)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 AlterTableAddConstraint (com.wplatform.ddal.command.ddl.AlterTableAddConstraint)1 AlterTableAlterColumn (com.wplatform.ddal.command.ddl.AlterTableAlterColumn)1