use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class DruidStatService method getSqlStat.
private String getSqlStat(Integer id) {
Map<String, Object> map = statManagerFacade.getSqlStatData(id);
if (map == null) {
return returnJSONResult(RESULT_CODE_ERROR, null);
}
String dbType = (String) map.get("DbType");
String sql = (String) map.get("SQL");
map.put("formattedSql", SQLUtils.format(sql, dbType));
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, dbType);
if (!statementList.isEmpty()) {
SQLStatement sqlStmt = statementList.get(0);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
sqlStmt.accept(visitor);
map.put("parsedTable", visitor.getTables().toString());
map.put("parsedFields", visitor.getColumns().toString());
map.put("parsedConditions", visitor.getConditions().toString());
map.put("parsedRelationships", visitor.getRelationships().toString());
map.put("parsedOrderbycolumns", visitor.getOrderByColumns().toString());
}
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
Date maxTimespanOccurTime = (Date) map.get("MaxTimespanOccurTime");
if (maxTimespanOccurTime != null) {
map.put("MaxTimespanOccurTime", format.format(maxTimespanOccurTime));
}
return returnJSONResult(map == null ? RESULT_CODE_ERROR : RESULT_CODE_SUCCESS, map);
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class OverTest method test_over.
public void test_over() throws Exception {
String sql = //
"SELECT SalesOrderID, ProductID, OrderQty" + //
" ,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Total'" + //
" ,AVG(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Avg'" + //
" ,COUNT(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Count'" + //
" ,MIN(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Min'" + //
" ,MAX(OrderQty) OVER(PARTITION BY SalesOrderID) AS 'Max' " + //
"FROM Sales.SalesOrderDetail " + "WHERE SalesOrderID IN(43659,43664);";
SQLStatementParser parser = new SQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new SchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertEquals(1, visitor.getTables().size());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddIndex_0 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD INDEX `ix` (`f2` ASC) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + "\n\tADD INDEX `ix` (`f2` ASC)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals(//
"alter table `test`.`tb1`" + "\n\tadd index `ix` (`f2` asc)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
TableStat tableStat = visitor.getTableStat("test.tb1");
assertNotNull(tableStat);
assertEquals(1, tableStat.getCreateIndexCount());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddIndex_1 method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD UNIQUE INDEX `ix2` (`fid` ASC) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + "\n\tADD UNIQUE INDEX `ix2` (`fid` ASC)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals(//
"alter table `test`.`tb1`" + "\n\tadd unique index `ix2` (`fid` asc)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
TableStat tableStat = visitor.getTableStat("test.tb1");
assertNotNull(tableStat);
assertEquals(1, tableStat.getAlterCount());
assertEquals(1, tableStat.getCreateIndexCount());
}
use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.
the class MySqlAlterTableAddPrimaryKey method test_alter_first.
public void test_alter_first() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` CHANGE COLUMN `fid` `fid` INT(11) NOT NULL DEFAULT NULL, ADD PRIMARY KEY (`fid`) ;";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
Assert.assertEquals(//
"ALTER TABLE `test`.`tb1`" + //
"\n\tCHANGE COLUMN `fid` `fid` INT(11) NOT NULL DEFAULT NULL,\n\t" + "ADD PRIMARY KEY (`fid`)", SQLUtils.toMySqlString(stmt));
Assert.assertEquals(//
"alter table `test`.`tb1`" + //
"\n\tchange column `fid` `fid` INT(11) not null default null,\n\t" + "add primary key (`fid`)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
TableStat tableStat = visitor.getTableStat("test.tb1");
assertNotNull(tableStat);
assertEquals(1, tableStat.getAlterCount());
assertEquals(1, tableStat.getCreateIndexCount());
}
Aggregations