use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class OdpsStatementParser method parseInsert.
public SQLStatement parseInsert() {
OdpsInsertStatement stmt = new OdpsInsertStatement();
if (lexer.token() == Token.FROM) {
lexer.nextToken();
if (lexer.token() == Token.IDENTIFIER) {
SQLName tableName = this.exprParser.name();
SQLExprTableSource from = new SQLExprTableSource(tableName);
stmt.setFrom(from);
} else {
accept(Token.LPAREN);
SQLSelectParser selectParser = createSQLSelectParser();
SQLSelect select = selectParser.select();
accept(Token.RPAREN);
String alias = lexer.stringVal();
accept(Token.IDENTIFIER);
SQLSubqueryTableSource from = new SQLSubqueryTableSource(select, alias);
stmt.setFrom(from);
}
}
for (; ; ) {
OdpsInsert insert = parseOdpsInsert();
stmt.addItem(insert);
if (lexer.token() != Token.INSERT) {
break;
}
}
return stmt;
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class SQLParserTest method test_select.
public void test_select() throws Exception {
String sql = " SELECT COUNT(*) FROM close_plan WHERE 1=1 AND close_type = ? AND target_type = ? AND target_id = ? AND( mi_name=? ) AND end_time >= ? ";
SQLSelectParser parser = new MySqlSelectParser(sql);
SQLSelect select = parser.select();
StringBuilder out = new StringBuilder();
MySql2OracleOutputVisitor visitor = new MySql2OracleOutputVisitor(out);
select.accept(visitor);
System.out.println(out);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect in project druid by alibaba.
the class MySqlSelectTest_11 method test_0.
public void test_0() throws Exception {
String sql = "select * from users where uid = :uid";
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("users")));
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals(//
"SELECT *" + //
"\nFROM users" + //
"\nWHERE uid = :uid", output);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect 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);
}
use of com.alibaba.druid.sql.ast.statement.SQLSelect 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);
}
Aggregations