Search in sources :

Example 11 with SQLStatement

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

the class DruidMysqlRouteStrategyTest method testModifySQLLimit.

public void testModifySQLLimit() throws Exception {
    final SchemaConfig schema = schemaMap.get("TESTDB");
    String sql = null;
    RouteResultset rrs = null;
    //SQL span multi datanode 
    sql = "select * from orders limit 2,3";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 2);
    NodeNameAsserter nameAsserter = new NodeNameAsserter("dn2", "dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    MySqlStatementParser parser = new MySqlStatementParser("SELECT * FROM orders LIMIT 0,5");
    SQLStatement statement = parser.parseStatement();
    Assert.assertEquals(statement.toString(), rrs.getNodes()[0].getStatement());
    //SQL  not span multi datanode
    sql = "select * from customer where id=10000 limit 2,3";
    rrs = routeStrategy.route(new SystemConfig(), schema, ServerParse.SELECT, sql, null, null, cachePool);
    Assert.assertEquals(true, rrs.isCacheAble());
    nodeMap = getNodeMap(rrs, 1);
    nameAsserter = new NodeNameAsserter("dn1");
    nameAsserter.assertRouteNodeNames(nodeMap.keySet());
    Assert.assertEquals(3, rrs.getLimitSize());
    Assert.assertEquals("select * from customer where id=10000 limit 2,3", rrs.getNodes()[0].getStatement());
}
Also used : SystemConfig(io.mycat.config.model.SystemConfig) SchemaConfig(io.mycat.config.model.SchemaConfig) RouteResultsetNode(io.mycat.route.RouteResultsetNode) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) RouteResultset(io.mycat.route.RouteResultset)

Example 12 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class MySqlMockExecuteHandlerImpl method executeQuery.

@Override
public ResultSet executeQuery(MockStatementBase statement, String sql) throws SQLException {
    SQLStatementParser parser = new MySqlStatementParser(sql);
    //
    List<SQLStatement> stmtList = parser.parseStatementList();
    if (stmtList.size() > 1) {
        throw new SQLException("not support multi-statment. " + sql);
    }
    if (stmtList.size() == 0) {
        throw new SQLException("executeQueryError : " + sql);
    }
    SQLStatement stmt = stmtList.get(0);
    if (stmt instanceof CobarShowStatus) {
        return showStatus(statement);
    }
    if (!(stmt instanceof SQLSelectStatement)) {
        throw new SQLException("executeQueryError : " + sql);
    }
    SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
    SQLSelectQuery query = select.getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        return executeQuery(statement, (SQLSelectQueryBlock) query);
    }
    throw new SQLException("TODO");
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLException(java.sql.SQLException) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) CobarShowStatus(com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 13 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class PagerUtils method getLimit.

/**
     * 
     * @param sql
     * @param dbType
     * @return if not exists limit, return -1;
     */
public static int getLimit(String sql, String dbType) {
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    if (stmtList.size() != 1) {
        return -1;
    }
    SQLStatement stmt = stmtList.get(0);
    if (stmt instanceof SQLSelectStatement) {
        SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
        SQLSelectQuery query = selectStmt.getSelect().getQuery();
        if (query instanceof SQLSelectQueryBlock) {
            if (query instanceof MySqlSelectQueryBlock) {
                SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
                if (limit == null) {
                    return -1;
                }
                SQLExpr rowCountExpr = limit.getRowCount();
                if (rowCountExpr instanceof SQLNumericLiteralExpr) {
                    int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
                    return rowCount;
                }
                return Integer.MAX_VALUE;
            }
            if (query instanceof OdpsSelectQueryBlock) {
                SQLLimit limit = ((OdpsSelectQueryBlock) query).getLimit();
                SQLExpr rowCountExpr = limit != null ? limit.getRowCount() : null;
                if (rowCountExpr instanceof SQLNumericLiteralExpr) {
                    int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
                    return rowCount;
                }
                return Integer.MAX_VALUE;
            }
            return -1;
        }
    }
    return -1;
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 14 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class SQLUtils method addCondition.

public static String addCondition(String sql, String condition, SQLBinaryOperator op, boolean left, String dbType) {
    if (sql == null) {
        throw new IllegalArgumentException("sql is null");
    }
    if (condition == null) {
        return sql;
    }
    if (op == null) {
        op = SQLBinaryOperator.BooleanAnd;
    }
    if (//
    op != SQLBinaryOperator.BooleanAnd && op != SQLBinaryOperator.BooleanOr) {
        throw new IllegalArgumentException("add condition not support : " + op);
    }
    List<SQLStatement> stmtList = parseStatements(sql, dbType);
    if (stmtList.size() == 0) {
        throw new IllegalArgumentException("not support empty-statement :" + sql);
    }
    if (stmtList.size() > 1) {
        throw new IllegalArgumentException("not support multi-statement :" + sql);
    }
    SQLStatement stmt = stmtList.get(0);
    SQLExpr conditionExpr = toSQLExpr(condition, dbType);
    addCondition(stmt, op, conditionExpr, left);
    return toSQLString(stmt, dbType);
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 15 with SQLStatement

use of com.alibaba.druid.sql.ast.SQLStatement in project druid by alibaba.

the class SQLUtils method addSelectItem.

public static String addSelectItem(String selectSql, String expr, String alias, boolean first, String dbType) {
    List<SQLStatement> stmtList = parseStatements(selectSql, dbType);
    if (stmtList.size() == 0) {
        throw new IllegalArgumentException("not support empty-statement :" + selectSql);
    }
    if (stmtList.size() > 1) {
        throw new IllegalArgumentException("not support multi-statement :" + selectSql);
    }
    SQLStatement stmt = stmtList.get(0);
    SQLExpr columnExpr = toSQLExpr(expr, dbType);
    addSelectItem(stmt, columnExpr, alias, first);
    return toSQLString(stmt, dbType);
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1652 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)915 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)431 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)346 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)273 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)254 Column (com.alibaba.druid.stat.TableStat.Column)122 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)109 PGSchemaStatVisitor (com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor)107 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)100 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 MySqlOutputVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor)60 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)46 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)44 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)34 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)33 Test (org.junit.Test)31 MysqlTest (com.alibaba.druid.sql.MysqlTest)29 ArrayList (java.util.ArrayList)29 SQLASTOutputVisitor (com.alibaba.druid.sql.visitor.SQLASTOutputVisitor)28