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);
}
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());
}
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")));
}
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")));
}
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);
}
Aggregations