Search in sources :

Example 6 with SchemaStatVisitor

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);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SimpleDateFormat(java.text.SimpleDateFormat) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) Date(java.util.Date)

Example 7 with SchemaStatVisitor

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());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 8 with SchemaStatVisitor

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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 9 with SchemaStatVisitor

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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 10 with SchemaStatVisitor

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());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)67 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 TableStat (com.alibaba.druid.stat.TableStat)18 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)9 Column (com.alibaba.druid.stat.TableStat.Column)8 SQLUtils (com.alibaba.druid.sql.SQLUtils)6 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)4 PhoenixSchemaStatVisitor (com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor)4 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)1 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)1 MySqlReplaceStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement)1