use of com.alibaba.druid.sql.ast.statement.SQLSelect 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")));
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect 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);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect 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.ast.statement.SQLSelect 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.ast.statement.SQLSelect in project druid by alibaba.
the class OracleSelectTest36 method test_0.
public void test_0() throws Exception {
//
String sql = //
"select ID,name from druid_test where (name>=? or name is null) and card_id<?";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
String result = SQLUtils.toOracleString(stmt);
String result_lcase = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
Assert.assertEquals("SELECT ID, name" + "\nFROM druid_test" + "\nWHERE (name >= ?" + "\n\t\tOR name IS NULL)" + "\n\tAND card_id < ?", result);
Assert.assertEquals("select ID, name" + "\nfrom druid_test" + "\nwhere (name >= ?" + "\n\t\tor name is null)" + "\n\tand card_id < ?", result_lcase);
{
SQLSelect select = ((SQLSelectStatement) stmt).getSelect();
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery();
SQLBinaryOpExpr where = (SQLBinaryOpExpr) queryBlock.getWhere();
Assert.assertEquals(SQLBinaryOperator.BooleanAnd, where.getOperator());
SQLBinaryOpExpr left = (SQLBinaryOpExpr) where.getLeft();
Assert.assertEquals(SQLBinaryOperator.BooleanOr, left.getOperator());
SQLBinaryOpExpr nameGTEQ = (SQLBinaryOpExpr) left.getLeft();
Assert.assertEquals(SQLBinaryOperator.GreaterThanOrEqual, nameGTEQ.getOperator());
SQLBinaryOpExpr nameIS = (SQLBinaryOpExpr) left.getRight();
Assert.assertEquals(SQLBinaryOperator.Is, nameIS.getOperator());
}
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("druid_test")));
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "name")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("druid_test", "card_id")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
Aggregations