use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class SQLExprParser method parseAny.
protected SQLExpr parseAny() {
SQLExpr sqlExpr;
lexer.nextToken();
if (lexer.token() == Token.LPAREN) {
accept(Token.LPAREN);
if (lexer.token() == Token.IDENTIFIER) {
SQLExpr expr = this.expr();
SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr("ANY");
methodInvokeExpr.addParameter(expr);
accept(Token.RPAREN);
return methodInvokeExpr;
}
SQLAnyExpr anyExpr = new SQLAnyExpr();
SQLSelect anySubQuery = createSelectParser().select();
anyExpr.setSubQuery(anySubQuery);
accept(Token.RPAREN);
anySubQuery.setParent(anyExpr);
sqlExpr = anyExpr;
} else {
sqlExpr = new SQLIdentifierExpr("ANY");
}
return sqlExpr;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class EqualTest_select method test_eq_select.
public void test_eq_select() throws Exception {
List stmtsA = SQLUtils.parseStatements("select * from a", JdbcConstants.ODPS);
List stmtsB = SQLUtils.parseStatements("select * from b", JdbcConstants.ODPS);
SQLSelect selectA = ((SQLSelectStatement) stmtsA.get(0)).getSelect();
SQLSelect selectB = ((SQLSelectStatement) stmtsB.get(0)).getSelect();
boolean eq = selectA.equals(selectB);
assertFalse(eq);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class MySqlSelectTest_15 method test_0.
public void test_0() throws Exception {
String sql = "SELECT 1--1 from t where id = 1";
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(1, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getOrderByColumns().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals(//
"SELECT 1 - -1" + //
"\nFROM t" + //
"\nWHERE id = 1", output);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class MySqlSelectTest_16 method test_0.
public void test_0() throws Exception {
String sql = "select a from t where not a>1 and not b<1";
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());
{
SQLExpr where = queryBlock.getWhere();
Assert.assertTrue(where instanceof SQLBinaryOpExpr);
SQLBinaryOpExpr binaryWhere = (SQLBinaryOpExpr) where;
Assert.assertEquals(binaryWhere.getOperator(), SQLBinaryOperator.BooleanAnd);
Assert.assertTrue(binaryWhere.getLeft() instanceof SQLNotExpr);
Assert.assertTrue(binaryWhere.getRight() instanceof SQLNotExpr);
}
// 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(2, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getOrderByColumns().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals(//
"SELECT a" + //
"\nFROM t" + //
"\nWHERE NOT a > 1" + //
"\n\tAND NOT b < 1", output);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class MySqlSelectTest_8 method test_0.
public void test_0() throws Exception {
String sql = "select 1";
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(0, visitor.getTables().size());
Assert.assertEquals(0, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getOrderByColumns().size());
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("mytable")));
}
Aggregations