Search in sources :

Example 31 with SQLSelect

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

the class OdpsStatementParser method parseInsert.

public SQLStatement parseInsert() {
    OdpsInsertStatement stmt = new OdpsInsertStatement();
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER) {
            SQLName tableName = this.exprParser.name();
            SQLExprTableSource from = new SQLExprTableSource(tableName);
            stmt.setFrom(from);
        } else {
            accept(Token.LPAREN);
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect select = selectParser.select();
            accept(Token.RPAREN);
            String alias = lexer.stringVal();
            accept(Token.IDENTIFIER);
            SQLSubqueryTableSource from = new SQLSubqueryTableSource(select, alias);
            stmt.setFrom(from);
        }
    }
    for (; ; ) {
        OdpsInsert insert = parseOdpsInsert();
        stmt.addItem(insert);
        if (lexer.token() != Token.INSERT) {
            break;
        }
    }
    return stmt;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelectParser(com.alibaba.druid.sql.parser.SQLSelectParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsInsert(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert) OdpsInsertStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsertStatement)

Example 32 with SQLSelect

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

the class SQLParserTest method test_select.

public void test_select() throws Exception {
    String sql = "   SELECT COUNT(*) FROM close_plan WHERE 1=1          AND close_type = ?             AND target_type = ?             AND target_id = ?         AND(    mi_name=?   )               AND end_time >= ?         ";
    SQLSelectParser parser = new MySqlSelectParser(sql);
    SQLSelect select = parser.select();
    StringBuilder out = new StringBuilder();
    MySql2OracleOutputVisitor visitor = new MySql2OracleOutputVisitor(out);
    select.accept(visitor);
    System.out.println(out);
}
Also used : MySqlSelectParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlSelectParser) SQLSelectParser(com.alibaba.druid.sql.parser.SQLSelectParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) MySql2OracleOutputVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySql2OracleOutputVisitor)

Example 33 with SQLSelect

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

the class MySqlSelectTest_11 method test_0.

public void test_0() throws Exception {
    String sql = "select * from users where uid = :uid";
    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(2, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("users")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM users" + //
    "\nWHERE uid = :uid", 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 34 with SQLSelect

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

the class MySqlSelectTest_12 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT COUNT(*) a FROM (select nickname,mobile,comment,createdate from ub_userdiscuss order by discuss_id desc) b  ";
    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(5, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertEquals(1, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ub_userdiscuss")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT COUNT(*) AS a" + //
    "\nFROM (SELECT nickname, mobile, comment, createdate" + //
    "\n\tFROM ub_userdiscuss" + //
    "\n\tORDER BY discuss_id DESC" + //
    "\n\t) b", 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 35 with SQLSelect

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

the class MySqlSelectTest_13 method test_0.

public void test_0() throws Exception {
    String sql = "select * from test /*!40101fff*/";
    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(0, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("test")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM test/*!40101fff*/", 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)

Aggregations

SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)40 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)21 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)19 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)16 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)15 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)7 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)6 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)6 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)3 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)3