Search in sources :

Example 26 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock 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 27 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock 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)

Example 28 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectTest_crossjoin method test_0.

public void test_0() throws Exception {
    String sql = "select resource0_.resource_id as resource1_13_, resource0_.asMenu as asMenu13_, resource0_.resource_description as resource3_13_, resource0_.menu_name as menu4_13_, resource0_.resource_name as resource5_13_, resource0_.rg_id as rg7_13_, resource0_.rt_id as rt8_13_, resource0_.resource_serial as resource6_13_ from hnisitc.hnisitc_resource resource0_ cross join hnisitc.hnisitc_resource_type resourcety1_ where resource0_.rt_id=resourcety1_.rt_id and resourcety1_.rt_name='METHOD'";
    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);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    //        
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(10, visitor.getColumns().size());
    Assert.assertEquals(3, 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)

Example 29 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectTest_group_concat_2 method test_0.

public void test_0() throws Exception {
    String sql = //
    "SELECT student_name, " + //
    "GROUP_CONCAT(DISTINCT test_score " + //
    "    ORDER BY test_score DESC SEPARATOR ' ') " + //
    "FROM student " + "GROUP BY student_name";
    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(0, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("student")));
    String output = SQLUtils.toMySqlString(stmt);
    Assert.assertEquals(//
    "SELECT student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')" + //
    "\nFROM student" + //
    "\nGROUP BY student_name", 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 30 with MySqlSelectQueryBlock

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock in project druid by alibaba.

the class MySqlSelectTest_7 method test_0.

public void test_0() throws Exception {
    String sql = "select * from tb order by id asc,name desc";
    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.assertNotNull(queryBlock.getOrderBy());
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    //        System.out.println("coditions : " + visitor.getConditions());
    //        System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertEquals(2, 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

MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)31 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)23 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)18 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)18 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)15 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)15 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)9 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)8 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)2 OdpsSelectQueryBlock (com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)2 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 HashMap (java.util.HashMap)2 List (java.util.List)2