Search in sources :

Example 81 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLInsertVisitor method supplyAutoIncrementColumn.

private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
    boolean isPreparedStatement = !getParameters().isEmpty();
    GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
    if (isPreparedStatement) {
        generatedKeyContext.getColumns().addAll(autoIncrementColumns);
    }
    TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
    for (String each : autoIncrementColumns) {
        SQLExpr sqlExpr;
        Object id = tableRule.generateId(each);
        generatedKeyContext.putValue(each, id);
        if (isPreparedStatement) {
            sqlExpr = new SQLVariantRefExpr("?");
            getParameters().add(id);
            ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
        } else {
            sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
        }
        getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
        columns.add(new SQLIdentifierExpr(each));
        values.add(sqlExpr);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 82 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

public boolean visit(final SQLOrderBy x) {
    for (SQLSelectOrderByItem each : x.getItems()) {
        SQLExpr expr = each.getExpr();
        OrderByType orderByType = null == each.getType() ? OrderByType.ASC : OrderByType.valueOf(each.getType());
        if (expr instanceof SQLIntegerExpr) {
            getParseContext().addOrderByColumn(((SQLIntegerExpr) expr).getNumber().intValue(), orderByType);
        } else if (expr instanceof SQLIdentifierExpr) {
            getParseContext().addOrderByColumn(Optional.<String>absent(), ((SQLIdentifierExpr) expr).getName(), orderByType);
        } else if (expr instanceof SQLPropertyExpr) {
            SQLPropertyExpr sqlPropertyExpr = (SQLPropertyExpr) expr;
            getParseContext().addOrderByColumn(Optional.of(sqlPropertyExpr.getOwner().toString()), sqlPropertyExpr.getName(), orderByType);
        }
    }
    return super.visit(x);
}
Also used : SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 83 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLSelectVisitor method visit.

/**
     * 将GROUP BY列放入parseResult.
     * 直接返回false,防止重复解析GROUP BY表达式.
     * 
     * @param x GROUP BY 表达式
     * @return false 停止遍历AST
     */
@Override
public boolean visit(final MySqlSelectGroupByExpr x) {
    OrderByType orderByType = null == x.getType() ? OrderByType.ASC : OrderByType.valueOf(x.getType());
    if (x.getExpr() instanceof SQLPropertyExpr) {
        SQLPropertyExpr expr = (SQLPropertyExpr) x.getExpr();
        getParseContext().addGroupByColumns(Optional.of(expr.getOwner().toString()), expr.getName(), orderByType);
    } else if (x.getExpr() instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr expr = (SQLIdentifierExpr) x.getExpr();
        getParseContext().addGroupByColumns(Optional.<String>absent(), expr.getName(), orderByType);
    }
    return super.visit(x);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) OrderByType(com.dangdang.ddframe.rdb.sharding.parser.result.merger.OrderByColumn.OrderByType)

Example 84 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class PagerUtils method limitOracle.

private static String limitOracle(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        OracleSelectQueryBlock queryBlock = (OracleSelectQueryBlock) query;
        if (queryBlock.getGroupBy() == null && select.getOrderBy() == null && offset <= 0) {
            SQLExpr condition = new //
            SQLBinaryOpExpr(//
            new SQLIdentifierExpr("ROWNUM"), //
            SQLBinaryOperator.LessThanOrEqual, //
            new SQLNumberExpr(count), JdbcConstants.ORACLE);
            if (queryBlock.getWhere() == null) {
                queryBlock.setWhere(condition);
            } else {
                queryBlock.setWhere(new //
                SQLBinaryOpExpr(//
                queryBlock.getWhere(), //
                SQLBinaryOperator.BooleanAnd, //
                condition, JdbcConstants.ORACLE));
            }
            return SQLUtils.toSQLString(select, dbType);
        }
    }
    OracleSelectQueryBlock countQueryBlock = new OracleSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLIdentifierExpr("ROWNUM"), "RN"));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    countQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.ORACLE));
    if (offset <= 0) {
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    OracleSelectQueryBlock offsetQueryBlock = new OracleSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("RN"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.ORACLE));
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 85 with SQLIdentifierExpr

use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.

the class PagerUtils method limitSQLServer.

private static String limitSQLServer(SQLSelect select, String dbType, int offset, int count) {
    SQLSelectQuery query = select.getQuery();
    SQLBinaryOpExpr gt = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.GreaterThan, //
    new SQLNumberExpr(offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr lteq = new //
    SQLBinaryOpExpr(//
    new SQLIdentifierExpr("ROWNUM"), //
    SQLBinaryOperator.LessThanOrEqual, //
    new SQLNumberExpr(count + offset), JdbcConstants.SQL_SERVER);
    SQLBinaryOpExpr pageCondition = new SQLBinaryOpExpr(gt, SQLBinaryOperator.BooleanAnd, lteq, JdbcConstants.SQL_SERVER);
    if (query instanceof SQLSelectQueryBlock) {
        SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) query;
        if (offset <= 0) {
            queryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
            return SQLUtils.toSQLString(select, dbType);
        }
        SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
        SQLOrderBy orderBy = select.getOrderBy();
        aggregateExpr.setOver(new SQLOver(orderBy));
        select.setOrderBy(null);
        queryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
        SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
        countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
        countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
        countQueryBlock.setWhere(pageCondition);
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLServerSelectQueryBlock countQueryBlock = new SQLServerSelectQueryBlock();
    countQueryBlock.getSelectList().add(new SQLSelectItem(new SQLPropertyExpr(new SQLIdentifierExpr("XX"), "*")));
    countQueryBlock.setFrom(new SQLSubqueryTableSource(select, "XX"));
    if (offset <= 0) {
        countQueryBlock.setTop(new SQLServerTop(new SQLNumberExpr(count)));
        return SQLUtils.toSQLString(countQueryBlock, dbType);
    }
    SQLAggregateExpr aggregateExpr = new SQLAggregateExpr("ROW_NUMBER");
    SQLOrderBy orderBy = select.getOrderBy();
    aggregateExpr.setOver(new SQLOver(orderBy));
    select.setOrderBy(null);
    countQueryBlock.getSelectList().add(new SQLSelectItem(aggregateExpr, "ROWNUM"));
    SQLServerSelectQueryBlock offsetQueryBlock = new SQLServerSelectQueryBlock();
    offsetQueryBlock.getSelectList().add(new SQLSelectItem(new SQLAllColumnExpr()));
    offsetQueryBlock.setFrom(new SQLSubqueryTableSource(new SQLSelect(countQueryBlock), "XXX"));
    offsetQueryBlock.setWhere(pageCondition);
    return SQLUtils.toSQLString(offsetQueryBlock, dbType);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAggregateExpr(com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)

Aggregations

SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)152 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)68 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)45 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)19 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)17 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)16 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)15 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)13 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)10 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)10 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)9 ArrayList (java.util.ArrayList)9 SQLName (com.alibaba.druid.sql.ast.SQLName)8