Search in sources :

Example 6 with SQLSelectStatement

use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.

the class SQLUtils method addSelectItem.

public static void addSelectItem(SQLStatement stmt, SQLExpr expr, String alias, boolean first) {
    if (expr == null) {
        return;
    }
    if (stmt instanceof SQLSelectStatement) {
        SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
        if (query instanceof SQLSelectQueryBlock) {
            SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
            addSelectItem(queryBlock, expr, alias, first);
        } else {
            throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
        }
        return;
    }
    throw new IllegalArgumentException("add selectItem not support " + stmt.getClass().getName());
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 7 with SQLSelectStatement

use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.

the class EqualTest_select method test_eq_select.

public void test_eq_select() throws Exception {
    List stmtsA = SQLUtils.parseStatements("select * from a", JdbcConstants.ODPS);
    List stmtsB = SQLUtils.parseStatements("select * from b", JdbcConstants.ODPS);
    SQLSelect selectA = ((SQLSelectStatement) stmtsA.get(0)).getSelect();
    SQLSelect selectB = ((SQLSelectStatement) stmtsB.get(0)).getSelect();
    boolean eq = selectA.equals(selectB);
    assertFalse(eq);
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) List(java.util.List)

Example 8 with SQLSelectStatement

use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.

the class MySqlSelectTest_15 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT 1--1 from t where id = 1";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT 1 - -1" + //
    "\nFROM t" + //
    "\nWHERE id = 1", output);
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Example 9 with SQLSelectStatement

use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.

the class MySqlSelectTest_16 method test_0.

public void test_0() throws Exception {
    String sql = "select a from t where not a>1 and not b<1";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());
    {
        SQLExpr where = queryBlock.getWhere();
        Assert.assertTrue(where instanceof SQLBinaryOpExpr);
        SQLBinaryOpExpr binaryWhere = (SQLBinaryOpExpr) where;
        Assert.assertEquals(binaryWhere.getOperator(), SQLBinaryOperator.BooleanAnd);
        Assert.assertTrue(binaryWhere.getLeft() instanceof SQLNotExpr);
        Assert.assertTrue(binaryWhere.getRight() instanceof SQLNotExpr);
    }
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT a" + //
    "\nFROM t" + //
    "\nWHERE NOT a > 1" + //
    "\n\tAND NOT b < 1", output);
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) 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 10 with SQLSelectStatement

use of com.alibaba.druid.sql.ast.statement.SQLSelectStatement in project druid by alibaba.

the class MySqlSelectTest_8 method test_0.

public void test_0() throws Exception {
    String sql = "select 1";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getOrderBy());
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
//        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)

Aggregations

SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)78 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)39 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)25 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)23 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)21 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)17 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)15 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)14 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)10 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)6 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)4 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)4 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)3 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)2 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)2