Search in sources :

Example 76 with MySqlSelectQueryBlock

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

the class MySqlSelectTest_185 method test_0.

public void test_0() throws Exception {
    String sql = "select\n" + "product.brand_id,\n" + "product.sort,\n" + "product.old_product_id,\n" + "product.old_url_key,\n" + "categoryProduct.category_id\n" + "from catalog_product as product, catalog_category_product as categoryProduct\n" + "where product.brand_id = categoryProduct.brand_id and product.lang_id = categoryProduct.lang_id and\n" + "product.product_id = categoryProduct.product_id and product.brand_id = ? and product.lang_id = ? and\n" + "categoryProduct.category_id = ? and product.status = 1 and\n" + "product.more_color between (0 AND 1) and product.master_color = 1\n" + "order by product.sort asc";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    assertNotNull(queryBlock.getOrderBy());
    // print(statementList);
    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());
    assertEquals(2, visitor.getTables().size());
    assertEquals(13, visitor.getColumns().size());
    assertEquals(10, visitor.getConditions().size());
    assertEquals(1, visitor.getOrderByColumns().size());
    assertTrue(visitor.containsTable("catalog_product"));
    assertTrue(visitor.containsTable("catalog_category_product"));
    assertTrue(visitor.containsColumn("catalog_product", "brand_id"));
    String output = SQLUtils.toMySqlString(stmt);
    assertEquals("SELECT product.brand_id, product.sort, product.old_product_id, product.old_url_key, categoryProduct.category_id\n" + "FROM catalog_product product, catalog_category_product categoryProduct\n" + "WHERE product.brand_id = categoryProduct.brand_id\n" + "\tAND product.lang_id = categoryProduct.lang_id\n" + "\tAND product.product_id = categoryProduct.product_id\n" + "\tAND product.brand_id = ?\n" + "\tAND product.lang_id = ?\n" + "\tAND categoryProduct.category_id = ?\n" + "\tAND product.status = 1\n" + "\tAND product.more_color BETWEEN (0\n" + "\t\tAND 1)\n" + "\tAND (product.master_color = 1)\n" + // 
    "ORDER BY product.sort ASC", 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 77 with MySqlSelectQueryBlock

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

the class MySqlSelectTest_184 method test_1.

public void test_1() throws Exception {
    String sql = "SELECT * FROM \"wenyu_meta_test\".\"WENYU_META_TEST_02\" LIMIT 4";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    assertNotNull(select.getQuery());
    MySqlSelectQueryBlock queryBlock = (MySqlSelectQueryBlock) select.getQuery();
    assertNull(queryBlock.getOrderBy());
    // print(statementList);
    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());
    assertEquals(1, visitor.getTables().size());
    assertEquals(1, visitor.getColumns().size());
    assertEquals(0, visitor.getConditions().size());
    assertEquals(0, visitor.getOrderByColumns().size());
    assertTrue(visitor.containsTable("wenyu_meta_test.WENYU_META_TEST_02"));
    assertTrue(visitor.containsColumn("wenyu_meta_test.WENYU_META_TEST_02", "*"));
    String output = SQLUtils.toMySqlString(stmt);
    assertEquals("SELECT *\n" + "FROM \"wenyu_meta_test\".\"WENYU_META_TEST_02\"\n" + // 
    "LIMIT 4", output);
    assertEquals("wenyu_meta_test.WENYU_META_TEST_02", visitor.getTables().keySet().iterator().next().getName());
    assertEquals("wenyu_meta_test.WENYU_META_TEST_02", visitor.getColumns().iterator().next().getTable());
}
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 78 with MySqlSelectQueryBlock

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

the class MySqlSelectTest_9 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT `group`.* FROM `group` WHERE (group.group_id=159754)";
    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("group")));
}
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 79 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)

Example 80 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(1, 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)

Aggregations

MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)82 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)41 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)34 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)31 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)28 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)25 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)23 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)19 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)13 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)9 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)8 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)8 HashMap (java.util.HashMap)8 Item (com.actiontech.dble.plan.common.item.Item)7 SQLNonTransientException (java.sql.SQLNonTransientException)7 Test (org.junit.Test)7 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)6 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)6