Search in sources :

Example 21 with SQLIntegerExpr

use of com.alibaba.druid.sql.ast.expr.SQLIntegerExpr in project Mycat-Server by MyCATApache.

the class BatchInsertSequence method route.

@Override
public void route(SystemConfig sysConfig, SchemaConfig schema, int sqlType, String realSQL, String charset, ServerConnection sc, LayerCachePool cachePool) {
    int rs = ServerParse.parse(realSQL);
    this.sqltype = rs & 0xff;
    this.sysConfig = sysConfig;
    this.schema = schema;
    this.charset = charset;
    this.sc = sc;
    this.cachePool = cachePool;
    try {
        MySqlStatementParser parser = new MySqlStatementParser(realSQL);
        SQLStatement statement = parser.parseStatement();
        MySqlInsertStatement insert = (MySqlInsertStatement) statement;
        if (insert.getValuesList() != null) {
            String tableName = StringUtil.getTableName(realSQL).toUpperCase();
            TableConfig tableConfig = schema.getTables().get(tableName);
            //获得表的主键字段
            String primaryKey = tableConfig.getPrimaryKey();
            SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr();
            sqlIdentifierExpr.setName(primaryKey);
            insert.getColumns().add(sqlIdentifierExpr);
            if (sequenceHandler == null) {
                int seqHandlerType = MycatServer.getInstance().getConfig().getSystem().getSequnceHandlerType();
                switch(seqHandlerType) {
                    case SystemConfig.SEQUENCEHANDLER_MYSQLDB:
                        sequenceHandler = IncrSequenceMySQLHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCALFILE:
                        sequenceHandler = IncrSequencePropHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_LOCAL_TIME:
                        sequenceHandler = IncrSequenceTimeHandler.getInstance();
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED:
                        sequenceHandler = DistributedSequenceHandler.getInstance(MycatServer.getInstance().getConfig().getSystem());
                        break;
                    case SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT:
                        sequenceHandler = IncrSequenceZKHandler.getInstance();
                        break;
                    default:
                        throw new java.lang.IllegalArgumentException("Invalid sequnce handler type " + seqHandlerType);
                }
            }
            for (ValuesClause vc : insert.getValuesList()) {
                SQLIntegerExpr sqlIntegerExpr = new SQLIntegerExpr();
                long value = sequenceHandler.nextId(tableName.toUpperCase());
                //插入生成的sequence值
                sqlIntegerExpr.setNumber(value);
                vc.addValue(sqlIntegerExpr);
            }
            String insertSql = insert.toString();
            this.executeSql = insertSql;
        }
    } catch (Exception e) {
        LOGGER.error("BatchInsertSequence.route(......)", e);
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) TableConfig(io.mycat.config.model.TableConfig) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 22 with SQLIntegerExpr

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

the class SQLSelectBuilderImpl method limit.

@Override
public SQLSelectBuilderImpl limit(int rowCount, int offset) {
    SQLSelectQueryBlock queryBlock = getQueryBlock();
    if (queryBlock instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mySqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
        SQLLimit limit = new SQLLimit();
        limit.setRowCount(new SQLIntegerExpr(rowCount));
        if (offset > 0) {
            limit.setOffset(new SQLIntegerExpr(offset));
        }
        mySqlQueryBlock.setLimit(limit);
        return this;
    }
    if (queryBlock instanceof SQLServerSelectQueryBlock) {
        SQLServerSelectQueryBlock sqlserverQueryBlock = (SQLServerSelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLServerTop top = new SQLServerTop();
            top.setExpr(new SQLIntegerExpr(rowCount));
            sqlserverQueryBlock.setTop(top);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof PGSelectQueryBlock) {
        PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
        SQLLimit limit = new SQLLimit();
        if (offset > 0) {
            limit.setOffset(new SQLIntegerExpr(offset));
        }
        limit.setRowCount(new SQLIntegerExpr(rowCount));
        pgQueryBlock.setLimit(limit);
        return this;
    }
    if (queryBlock instanceof DB2SelectQueryBlock) {
        DB2SelectQueryBlock db2QueryBlock = (DB2SelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
            db2QueryBlock.setFirst(rowCountExpr);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof OracleSelectQueryBlock) {
        OracleSelectQueryBlock oracleQueryBlock = (OracleSelectQueryBlock) queryBlock;
        if (offset <= 0) {
            SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
            SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, rowCountExpr, false, oracleQueryBlock.getWhere());
            queryBlock.setWhere(newCondition);
        } else {
            throw new UnsupportedOperationException("not support offset");
        }
        return this;
    }
    if (queryBlock instanceof OdpsSelectQueryBlock) {
        OdpsSelectQueryBlock odpsQueryBlock = (OdpsSelectQueryBlock) queryBlock;
        if (offset > 0) {
            throw new UnsupportedOperationException("not support offset");
        }
        odpsQueryBlock.setLimit(new SQLLimit(new SQLIntegerExpr(rowCount)));
        return this;
    }
    throw new UnsupportedOperationException();
}
Also used : SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 23 with SQLIntegerExpr

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

the class OracleToMySqlOutputVisitor method visit.

public boolean visit(OracleSelectQueryBlock x) {
    boolean parentIsSelectStatment = false;
    {
        if (x.getParent() instanceof SQLSelect) {
            SQLSelect select = (SQLSelect) x.getParent();
            if (select.getParent() instanceof SQLSelectStatement || select.getParent() instanceof SQLSubqueryTableSource) {
                parentIsSelectStatment = true;
            }
        }
    }
    if (!parentIsSelectStatment) {
        return super.visit(x);
    }
    if (//
    x.getWhere() instanceof SQLBinaryOpExpr && //
    x.getFrom() instanceof SQLSubqueryTableSource) {
        int rownum;
        String ident;
        SQLBinaryOpExpr where = (SQLBinaryOpExpr) x.getWhere();
        if (where.getRight() instanceof SQLIntegerExpr && where.getLeft() instanceof SQLIdentifierExpr) {
            rownum = ((SQLIntegerExpr) where.getRight()).getNumber().intValue();
            ident = ((SQLIdentifierExpr) where.getLeft()).getName();
        } else {
            return super.visit(x);
        }
        SQLSelect select = ((SQLSubqueryTableSource) x.getFrom()).getSelect();
        SQLSelectQueryBlock queryBlock = null;
        SQLSelect subSelect = null;
        SQLBinaryOpExpr subWhere = null;
        boolean isSubQueryRowNumMapping = false;
        if (select.getQuery() instanceof SQLSelectQueryBlock) {
            queryBlock = (SQLSelectQueryBlock) select.getQuery();
            if (queryBlock.getWhere() instanceof SQLBinaryOpExpr) {
                subWhere = (SQLBinaryOpExpr) queryBlock.getWhere();
            }
            for (SQLSelectItem selectItem : queryBlock.getSelectList()) {
                if (isRowNumber(selectItem.getExpr())) {
                    if (where.getLeft() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) where.getLeft()).getName().equals(selectItem.getAlias())) {
                        isSubQueryRowNumMapping = true;
                    }
                }
            }
            SQLTableSource subTableSource = queryBlock.getFrom();
            if (subTableSource instanceof SQLSubqueryTableSource) {
                subSelect = ((SQLSubqueryTableSource) subTableSource).getSelect();
            }
        }
        if ("ROWNUM".equalsIgnoreCase(ident)) {
            SQLBinaryOperator op = where.getOperator();
            Integer limit = null;
            if (op == SQLBinaryOperator.LessThanOrEqual) {
                limit = rownum;
            } else if (op == SQLBinaryOperator.LessThan) {
                limit = rownum - 1;
            }
            if (limit != null) {
                select.accept(this);
                println();
                print0(ucase ? "LIMIT " : "limit ");
                print(limit);
                return false;
            }
        } else if (isSubQueryRowNumMapping) {
            SQLBinaryOperator op = where.getOperator();
            SQLBinaryOperator subOp = subWhere.getOperator();
            if (//
            isRowNumber(subWhere.getLeft()) && subWhere.getRight() instanceof SQLIntegerExpr) {
                int subRownum = ((SQLIntegerExpr) subWhere.getRight()).getNumber().intValue();
                Integer offset = null;
                if (op == SQLBinaryOperator.GreaterThanOrEqual) {
                    offset = rownum + 1;
                } else if (op == SQLBinaryOperator.GreaterThan) {
                    offset = rownum;
                }
                if (offset != null) {
                    Integer limit = null;
                    if (subOp == SQLBinaryOperator.LessThanOrEqual) {
                        limit = subRownum - offset;
                    } else if (subOp == SQLBinaryOperator.LessThan) {
                        limit = subRownum - 1 - offset;
                    }
                    if (limit != null) {
                        subSelect.accept(this);
                        println();
                        print0(ucase ? "LIMIT " : "limit ");
                        print(offset);
                        print0(", ");
                        print(limit);
                        return false;
                    }
                }
            }
        }
    }
    return super.visit(x);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 24 with SQLIntegerExpr

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

the class SchemaStatVisitor method visit.

public boolean visit(SQLOrderBy x) {
    final SQLASTVisitor orderByVisitor = createOrderByVisitor(x);
    SQLSelectQueryBlock query = null;
    if (x.getParent() instanceof SQLSelectQueryBlock) {
        query = (SQLSelectQueryBlock) x.getParent();
    }
    if (query != null) {
        for (SQLSelectOrderByItem item : x.getItems()) {
            SQLExpr expr = item.getExpr();
            if (expr instanceof SQLIntegerExpr) {
                int intValue = ((SQLIntegerExpr) expr).getNumber().intValue() - 1;
                if (intValue < query.getSelectList().size()) {
                    SQLSelectItem selectItem = query.getSelectList().get(intValue);
                    selectItem.getExpr().accept(orderByVisitor);
                }
            } else if (expr instanceof MySqlExpr || expr instanceof OracleExpr) {
                continue;
            }
        }
    }
    x.accept(orderByVisitor);
    return true;
}
Also used : MySqlExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) OracleExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)24 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)16 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)9 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 List (java.util.List)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)3 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)3 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)3 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)3 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2